PageTemplate 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/test.rb CHANGED
@@ -23,8 +23,8 @@
23
23
 
24
24
  require 'test/unit' unless defined? $ZENTEST and $ZENTEST
25
25
 
26
- require 'PageTemplate/commands.rb'
27
26
  require 'PageTemplate/parser.rb'
27
+ require 'PageTemplate/commands.rb'
28
28
  require 'PageTemplate/case.rb'
29
29
 
30
30
  module TestPageTemplate
@@ -68,11 +68,11 @@ module TestPageTemplate
68
68
 
69
69
  class TestCaseCommand < Test::Unit::TestCase
70
70
  def setup
71
- @@cc = PageTemplate::CaseCommand.new('case foo')
71
+ @@cc = PageTemplate::CaseCommand.new('foo')
72
72
  end
73
73
 
74
74
  def test_initialize
75
- assert(PageTemplate::CaseCommand.new('case foo'))
75
+ assert(PageTemplate::CaseCommand.new('foo'))
76
76
  end
77
77
 
78
78
  def test_add
@@ -82,24 +82,24 @@ module TestPageTemplate
82
82
  def test_current_case
83
83
  # Shouldn't I be failing? I don't see an accessor for CaseCommand.current_case
84
84
  assert_equal(nil, @@cc.current_case)
85
- @@cc.modifies?("when 1")
85
+ @@cc.when("1")
86
86
  assert_equal("1", @@cc.current_case)
87
87
  end
88
88
 
89
89
  def test_modifies_eh
90
- assert(@@cc.modifies?("when 1"))
91
- assert(@@cc.modifies?("else"))
92
- assert_equal(false, @@cc.modifies?("unless"))
90
+ assert(@@cc.when("1"))
91
+ assert(@@cc.else)
93
92
  end
94
93
 
95
94
  def test_output
95
+ p = PageTemplate::Parser.new
96
96
  c_1 = PageTemplate::TextCommand.new("I am 1")
97
97
  c_2 = PageTemplate::TextCommand.new("I am 2")
98
98
  c_3 = PageTemplate::TextCommand.new("I don't know what I am")
99
99
  @@cc.add(c_3)
100
- @@cc.modifies?("when 1")
100
+ @@cc.when("1")
101
101
  @@cc.add(c_1)
102
- @@cc.modifies?("when 2")
102
+ @@cc.when("2")
103
103
  @@cc.add(c_2)
104
104
  ns = PageTemplate::Namespace.new()
105
105
  ns["foo"] = nil
@@ -161,14 +161,18 @@ module TestPageTemplate
161
161
 
162
162
  class TestFileSource < Test::Unit::TestCase
163
163
  def setup
164
- @p = PageTemplate::Parser.new()
165
- @@fs = PageTemplate::FileSource.new(@p.args)
164
+ @@args = {}
165
+ @@fs = PageTemplate::FileSource.new(@@args)
166
+ @@filename = 'tdata/dummy.txt'
167
+ @@location = File.join(Dir.getwd, @@filename)
168
+ @@text = IO.read(@@location)
166
169
  end
167
170
 
168
171
  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
+ assert_equal(@@text, @@fs.get(@@filename),
173
+ "Dir.getwd is a path by default")
174
+ assert_equal(@@text, @@fs.get(@@location),
175
+ "Full path is allowed by default")
172
176
  end
173
177
 
174
178
  def test_get_filename
@@ -178,22 +182,48 @@ module TestPageTemplate
178
182
  end
179
183
 
180
184
  def test_paths
