public
Rubygem
Description: Liquid markup language. Save, customer facing template language for flexible web apps.
Homepage: http://www.liquidmarkup.org
Clone URL: git://github.com/tobi/liquid.git
Click here to lend your support to: liquid and make a donation at www.pledgie.com !
Add some failing tests for Context.

Using an attribute should only cause the corresponding method to be invoked
once. Replacing a lambda with its return value should work for arrays.
Array index syntax shouldn't allow calls to special methods.
mhw (author)
Tue Jul 08 09:35:36 -0700 2008
commit  50edd0f5b92cc38f7da032fc0caea6dbdadd9f07
tree    794746d7bc4c9ae42ebb85452323a90db324ec4c
parent  819b70204fb692223a34c914881ca3d757767a8e
...
40
41
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
44
45
...
299
300
301
 
 
 
 
 
 
 
 
302
303
304
...
364
365
366
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
367
368
369
...
382
383
384
 
 
 
 
 
 
385
386
387
...
402
403
404
 
 
 
 
 
 
 
 
 
 
405
406
407
...
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
...
321
322
323
324
325
326
327
328
329
330
331
332
333
334
...
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
...
428
429
430
431
432
433
434
435
436
437
438
439
...
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
0
@@ -40,6 +40,28 @@ class CategoryDrop
0
   end
0
 end
0
 
0
+class CounterDrop < Liquid::Drop
0
+  def count
0
+    @count ||= 0
0
+    @count += 1
0
+  end
0
+end
0
+
0
+class ArrayLike
0
+  def fetch(index)
0
+  end
0
+
0
+  def [](index)
0
+    @counts ||= []
0
+    @counts[index] ||= 0
0
+    @counts[index] += 1
0
+  end
0
+
0
+  def to_liquid
0
+    self
0
+  end
0
+end
0
+
0
 
0
 class ContextTest < Test::Unit::TestCase
0
   include Liquid
0
@@ -299,6 +321,14 @@ class ContextTest < Test::Unit::TestCase
0
     assert_equal 'freestyle', @context['products[nested.var].last']
0
   end
0
 
0
+  def test_hash_notation_only_for_hash_access
0
+    @context['array'] = [1,2,3,4,5]
0
+    @context['hash'] = {'first' => 'Hello'}
0
+
0
+    assert_equal 1, @context['array.first']
0
+    assert_equal nil, @context['array["first"]']
0
+    assert_equal 'Hello', @context['hash["first"]']
0
+  end
0
 
0
   def test_first_can_appear_in_middle_of_callchain
0
 
0
@@ -364,6 +394,22 @@ class ContextTest < Test::Unit::TestCase
0
     assert_equal 100, @context['cents.cents.cents.amount']
0
   end
0
 
0
+  def test_drop_with_variable_called_only_once
0
+    @context['counter'] = CounterDrop.new
0
+
0
+    assert_equal 1, @context['counter.count']
0
+    assert_equal 2, @context['counter.count']
0
+    assert_equal 3, @context['counter.count']
0
+  end
0
+
0
+  def test_drop_with_key_called_only_once
0
+    @context['counter'] = CounterDrop.new
0
+
0
+    assert_equal 1, @context['counter["count"]']
0
+    assert_equal 2, @context['counter["count"]']
0
+    assert_equal 3, @context['counter["count"]']
0
+  end
0
+
0
   def test_proc_as_variable
0
     @context['dynamic'] = Proc.new { 'Hello' }
0
 
0
@@ -382,6 +428,12 @@ class ContextTest < Test::Unit::TestCase
0
     assert_equal 'Hello', @context['dynamic.lambda']
0
   end
0
 
0
+  def test_array_containing_lambda_as_variable
0
+    @context['dynamic'] = [1,2, lambda { 'Hello' } ,4,5]
0
+
0
+    assert_equal 'Hello', @context['dynamic[2]']
0
+  end
0
+
0
   def test_lambda_is_called_once
0
     @context['callcount'] = lambda { @global ||= 0; @global += 1; @global.to_s }
0
 
0
@@ -402,6 +454,16 @@ class ContextTest < Test::Unit::TestCase
0
     @global = nil
0
   end
0
 
0
+  def test_lambda_in_array_is_called_once
0
+    @context['callcount'] = [1,2, lambda { @global ||= 0; @global += 1; @global.to_s } ,4,5]
0
+
0
+    assert_equal '1', @context['callcount[2]']
0
+    assert_equal '1', @context['callcount[2]']
0
+    assert_equal '1', @context['callcount[2]']
0
+
0
+    @global = nil
0
+  end
0
+
0
   def test_access_to_context_from_proc
0
     @context.registers[:magic] = 345392
0
 

Comments