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.
This commit is contained in:
Mark H. Wilkinson
2008-07-08 17:35:36 +01:00
parent 819b70204f
commit 50edd0f5b9

View File

@@ -40,6 +40,28 @@ class CategoryDrop
end
end
class CounterDrop < Liquid::Drop
def count
@count ||= 0
@count += 1
end
end
class ArrayLike
def fetch(index)
end
def [](index)
@counts ||= []
@counts[index] ||= 0
@counts[index] += 1
end
def to_liquid
self
end
end
class ContextTest < Test::Unit::TestCase
include Liquid
@@ -299,6 +321,14 @@ class ContextTest < Test::Unit::TestCase
assert_equal 'freestyle', @context['products[nested.var].last']
end
def test_hash_notation_only_for_hash_access
@context['array'] = [1,2,3,4,5]
@context['hash'] = {'first' => 'Hello'}
assert_equal 1, @context['array.first']
assert_equal nil, @context['array["first"]']
assert_equal 'Hello', @context['hash["first"]']
end
def test_first_can_appear_in_middle_of_callchain
@@ -364,6 +394,22 @@ class ContextTest < Test::Unit::TestCase
assert_equal 100, @context['cents.cents.cents.amount']
end
def test_drop_with_variable_called_only_once
@context['counter'] = CounterDrop.new
assert_equal 1, @context['counter.count']
assert_equal 2, @context['counter.count']
assert_equal 3, @context['counter.count']
end
def test_drop_with_key_called_only_once
@context['counter'] = CounterDrop.new
assert_equal 1, @context['counter["count"]']
assert_equal 2, @context['counter["count"]']
assert_equal 3, @context['counter["count"]']
end
def test_proc_as_variable
@context['dynamic'] = Proc.new { 'Hello' }
@@ -382,6 +428,12 @@ class ContextTest < Test::Unit::TestCase
assert_equal 'Hello', @context['dynamic.lambda']
end
def test_array_containing_lambda_as_variable
@context['dynamic'] = [1,2, lambda { 'Hello' } ,4,5]
assert_equal 'Hello', @context['dynamic[2]']
end
def test_lambda_is_called_once
@context['callcount'] = lambda { @global ||= 0; @global += 1; @global.to_s }
@@ -402,6 +454,16 @@ class ContextTest < Test::Unit::TestCase
@global = nil
end
def test_lambda_in_array_is_called_once
@context['callcount'] = [1,2, lambda { @global ||= 0; @global += 1; @global.to_s } ,4,5]
assert_equal '1', @context['callcount[2]']
assert_equal '1', @context['callcount[2]']
assert_equal '1', @context['callcount[2]']
@global = nil
end
def test_access_to_context_from_proc
@context.registers[:magic] = 345392