181
- assert_equal([Dir.getwd], @@fs.paths)
185
+ assert_equal([Dir.getwd,'/'], @@fs.paths)
186
+ end
187
+
188
+ def test_include_path
189
+ fs = PageTemplate::FileSource.new(
190
+ 'include_path'=>File.join(Dir.getwd,'tdata')
191
+ )
192
+ assert_equal([File.join(Dir.getwd,'tdata')],fs.paths,
193
+ "Explicit set include_path should only include the one element")
194
+ assert_equal(@@text,fs.get('dummy.txt'),
195
+ "Just a filename of something in an include path works")
196
+ end
197
+ def test_include_paths
198
+ location = File.dirname(@@location)
199
+ ptlocation = File.join(Dir.getwd,'lib')
200
+ pt = IO.read(File.join(ptlocation,'PageTemplate.rb'))
201
+ fs = PageTemplate::FileSource.new(
202
+ 'include_paths'=>[location,ptlocation]
203
+ )
204
+ assert_equal([location,ptlocation],fs.paths,
205
+ "Explicit set include_paths should include all set elements")
206
+ assert_equal(@@text,fs.get('dummy.txt'),
207
+ "Just a filename of something in an include path works")
208
+ assert_equal(pt,fs.get('PageTemplate.rb'),
209
+ "Just a filename of something in an include path works")
182
210
  end
183
211
  end
184
212
 
185
213
  class TestIfCommand < Test::Unit::TestCase
186
214
  def setup
187
- @@if = PageTemplate::IfCommand.new("if flag")
188
- @@unless = PageTemplate::IfCommand.new("unless flag")
215
+ parser = PageTemplate::Parser.new()
216
+ @@if = PageTemplate::IfCommand.new("if", "flag")
217
+ @@unless = PageTemplate::IfCommand.new("unless", "flag")
189
218
  end
190
219
 
191
220
  def test_initialize
221
+ parser = PageTemplate::Parser.new
192
222
  good_1 = "if flag"
193
223
  good_2 = "unless flag"
194
224
  bad_1 = "heck no"
195
- assert(PageTemplate::IfCommand.new(good_1))
196
- assert(PageTemplate::IfCommand.new(good_2))
225
+ assert(PageTemplate::IfCommand.new("if", "flag"))
226
+ assert(PageTemplate::IfCommand.new("unless", "flag"))
197
227
  assert_raises(ArgumentError,
198
228
  "IfCommand will not initialize without a command declaring 'if/unless'") {
199
229
  PageTemplate::IfCommand.new(bad_1)
@@ -208,32 +238,30 @@ module TestPageTemplate
208
238
  "IfCommand#add adds a command to the block to be executed when false")
209
239
  end
210
240
 
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
-
241
+ def test_else
242
+ assert(@@if.else)
243
+ assert(@@unless.else)
228
244
  end
229
245
 
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") }
246
+ def test_elsif
247
+ ns = PageTemplate::Namespace.new
248
+ parser = PageTemplate::Parser.new
249
+ tester = PageTemplate::IfCommand.new('if','redflag')
250
+ tester.add(PageTemplate::TextCommand.new('Red Flag'))
251
+ tester.elsif('blueflag')
252
+ tester.add(PageTemplate::TextCommand.new('Blue Flag'))
253
+ tester.elsif('greenflag')
254
+ tester.add(PageTemplate::TextCommand.new('Green Flag'))
255
+ tester.else
256
+ tester.add(PageTemplate::TextCommand.new('No Flag'))
257
+
258
+ assert_equal('No Flag',tester.output(ns))
259
+ ns['greenflag'] = true
260
+ assert_equal('Green Flag',tester.output(ns))
261
+ ns['blueflag'] = true
262
+ assert_equal('Blue Flag',tester.output(ns))
263
+ ns['redflag'] = true
264
+ assert_equal('Red Flag',tester.output(ns))
237
265
  end
238
266
 
239
267
  def test_output
@@ -248,8 +276,8 @@ module TestPageTemplate
248
276
  ns["flag"] = false
249
277
  assert_equal("", @@if.output(ns))
250
278
  assert_equal("dude!", @@unless.output(ns))
251
- @@if.modifies?("else")
252
- @@unless.modifies?("else")
279
+ @@if.else
280
+ @@unless.else
253
281
  c2 = PageTemplate::TextCommand.new("sweet!")
254
282
  @@if.add(c2)
255
283
  @@unless.add(c2)
@@ -260,35 +288,36 @@ module TestPageTemplate
260
288
  assert_equal("sweet!", @@if.output(ns))
261
289
  assert_equal("dude!", @@unless.output(ns))
262
290
  end
