diff --git a/lib/liquid.rb b/lib/liquid.rb index 0e198bb..b98d4d9 100644 --- a/lib/liquid.rb +++ b/lib/liquid.rb @@ -75,6 +75,7 @@ require 'liquid/utils' require 'liquid/tokenizer' require 'liquid/parse_context' require 'liquid/partial_cache' +require 'liquid/usage' # Load all the tags of the standard library # diff --git a/lib/liquid/usage.rb b/lib/liquid/usage.rb new file mode 100644 index 0000000..26b118b --- /dev/null +++ b/lib/liquid/usage.rb @@ -0,0 +1,19 @@ +module Liquid + # Usage is used to store + class Usage + @messages = {} + class << self + def enable + Dir["#{__dir__}/usages/*.rb"].each { |f| require f } + end + + def track(message) + @messages[message] = true + end + + def results + @messages + end + end + end +end diff --git a/lib/liquid/usages/usage_enabled.rb b/lib/liquid/usages/usage_enabled.rb new file mode 100644 index 0000000..c5f3c33 --- /dev/null +++ b/lib/liquid/usages/usage_enabled.rb @@ -0,0 +1 @@ +Liquid::Usage.track("Usage is enabled") diff --git a/test/integration/usage_test.rb b/test/integration/usage_test.rb new file mode 100644 index 0000000..54504e9 --- /dev/null +++ b/test/integration/usage_test.rb @@ -0,0 +1,34 @@ +require 'test_helper' + +module Liquid + class TestUsage < Usage + @messages = {} + class << self + def enable + Dir["#{__dir__}/usages/*.rb"].each { |f| require f } + end + end + end +end + +class UsageTest < Minitest::Test + include Liquid + + Usage.enable + + def test_test_usages + Dir["#{__dir__}/usages/*.rb"].each { |f| require f } + + template = Template.parse(%({{test}})) + assert_equal 'worked', template.render!('test' => 'worked') + assert_equal 'worked wonderfully', template.render!('test' => 'worked wonderfully') + assert_equal true, Usage.results["Using try_variable_find_in_environment"] + end + + def test_live_usages + template = Template.parse(%({{test}})) + assert_equal 'worked', template.render!('test' => 'worked') + assert_equal 'worked wonderfully', template.render!('test' => 'worked wonderfully') + assert_equal true, Usage.results["Usage is enabled"] + end +end diff --git a/test/integration/usages/try_variables.rb b/test/integration/usages/try_variables.rb new file mode 100644 index 0000000..ba412de --- /dev/null +++ b/test/integration/usages/try_variables.rb @@ -0,0 +1,23 @@ +module Liquid + class Context + remove_method :try_variable_find_in_environments + def try_variable_find_in_environments(key, raise_on_not_found:) + Usage.track("Using try_variable_find_in_environment") + @environments.each do |environment| + found_variable = lookup_and_evaluate(environment, key, raise_on_not_found: raise_on_not_found) + if !found_variable.nil? || @strict_variables && raise_on_not_found + return found_variable + end + Usage.track("try_variable_find_in_environment reports Nil but responds to key") if environment.key?(key) + end + @static_environments.each do |environment| + found_variable = lookup_and_evaluate(environment, key, raise_on_not_found: raise_on_not_found) + if !found_variable.nil? || @strict_variables && raise_on_not_found + return found_variable + end + Usage.track("try_variable_find_in_environment reports Nil but responds to key") if environment.key?(key) + end + nil + end + end +end