From 57d5426eeda04a4fd21c328176fca65cd2e9f8c7 Mon Sep 17 00:00:00 2001 From: Ken Dreyer Date: Sat, 17 May 2014 22:26:36 -0600 Subject: [PATCH] tests: reset Strainer's filters after modification Three tests in the test suite use the Liquid::Template.register_filter function to register custom filters with Liquid::Strainer. The problem is that these register_filter calls leave the Liquid::Strainer object in an altered state. As an example, the FiltersTest's test_local_filter relies on the default behavior of Liquid::Strainer operator, and the test was failing if register_function had been called earlier. The same thing was happening with FiltersInTemplate's test_local_global. The problem was present when the Filters test classes were loaded inside a single ruby process that also loaded HashOrderingTest. One example is "rake test", which runs "require" on every test file. Another basic example is the following command: ruby -Itest -e "require 'integration/hash_ordering_test'; require 'integration/filter_test'" Update the tests to always reset Liquid::Strainer's filters back to the default list of filters. With this change, FiltersTest and FiltersInTemplate now pass. --- test/integration/context_test.rb | 3 +++ test/integration/filter_test.rb | 3 +++ test/integration/hash_ordering_test.rb | 5 ++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/test/integration/context_test.rb b/test/integration/context_test.rb index 555a1f2..942a506 100644 --- a/test/integration/context_test.rb +++ b/test/integration/context_test.rb @@ -16,9 +16,12 @@ class ContextTest < Minitest::Test end end + original_filters = Array.new(Strainer.class_eval('@@filters')) Template.register_filter(global) assert_equal 'Global test', Template.parse("{{'test' | notice }}").render! assert_equal 'Local test', Template.parse("{{'test' | notice }}").render!({}, :filters => [local]) + ensure + Strainer.class_eval('@@filters = ' + original_filters.to_s) end def test_has_key_will_not_add_an_error_for_missing_keys diff --git a/test/integration/filter_test.rb b/test/integration/filter_test.rb index 4789d81..f214f94 100644 --- a/test/integration/filter_test.rb +++ b/test/integration/filter_test.rb @@ -111,11 +111,14 @@ class FiltersInTemplate < Minitest::Test include Liquid def test_local_global + original_filters = Array.new(Strainer.class_eval('@@filters')) Template.register_filter(MoneyFilter) assert_equal " 1000$ ", Template.parse("{{1000 | money}}").render!(nil, nil) assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render!(nil, :filters => CanadianMoneyFilter) assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render!(nil, :filters => [CanadianMoneyFilter]) + ensure + Strainer.class_eval('@@filters = ' + original_filters.to_s) end def test_local_filter_with_deprecated_syntax diff --git a/test/integration/hash_ordering_test.rb b/test/integration/hash_ordering_test.rb index cbd350d..51b095a 100644 --- a/test/integration/hash_ordering_test.rb +++ b/test/integration/hash_ordering_test.rb @@ -16,10 +16,13 @@ class HashOrderingTest < Minitest::Test include Liquid def test_global_register_order + original_filters = Array.new(Strainer.class_eval('@@filters')) Template.register_filter(MoneyFilter) Template.register_filter(CanadianMoneyFilter) assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render(nil, nil) + ensure + Strainer.class_eval('@@filters = ' + original_filters.to_s) end - + end