291
+ def test_empty_false
292
+ parser = PageTemplate::Parser.new('empty_is_true' => false)
293
+ template = parser.parse('[%if foo%]Foo[%else%]Bar[%end%]')
294
+ template['foo'] = []
295
+ assert_equal(template.output,'Bar')
296
+ parser.args['empty_is_true'] = true
297
+ assert_equal(template.output,'Foo')
298
+ end
263
299
  end
264
300
 
265
301
  class TestIncludeCommand < Test::Unit::TestCase
266
302
  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))
303
+ parser = PageTemplate::Parser.new()
304
+ parser['dummy'] = '/tdata/dummy.txt'
305
+ i = PageTemplate::IncludeCommand.new('dummy')
306
+ parser.load('tdata/dummy.txt')
307
+ assert_equal(parser.output, i.output(parser))
272
308
  end
273
309
  end
274
310
 
275
311
  class TestLoopCommand < Test::Unit::TestCase
276
312
  def setup
277
- @@in = PageTemplate::LoopCommand.new("in list")
278
- @@loop = PageTemplate::LoopCommand.new("loop list")
313
+ @@in = PageTemplate::LoopCommand.new("in", "list", "")
314
+ @@loop = PageTemplate::LoopCommand.new("loop", "list", "")
279
315
  end
280
316
 
281
317
  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) }
318
+ assert(PageTemplate::LoopCommand.new("in", "list", ""))
319
+ assert(PageTemplate::LoopCommand.new("in", "loop", ""))
320
+ assert(PageTemplate::LoopCommand.new("in", "list", "item"))
292
321
  end
293
322
 
294
323
  def test_add
@@ -297,39 +326,19 @@ module TestPageTemplate
297
326
  "LoopCommand#add adds a command to the block to be executed in the loop")
298
327
  end
299
328
 
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") }
329
+ def test_else
330
+ assert(@@loop.else)
324
331
  end
325
332
 
326
333
  def test_output
334
+ parser = PageTemplate::Parser.new()
327
335
  ns = PageTemplate::Namespace.new()
336
+ ns.parent = parser
328
337
  ns["list"] = [1, 2, 3]
329
338
 
330
339
  # Simple TextCommands in a Loop
331
340
  c = PageTemplate::TextCommand.new("Dude!")
332
- loop_1 = PageTemplate::LoopCommand.new("loop list")
341
+ loop_1 = PageTemplate::LoopCommand.new("loop", "list", "")
333
342
  loop_1.add(c)
334
343
  assert_equal("Dude!Dude!Dude!", loop_1.output(ns))
335
344
  c_1 = PageTemplate::TextCommand.new("Sweet!")
@@ -341,9 +350,9 @@ module TestPageTemplate
341
350
  # Simple TextCommands in a Loop with an Else block
342
351
  c_1 = PageTemplate::TextCommand.new("Sweet!")
343
352
  c_2 = PageTemplate::TextCommand.new("Dude?")
344
- loop_2 = PageTemplate::LoopCommand.new("loop list")
353
+ loop_2 = PageTemplate::LoopCommand.new("loop", "list", "")
345
354
  loop_2.add(c_1)
346
- loop_2.modifies?("else")
355
+ loop_2.else
347
356
  loop_2.add(c_2)
348
357
  ns["list"] = nil
349
358
  assert_equal("Dude?", loop_2.output(ns))
@@ -355,15 +364,15 @@ module TestPageTemplate
355
364
  c_2 = PageTemplate::TextCommand.new("I know kung fu.")
356
365
  c_3 = PageTemplate::TextCommand.new("Prove it.")
357
366
  c_4 = PageTemplate::TextCommand.new("Last Post!")
358
- if_1 = PageTemplate::IfCommand.new("if __FIRST__")
367
+ if_1 = PageTemplate::IfCommand.new("if", "__FIRST__")
359
368
  if_1.add(c_1)
360
- if_2 = PageTemplate::IfCommand.new("if __ODD__")
369
+ if_2 = PageTemplate::IfCommand.new("if", "__ODD__")
361
370
  if_2.add(c_2)
362
- if_2.modifies?("else")
371
+ if_2.else
363
372
  if_2.add(c_3)
