PageTemplate 1.2.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. data/Changes +24 -0
  2. data/MANIFEST +0 -1
  3. data/README.txt +10 -8
  4. data/Rakefile +30 -9
  5. data/lib/PageTemplate/case.rb +72 -0
  6. data/lib/PageTemplate/commands.rb +500 -0
  7. data/lib/PageTemplate/parser.rb +373 -0
  8. data/lib/PageTemplate.rb +47 -991
  9. data/setup-usage.txt +129 -0
  10. data/setup.rb +1360 -0
  11. data/tdata/dummy.txt +2 -0
  12. data/test.rb +616 -0
  13. metadata +13 -169
  14. data/CVS/Entries +0 -10
  15. data/CVS/Repository +0 -1
  16. data/CVS/Root +0 -1
  17. data/Changes~ +0 -7
  18. data/InstalledFiles +0 -7
  19. data/Rakefile~ +0 -54
  20. data/TC_PageTemplate.rb +0 -974
  21. data/TC_PageTemplate.rb~ +0 -965
  22. data/config.save +0 -12
  23. data/doc/classes/BlockCommand.html +0 -229
  24. data/doc/classes/BlockCommand.src/M000004.html +0 -18
  25. data/doc/classes/BlockCommand.src/M000005.html +0 -25
  26. data/doc/classes/BlockCommand.src/M000006.html +0 -18
  27. data/doc/classes/BlockCommand.src/M000007.html +0 -23
  28. data/doc/classes/BlockCommand.src/M000008.html +0 -24
  29. data/doc/classes/Command.html +0 -167
  30. data/doc/classes/Command.src/M000032.html +0 -19
  31. data/doc/classes/Command.src/M000033.html +0 -18
  32. data/doc/classes/CommentCommand.html +0 -146
  33. data/doc/classes/CommentCommand.src/M000009.html +0 -18
  34. data/doc/classes/IfCommand.html +0 -193
  35. data/doc/classes/IfCommand.src/M000042.html +0 -19
  36. data/doc/classes/IfCommand.src/M000043.html +0 -20
  37. data/doc/classes/IfCommand.src/M000044.html +0 -20
  38. data/doc/classes/IfElseCommand.html +0 -189
  39. data/doc/classes/IfElseCommand.src/M000025.html +0 -19
  40. data/doc/classes/IfElseCommand.src/M000026.html +0 -23
  41. data/doc/classes/IfElseCommand.src/M000027.html +0 -20
  42. data/doc/classes/IncludeCommand.html +0 -171
  43. data/doc/classes/IncludeCommand.src/M000019.html +0 -26
  44. data/doc/classes/IncludeCommand.src/M000020.html +0 -28
  45. data/doc/classes/IncludeCommand.src/M000021.html +0 -18
  46. data/doc/classes/IncludeCommandError.html +0 -118
  47. data/doc/classes/LoopCommand.html +0 -197
  48. data/doc/classes/LoopCommand.src/M000001.html +0 -19
  49. data/doc/classes/LoopCommand.src/M000002.html +0 -42
  50. data/doc/classes/LoopCommand.src/M000003.html +0 -20
  51. data/doc/classes/LoopElseCommand.html +0 -190
  52. data/doc/classes/LoopElseCommand.src/M000022.html +0 -19
  53. data/doc/classes/LoopElseCommand.src/M000023.html +0 -23
  54. data/doc/classes/LoopElseCommand.src/M000024.html +0 -20
  55. data/doc/classes/Namespace.html +0 -297
  56. data/doc/classes/Namespace.src/M000034.html +0 -19
  57. data/doc/classes/Namespace.src/M000035.html +0 -18
  58. data/doc/classes/Namespace.src/M000036.html +0 -18
  59. data/doc/classes/Namespace.src/M000037.html +0 -19
  60. data/doc/classes/Namespace.src/M000038.html +0 -36
  61. data/doc/classes/Namespace.src/M000039.html +0 -21
  62. data/doc/classes/Namespace.src/M000040.html +0 -18
  63. data/doc/classes/Namespace.src/M000041.html +0 -18
  64. data/doc/classes/PageTemplate.html +0 -380
  65. data/doc/classes/PageTemplate.src/M000010.html +0 -49
  66. data/doc/classes/PageTemplate.src/M000011.html +0 -18
  67. data/doc/classes/PageTemplate.src/M000012.html +0 -29
  68. data/doc/classes/PageTemplate.src/M000013.html +0 -18
  69. data/doc/classes/PageTemplate.src/M000014.html +0 -18
  70. data/doc/classes/PageTemplate.src/M000015.html +0 -18
  71. data/doc/classes/PageTemplate.src/M000016.html +0 -18
  72. data/doc/classes/PageTemplate.src/M000017.html +0 -18
  73. data/doc/classes/PageTemplate.src/M000018.html +0 -19
  74. data/doc/classes/Syntax/CachedParser.html +0 -163
  75. data/doc/classes/Syntax/CachedParser.src/M000048.html +0 -19
  76. data/doc/classes/Syntax/CachedParser.src/M000049.html +0 -53
  77. data/doc/classes/Syntax/Glossary.html +0 -251
  78. data/doc/classes/Syntax/Glossary.src/M000050.html +0 -20
  79. data/doc/classes/Syntax/Glossary.src/M000051.html +0 -29
  80. data/doc/classes/Syntax/Parser.html +0 -261
  81. data/doc/classes/Syntax/Parser.src/M000052.html +0 -22
  82. data/doc/classes/Syntax/Parser.src/M000053.html +0 -36
  83. data/doc/classes/Syntax/Parser.src/M000054.html +0 -18
  84. data/doc/classes/Syntax/Parser.src/M000055.html +0 -21
  85. data/doc/classes/Syntax/Parser.src/M000056.html +0 -53
  86. data/doc/classes/Syntax/Parser.src/M000057.html +0 -111
  87. data/doc/classes/Syntax.html +0 -139
  88. data/doc/classes/TextCommand.html +0 -185
  89. data/doc/classes/TextCommand.src/M000045.html +0 -18
  90. data/doc/classes/TextCommand.src/M000046.html +0 -18
  91. data/doc/classes/TextCommand.src/M000047.html +0 -18
  92. data/doc/classes/UnlessCommand.html +0 -151
  93. data/doc/classes/UnlessCommand.src/M000028.html +0 -20
  94. data/doc/classes/ValueCommand.html +0 -186
  95. data/doc/classes/ValueCommand.src/M000029.html +0 -18
  96. data/doc/classes/ValueCommand.src/M000030.html +0 -18
  97. data/doc/classes/ValueCommand.src/M000031.html +0 -20
  98. data/doc/created.rid +0 -1
  99. data/doc/files/README_txt.html +0 -224
  100. data/doc/files/lib/PageTemplate_rb.html +0 -119
  101. data/doc/fr_class_index.html +0 -44
  102. data/doc/fr_file_index.html +0 -28
  103. data/doc/fr_method_index.html +0 -83
  104. data/doc/index.html +0 -24
  105. data/doc/rdoc-style.css +0 -208
  106. data/install.rb +0 -1015
  107. data/lib/CVS/Entries +0 -2
  108. data/lib/CVS/Repository +0 -1
  109. data/lib/CVS/Root +0 -1
  110. data/lib/PageTemplate.rb~ +0 -1004
  111. data/pkg/PageTemplate-1.2.0.gem +0 -0
  112. data/tdata/CVS/Entries +0 -28
  113. data/tdata/CVS/Repository +0 -1
  114. data/tdata/CVS/Root +0 -1
  115. data/tdata/cm.txt +0 -3
  116. data/tdata/complex.txt +0 -116
  117. data/tdata/i1.txt +0 -1
  118. data/tdata/i2.txt +0 -7
  119. data/tdata/ib1.txt +0 -7
  120. data/tdata/ib2.txt +0 -13
  121. data/tdata/ie1.txt +0 -4
  122. data/tdata/ie2.txt +0 -15
  123. data/tdata/include.1.txt +0 -1
  124. data/tdata/include.2.txt +0 -1
  125. data/tdata/include.3.txt +0 -1
  126. data/tdata/include.4.nf.out.txt +0 -2
  127. data/tdata/include.4.out.txt +0 -3
  128. data/tdata/include.4.txt +0 -2
  129. data/tdata/include.4a.txt +0 -1
  130. data/tdata/include.4b.txt +0 -2
  131. data/tdata/include.5.txt +0 -2
  132. data/tdata/l1.txt +0 -5
  133. data/tdata/l2.txt +0 -7
  134. data/tdata/metadata.1.txt +0 -5
  135. data/tdata/metadata.2.txt +0 -5
  136. data/tdata/metadata.3.txt +0 -5
  137. data/tdata/metadata.4.txt +0 -5
  138. data/tdata/nm.txt +0 -4
  139. data/tdata/p/CVS/Entries +0 -5
  140. data/tdata/p/CVS/Repository +0 -1
  141. data/tdata/p/CVS/Root +0 -1
  142. data/tdata/p/CVS/b2.txt,t +0 -0
  143. data/tdata/p/_b1.txt +0 -0
  144. data/tdata/p/b1.txt +0 -4
  145. data/tdata/p/b2.txt +0 -4
  146. data/tdata/p/b2.txt~ +0 -4
  147. data/tdata/p/cb1.txt +0 -4
  148. data/tdata/t.txt +0 -2
  149. data/tdata/v1.txt +0 -11
  150. data/tdata/v2.txt +0 -11
