mirror of
https://github.com/kemko/liquid.git
synced 2026-01-01 15:55:40 +03:00
Initial concept
This commit is contained in:
@@ -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
|
||||
#
|
||||
|
||||
19
lib/liquid/usage.rb
Normal file
19
lib/liquid/usage.rb
Normal file
@@ -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
|
||||
1
lib/liquid/usages/usage_enabled.rb
Normal file
1
lib/liquid/usages/usage_enabled.rb
Normal file
@@ -0,0 +1 @@
|
||||
Liquid::Usage.track("Usage is enabled")
|
||||
34
test/integration/usage_test.rb
Normal file
34
test/integration/usage_test.rb
Normal file
@@ -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
|
||||
23
test/integration/usages/try_variables.rb
Normal file
23
test/integration/usages/try_variables.rb
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user