364
- if_3 = PageTemplate::IfCommand.new("if __LAST__")
373
+ if_3 = PageTemplate::IfCommand.new("if", "__LAST__")
365
374
  if_3.add(c_4)
366
- loop_1 = PageTemplate::LoopCommand.new("in cliches")
375
+ loop_1 = PageTemplate::LoopCommand.new("in", "cliches", "")
367
376
  loop_1.add(if_1)
368
377
  loop_1.add(if_2)
369
378
  loop_1.add(if_3)
@@ -372,14 +381,26 @@ module TestPageTemplate
372
381
  assert_equal(expected, loop_1.output(ns))
373
382
 
374
383
  # 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())
384
+ loop_1 = PageTemplate::LoopCommand.new("in", "list", "item")
385
+ c_1 = PageTemplate::ValueCommand.new("item", nil)
377
386
  c_2 = PageTemplate::TextCommand.new(" ")
378
387
  loop_1.add(c_1)
379
388
  loop_1.add(c_2)
380
389
  ns["list"] = [0, -1, 2, -3, 4]
381
390
  expected = ns["list"].join(" ") + " "
382
391
  assert_equal(expected, loop_1.output(ns))
392
+
393
+ # A loop with multiple iterators
394
+ loop_1 = PageTemplate::LoopCommand.new("in", "names", "key val")
395
+ c1 = PageTemplate::ValueCommand.new("key",nil)
396
+ c2 = PageTemplate::ValueCommand.new("val",nil)
397
+ tc = PageTemplate::TextCommand.new(": ")
398
+ loop_1.add c1
399
+ loop_1.add tc
400
+ loop_1.add c2
401
+ ns["names"] = { 'Brian' => 'Wisti', 'Greg' => 'Millam' }
402
+ out = ns["names"].map { |fn,ln| "#{fn}: #{ln}" }.join('')
403
+ assert_equal(out,loop_1.output(ns))
383
404
  end
384
405
  end
385
406
 
@@ -411,6 +432,17 @@ module TestPageTemplate
411
432
  @@ns.object = -5
412
433
  assert_equal(5, @@ns.get("abs"),
413
434
  "Namespace#get will send get requests to a contained object if it is available")
435
+
436
+ assert_equal(3,@@ns.get("succ.succ.abs"),
437
+ "Namespace#get will send get requests subsequently to accessors if available")
438
+
439
+ @@ns.set("list", ['one','two','three'])
440
+ assert_equal('two',@@ns.get('list.1'),
441
+ "If an accessor looks like an integer, it's an array index")
442
+
443
+ @@ns.set('fruits',{'a'=>'apple','b'=>'banana'})
444
+ assert_equal('banana',@@ns.get('fruits.b'),
445
+ "If an object has_key?(b), it checks get object['b'] rather than send(:b)")
414
446
  end
415
447
 
416
448
  def test_index
@@ -540,29 +572,38 @@ module TestPageTemplate
540
572
  end
541
573
 
542
574
  class TestSyntaxGlossary < Test::Unit::TestCase
575
+ # Test modifications by gmillam
543
576
  def setup
544
- @@g = PageTemplate::SyntaxGlossary.new(
545
- /<(.+?)>/,
546
-
547
- /^--/i => PageTemplate::CommentCommand
548
- )
577
+ @@g = Class.new(PageTemplate::SyntaxGlossary)
578
+ @@g.directive = /<(.+?)>/
579
+ @@g.define(/^--(.*)/) { |match|
580
+ PageTemplate::CommentCommand.new(match[1])
581
+ }
582
+ @@g.default { |command|
583
+ PageTemplate::UnknownCommand.new(command)
584
+ }
549
585
  end
550
586
 
551
587
  def test_define
552
- assert_equal(PageTemplate::UnknownCommand, @@g.lookup("foo"))
588
+ # lookup() now returns instances, not classes
589
+ # assert_equal(PageTemplate::UnknownCommand, @@g.lookup("foo"))
590
+ assert_raises(ArgumentError,
591
+ "SyntaxGlossary#define requires a Regexp for its argument") {
592
+ @@g.define("foo") {}
593
+ }
553
594
  assert_raises(ArgumentError,
554
- "SyntaxGlossary#define requires a Regexp for its first arg") {
555
- @@g.define("foo", PageTemplate::CommentCommand)
595
+ "SyntaxGlossary#define requires a block to be passed") {
596
+ @@g.define("foo")
556
597
  }
