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/TC_PageTemplate.rb DELETED
@@ -1,974 +0,0 @@
1
- #!/usr/local/bin/ruby -w
2
-
3
- require 'test/unit'
4
- require './lib/PageTemplate.rb'
5
-
6
- require "date"
7
-
8
- class TC_Namespace < Test::Unit::TestCase
9
- @@namespace = Namespace.new()
10
-
11
- def testNamespaceSet
12
- assert(@@namespace.set("name", "Frank"))
13
- end
14
-
15
- def testNamespaceGet
16
- @@namespace.set("name", "Frank")
17
- assert_equal("Frank", @@namespace.get("name"))
18
- end
19
-
20
- def testTrue
21
- assert_not_nil(@@namespace.true?("name"))
22
- assert_nil(@@namespace.true?("null"))
23
- @@namespace.set("falseFlag", false)
24
- assert_nil(@@namespace.true?("falseFlag"))
25
- end
26
-
27
- def testNamespacePush
28
- assert_equal("Frank", @@namespace.get("name"))
29
- names = { 'name' => "Eddie Baby" }
30
-
31
- # Pushing a hash (and nested namespace)
32
- assert(@@namespace.push(names))
33
- assert_equal("Eddie Baby", @@namespace.get("name"))
34
-
35
- # Pushing an object
36
- date = Date.today()
37
- date2 = Date.today()
38
-
39
- @@namespace.push(date)
40
- assert_equal(date2.year, @@namespace.get("year"))
41
- end
42
-
43
- def testNamespacePop
44
- names = { 'name' => "Eddie Baby" }
45
- @@namespace.push(names)
46
- assert_equal("Eddie Baby", @@namespace.get("name"))
47
- assert(@@namespace.pop())
48
- assert_equal("Frank", @@namespace.get("name"))
49
- assert_nil(@@namespace.pop())
50
- end
51
-
52
- end
53
-
54
- class TC_Command < Test::Unit::TestCase
55
- @@command = Command.new()
56
-
57
- def testCommandOutput
58
- assert_raises(NotImplementedError) { @@command.output() }
59
- end
60
-
61
- end
62
-
63
- class TC_CommentCommand < Test::Unit::TestCase
64
- @@command = CommentCommand.new()
65
-
66
- def testCommentCommand
67
- assert_equal(nil, @@command.output,
68
- "CommentCommands don't have any output")
69
- end
70
-
71
- end
72
-
73
- class TC_BlockCommand < Test::Unit::TestCase
74
- @@command = BlockCommand.new()
75
-
76
- def testAddCommand
77
- aCommand = TextCommand.new("Hello")
78
- assert(@@command.add(aCommand))
79
-
80
- assert_raises(TypeError) { @@command.add("Hello") }
81
- end
82
-
83
- def testOutput
84
- bCommand = TextCommand.new(", ")
85
- cCommand = TextCommand.new("World")
86
-
87
- @@command.add(bCommand)
88
- @@command.add(cCommand)
89
-
90
- assert_equal("Hello, World", @@command.output)
91
- end
92
- end
93
-
94
- class TC_TextCommand < Test::Unit::TestCase
95
- @@command = TextCommand.new("Just testing")
96
-
97
- def testCommandOutput
98
- assert_equal("Just testing", @@command.output)
99
- end
100
-
101
- end
102
-
103
- class TC_ValueCommand < Test::Unit::TestCase
104
- @@valueCommand = ValueCommand.new("name")
105
-
106
- def testOutput
107
- ns = Namespace.new()
108
- ns.set("name", "Frank")
109
- assert_equal(ns.get("name"), @@valueCommand.output(ns))
110
- end
111
- end
112
-
113
- class TC_IfCommand < Test::Unit::TestCase
114
-
115
- def testOutput
116
- aCommand = TextCommand.new("Programming ")
117
- bCommand = TextCommand.new("Ruby")
118
- commandBlock = BlockCommand.new()
119
- commandBlock.add(aCommand)
120
- commandBlock.add(bCommand)
121
-
122
- ns = Namespace.new()
123
- ns.set("testing", 1)
124
-
125
- # First test for output with true condition
126
- ifCommand = IfCommand.new("testing", commandBlock)
127
- assert_equal("Programming Ruby", ifCommand.output(ns))
128
-
129
- # Test for nil output with false condition
130
- ifCommand = IfCommand.new("production", commandBlock)
131
- assert_nil(ifCommand.output(ns))
132
-
133
- ns.set("production", false)
134
- assert_nil(ifCommand.output(ns))
135
- end
136
- end
137
-
138
- class TC_UnlessCommand < Test::Unit::TestCase
139
- def testOutput
140
- aCommand = TextCommand.new("Dude!")
141
- bCommand = TextCommand.new("Sweet!")
142
- commandBlock = BlockCommand.new()
143
- commandBlock.add(aCommand)
144
- commandBlock.add(bCommand)
145
- ns = Namespace.new()
146
- ns["testing"] = false
147
-
148
- # First test for output with false condition
149
- unlessCommand = UnlessCommand.new("testing", commandBlock)
150
- assert_equal("Dude!Sweet!", unlessCommand.output(ns))
151
- ns["testing"] = true
152
- assert_nil(unlessCommand.output(ns))
153
- end
154
- end
155
-
156
- class TC_IfElseCommand < Test::Unit::TestCase
157
-
158
- def testOutput
159
- aCommand = TextCommand.new("This is ")
160
- bCommand = TextCommand.new("True")
161
- cCommand = TextCommand.new("False")
162
- ifBlock = BlockCommand.new()
163
- ifBlock.add(aCommand)
164
- ifBlock.add(bCommand)
165
- elseBlock = BlockCommand.new()
166
- elseBlock.add(aCommand)
167
- elseBlock.add(cCommand)
168
-
169
- ns = Namespace.new()
170
- ns.set("testing", 1)
171
-
172
- # First test for output with true condition
173
- ifElseCommand = IfElseCommand.new("testing", ifBlock, elseBlock)
174
- assert_equal("This is True", ifElseCommand.output(ns))
175
-
176
- # Test for output with false condition
177
- ifElseCommand = IfElseCommand.new("live", ifBlock, elseBlock)
178
- assert_equal("This is False", ifElseCommand.output(ns))
179
- end
180
-
181
- end
182
-
183
- class TC_LoopCommand < Test::Unit::TestCase
184
-
185
- def testOutput
186
- # Build a list of hashes for the namespace
187
- loopItems = [
188
- { 'x'=> '1 ', 'y'=> '2 ', 'z'=> '3' },
189
- { 'x'=> 'a ', 'y'=> 'b ', 'z'=> 'c' },
190
- ]
191
- ns = Namespace.new()
192
- ns.set('list', loopItems)
193
-
194
- # Build a command list
195
- xCommand = ValueCommand.new('x')
196
- yCommand = ValueCommand.new('y')
197
- zCommand = ValueCommand.new('z')
198
- commandBlock = BlockCommand.new()
199
- commandBlock.add(xCommand)
200
- commandBlock.add(yCommand)
201
- commandBlock.add(zCommand)
202
-
203
- loopCommand = LoopCommand.new('list', commandBlock)
204
- assert_equal("1 2 3a b c", loopCommand.output(ns))
205
- loopCommand = LoopCommand.new('noshow', commandBlock)
206
- assert_nil(loopCommand.output(ns))
207
- ns.set("string", "hello")
208
- loopCommand = LoopCommand.new("string", commandBlock)
209
- assert_raises(TypeError) { loopCommand.output(ns) }
210
- end
211
- end
212
-
213
- class TC_LoopElseCommand < Test::Unit::TestCase
214
-
215
- def testOutput
216
- # Build a list of hashes for the namespace
217
- loopItems = [
218
- { 'x'=> '1 ', 'y'=> '2 ', 'z'=> '3' },
219
- { 'x'=> 'a ', 'y'=> 'b ', 'z'=> 'c' },
220
- ]
221
- ns = Namespace.new()
222
- ns.set('list', loopItems)
223
-
224
- # Build a command list
225
- xCommand = ValueCommand.new('x')
226
- yCommand = ValueCommand.new('y')
227
- zCommand = ValueCommand.new('z')
228
- commandBlock = BlockCommand.new()
229
- commandBlock.add(xCommand)
230
- commandBlock.add(yCommand)
231
- commandBlock.add(zCommand)
232
-
233
- # Build a command list for the else block
234
- aCommand = TextCommand.new("In the else block")
235
- elseCommands = BlockCommand.new()
236
- elseCommands.add(aCommand)
237
-
238
- # Build the LoopCommand
239
- loopCommand =
240
- LoopElseCommand.new('list', commandBlock, elseCommands)
241
-
242
- # Test for output when loop variable is true
243
- assert_equal("1 2 3a b c", loopCommand.output(ns))
244
-
245
- # Test for nil when loop variable is false
246
- loopCommand =
247
- LoopElseCommand.new('noshow', commandBlock, elseCommands)
248
- assert_equal("In the else block", loopCommand.output(ns))
249
- end
250
-
251
- end
252
-
253
-
254
- class TC_NestedCommands < Test::Unit::TestCase
255
-
256
- def testNestedIfs
257
- ns = Namespace.new()
258
-
259
- innerBlock = BlockCommand.new()
260
- innerBlock.add(TextCommand.new("Inner"))
261
- innerIf = IfCommand.new("inner", innerBlock)
262
-
263
- outerBlock = BlockCommand.new()
264
- outerBlock.add(TextCommand.new("Outer"))
265
- outerBlock.add(innerIf)
266
- outerIf = IfCommand.new("outer", outerBlock)
267
-
268
- mainBlock = BlockCommand.new()
269
- mainBlock.add(outerIf)
270
-
271
- # True in a true should yield result of both command lists
272
- ns.set("outer", 1)
273
- ns.set("inner", 1)
274
- assert_equal("OuterInner", mainBlock.output(ns))
275
-
276
- # False in a true should yield result of outside list
277
- ns.set("inner", false)
278
- assert_equal("Outer", mainBlock.output(ns))
279
-
280
- # False in a false should yield nil
281
- ns.set("outer", false)
282
- assert_nil(mainBlock.output(ns))
283
-
284
- # True in a false should yield nil
285
- ns.set("inner", 1)
286
- ns.set("outer", false)
287
- assert_nil(mainBlock.output(ns))
288
- end
289
-
290
- def testNestedLoops
291
- loop1 = [
292
- { 'a' => 'o1 ', 'b' => 'o2 ', 'c' => [
293
- { 'x' => 'i1 ', 'y' => 'i2 ', 'z' => 'i3 ' },
294
- { 'x' => 'i4 ', 'y' => 'i5 ', 'z' => 'i6 ' } ]
295
- },
296
- { 'a' => 'o3 ', 'b' => 'o4 ', 'c' => [
297
- { 'x' => 'j1 ', 'y' => 'j2 ', 'z' => 'j3 ' },
298
- { 'x' => 'j4 ', 'y' => 'j5 ', 'z' => 'j6 ' } ]
299
- }
300
- ]
301
- ns = Namespace.new()
302
- ns.set("loop", loop1)
303
-
304
- aCommand = ValueCommand.new('a')
305
- bCommand = ValueCommand.new('b')
306
- xCommand = ValueCommand.new('x')
307
- yCommand = ValueCommand.new('y')
308
- zCommand = ValueCommand.new('z')
309
- innerBlock = BlockCommand.new()
310
- innerBlock.add(xCommand)
311
- innerBlock.add(yCommand)
312
- innerBlock.add(zCommand)
313
- cCommand = LoopCommand.new('c', innerBlock)
314
- outerBlock = BlockCommand.new()
315
- outerBlock.add(aCommand)
316
- outerBlock.add(bCommand)
317
- outerBlock.add(cCommand)
318
- loopCommand = LoopCommand.new('loop', outerBlock)
319
-
320
- # Loop in a loop should yield lots of text ;-)
321
- # 'o1 o2 i1 i2 i3 i4 i5 i6 o3 o4 j1 j2 j3 j4 j5 j6 '
322
- assert_equal('o1 o2 i1 i2 i3 i4 i5 i6 o3 o4 j1 j2 j3 j4 j5 j6 ',
323
- loopCommand.output(ns))
324
-
325
- # False in a loop should yield outside loop
326
- # 'o1 o2 o3 o4 '
327
- loop1.each { |item| item['c'] = [] }
328
- assert_equal('o1 o2 o3 o4 ', loopCommand.output(ns))
329
- end
330
-
331
- def testMixedNesting
332
- # Loop in If
333
- loop1 = [
334
- { 'a' => '1 ', 'b' => '2 ', 'c' => '3 ' },
335
- { 'a' => 'x ', 'b' => 'y ', 'c' => 'z ' }
336
- ]
337
-
338
- aCommand = ValueCommand.new('a')
339
- bCommand = ValueCommand.new('b')
340
- cCommand = ValueCommand.new('c')
341
- loopBlock = BlockCommand.new()
342
- loopBlock.add(aCommand)
343
- loopBlock.add(bCommand)
344
- loopBlock.add(cCommand)
345
- loopCommand = LoopCommand.new("loop", loopBlock)
346
- ifBlock = BlockCommand.new()
347
- ifBlock.add(loopCommand)
348
- ifCommand = IfCommand.new("flag", ifBlock)
349
-
350
- ns = Namespace.new()
351
- ns.set("loop", loop1)
352
- ns.set("flag", 1)
353
- assert_equal("1 2 3 x y z ", ifCommand.output(ns))
354
- ns.set("flag", false)
355
- assert_nil(ifCommand.output(ns))
356
-
357
- # If in Loop
358
-
359
- dCommand = TextCommand.new("(true)")
360
- ifBlock = BlockCommand.new()
361
- ifBlock.add(dCommand)
362
- ifCommand = IfCommand.new("flag", ifBlock)
363
- loopBlock = BlockCommand.new()
364
- loopBlock.add(aCommand)
365
- loopBlock.add(bCommand)
366
- loopBlock.add(cCommand)
367
- loopBlock.add(ifCommand)
368
- loopCommand = LoopCommand.new("loop", loopBlock)
369
-
370
- loop1[-1]['flag'] = 1
371
- assert_equal("1 2 3 x y z (true)", loopCommand.output(ns))
372
- loop1[-1]['flag'] = false
373
- assert_equal("1 2 3 x y z ", loopCommand.output(ns))
374
- end
375
-
376
- end
377
-
378
- class TC_SyntaxGlossary < Test::Unit::TestCase
379
- @@syntax = Syntax::Glossary.new( /\[%(.+?)%\]/,
380
- 'value' => /var (\w+)/,
381
- 'ifopen' => /if (\w+)/,
382
- 'ifclose' => /endif/,
383
- 'unlessopen' => /unless (\w+)/,
384
- 'unlessclose' => /endunless/,
385
- 'ifbranch' => /else/,
386
- 'loopopen' => /in (\w+)/,
387
- 'loopclose' => /endin/,
388
- 'loopbranch' => /no/,
389
- 'include' => /include (\w+)/,
390
- 'comment' => /--.+?--/
391
- )
392
-
393
- def testGlossary
394
- assert_equal(/\[%(.+?)%\]/, @@syntax.directive)
395
- assert_equal(/in (\w+)/, @@syntax.glossary['loopopen'])
396
-
397
- badSyntax = {
398
- 'flargle' => /flargle/
399
- }
400
- assert_raises(ArgumentError) {
401
- Syntax::Glossary.new(/%(.+?)%/, badSyntax)
402
- }
403
- end
404
-
405
- def testLookup
406
- assert_equal(["value", "name"], @@syntax.lookup("var name"))
407
- assert_equal(["ifopen", "name"], @@syntax.lookup("if name"))
408
- assert_equal(["ifclose", nil], @@syntax.lookup("endif"))
409
- assert_equal(["ifbranch", nil], @@syntax.lookup("else"))
410
- assert_equal(["loopopen", "x"], @@syntax.lookup("in x"))
411
- assert_equal(["loopclose", nil], @@syntax.lookup("endin"))
412
- assert_equal(["loopbranch", nil], @@syntax.lookup("no"))
413
- assert_equal(["include", "foo"], @@syntax.lookup("include foo"))
414
- assert_equal(["comment", nil], @@syntax.lookup("-- blarg! --"))
415
- assert_nil(@@syntax.lookup("foo"))
416
- assert_nil(@@syntax.lookup("foo bar"))
417
- end
418
-
419
- end
420
-
421
- class TC_SyntaxParser < Test::Unit::TestCase
422
- @@parser = Syntax::Parser.new(Syntax::DEFAULT)
423
-
424
- def testParse
425
- assert_equal([[1, ""]], @@parser.parse([""]))
426
- assert_equal([
427
- [1, "Hello "],
428
- [1, ["value", "world"]],
429
- [1, ""]
430
- ], @@parser.parse(["Hello [%var world%]"])
431
- )
432
-
433
- assert_equal([
434
- [1, ""],
435
- [1, ["value", "articleno"]],
436
- [1, ""]
437
- ], @@parser.parse(["[%var articleno%]"]))
438
- assert_equal([
439
- [1, ""],
440
- [1, ["value", "articleno"]],
441
- [1, ""]
442
- ], @@parser.parse(["[% var articleno %\]"]))
443
-
444
- multiline = [
445
- "Hello [%var world%\]",
446
- "[%if return%]",
447
- "Welcome Back!",
448
- "[%endif%]"
449
- ]
450
-
451
- mResult = [
452
- [1, "Hello "],
453
- [1, ["value", "world"]],
454
- [1, ""],
455
- [2, ""],
456
- [2, ["ifopen", "return"]],
457
- [2, ""],
458
- [3, "Welcome Back!"],
459
- [4, ""],
460
- [4, ["ifclose", nil]],
461
- [4, ""]
462
- ]
463
-
464
- assert_equal(mResult, @@parser.parse(multiline))
465
-
466
- multiline = [
467
- "Hello [%var world%\]",
468
- "[%unless return%]",
469
- "Welcome!",
470
- "[%endunless%]"
471
- ]
472
-
473
- mResult = [
474
- [1, "Hello "],
475
- [1, ["value", "world"]],
476
- [1, ""],
477
- [2, ""],
478
- [2, ["unlessopen", "return"]],
479
- [2, ""],
480
- [3, "Welcome!"],
481
- [4, ""],
482
- [4, ["unlessclose", nil]],
483
- [4, ""]
484
- ]
485
-
486
- assert_equal(mResult, @@parser.parse(multiline))
487
-
488
- end
489
-
490
- def testCompile
491
- directives = [
492
- "Hello [%var world%]",
493
- "[%if return%]",
494
- "Welcome Back!",
495
- "[%endif%]"
496
- ]
497
- assert(@@parser.compile(@@parser.parse(directives)))
498
-
499
- directives = [
500
- "Hello [%var world%]",
501
- "[%unless return%]",
502
- "Welcome!",
503
- "[%endunless%]"
504
- ]
505
- assert(@@parser.compile(@@parser.parse(directives)))
506
- end
507
-
508
- def testBuild
509
- assert(@@parser.build("tdata/p/b1.txt"))
510
- # Make sure nothing panics if we ask for the commands again.
511
- assert(@@parser.build("tdata/p/b1.txt"))
512
- assert(@@parser.build("tdata/p/b2.txt"))
513
- end
514
- end
515
-
516
- class TC_SyntaxCachedParser < Test::Unit::TestCase
517
- @@parser = Syntax::CachedParser.new(Syntax::DEFAULT)
518
-
519
- def testBuild
520
- assert(@@parser.build("tdata/p/b1.txt"))
521
- assert(File.exists?("tdata/p/_b1.txt"))
522
-
523
- ns = Namespace.new()
524
- ns["world"] = "World"
525
- assert(@@parser.output(ns))
526
- end
527
-
528
- def testCachedBuild
529
- assert(@@parser.build("tdata/p/cb1.txt"))
530
- assert(File.exists?("tdata/p/_cb1.txt"))
531
- assert(@@parser.reset)
532
- assert(@@parser.build("tdata/p/cb1.txt"))
533
- end
534
-
535
- def testCachedRebuild
536
- # Create a new parser and build with the cached information.
537
- p2 = Syntax::CachedParser.new(Syntax::DEFAULT)
538
- assert(p2.build("tdata/p/cb1.txt"))
539
-
540
- # Clean up after ourselves.
541
- system("rm -f tdata/p/_cb1.txt")
542
- end
543
- end
544
-
545
- class TC_PageTemplate < Test::Unit::TestCase
546
- @@template = PageTemplate.new( 'syntax' => Syntax::DEFAULT )
547
-
548
- def testLoadFile
549
- assert(@@template.load("tdata/t.txt"))
550
- assert_equal("tdata/t.txt", @@template.file)
551
- assert_raises(ArgumentError) { @@template.load("tdata/null") }
552
- end
553
-
554
- def testInitializeTemplate
555
- assert(PageTemplate.new())
556
- assert(PageTemplate.new("filename" => "tdata/t.txt"))
557
- assert_raises(ArgumentError) { PageTemplate.new("filename" => "tdata/null") }
558
- end
559
-
560
- def testSetParameter
561
- assert(@@template.setParameter("name", "PageTemplate"))
562
- end
563
-
564
- def testGetParameter
565
- @@template.setParameter("name", "PageTemplate")
566
- assert_equal("PageTemplate", @@template.getParameter("name"))
567
- assert_nil(@@template.getParameter("null"))
568
- end
569
-
570
- def testHashAccess
571
- @@template['hash'] = 'test'
572
- assert_equal('test', @@template['hash'])
573
- end
574
-
575
- def testOutputNoMarkup
576
- @@template.load("tdata/nm.txt")
577
- cmpFile = File.open("tdata/nm.txt").read()
578
- assert_equal(cmpFile, @@template.output())
579
- end
580
-
581
- def testOutputCommentMarkup
582
- @@template.load("tdata/cm.txt")
583
- assert(@@template.output !~ /\[%--/)
584
- end
585
-
586
- def testOutputValueMarkup
587
-
588
- # Valid markup, value exists.
589
- @@template['title'] = "ValueMarkup"
590
- @@template.load("tdata/v1.txt")
591
- assert(@@template.output =~ /<h1>ValueMarkup<\/h1>/)
592
-
593
- # Valid markup, value does not exist.
594
- @@template['title'] = ""
595
- assert(@@template.output =~ /<h1><\/h1>/)
596
-
597
- # Invalid markup.
598
- @@template.load("tdata/v2.txt")
599
- assert(@@template.output =~ /<h1>title%\]<\/h1>/)
600
- end
601
-
602
- def testOutputUnlessMarkup
603
- @@template['return'] = true
604
- @@template.load("tdata/p/b2.txt")
605
- assert(@@template.output !~ /Welcome/)
606
- @@template['return'] = false
607
- assert(@@template.output =~ /Welcome/)
608
- end
609
-
610
- def testOutputIfMarkup
611
- @@template['flag'] = true
612
- @@template.load("tdata/i1.txt")
613
- assert(@@template.output =~ /It's all true!/)
614
-
615
- @@template['flag'] = false
616
- assert(@@template.output !~ /It's all true!/)
617
-
618
- # Nested if expressions
619
- @@template.load("tdata/i2.txt")
620
-
621
- # 1. Both true.
622
- @@template['outer'] = true
623
- @@template['inner'] = true
624
- assert(@@template.output =~ /This is outer text/)
625
- assert(@@template.output =~ /This is inner text/)
626
-
627
- # 2. Outer true, inner false.
628
- @@template['outer'] = true
629
- @@template['inner'] = false
630
- assert(@@template.output =~ /This is outer text/)
631
- assert(@@template.output !~ /This is inner text/)
632
-
633
- # 3. Inner true, outer false.
634
- @@template['outer'] = false
635
- @@template['inner'] = true
636
- assert(@@template.output !~ /This is outer text/)
637
- assert(@@template.output !~ /This is inner text/)
638
- end
639
-
640
- def testOutputIfBranchMarkup
641
- @@template.load("tdata/ie1.txt")
642
-
643
- # True: if block executes, not else block
644
- @@template["flag"] = true
645
- assert(@@template.output =~ /It's all true!/)
646
- assert(@@template.output !~ /Nothing is true/)
647
-
648
- # False: else block executes, not if block
649
- @@template["flag"] = false
650
- assert(@@template.output =~ /Nothing is true/)
651
- assert(@@template.output !~ /It's all true!/)
652
- end
653
-
654
- def testOutputNestedIfBranchMarkup
655
- @@template.load("tdata/ie2.txt")
656
-
657
- # Outer true, inner true
658
- @@template['outer'] = true
659
- @@template['inner'] = true
660
- assert(@@template.output =~ /Outer is true/)
661
- assert(@@template.output =~ /Inner is true/)
662
-
663
- # Outer true, inner false
664
- @@template['outer'] = true
665
- @@template['inner'] = false
666
- assert(@@template.output =~ /Outer is true/)
667
- assert(@@template.output =~ /Inner is false/)
668
-
669
- # Outer false, inner true
670
- @@template['outer'] = false
671
- @@template['inner'] = true
672
- assert(@@template.output =~ /Outer is false/)
673
- assert(@@template.output =~ /Inner is true/)
674
-
675
- # Outer false, inner false
676
- @@template['outer'] = false
677
- @@template['inner'] = false
678
- assert(@@template.output =~ /Outer is false/)
679
- assert(@@template.output =~ /Inner is false/)
680
- end
681
-
682
- def testOutputLoopMarkup
683
- @@template.load("tdata/l1.txt")
684
-
685
- # Loop Exists
686
- myLoop = []
687
- 1.upto(5) { |v| myLoop << {'value' => v} }
688
-
689
- @@template['loop'] = myLoop
690
- assert(@@template.output =~ /<li>3<\/li>/)
691
-
692
- # Loop Doesn't Exist
693
- @@template['loop'] = false
694
- assert(@@template.output !~ /<li>3<\/li>/)
695
-
696
- # Loop Is Empty List
697
- @@template['loop'] = []
698
- assert(@@template.output !~ /<li>.*?<\/li>/)
699
- end
700
-
701
- def testLoopMetavariables
702
- @@template.load("tdata/metadata.1.txt")
703
- myLoop = []
704
- 1.upto(5) { |v| myLoop << {'v' => v} }
705
-
706
- @@template['loop'] = myLoop
707
- assert(@@template.output =~ /<li>--1--<\/li>/,
708
- "__FIRST__ is set for the first iteration through a loop")
709
- assert(@@template.output =~ /<li>5<\/li>/,
710
- "__FIRST__ is unset after the first iteration through a loop")
711
-
712
- @@template.load("tdata/metadata.2.txt")
713
- assert(@@template.output =~ /<li>--5--<\/li>/,
714
- "__LAST__ is set for the last iteration through a loop")
715
- assert(@@template.output =~ /<li>1<\/li>/,
716
- "__LAST__ is unset before the last iteration through a loop")
717
-
718
- @@template.load("tdata/metadata.3.txt")
719
- assert(@@template.output =~ /<li>--1--<\/li>/,
720
- "__ODD__ is set for odd-numbered iterations through a loop (1st, 3rd, etc)")
721
- assert(@@template.output =~ /<li>2<\/li>/,
722
- "__ODD__ is unset for even-numbered iterations through a loop (2nd, 4th, etc)")
723
- end
724
-
725
- def testOutputNestedLoopMarkup
726
- @@template.load("tdata/l2.txt")
727
-
728
- # Outer Loop Exists, Inner Loop Exists
729
- innerLoop = []
730
- 'a'.upto('f') { |v| innerLoop << { 'value' => v } }
731
- outerLoop = []
732
- 1.upto(5) do |v|
733
- outerLoop << {'value' => v, 'inner' => innerLoop}
734
- end
735
- @@template['loop'] = outerLoop
736
- assert(@@template.output =~ /<li>\d/)
737
- assert(@@template.output =~ /<li>[a-f]<\/li>/)
738
-
739
- # Outer Loop Exists, Inner Loop Empty
740
- outerLoop.each { |x| x['inner'] = [] }
741
- assert(@@template.output !~ /<li>[a-f]<\/li>/)
742
-
743
- # Outer Loop Exists, Alternate Inner Loops Empty
744
- outerLoop.each_with_index do |x, i|
745
- x['inner'] = innerLoop if i % 2 == 0
746
- end
747
- assert(@@template.output =~ /<li>4\s+?<ul><\/ul>/m)
748
- assert(@@template.output =~ /<li>3\s+?<ul><li>a/m)
749
-
750
- # Outer Loop Empty
751
- @@template['loop'] = false
752
- assert(@@template.output !~ /<li>/)
753
- end
754
-
755
- def testOutputLoopBranchMarkup
756
- @@template.load("tdata/ib1.txt")
757
-
758
- # Loop exists
759
- myLoop = []
760
- 1.upto(5) { |v| myLoop << {'value' => v} }
761
- @@template['loop'] = myLoop
762
- assert(@@template.output =~ /<li>\d<\/li>/)
763
- assert(@@template.output !~ /No values/)
764
-
765
- # No loop
766
- @@template['loop'] = []
767
- assert(@@template.output !~ /<li>\d<\/li>/)
768
- assert(@@template.output =~ /No values/)
769
- end
770
-
771
- def testOutputNestedLoopBranchMarkup
772
- @@template.load("tdata/ib2.txt")
773
- # Outer Loop Exists, Inner Loop Exists
774
- innerLoop = []
775
- 'a'.upto('f') { |v| innerLoop << { 'value' => v } }
776
- outerLoop = []
777
- 1.upto(5) do |v|
778
- outerLoop << {'value' => v, 'inner' => innerLoop}
779
- end
780
- @@template['loop'] = outerLoop
781
- assert(@@template.output =~ /<li>\d/)
782
- assert(@@template.output =~ /<li>[a-f]<\/li>/)
783
-
784
- # Outer Loop Exists, Inner Loop Empty
785
- outerLoop.each { |x| x['inner'] = [] }
786
- assert(@@template.output =~ /No inner values/)
787
-
788
- # Outer Loop Exists, Alternate Inner Loops Empty
789
- outerLoop.each_with_index do |x, i|
790
- x['inner'] = innerLoop if i % 2 == 0
791
- end
792
- assert(@@template.output =~ /<li>4\s+?<ul>.+?No inner values/m)
793
- assert(@@template.output =~ /<li>3\s+?<ul>.+?<li>a/m)
794
-
795
- # Outer Loop Empty
796
- @@template['loop'] = false
797
- assert(@@template.output =~ /No values/)
798
- end
799
-
800
- def testOutputComplexMarkup
801
- title = "Testing"
802
- reverse = true
803
-
804
- languages = [
805
- { 'name'=> 'C', 'author'=> 'Dennis Ritchie' },
806
- { 'name'=> 'C++', 'author'=> 'Bjarne Stroustrup' },
807
- { 'name'=> 'Perl', 'author'=> 'Larry Wall',
808
- 'guideline'=> 'TMTOWTDI - There\'s More Than One Way To Do It'},
809
- { 'name'=> 'Python', 'author'=> 'Guido van Rossum',
810
- 'guideline'=> 'There Should Be One Obvious Way To Do It' },
811
- { 'name'=> 'Ruby', 'author'=> 'Yukihiro Matsumoto',
812
- 'guideline'=> 'TPOL - The Principle Of Least Surprise'}
813
- ]
814
-
815
- favorites = [
816
- { 'topic'=> 'Interesting Comic Books',
817
- 'items'=> [
818
- {'title'=> 'Dropsie Avenue',
819
- 'creator'=> 'Will Eisner'},
820
- {'title'=> 'Cerebus',
821
- 'creator'=> 'Dave Sim'},
822
- {'title'=> 'Jar Of Fools',
823
- 'creator'=> 'Jason Lutes'}]},
824
- { 'topic'=> 'Favorite Albums',
825
- 'items'=> [
826
- {'title'=> 'Amnesiac',
827
- 'creator'=> 'Radiohead'},
828
- {'title'=> 'The Moon and Antarctica',
829
- 'creator'=> 'Modest Mouse'},
830
- {'title'=> 'Dirt Track Date',
831
- 'creator'=> 'Southern Culture On The Skids'},
832
- {'title'=> 'My Motor',
833
- 'creator'=> 'Dorkweed'},
834
- {'title'=> 'Swordfishtrombones',
835
- 'creator'=> 'Tom Waits'}]}
836
- ]
837
-
838
- page = PageTemplate.new('filename' => "tdata/complex.txt")
839
- page.setParameter("title", title)
840
- page.setParameter("ifTest", 1)
841
- page.setParameter("languages", languages)
842
- page.setParameter("favorites", favorites)
843
- if reverse == 1
844
- page.setParameter("reverse", 1)
845
- end
846
-
847
- assert(page.output)
848
- end
849
-
850
- def testCachedTemplate
851
- assert(ct = PageTemplate.new('filename' => "tdata/complex.txt",
852
- 'use_cache' => true
853
- ))
854
-
855
- languages = [
856
- { 'name'=> 'C', 'author'=> 'Dennis Ritchie' },
857
- { 'name'=> 'C++', 'author'=> 'Bjarne Stroustrup' },
858
- { 'name'=> 'Perl', 'author'=> 'Larry Wall',
859
- 'guideline'=> 'TMTOWTDI - There\'s More Than One Way To Do It'},
860
- { 'name'=> 'Python', 'author'=> 'Guido van Rossum',
861
- 'guideline'=> 'There Should Be One Obvious Way To Do It' },
862
- { 'name'=> 'Ruby', 'author'=> 'Yukihiro Matsumoto',
863
- 'guideline'=> 'TPOL - The Principle Of Least Surprise'}
864
- ]
865
-
866
- favorites = [
867
- { 'topic'=> 'Interesting Comic Books',
868
- 'items'=> [
869
- {'title'=> 'Dropsie Avenue',
870
- 'creator'=> 'Will Eisner'},
871
- {'title'=> 'Cerebus',
872
- 'creator'=> 'Dave Sim'},
873
- {'title'=> 'Jar Of Fools',
874
- 'creator'=> 'Jason Lutes'}]},
875
- { 'topic'=> 'Favorite Albums',
876
- 'items'=> [
877
- {'title'=> 'Amnesiac',
878
- 'creator'=> 'Radiohead'},
879
- {'title'=> 'The Moon and Antarctica',
880
- 'creator'=> 'Modest Mouse'},
881
- {'title'=> 'Dirt Track Date',
882
- 'creator'=> 'Southern Culture On The Skids'},
883
- {'title'=> 'My Motor',
884
- 'creator'=> 'Dorkweed'},
885
- {'title'=> 'Swordfishtrombones',
886
- 'creator'=> 'Tom Waits'}]}
887
- ]
888
- ct["languages"] = languages
889
- ct["favorites"] = favorites
890
- assert(ct.output)
891
- assert(File.exists?("tdata/_complex.txt"))
892
- system("rm -f tdata/_complex.txt")
893
-
894
- end
895
-
896
- def testClearFile
897
- assert(@@template.clearFile)
898
- end
899
- end
900
-
901
- class TC_Include < Test::Unit::TestCase
902
- def testIncludeCommand()
903
- ns = Namespace.new()
904
-
905
- incCommand = IncludeCommand.new("tdata/include.1.txt")
906
- text = File.open("tdata/include.1.txt").read()
907
- assert_equal(text, incCommand.output(ns),
908
- "IncludeCommands can be initialized with relative filenames")
909
-
910
- text = File.open("tdata/include.2.txt").read()
911
- incCommand = IncludeCommand.new("tdata/include.2.txt")
912
- assert_equal(text, incCommand.output(ns))
913
-
914
- # Including text with template markup.
915
- ns["x"] = "Dude!"
916
- pt = PageTemplate.new()
917
- pt.load("tdata/include.3.txt")
918
- pt["x"] = "Dude!"
919
- incCommand = IncludeCommand.new("tdata/include.3.txt")
920
- assert_equal(pt.output(), incCommand.output(ns),
921
- "Include Commands can parse standard Template markup")
922
- end
923
-
924
- def testIncludePath
925
- incCommand = IncludeCommand.new("include.4.txt", [ "tdata" ])
926
- text = File.open("tdata/include.4.out.txt").read()
927
- assert_equal(text, incCommand.output,
928
- "IncludeCommand accepts an array of paths as a second arg")
929
-
930
- incCommand = IncludeCommand.new("include.4.txt", [ "/etc", "tdata" ])
931
- text = File.open("tdata/include.4.out.txt").read()
932
- assert_equal(text, incCommand.output,
933
- "IncludeCommand accepts an array of paths as a second arg")
934
-
935
- pt = PageTemplate.new('include_path' => "tdata")
936
- pt.load("tdata/include.4b.txt")
937
- pt["file"] = "include.4a.txt"
938
- text = File.open("tdata/include.4.out.txt").read()
939
- assert_equal(text, pt.output,
940
- "Include Commands can be given a variable name as an argument")
941
-
942
- pt = PageTemplate.new('include_path' => "tdata")
943
- pt.load("tdata/include.4b.txt")
944
- pt["file"] = "secret-of-life.txt"
945
- text = File.open("tdata/include.4.nf.out.txt").read()
946
- assert_equal(text, pt.output,
947
- "Include Commands will output a warning if file isn't found.")
948
-
949
- # Support for multiple include paths.
950
- pt = PageTemplate.new()
951
- assert_equal([ Dir.getwd() ], pt.path)
952
- assert(pt.add_path('tdata'),
953
- "Use #add_path(path) to add an include path.")
954
- assert_equal([ Dir.getwd(), 'tdata'], pt.path)
955
- pt.add_path('tdata/inc-1')
956
- assert_equal([ Dir.getwd, "tdata", "tdata/inc-1"], pt.path)
957
- pt = PageTemplate.new('include_path' => %w{tdata tdata/inc-1})
958
- assert_equal([ Dir.getwd, "tdata", "tdata/inc-1"], pt.path)
959
- end
960
-
961
- def testIncludeSyntax
962
- text = File.open("tdata/include.4.out.txt").read()
963
- pt = PageTemplate.new("include_path" => "tdata")
964
- pt.load("include.4.txt")
965
- assert_equal(text, pt.output(),
966
- "PageTemplate supports 'include' syntax")
967
- pt = PageTemplate.new('include_path' => "tdata")
968
- pt.load("include.5.txt")
969
- assert_equal(text, pt.output(),
970
- "PageTemplate supports 'include' syntax")
971
-
972
- end
973
-
974
- end