test/unit/context_unit_test.rb in liquid-4.0.0 vs test/unit/context_unit_test.rb in liquid-4.0.1

- old
+ new

@@ -68,14 +68,10 @@ def setup @context = Liquid::Context.new end - def teardown - Spy.teardown - end - def test_variables @context['string'] = 'string' assert_equal 'string', @context['string'] @context['num'] = 5 @@ -96,16 +92,16 @@ @context['bool'] = false assert_equal false, @context['bool'] @context['nil'] = nil - assert_equal nil, @context['nil'] - assert_equal nil, @context['nil'] + assert_nil @context['nil'] + assert_nil @context['nil'] end def test_variables_not_existing - assert_equal nil, @context['does_not_exist'] + assert_nil @context['does_not_exist'] end def test_scoping @context.push @context.pop @@ -183,11 +179,11 @@ def test_add_item_in_inner_scope @context.push @context['test'] = 'test' assert_equal 'test', @context['test'] @context.pop - assert_equal nil, @context['test'] + assert_nil @context['test'] end def test_hierachical_data @context['hash'] = { "name" => 'tobi' } assert_equal 'tobi', @context['hash.name'] @@ -298,11 +294,11 @@ 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_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' } ] } @@ -448,18 +444,14 @@ @context['category'] = Category.new("foobar") assert_kind_of CategoryDrop, @context['category'] assert_equal @context, @context['category'].context 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?) - - @context.interrupt? - - refute mock_any.has_been_called? - assert mock_empty.has_been_called? + def test_interrupt_avoids_object_allocations + assert_no_object_allocations do + @context.interrupt? + end end def test_context_initialization_with_a_proc_in_environment contx = Context.new([test: ->(c) { c['poutine'] }], { test: :foo }) @@ -477,7 +469,21 @@ end def test_apply_global_filter_when_no_global_filter_exist context = Context.new assert_equal 'hi', context.apply_global_filter('hi') + end + + private + + def assert_no_object_allocations + unless RUBY_ENGINE == 'ruby' + skip "stackprof needed to count object allocations" + end + require 'stackprof' + + profile = StackProf.run(mode: :object) do + yield + end + assert_equal 0, profile[:samples] end end # ContextTest