557
- assert(@@g.define(/^foo/, PageTemplate::CommentCommand),
598
+ assert(@@g.define(/^foo/) {|m,p|},
558
599
  "Use SyntaxGlossary#define to add or change SyntaxGlossary entries")
559
600
  end
560
601
 
561
602
  def test_define_global_var
562
- assert_equal(PageTemplate::UnknownCommand, @@g.lookup("d"))
603
+ assert_equal(true, @@g.lookup("d").is_a?(PageTemplate::UnknownCommand))
563
604
  assert(@@g.define_global_var(/d/),
564
605
  "Use SyntaxGlossary#define_global_var to create explicit ValueCommands")
565
- assert_equal(PageTemplate::ValueCommand, @@g.lookup("d"))
606
+ assert_equal(true, @@g.lookup("d").is_a?(PageTemplate::ValueCommand))
566
607
  end
567
608
 
568
609
  def test_directive
@@ -574,11 +615,12 @@ module TestPageTemplate
574
615
  directive = /\[.+?\]/
575
616
  assert(@@g.directive = directive)
576
617
  assert_equal(directive, @@g.directive)
618
+ @@g.directive = /<(.+?)>/
577
619
  end
578
620
 
579
621
  def test_lookup
580
- assert_equal(PageTemplate::CommentCommand, @@g.lookup("--"))
581
- assert_equal(PageTemplate::UnknownCommand, @@g.lookup("++"))
622
+ assert_equal(true, @@g.lookup("--").is_a?(PageTemplate::CommentCommand))
623
+ assert_equal(true, @@g.lookup("++").is_a?(PageTemplate::UnknownCommand))
582
624
  end
583
625
  end
584
626
 
@@ -593,7 +635,7 @@ module TestPageTemplate
593
635
  class TestUnknownCommand < Test::Unit::TestCase
594
636
  def test_output
595
637
  parser = PageTemplate::Parser.new()
596
- uc = PageTemplate::UnknownCommand.new("waffle olive mango", parser)
638
+ uc = PageTemplate::UnknownCommand.new("waffle olive mango")
597
639
  assert_equal("[ Unknown Command: waffle olive mango ]", uc.output(parser.namespace))
598
640
  end
599
641
  end
@@ -602,15 +644,35 @@ module TestPageTemplate
602
644
  def test_output
603
645
  p = PageTemplate::Parser.new()
604
646
  p['x'] = 'y'
605
- v = PageTemplate::ValueCommand.new('var x', p)
647
+ v = PageTemplate::ValueCommand.new('x',nil)
606
648
  assert_equal('y', v.output())
607
649
  assert_equal('y', v.output(p.namespace))
608
650
  p['x'] = 1
609
- v = PageTemplate::ValueCommand.new('var x.succ.foo', p)
610
- w = PageTemplate::ValueCommand.new('var x.succ.succ.succ', p)
651
+ v = PageTemplate::ValueCommand.new('x.succ.foo', nil)
652
+ w = PageTemplate::ValueCommand.new('x.succ.succ.succ', nil)
611
653
  assert_equal('4',w.output(p.namespace))
612
654
  assert_equal('',v.output(p.namespace))
613
655
  end
656
+ def test_raise_on_error
657
+ p = PageTemplate::Parser.new
658
+ template = p.parse("[% var foo.bar %]")
659
+ foo = "hello"
660
+ def foo.bar
661
+ asdf # method unknown error
662
+ end
663
+ template["foo"] = foo
664
+ assert_equal(
665
+ template.output,
666
+ "[ Error: undefined local variable or method `asdf' for \"hello\":String ]",
667
+ "With raise_on_error not set, we get error strings")
668
+ p.args['raise_on_error'] = true
669
+ assert_raises(NameError,
670
+ "With raise on error set, user has to catch errors") {
671
+ template.output
672
+ }
673
+ rescue Exception => er
674
+ puts "error: #{er.class}"
675
+ end
614
676
  end
615
677
  end
616
678