test/unit/context_unit_test.rb in liquid-3.0.6 vs test/unit/context_unit_test.rb in liquid-4.0.0.rc1
- old
+ new
@@ -120,34 +120,29 @@
@context.pop
end
end
def test_length_query
+ @context['numbers'] = [1, 2, 3, 4]
- @context['numbers'] = [1,2,3,4]
-
assert_equal 4, @context['numbers.size']
- @context['numbers'] = {1 => 1,2 => 2,3 => 3,4 => 4}
+ @context['numbers'] = { 1 => 1, 2 => 2, 3 => 3, 4 => 4 }
assert_equal 4, @context['numbers.size']
- @context['numbers'] = {1 => 1,2 => 2,3 => 3,4 => 4, 'size' => 1000}
+ @context['numbers'] = { 1 => 1, 2 => 2, 3 => 3, 4 => 4, 'size' => 1000 }
assert_equal 1000, @context['numbers.size']
-
end
def test_hyphenated_variable
-
@context['oh-my'] = 'godz'
assert_equal 'godz', @context['oh-my']
-
end
def test_add_filter
-
filter = Module.new do
def hi(output)
output + ' hi!'
end
end
@@ -159,15 +154,13 @@
context = Context.new
assert_equal 'hi?', context.invoke(:hi, 'hi?')
context.add_filters(filter)
assert_equal 'hi? hi!', context.invoke(:hi, 'hi?')
-
end
def test_only_intended_filters_make_it_there
-
filter = Module.new do
def hi(output)
output + ' hi!'
end
end
@@ -194,11 +187,11 @@
@context.pop
assert_equal nil, @context['test']
end
def test_hierachical_data
- @context['hash'] = {"name" => 'tobi'}
+ @context['hash'] = { "name" => 'tobi' }
assert_equal 'tobi', @context['hash.name']
assert_equal 'tobi', @context['hash["name"]']
end
def test_keywords
@@ -223,60 +216,60 @@
assert_equal 'newvalue', @context['test']
assert_equal 'bar', @context['foo']
end
def test_array_notation
- @context['test'] = [1,2,3,4,5]
+ @context['test'] = [1, 2, 3, 4, 5]
assert_equal 1, @context['test[0]']
assert_equal 2, @context['test[1]']
assert_equal 3, @context['test[2]']
assert_equal 4, @context['test[3]']
assert_equal 5, @context['test[4]']
end
def test_recoursive_array_notation
- @context['test'] = {'test' => [1,2,3,4,5]}
+ @context['test'] = { 'test' => [1, 2, 3, 4, 5] }
assert_equal 1, @context['test.test[0]']
- @context['test'] = [{'test' => 'worked'}]
+ @context['test'] = [{ 'test' => 'worked' }]
assert_equal 'worked', @context['test[0].test']
end
def test_hash_to_array_transition
@context['colors'] = {
- 'Blue' => ['003366','336699', '6699CC', '99CCFF'],
- 'Green' => ['003300','336633', '669966', '99CC99'],
- 'Yellow' => ['CC9900','FFCC00', 'FFFF99', 'FFFFCC'],
- 'Red' => ['660000','993333', 'CC6666', 'FF9999']
+ 'Blue' => ['003366', '336699', '6699CC', '99CCFF'],
+ 'Green' => ['003300', '336633', '669966', '99CC99'],
+ 'Yellow' => ['CC9900', 'FFCC00', 'FFFF99', 'FFFFCC'],
+ 'Red' => ['660000', '993333', 'CC6666', 'FF9999']
}
assert_equal '003366', @context['colors.Blue[0]']
assert_equal 'FF9999', @context['colors.Red[3]']
end
def test_try_first
- @context['test'] = [1,2,3,4,5]
+ @context['test'] = [1, 2, 3, 4, 5]
assert_equal 1, @context['test.first']
assert_equal 5, @context['test.last']
- @context['test'] = {'test' => [1,2,3,4,5]}
+ @context['test'] = { 'test' => [1, 2, 3, 4, 5] }
assert_equal 1, @context['test.test.first']
assert_equal 5, @context['test.test.last']
@context['test'] = [1]
assert_equal 1, @context['test.first']
assert_equal 1, @context['test.last']
end
def test_access_hashes_with_hash_notation
- @context['products'] = {'count' => 5, 'tags' => ['deepsnow', 'freestyle'] }
- @context['product'] = {'variants' => [ {'title' => 'draft151cm'}, {'title' => 'element151cm'} ]}
+ @context['products'] = { 'count' => 5, 'tags' => ['deepsnow', 'freestyle'] }
+ @context['product'] = { 'variants' => [ { 'title' => 'draft151cm' }, { 'title' => 'element151cm' } ] }
assert_equal 5, @context['products["count"]']
assert_equal 'deepsnow', @context['products["tags"][0]']
assert_equal 'deepsnow', @context['products["tags"].first']
assert_equal 'draft151cm', @context['product["variants"][0]["title"]']
@@ -292,89 +285,86 @@
assert_equal 'baz', @context['["foo"]']
assert_equal 'baz', @context['[bar]']
end
def test_access_hashes_with_hash_access_variables
-
@context['var'] = 'tags'
- @context['nested'] = {'var' => 'tags'}
- @context['products'] = {'count' => 5, 'tags' => ['deepsnow', 'freestyle'] }
+ @context['nested'] = { 'var' => 'tags' }
+ @context['products'] = { 'count' => 5, 'tags' => ['deepsnow', 'freestyle'] }
assert_equal 'deepsnow', @context['products[var].first']
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'}
+ @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
+ @context['product'] = { 'variants' => [ { 'title' => 'draft151cm' }, { 'title' => 'element151cm' } ] }
- @context['product'] = {'variants' => [ {'title' => 'draft151cm'}, {'title' => 'element151cm'} ]}
-
assert_equal 'draft151cm', @context['product.variants[0].title']
assert_equal 'element151cm', @context['product.variants[1].title']
assert_equal 'draft151cm', @context['product.variants.first.title']
assert_equal 'element151cm', @context['product.variants.last.title']
-
end
def test_cents
- @context.merge( "cents" => HundredCentes.new )
+ @context.merge("cents" => HundredCentes.new)
assert_equal 100, @context['cents']
end
def test_nested_cents
- @context.merge( "cents" => { 'amount' => HundredCentes.new} )
+ @context.merge("cents" => { 'amount' => HundredCentes.new })
assert_equal 100, @context['cents.amount']
- @context.merge( "cents" => { 'cents' => { 'amount' => HundredCentes.new} } )
+ @context.merge("cents" => { 'cents' => { 'amount' => HundredCentes.new } })
assert_equal 100, @context['cents.cents.amount']
end
def test_cents_through_drop
- @context.merge( "cents" => CentsDrop.new )
+ @context.merge("cents" => CentsDrop.new)
assert_equal 100, @context['cents.amount']
end
def test_nested_cents_through_drop
- @context.merge( "vars" => {"cents" => CentsDrop.new} )
+ @context.merge("vars" => { "cents" => CentsDrop.new })
assert_equal 100, @context['vars.cents.amount']
end
def test_drop_methods_with_question_marks
- @context.merge( "cents" => CentsDrop.new )
+ @context.merge("cents" => CentsDrop.new)
assert @context['cents.non_zero?']
end
def test_context_from_within_drop
- @context.merge( "test" => '123', "vars" => ContextSensitiveDrop.new )
+ @context.merge("test" => '123', "vars" => ContextSensitiveDrop.new)
assert_equal '123', @context['vars.test']
end
def test_nested_context_from_within_drop
- @context.merge( "test" => '123', "vars" => {"local" => ContextSensitiveDrop.new } )
+ @context.merge("test" => '123', "vars" => { "local" => ContextSensitiveDrop.new })
assert_equal '123', @context['vars.local.test']
end
def test_ranges
- @context.merge( "test" => '5' )
+ @context.merge("test" => '5')
assert_equal (1..5), @context['(1..5)']
assert_equal (1..5), @context['(1..test)']
assert_equal (5..5), @context['(test..test)']
end
def test_cents_through_drop_nestedly
- @context.merge( "cents" => {"cents" => CentsDrop.new} )
+ @context.merge("cents" => { "cents" => CentsDrop.new })
assert_equal 100, @context['cents.cents.amount']
- @context.merge( "cents" => { "cents" => {"cents" => CentsDrop.new}} )
+ @context.merge("cents" => { "cents" => { "cents" => CentsDrop.new } })
assert_equal 100, @context['cents.cents.cents.amount']
end
def test_drop_with_variable_called_only_once
@context['counter'] = CounterDrop.new
@@ -391,11 +381,11 @@
assert_equal 2, @context['counter["count"]']
assert_equal 3, @context['counter["count"]']
end
def test_proc_as_variable
- @context['dynamic'] = Proc.new { 'Hello' }
+ @context['dynamic'] = proc { 'Hello' }
assert_equal 'Hello', @context['dynamic']
end
def test_lambda_as_variable
@@ -409,11 +399,11 @@
assert_equal 'Hello', @context['dynamic.lambda']
end
def test_array_containing_lambda_as_variable
- @context['dynamic'] = [1,2, proc { 'Hello' } ,4,5]
+ @context['dynamic'] = [1, 2, proc { 'Hello' }, 4, 5]
assert_equal 'Hello', @context['dynamic[2]']
end
def test_lambda_is_called_once
@@ -435,11 +425,11 @@
@global = nil
end
def test_lambda_in_array_is_called_once
- @context['callcount'] = [1,2, proc { @global ||= 0; @global += 1; @global.to_s } ,4,5]
+ @context['callcount'] = [1, 2, proc { @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]']
@@ -461,32 +451,33 @@
end
def test_use_empty_instead_of_any_in_interrupt_handling_to_avoid_lots_of_unnecessary_object_allocations
mock_any = Spy.on_instance_method(Array, :any?)
mock_empty = Spy.on_instance_method(Array, :empty?)
- mock_has_interrupt = Spy.on(@context, :has_interrupt?).and_call_through
- @context.has_interrupt?
+ @context.interrupt?
refute mock_any.has_been_called?
assert mock_empty.has_been_called?
- end
-
- def test_variable_lookup_caches_markup
- mock_scan = Spy.on_instance_method(String, :scan).and_return(["string"])
-
- @context['string'] = 'string'
- @context['string']
- @context['string']
-
- assert_equal 1, mock_scan.calls.size
end
def test_context_initialization_with_a_proc_in_environment
- contx = Context.new([:test => lambda { |c| c['poutine']}], {:test => :foo})
+ contx = Context.new([test: ->(c) { c['poutine'] }], { test: :foo })
assert contx
assert_nil contx['poutine']
end
+ def test_apply_global_filter
+ global_filter_proc = ->(output) { "#{output} filtered" }
+ context = Context.new
+ context.global_filter = global_filter_proc
+
+ assert_equal 'hi filtered', context.apply_global_filter('hi')
+ end
+
+ def test_apply_global_filter_when_no_global_filter_exist
+ context = Context.new
+ assert_equal 'hi', context.apply_global_filter('hi')
+ end
end # ContextTest