data/tdata/dummy.txt ADDED
@@ -0,0 +1,2 @@
1
+ I am a simple text file for testing FileSource#get_filename.
2
+
data/test.rb ADDED
@@ -0,0 +1,616 @@
1
+ # Code Generated by ZenTest v. 2.3.0
2
+ # Couldn't find class for name User
3
+ # classname: asrt / meth = ratio%
4
+ # PageTemplate::DefaultPreprocessor: 0 / 5 = 0.00%
5
+ # PageTemplate::Namespace: 0 / 5 = 0.00%
6
+ # PageTemplate::CaseCommand: 0 / 6 = 0.00%
7
+ # PageTemplate::LoopCommand: 0 / 6 = 0.00%
8
+ # PageTemplate: 0 / 1 = 0.00%
9
+ # PageTemplate::TextCommand: 0 / 3 = 0.00%
10
+ # PageTemplate::UnknownCommand: 0 / 3 = 0.00%
11
+ # PageTemplate::CommentCommand: 0 / 3 = 0.00%
12
+ # PageTemplate::FileSource: 0 / 5 = 0.00%
13
+ # PageTemplate::MiniValueCommand: 0 / 1 = 0.00%
14
+ # User: 0 / 1 = 0.00%
15
+ # PageTemplate::IncludeCommand: 0 / 3 = 0.00%
16
+ # PageTemplate::IfCommand: 0 / 6 = 0.00%
17
+ # PageTemplate::Command: 0 / 2 = 0.00%
18
+ # PageTemplate::ValueCommand: 0 / 3 = 0.00%
19
+ # PageTemplate::BlockCommand: 0 / 7 = 0.00%
20
+ # PageTemplate::StackableCommand: 0 / 3 = 0.00%
21
+ # PageTemplate::Parser: 0 / 9 = 0.00%
22
+ # PageTemplate::SyntaxGlossary: 0 / 4 = 0.00%
23
+
24
+ require 'test/unit' unless defined? $ZENTEST and $ZENTEST
25
+
26
+ require 'PageTemplate/commands.rb'
27
+ require 'PageTemplate/parser.rb'
28
+ require 'PageTemplate/case.rb'
29
+
30
+ module TestPageTemplate
31
+ class TestBlockCommand < Test::Unit::TestCase
32
+ def setup
33
+ @@bc = PageTemplate::BlockCommand.new()
34
+ end
35
+
36
+ def test_add
37
+ c = PageTemplate::CommentCommand.new('foo')
38
+ assert(@@bc.add(c),
39
+ "Use BlockCommand#add to add a new Command to the Block")
40
+ assert_raises(TypeError,
41
+ "BlockCommand#add only accepts Commands") {
42
+ @@bc.add(23)
43
+ }
44
+ end
45
+
46
+ def test_length
47
+ assert_equal(0, @@bc.length,
48
+ "Use BlockCommand#length to find out how many commands are "\
49
+ "in the Block")
50
+ c = PageTemplate::CommentCommand.new('foo')
51
+ @@bc.add(c)
52
+ assert_equal(1, @@bc.length,
53
+ "BlockCommand#length reflects new Commands that have been " \
54
+ "added")
55
+ end
56
+
57
+ def test_output
58
+ c_1 = PageTemplate::TextCommand.new("Dude! What's mine say?\n")
59
+ c_2 = PageTemplate::TextCommand.new("Sweet! What's mine say?\n")
60
+ c_3 = PageTemplate::TextCommand.new("That never gets old.\n")
61
+ @@bc.add(c_1)
62
+ @@bc.add(c_2)
63
+ @@bc.add(c_3)
64
+ expected = c_1.output + c_2.output + c_3.output
65
+ assert_equal(expected, @@bc.output)
66
+ end
67
+ end
68
+
69
+ class TestCaseCommand < Test::Unit::TestCase
70
+ def setup
71
+ @@cc = PageTemplate::CaseCommand.new('case foo')
72
+ end
73
+
74
+ def test_initialize
75
+ assert(PageTemplate::CaseCommand.new('case foo'))
76
+ end
77
+
78
+ def test_add
79
+ assert(@@cc.add(PageTemplate::TextCommand.new("More foo?")))
80
+ end
81
+
82
+ def test_current_case
83
+ # Shouldn't I be failing? I don't see an accessor for CaseCommand.current_case
84
+ assert_equal(nil, @@cc.current_case)
85
+ @@cc.modifies?("when 1")
86
+ assert_equal("1", @@cc.current_case)
87
+ end
88
+
89
+ def test_modifies_eh
90
+ assert(@@cc.modifies?("when 1"))
91
+ assert(@@cc.modifies?("else"))
92
+ assert_equal(false, @@cc.modifies?("unless"))
93
+ end
94
+
95
+ def test_output
96
+ c_1 = PageTemplate::TextCommand.new("I am 1")
97
+ c_2 = PageTemplate::TextCommand.new("I am 2")
98
+ c_3 = PageTemplate::TextCommand.new("I don't know what I am")
99
+ @@cc.add(c_3)
100
+ @@cc.modifies?("when 1")
101
+ @@cc.add(c_1)
102
+ @@cc.modifies?("when 2")
103
+ @@cc.add(c_2)
104
+ ns = PageTemplate::Namespace.new()
105
+ ns["foo"] = nil
106
+ assert_equal(c_3.output, @@cc.output(ns))
107
+ ns["foo"] = "1"
108
+ assert_equal(c_1.output, @@cc.output(ns))
109
+ ns["foo"] = "2"
110
+ assert_equal(c_2.output, @@cc.output(ns))
111
+ end
112
+ end
113
+
114
+ class TestCommand < Test::Unit::TestCase
115
+ def test_output
116
+ assert_raises(NotImplementedError,
117
+ "Command is not to be directly used.") {
118
+ c = PageTemplate::Command.new
119
+ c.output
120
+ }
121
+ end
122
+ end
123
+
124
+ class TestCommentCommand < Test::Unit::TestCase
125
+ def test_output
126
+ c = PageTemplate::CommentCommand.new("foo")
127
+ assert_equal('[ Comment: foo ]', c.to_s,
128
+ "CommentCommand stores the input string as a comment.")
129
+ assert_equal('', c.output(),
130
+ "CommentCommands output the empty string regardless of content.")
131
+ c = PageTemplate::CommentCommand.new
132
+ assert_equal('[ Comment: ]', c.to_s,
133
+ "Comment defaults to the string '' if you don't provide a comment.")
134
+ c = PageTemplate::CommentCommand.new(23)
135
+ assert_equal('[ Comment: 23 ]', c.to_s,
136
+ "CommentCommand#to_s automatically converts comments to Strings.")
137
+ end
138
+ end
139
+
140
+ class TestDefaultPreprocessor < Test::Unit::TestCase
141
+ def test_process
142
+ assert_equal("str", PageTemplate::DefaultPreprocessor.process("str"),
143
+ "DefaultPreprocessor.process returns a copy of the string handed to it.")
144
+ end
145
+
146
+ def test_reverse
147
+ assert_equal("rts", PageTemplate::DefaultPreprocessor.reverse("str"),
148
+ "DefaultPreprocessor.reverse returns a reversed copy of the string handed to it.")
149
+ end
150
+
151
+ def test_escapeHTML
152
+ assert_equal("&lt;str&gt;", PageTemplate::DefaultPreprocessor.escapeHTML("<str>"),
153
+ "DefaultPreprocessor.escapeHTML returns a copy of the string input with HTML converted to entities.")
154
+ end
155
+
156
+ def test_escapeURI
157
+ assert_equal("+", PageTemplate::DefaultPreprocessor.escapeURI(" "),
158
+ "DefaultPreprocessor.escapeURI returns a URI-encoded copy of the string handed to it.")
159
+ end
160
+ end
161
+
162
+ class TestFileSource < Test::Unit::TestCase
163
+ def setup
164
+ @p = PageTemplate::Parser.new()
165
+ @@fs = PageTemplate::FileSource.new(@p.args)
166
+ end
167
+
168
+ def test_get
169
+ filename = 'tdata/dummy.txt'
170
+ location = File.join(Dir.getwd, filename)
171
+ assert_equal(IO.read(location), @@fs.get(filename))
172
+ end
173
+
174
+ def test_get_filename
175
+ filename = 'tdata/dummy.txt'
176
+ location = File.join(Dir.getwd, filename)
177
+ assert_equal(location, @@fs.get_filename(filename))
178
+ end
179
+
180
+ def test_paths
181
+ assert_equal([Dir.getwd], @@fs.paths)
182
+ end
183
+ end
184
+
185
+ class TestIfCommand < Test::Unit::TestCase
186
+ def setup
187
+ @@if = PageTemplate::IfCommand.new("if flag")
188
+ @@unless = PageTemplate::IfCommand.new("unless flag")
189
+ end
190
+
191
+ def test_initialize
192
+ good_1 = "if flag"
193
+ good_2 = "unless flag"
194
+ bad_1 = "heck no"
195
+ assert(PageTemplate::IfCommand.new(good_1))
196
+ assert(PageTemplate::IfCommand.new(good_2))
197
+ assert_raises(ArgumentError,
198
+ "IfCommand will not initialize without a command declaring 'if/unless'") {
199
+ PageTemplate::IfCommand.new(bad_1)
200
+ }
201
+ end
202
+
203
+ def test_add
204
+ c = PageTemplate::TextCommand.new("bleargh")
205
+ assert(@@if.add(c),
206
+ "IfCommand#add adds a command to the block to be executed when true")
207
+ assert(@@unless.add(c),
208
+ "IfCommand#add adds a command to the block to be executed when false")
209
+ end
210
+
211
+ def test_closes_eh
212
+ closers = [
213
+ [ "endif", 0, nil ],
214
+ [ "/if", 0, nil ],
215
+ [ "end if", 0, nil ],
216
+ [ "/ if", 0, nil ],
217
+ [ "endunless", nil, 0 ],
218
+ [ "/unless", nil, 0 ],
219
+ [ "end unless", nil, 0 ],
220
+ [ "/ unless", nil, 0 ]
221
+ ]
222
+
223
+ closers.each do |x|
224
+ assert_equal(x[1], @@if.closes?(x[0]))
225
+ assert_equal(x[2], @@unless.closes?(x[0]))
226
+ end
227
+
228
+ end
229
+
230
+ def test_modifies_eh
231
+ assert_equal(false, @@if.modifies?("otherwise"))
232
+ assert(@@if.modifies?("else"))
233
+ assert_raises(ArgumentError) { @@if.modifies?("else") }
234
+ assert_equal(false, @@unless.modifies?("otherwise"))
235
+ assert(@@unless.modifies?("else"))
236
+ assert_raises(ArgumentError) { @@unless.modifies?("else") }
237
+ end
238
+
239
+ def test_output
240
+ ns = PageTemplate::Namespace.new()
241
+ c = PageTemplate::TextCommand.new("dude!")
242
+ assert_equal("dude!", c.output())
243
+ @@if.add(c)
244
+ @@unless.add(c)
245
+ ns["flag"] = true
246
+ assert_equal("dude!", @@if.output(ns))
247
+ assert_equal("", @@unless.output(ns))
248
+ ns["flag"] = false
249
+ assert_equal("", @@if.output(ns))
250
+ assert_equal("dude!", @@unless.output(ns))
251
+ @@if.modifies?("else")
252
+ @@unless.modifies?("else")
253
+ c2 = PageTemplate::TextCommand.new("sweet!")
254
+ @@if.add(c2)
255
+ @@unless.add(c2)
256
+ ns["flag"] = true
257
+ assert_equal("dude!", @@if.output(ns))
258
+ assert_equal("sweet!", @@unless.output(ns))
259
+ ns["flag"] = false
260
+ assert_equal("sweet!", @@if.output(ns))
261
+ assert_equal("dude!", @@unless.output(ns))
262
+ end
263
+ end
264
+
265
+ class TestIncludeCommand < Test::Unit::TestCase
266
+ def test_output
267
+ p = PageTemplate::Parser.new()
268
+ p['dummy'] = '/tdata/dummy.txt'
269
+ i = PageTemplate::IncludeCommand.new('include dummy', p)
270
+ p.load('tdata/dummy.txt')
271
+ assert_equal(p.output, i.output(p.namespace))
272
+ end
273
+ end
274
+
275
+ class TestLoopCommand < Test::Unit::TestCase
276
+ def setup
277
+ @@in = PageTemplate::LoopCommand.new("in list")
278
+ @@loop = PageTemplate::LoopCommand.new("loop list")
279
+ end
280
+
281
+ def test_initialize
282
+ good_1 = "in list"
283
+ assert(PageTemplate::LoopCommand.new(good_1))
284
+ good_2 = "in loop"
285
+ assert(PageTemplate::LoopCommand.new(good_2))
286
+ good_3 = "in list: item"
287
+ assert(PageTemplate::LoopCommand.new(good_3))
288
+ bad_1 = "bad list"
289
+ assert_raises(ArgumentError) { PageTemplate::LoopCommand.new(bad_1) }
290
+ bad_2 = "loop"
291
+ assert_raises(ArgumentError) { PageTemplate::LoopCommand.new(bad_2) }
292
+ end
293
+
294
+ def test_add
295
+ c = PageTemplate::TextCommand.new("bleargh")
296
+ assert(@@loop.add(c),
297
+ "LoopCommand#add adds a command to the block to be executed in the loop")
298
+ end
299
+
300
+ def test_closes_eh
301
+ closers = [
302
+ [ "endin", 0, nil],
303
+ [ "/in", 0, nil],
304
+ [ "end in", 0, nil],
305
+ [ "/ in", 0, nil],
306
+ [ "endloop", nil, 0],
307
+ [ "/loop", nil, 0],
308
+ [ "end loop", nil, 0],
309
+ [ "/ loop", nil, 0]
310
+ ]
311
+
312
+ closers.each do |x|
313
+ assert_equal(x[1], @@in.closes?(x[0]),
314
+ "#{x[0]} on 'in' is expected to be #{x[1]}")
315
+ assert_equal(x[2], @@loop.closes?(x[0]),
316
+ "#{x[0]} on 'loop' is expected to be #{x[2]}")
317
+ end
318
+ end
319
+
320
+ def test_modifies_eh
321
+ assert_equal(false, @@loop.modifies?("otherwise"))
322
+ assert(@@loop.modifies?("else"))
323
+ assert_raises(ArgumentError) { @@loop.modifies?("else") }
324
+ end
325
+
326
+ def test_output
327
+ ns = PageTemplate::Namespace.new()
328
+ ns["list"] = [1, 2, 3]
329
+
330
+ # Simple TextCommands in a Loop
331
+ c = PageTemplate::TextCommand.new("Dude!")
332
+ loop_1 = PageTemplate::LoopCommand.new("loop list")
333
+ loop_1.add(c)
334
+ assert_equal("Dude!Dude!Dude!", loop_1.output(ns))
335
+ c_1 = PageTemplate::TextCommand.new("Sweet!")
336
+ loop_1.add(c_1)
337
+ assert_equal("Dude!Sweet!Dude!Sweet!Dude!Sweet!", loop_1.output(ns))
338
+ ns["list"] = nil
339
+ assert_equal("", loop_1.output(ns))
340
+
341
+ # Simple TextCommands in a Loop with an Else block
342
+ c_1 = PageTemplate::TextCommand.new("Sweet!")
343
+ c_2 = PageTemplate::TextCommand.new("Dude?")
344
+ loop_2 = PageTemplate::LoopCommand.new("loop list")
345
+ loop_2.add(c_1)
346
+ loop_2.modifies?("else")
347
+ loop_2.add(c_2)
348
+ ns["list"] = nil
349
+ assert_equal("Dude?", loop_2.output(ns))
350
+ ns["list"] = [1, 2]
351
+ assert_equal("Sweet!Sweet!", loop_2.output(ns))
352
+
353
+ # A Loop that uses its metavariables
354
+ c_1 = PageTemplate::TextCommand.new("First Post!")
355
+ c_2 = PageTemplate::TextCommand.new("I know kung fu.")
356
+ c_3 = PageTemplate::TextCommand.new("Prove it.")
357
+ c_4 = PageTemplate::TextCommand.new("Last Post!")
358
+ if_1 = PageTemplate::IfCommand.new("if __FIRST__")
359
+ if_1.add(c_1)
360
+ if_2 = PageTemplate::IfCommand.new("if __ODD__")
361
+ if_2.add(c_2)
362
+ if_2.modifies?("else")
363
+ if_2.add(c_3)
364
+ if_3 = PageTemplate::IfCommand.new("if __LAST__")
365
+ if_3.add(c_4)
366
+ loop_1 = PageTemplate::LoopCommand.new("in cliches")
367
+ loop_1.add(if_1)
368
+ loop_1.add(if_2)
369
+ loop_1.add(if_3)
370
+ ns["cliches"] = [1, 2]
371
+ expected = c_1.output + c_2.output + c_3.output + c_4.output
372
+ assert_equal(expected, loop_1.output(ns))
373
+
374
+ # A Loop with a named iterator over the items in its list
375
+ loop_1 = PageTemplate::LoopCommand.new("in list: item")
376
+ c_1 = PageTemplate::ValueCommand.new("item", PageTemplate::Parser.new())
377
+ c_2 = PageTemplate::TextCommand.new(" ")
378
+ loop_1.add(c_1)
379
+ loop_1.add(c_2)
380
+ ns["list"] = [0, -1, 2, -3, 4]
381
+ expected = ns["list"].join(" ") + " "
382
+ assert_equal(expected, loop_1.output(ns))
383
+ end
384
+ end
385
+
386
+ class TestNamespace < Test::Unit::TestCase
387
+ def setup
388
+ @@ns = PageTemplate::Namespace.new
389
+ end
390
+
391
+ def test_clear_cache
392
+ assert(@@ns.clear_cache,
393
+ "Use Namespace#clear_cache to reset a Namespace's values.")
394
+ end
395
+
396
+ def test_get
397
+ assert_nil(@@ns.get("Santa Claus"),
398
+ "If a key is not set, Namespace#get returns nil")
399
+ @@ns.set("Santa Claus", "Kris Kringle")
400
+ assert_equal("Kris Kringle", @@ns.get("Santa Claus"),
401
+ "Use Namespace#get to access to Namespace value")
402
+
403
+ assert_equal(nil, @@ns.get("occupation"))
404
+ p = PageTemplate::Namespace.new()
405
+ p["occupation"] = "Foole"
406
+ @@ns.parent = p
407
+ assert_equal("Foole", @@ns.get("occupation"),
408
+ "Namespace#get will search parent Namespaces if they are available")
409
+
410
+ assert_equal(nil, @@ns.get("abs"))
411
+ @@ns.object = -5
412
+ assert_equal(5, @@ns.get("abs"),
413
+ "Namespace#get will send get requests to a contained object if it is available")
414
+ end
415
+
416
+ def test_index
417
+ @@ns.set("Santa Claus", "Kris Kringle")
418
+ assert_equal("Kris Kringle", @@ns["Santa Claus"],
419
+ "Use Namespace#get to access to Namespace value")
420
+ end
421
+
422
+ def test_index_equals
423
+ assert(@@ns["Dude"] = "Sweet!",
424
+ "Namespace#[]= is an alias for Namespace#set.")
425
+ assert_equal("Sweet!", @@ns["Dude"])
426
+ end
427
+
428
+ def test_object
429
+ assert_equal(nil, @@ns.object)
430
+ @@ns.object = -5
431
+ assert_equal(-5, @@ns.object,
432
+ "Namespace#object is a getter for a Namespace's object")
433
+ end
434
+
435
+ def test_object_equals
436
+ obj = -5
437
+ assert(@@ns.object = obj,
438
+ "Namespace#object= is a setter for a Namespace's object")
439
+ assert_equal(-4, @@ns.get('succ'),
440
+ "Namespace#object= defines an object to search for synonyms")
441
+ end
442
+
443
+ def test_parent
444
+ assert_equal(nil, @@ns.parent)
445
+ p = PageTemplate::Namespace.new()
446
+ p["occupation"] = "Foole"
447
+ @@ns.parent = p
448
+ assert_equal(p, @@ns.parent,
449
+ "'parent' is a getter for a parent Namespace")
450
+ end
451
+
452
+ def test_parent_equals
453
+ p = PageTemplate::Namespace.new()
454
+ p["occupation"] = "Foole"
455
+ assert(@@ns.parent = p,
456
+ "'parent' is a setter for a parent Namespace")
457
+ end
458
+
459
+ def test_set
460
+ assert(@@ns.set("dude", "sweet!"),
461
+ "Use Namespace#set to set names to a value in your Namespace.")
462
+ assert(@@ns["sweet"] = "dude",
463
+ "Namespace#[]= is an alias for Namespace#set.")
464
+ end
465
+
466
+ def test_true_eh
467
+ assert_equal(false, @@ns.true?("enlightenment"),
468
+ "If a flag doesn't exist, then Namespace#true? returns false.")
469
+ @@ns["dude"] = false
470
+ assert_equal(false, @@ns.true?("dude"),
471
+ "If a flag is set to false, then Namespace#true? returns false.")
472
+ @@ns["dude"] = true
473
+ assert_equal(true, @@ns.true?("dude"),
474
+ "If a flag is set to true, then Namespace#true? returns true.")
475
+ @@ns["dude"] = "sweet!"
476
+ assert_equal(true, @@ns.true?("dude"),
477
+ "If a flag is set to pretty much anything but false, then Namespace#true? returns true.")
478
+ end
479
+ end
480
+
481
+ class TestParser < Test::Unit::TestCase
482
+ def setup
483
+ @@tc = PageTemplate::TextCommand.new("[ Template 'foo' not found ]")
484
+ @@p = PageTemplate::Parser.new()
485
+ end
486
+
487
+ def test_args
488
+ assert_equal({}, @@p.args)
489
+ end
490
+
491
+ def test_clearCommands
492
+ @@p.clearCommands
493
+ assert_equal(nil, @@p.commands)
494
+ end
495
+
496
+ def test_commands
497
+ assert_equal(nil, @@p.commands)
498
+ @@p.load('foo')
499
+ assert(@@p.commands)
500
+ end
501
+
502
+ def test_compile
503
+ assert_equal(@@tc.output, @@p.compile('foo').output)
504
+ end
505
+
506
+ def test_default_processor
507
+ assert_equal(:process, @@p.default_processor)
508
+ end
509
+
510
+ def test_load
511
+ assert(@@p.load('foo'))
512
+ end
513
+
514
+ def test_output
515
+ assert_equal('', @@p.output)
516
+ @@p.load('foo')
517
+ assert_equal(@@tc.output, @@p.output)
518
+ end
519
+
520
+ def test_parse
521
+ assert_equal('', @@p.parse('').output)
522
+ assert_equal('foo', @@p.parse('foo').output)
523
+ @@p['x'] = 'y'
524
+ assert_equal('y', @@p.parse('[%var x%]').output)
525
+ end
526
+
527
+ def test_preprocessor
528
+ assert_equal(PageTemplate::DefaultPreprocessor, @@p.preprocessor)
529
+ end
530
+
531
+ def test_index
532
+ assert_equal(nil, @@p['foo'])
533
+ @@p['foo'] = "Are you sick of foo yet?"
534
+ assert_equal("Are you sick of foo yet?", @@p['foo'])
535
+ end
536
+
537
+ def test_index_equals
538
+ assert(@@p['foo'] = "I'm sick of foo!")
539
+ end
540
+ end
541
+
542
+ class TestSyntaxGlossary < Test::Unit::TestCase
543
+ def setup
544
+ @@g = PageTemplate::SyntaxGlossary.new(
545
+ /<(.+?)>/,
546
+
547
+ /^--/i => PageTemplate::CommentCommand
548
+ )
549
+ end
550
+
551
+ def test_define
552
+ assert_equal(PageTemplate::UnknownCommand, @@g.lookup("foo"))
553
+ assert_raises(ArgumentError,
554
+ "SyntaxGlossary#define requires a Regexp for its first arg") {
555
+ @@g.define("foo", PageTemplate::CommentCommand)
556
+ }
557
+ assert(@@g.define(/^foo/, PageTemplate::CommentCommand),
558
+ "Use SyntaxGlossary#define to add or change SyntaxGlossary entries")
559
+ end
560
+
561
+ def test_define_global_var
562
+ assert_equal(PageTemplate::UnknownCommand, @@g.lookup("d"))
563
+ assert(@@g.define_global_var(/d/),
564
+ "Use SyntaxGlossary#define_global_var to create explicit ValueCommands")
565
+ assert_equal(PageTemplate::ValueCommand, @@g.lookup("d"))
566
+ end
567
+
568
+ def test_directive
569
+ assert_equal(/<(.+?)>/, @@g.directive)
570
+ end
571
+
572
+ def test_directive_equals
573
+ assert_equal(/<(.+?)>/, @@g.directive)
574
+ directive = /\[.+?\]/
575
+ assert(@@g.directive = directive)
576
+ assert_equal(directive, @@g.directive)
577
+ end
578
+
579
+ def test_lookup
580
+ assert_equal(PageTemplate::CommentCommand, @@g.lookup("--"))
581
+ assert_equal(PageTemplate::UnknownCommand, @@g.lookup("++"))
582
+ end
583
+ end
584
+
585
+ class TestTextCommand < Test::Unit::TestCase
586
+ def test_output
587
+ c = PageTemplate::TextCommand.new("Dude!")
588
+ assert_equal("Dude!", c.output(),
589
+ "A TextCommand outputs a simple text string.")
590
+ end
591
+ end
592
+
593
+ class TestUnknownCommand < Test::Unit::TestCase
594
+ def test_output
595
+ parser = PageTemplate::Parser.new()
596
+ uc = PageTemplate::UnknownCommand.new("waffle olive mango", parser)
597
+ assert_equal("[ Unknown Command: waffle olive mango ]", uc.output(parser.namespace))
598
+ end
599
+ end
600
+
601
+ class TestValueCommand < Test::Unit::TestCase
602
+ def test_output
603
+ p = PageTemplate::Parser.new()
604
+ p['x'] = 'y'
605
+ v = PageTemplate::ValueCommand.new('var x', p)
606
+ assert_equal('y', v.output())
607
+ assert_equal('y', v.output(p.namespace))
608
+ p['x'] = 1
609
+ v = PageTemplate::ValueCommand.new('var x.succ.foo', p)
610
+ w = PageTemplate::ValueCommand.new('var x.succ.succ.succ', p)
611
+ assert_equal('4',w.output(p.namespace))
612
+ assert_equal('',v.output(p.namespace))
613
+ end
614
+ end
615
+ end
616
+