Strip trailing whitespace.

This commit is contained in:
Mark H. Wilkinson
2008-07-09 18:34:41 +01:00
parent 6dd4f716f8
commit eb609ee4aa
2 changed files with 167 additions and 167 deletions
+70 -70
View File
@@ -1,13 +1,13 @@
module Liquid module Liquid
# Context keeps the variable stack and resolves variables, as well as keywords # Context keeps the variable stack and resolves variables, as well as keywords
# #
# context['variable'] = 'testing' # context['variable'] = 'testing'
# context['variable'] #=> 'testing' # context['variable'] #=> 'testing'
# context['true'] #=> true # context['true'] #=> true
# context['10.2232'] #=> 10.2232 # context['10.2232'] #=> 10.2232
# #
# context.stack do # context.stack do
# context['bob'] = 'bobsen' # context['bob'] = 'bobsen'
# end # end
# #
@@ -15,49 +15,49 @@ module Liquid
class Context class Context
attr_reader :scopes attr_reader :scopes
attr_reader :errors, :registers attr_reader :errors, :registers
def initialize(assigns = {}, registers = {}, rethrow_errors = false) def initialize(assigns = {}, registers = {}, rethrow_errors = false)
@scopes = [(assigns || {})] @scopes = [(assigns || {})]
@registers = registers @registers = registers
@errors = [] @errors = []
@rethrow_errors = rethrow_errors @rethrow_errors = rethrow_errors
end end
def strainer def strainer
@strainer ||= Strainer.create(self) @strainer ||= Strainer.create(self)
end end
# adds filters to this context. # adds filters to this context.
# this does not register the filters with the main Template object. see <tt>Template.register_filter</tt> # this does not register the filters with the main Template object. see <tt>Template.register_filter</tt>
# for that # for that
def add_filters(filters) def add_filters(filters)
filters = [filters].flatten.compact filters = [filters].flatten.compact
filters.each do |f| filters.each do |f|
raise ArgumentError, "Expected module but got: #{f.class}" unless f.is_a?(Module) raise ArgumentError, "Expected module but got: #{f.class}" unless f.is_a?(Module)
strainer.extend(f) strainer.extend(f)
end end
end end
def handle_error(e) def handle_error(e)
errors.push(e) errors.push(e)
raise if @rethrow_errors raise if @rethrow_errors
case e case e
when SyntaxError when SyntaxError
"Liquid syntax error: #{e.message}" "Liquid syntax error: #{e.message}"
else else
"Liquid error: #{e.message}" "Liquid error: #{e.message}"
end end
end end
def invoke(method, *args) def invoke(method, *args)
if strainer.respond_to?(method) if strainer.respond_to?(method)
strainer.__send__(method, *args) strainer.__send__(method, *args)
else else
args.first args.first
end end
end end
# push new local scope on the stack. use <tt>Context#stack</tt> instead # push new local scope on the stack. use <tt>Context#stack</tt> instead
@@ -65,23 +65,23 @@ module Liquid
raise StackLevelError, "Nesting too deep" if @scopes.length > 100 raise StackLevelError, "Nesting too deep" if @scopes.length > 100
@scopes.unshift({}) @scopes.unshift({})
end end
# merge a hash of variables in the current local scope # merge a hash of variables in the current local scope
def merge(new_scopes) def merge(new_scopes)
@scopes[0].merge!(new_scopes) @scopes[0].merge!(new_scopes)
end end
# pop from the stack. use <tt>Context#stack</tt> instead # pop from the stack. use <tt>Context#stack</tt> instead
def pop def pop
raise ContextError if @scopes.size == 1 raise ContextError if @scopes.size == 1
@scopes.shift @scopes.shift
end end
# pushes a new local scope on the stack, pops it at the end of the block # pushes a new local scope on the stack, pops it at the end of the block
# #
# Example: # Example:
# #
# context.stack do # context.stack do
# context['var'] = 'hi' # context['var'] = 'hi'
# end # end
# context['var] #=> nil # context['var] #=> nil
@@ -91,33 +91,33 @@ module Liquid
push push
begin begin
result = yield result = yield
ensure ensure
pop pop
end end
result result
end end
# Only allow String, Numeric, Hash, Array, Proc, Boolean or <tt>Liquid::Drop</tt> # Only allow String, Numeric, Hash, Array, Proc, Boolean or <tt>Liquid::Drop</tt>
def []=(key, value) def []=(key, value)
@scopes[0][key] = value @scopes[0][key] = value
end end
def [](key) def [](key)
resolve(key) resolve(key)
end end
def has_key?(key) def has_key?(key)
resolve(key) != nil resolve(key) != nil
end end
private private
# Look up variable, either resolve directly after considering the name. We can directly handle # Look up variable, either resolve directly after considering the name. We can directly handle
# Strings, digits, floats and booleans (true,false). If no match is made we lookup the variable in the current scope and # Strings, digits, floats and booleans (true,false). If no match is made we lookup the variable in the current scope and
# later move up to the parent blocks to see if we can resolve the variable somewhere up the tree. # later move up to the parent blocks to see if we can resolve the variable somewhere up the tree.
# Some special keywords return symbols. Those symbols are to be called on the rhs object in expressions # Some special keywords return symbols. Those symbols are to be called on the rhs object in expressions
# #
# Example: # Example:
# #
# products == empty #=> products.empty? # products == empty #=> products.empty?
# #
@@ -128,9 +128,9 @@ module Liquid
when 'true' when 'true'
true true
when 'false' when 'false'
false false
when 'blank' when 'blank'
:blank? :blank?
when 'empty' when 'empty'
:empty? :empty?
# Single quoted strings # Single quoted strings
@@ -138,27 +138,27 @@ module Liquid
$1.to_s $1.to_s
# Double quoted strings # Double quoted strings
when /^"(.*)"$/ when /^"(.*)"$/
$1.to_s $1.to_s
# Integer and floats # Integer and floats
when /^(\d+)$/ when /^(\d+)$/
$1.to_i $1.to_i
# Ranges # Ranges
when /^\((\S+)\.\.(\S+)\)$/ when /^\((\S+)\.\.(\S+)\)$/
(resolve($1).to_i..resolve($2).to_i) (resolve($1).to_i..resolve($2).to_i)
# Floats # Floats
when /^(\d[\d\.]+)$/ when /^(\d[\d\.]+)$/
$1.to_f $1.to_f
else else
variable(key) variable(key)
end end
end end
# fetches an object starting at the local scope and then moving up # fetches an object starting at the local scope and then moving up
# the hierachy # the hierachy
def find_variable(key) def find_variable(key)
@scopes.each do |scope| @scopes.each do |scope|
if scope.has_key?(key) if scope.has_key?(key)
variable = scope[key] variable = scope[key]
variable = scope[key] = variable.call(self) if variable.is_a?(Proc) variable = scope[key] = variable.call(self) if variable.is_a?(Proc)
variable = variable.to_liquid variable = variable.to_liquid
variable.context = self if variable.respond_to?(:context=) variable.context = self if variable.respond_to?(:context=)
@@ -169,9 +169,9 @@ module Liquid
end end
# resolves namespaced queries gracefully. # resolves namespaced queries gracefully.
# #
# Example # Example
# #
# @context['hash'] = {"name" => 'tobi'} # @context['hash'] = {"name" => 'tobi'}
# assert_equal 'tobi', @context['hash.name'] # assert_equal 'tobi', @context['hash.name']
# assert_equal 'tobi', @context['hash[name]'] # assert_equal 'tobi', @context['hash[name]']
@@ -179,65 +179,65 @@ module Liquid
def variable(markup) def variable(markup)
parts = markup.scan(VariableParser) parts = markup.scan(VariableParser)
square_bracketed = /^\[(.*)\]$/ square_bracketed = /^\[(.*)\]$/
first_part = parts.shift first_part = parts.shift
if first_part =~ square_bracketed if first_part =~ square_bracketed
first_part = resolve($1) first_part = resolve($1)
end end
if object = find_variable(first_part)
parts.each do |part|
# If object is a hash we look for the presence of the key and if its available if object = find_variable(first_part)
parts.each do |part|
# If object is a hash we look for the presence of the key and if its available
# we return it # we return it
if part =~ square_bracketed if part =~ square_bracketed
part = resolve($1) part = resolve($1)
object[pos] = object[part].call(self) if object[part].is_a?(Proc) and object.respond_to?(:[]=) object[pos] = object[part].call(self) if object[part].is_a?(Proc) and object.respond_to?(:[]=)
object = object[part].to_liquid object = object[part].to_liquid
else else
# Hash # Hash
if object.respond_to?(:has_key?) and object.has_key?(part) if object.respond_to?(:has_key?) and object.has_key?(part)
# if its a proc we will replace the entry in the hash table with the proc # if its a proc we will replace the entry in the hash table with the proc
res = object[part] res = object[part]
res = object[part] = res.call(self) if res.is_a?(Proc) and object.respond_to?(:[]=) res = object[part] = res.call(self) if res.is_a?(Proc) and object.respond_to?(:[]=)
object = res.to_liquid object = res.to_liquid
# Array # Array
elsif object.respond_to?(:fetch) and part =~ /^\d+$/ elsif object.respond_to?(:fetch) and part =~ /^\d+$/
pos = part.to_i pos = part.to_i
object[pos] = object[pos].call(self) if object[pos].is_a?(Proc) and object.respond_to?(:[]=) object[pos] = object[pos].call(self) if object[pos].is_a?(Proc) and object.respond_to?(:[]=)
object = object[pos].to_liquid object = object[pos].to_liquid
# Some special cases. If no key with the same name was found we interpret following calls # Some special cases. If no key with the same name was found we interpret following calls
# as commands and call them on the current object # as commands and call them on the current object
elsif object.respond_to?(part) and ['size', 'first', 'last'].include?(part) elsif object.respond_to?(part) and ['size', 'first', 'last'].include?(part)
object = object.send(part.intern).to_liquid object = object.send(part.intern).to_liquid
# No key was present with the desired value and it wasn't one of the directly supported # No key was present with the desired value and it wasn't one of the directly supported
# keywords either. The only thing we got left is to return nil # keywords either. The only thing we got left is to return nil
else else
return nil return nil
end end
end end
# If we are dealing with a drop here we have to # If we are dealing with a drop here we have to
object.context = self if object.respond_to?(:context=) object.context = self if object.respond_to?(:context=)
end end
end end
object object
end end
private private
def execute_proc(proc) def execute_proc(proc)
proc.call(self) proc.call(self)
end end
+97 -97
View File
@@ -9,7 +9,7 @@ class CentsDrop < Liquid::Drop
def amount def amount
HundredCentes.new HundredCentes.new
end end
def non_zero? def non_zero?
true true
end end
@@ -52,41 +52,41 @@ class ContextTest < Test::Unit::TestCase
def test_variables def test_variables
@context['string'] = 'string' @context['string'] = 'string'
assert_equal 'string', @context['string'] assert_equal 'string', @context['string']
@context['num'] = 5 @context['num'] = 5
assert_equal 5, @context['num'] assert_equal 5, @context['num']
@context['time'] = Time.parse('2006-06-06 12:00:00') @context['time'] = Time.parse('2006-06-06 12:00:00')
assert_equal Time.parse('2006-06-06 12:00:00'), @context['time'] assert_equal Time.parse('2006-06-06 12:00:00'), @context['time']
@context['date'] = Date.today @context['date'] = Date.today
assert_equal Date.today, @context['date'] assert_equal Date.today, @context['date']
now = DateTime.now now = DateTime.now
@context['datetime'] = now @context['datetime'] = now
assert_equal now, @context['datetime'] assert_equal now, @context['datetime']
@context['bool'] = true @context['bool'] = true
assert_equal true, @context['bool'] assert_equal true, @context['bool']
@context['bool'] = false @context['bool'] = false
assert_equal false, @context['bool'] assert_equal false, @context['bool']
@context['nil'] = nil @context['nil'] = nil
assert_equal nil, @context['nil'] assert_equal nil, @context['nil']
assert_equal nil, @context['nil'] assert_equal nil, @context['nil']
end end
def test_variables_not_existing def test_variables_not_existing
assert_equal nil, @context['does_not_exist'] assert_equal nil, @context['does_not_exist']
end end
def test_scoping def test_scoping
assert_nothing_raised do assert_nothing_raised do
@context.push @context.push
@context.pop @context.pop
end end
assert_raise(Liquid::ContextError) do assert_raise(Liquid::ContextError) do
@context.pop @context.pop
end end
@@ -97,71 +97,71 @@ class ContextTest < Test::Unit::TestCase
@context.pop @context.pop
end end
end end
def test_length_query def test_length_query
@context['numbers'] = [1,2,3,4] @context['numbers'] = [1,2,3,4]
assert_equal 4, @context['numbers.size'] 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'] 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'] assert_equal 1000, @context['numbers.size']
end end
def test_hyphenated_variable def test_hyphenated_variable
@context['oh-my'] = 'godz' @context['oh-my'] = 'godz'
assert_equal 'godz', @context['oh-my'] assert_equal 'godz', @context['oh-my']
end end
def test_add_filter def test_add_filter
filter = Module.new do filter = Module.new do
def hi(output) def hi(output)
output + ' hi!' output + ' hi!'
end end
end end
context = Context.new(@template) context = Context.new(@template)
context.add_filters(filter) context.add_filters(filter)
assert_equal 'hi? hi!', context.invoke(:hi, 'hi?') assert_equal 'hi? hi!', context.invoke(:hi, 'hi?')
context = Context.new(@template) context = Context.new(@template)
assert_equal 'hi?', context.invoke(:hi, 'hi?') assert_equal 'hi?', context.invoke(:hi, 'hi?')
context.add_filters(filter) context.add_filters(filter)
assert_equal 'hi? hi!', context.invoke(:hi, 'hi?') assert_equal 'hi? hi!', context.invoke(:hi, 'hi?')
end end
def test_override_global_filter def test_override_global_filter
global = Module.new do global = Module.new do
def notice(output) def notice(output)
"Global #{output}" "Global #{output}"
end end
end end
local = Module.new do local = Module.new do
def notice(output) def notice(output)
"Local #{output}" "Local #{output}"
end end
end end
Template.register_filter(global) Template.register_filter(global)
assert_equal 'Global test', Template.parse("{{'test' | notice }}").render assert_equal 'Global test', Template.parse("{{'test' | notice }}").render
assert_equal 'Local test', Template.parse("{{'test' | notice }}").render({}, :filters => [local]) assert_equal 'Local test', Template.parse("{{'test' | notice }}").render({}, :filters => [local])
end end
def test_only_intended_filters_make_it_there def test_only_intended_filters_make_it_there
filter = Module.new do filter = Module.new do
def hi(output) def hi(output)
output + ' hi!' output + ' hi!'
end end
@@ -172,28 +172,28 @@ class ContextTest < Test::Unit::TestCase
context.add_filters(filter) context.add_filters(filter)
assert_equal (methods + ['hi']).sort, context.strainer.methods.sort assert_equal (methods + ['hi']).sort, context.strainer.methods.sort
end end
def test_add_item_in_outer_scope def test_add_item_in_outer_scope
@context['test'] = 'test' @context['test'] = 'test'
@context.push @context.push
assert_equal 'test', @context['test'] assert_equal 'test', @context['test']
@context.pop @context.pop
assert_equal 'test', @context['test'] assert_equal 'test', @context['test']
end end
def test_add_item_in_inner_scope def test_add_item_in_inner_scope
@context.push @context.push
@context['test'] = 'test' @context['test'] = 'test'
assert_equal 'test', @context['test'] assert_equal 'test', @context['test']
@context.pop @context.pop
assert_equal nil, @context['test'] assert_equal nil, @context['test']
end end
def test_hierachical_data 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 end
def test_keywords def test_keywords
assert_equal true, @context['true'] assert_equal true, @context['true']
assert_equal false, @context['false'] assert_equal false, @context['false']
@@ -203,20 +203,20 @@ class ContextTest < Test::Unit::TestCase
assert_equal 100, @context['100'] assert_equal 100, @context['100']
assert_equal 100.00, @context['100.00'] assert_equal 100.00, @context['100.00']
end end
def test_strings def test_strings
assert_equal "hello!", @context['"hello!"'] assert_equal "hello!", @context['"hello!"']
assert_equal "hello!", @context["'hello!'"] assert_equal "hello!", @context["'hello!'"]
end end
def test_merge def test_merge
@context.merge({ "test" => "test" }) @context.merge({ "test" => "test" })
assert_equal 'test', @context['test'] assert_equal 'test', @context['test']
@context.merge({ "test" => "newvalue", "foo" => "bar" }) @context.merge({ "test" => "newvalue", "foo" => "bar" })
assert_equal 'newvalue', @context['test'] assert_equal 'newvalue', @context['test']
assert_equal 'bar', @context['foo'] assert_equal 'bar', @context['foo']
end end
def test_array_notation def test_array_notation
@context['test'] = [1,2,3,4,5] @context['test'] = [1,2,3,4,5]
@@ -224,49 +224,49 @@ class ContextTest < Test::Unit::TestCase
assert_equal 2, @context['test[1]'] assert_equal 2, @context['test[1]']
assert_equal 3, @context['test[2]'] assert_equal 3, @context['test[2]']
assert_equal 4, @context['test[3]'] assert_equal 4, @context['test[3]']
assert_equal 5, @context['test[4]'] assert_equal 5, @context['test[4]']
end end
def test_recoursive_array_notation 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]'] assert_equal 1, @context['test.test[0]']
@context['test'] = [{'test' => 'worked'}] @context['test'] = [{'test' => 'worked'}]
assert_equal 'worked', @context['test[0].test'] assert_equal 'worked', @context['test[0].test']
end end
def test_hash_to_array_transition def test_hash_to_array_transition
@context['colors'] = { @context['colors'] = {
'Blue' => ['003366','336699', '6699CC', '99CCFF'], 'Blue' => ['003366','336699', '6699CC', '99CCFF'],
'Green' => ['003300','336633', '669966', '99CC99'], 'Green' => ['003300','336633', '669966', '99CC99'],
'Yellow' => ['CC9900','FFCC00', 'FFFF99', 'FFFFCC'], 'Yellow' => ['CC9900','FFCC00', 'FFFF99', 'FFFFCC'],
'Red' => ['660000','993333', 'CC6666', 'FF9999'] 'Red' => ['660000','993333', 'CC6666', 'FF9999']
} }
assert_equal '003366', @context['colors.Blue[0]'] assert_equal '003366', @context['colors.Blue[0]']
assert_equal 'FF9999', @context['colors.Red[3]'] assert_equal 'FF9999', @context['colors.Red[3]']
end end
def test_try_first 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 1, @context['test.first']
assert_equal 5, @context['test.last'] 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 1, @context['test.test.first']
assert_equal 5, @context['test.test.last'] assert_equal 5, @context['test.test.last']
@context['test'] = [1] @context['test'] = [1]
assert_equal 1, @context['test.first'] assert_equal 1, @context['test.first']
assert_equal 1, @context['test.last'] assert_equal 1, @context['test.last']
end end
def test_access_hashes_with_hash_notation def test_access_hashes_with_hash_notation
@context['products'] = {'count' => 5, 'tags' => ['deepsnow', 'freestyle'] } @context['products'] = {'count' => 5, 'tags' => ['deepsnow', 'freestyle'] }
@context['product'] = {'variants' => [ {'title' => 'draft151cm'}, {'title' => 'element151cm'} ]} @context['product'] = {'variants' => [ {'title' => 'draft151cm'}, {'title' => 'element151cm'} ]}
@@ -279,17 +279,17 @@ class ContextTest < Test::Unit::TestCase
assert_equal 'draft151cm', @context['product["variants"][0]["title"]'] assert_equal 'draft151cm', @context['product["variants"][0]["title"]']
assert_equal 'element151cm', @context['product["variants"].last["title"]'] assert_equal 'element151cm', @context['product["variants"].last["title"]']
end end
def test_access_variable_with_hash_notation def test_access_variable_with_hash_notation
@context['foo'] = 'baz' @context['foo'] = 'baz'
@context['bar'] = 'foo' @context['bar'] = 'foo'
assert_equal 'baz', @context['["foo"]'] assert_equal 'baz', @context['["foo"]']
assert_equal 'baz', @context['[bar]'] assert_equal 'baz', @context['[bar]']
end end
def test_access_hashes_with_hash_access_variables def test_access_hashes_with_hash_access_variables
@context['var'] = 'tags' @context['var'] = 'tags'
@context['nested'] = {'var' => 'tags'} @context['nested'] = {'var' => 'tags'}
@context['products'] = {'count' => 5, 'tags' => ['deepsnow', 'freestyle'] } @context['products'] = {'count' => 5, 'tags' => ['deepsnow', 'freestyle'] }
@@ -297,7 +297,7 @@ class ContextTest < Test::Unit::TestCase
assert_equal 'deepsnow', @context['products[var].first'] assert_equal 'deepsnow', @context['products[var].first']
assert_equal 'freestyle', @context['products[nested.var].last'] assert_equal 'freestyle', @context['products[nested.var].last']
end end
def test_first_can_appear_in_middle_of_callchain def test_first_can_appear_in_middle_of_callchain
@@ -307,9 +307,9 @@ class ContextTest < Test::Unit::TestCase
assert_equal 'element151cm', @context['product.variants[1].title'] assert_equal 'element151cm', @context['product.variants[1].title']
assert_equal 'draft151cm', @context['product.variants.first.title'] assert_equal 'draft151cm', @context['product.variants.first.title']
assert_equal 'element151cm', @context['product.variants.last.title'] assert_equal 'element151cm', @context['product.variants.last.title']
end end
def test_cents def test_cents
@context.merge( "cents" => HundredCentes.new ) @context.merge( "cents" => HundredCentes.new )
assert_equal 100, @context['cents'] assert_equal 100, @context['cents']
@@ -317,37 +317,37 @@ class ContextTest < Test::Unit::TestCase
def test_nested_cents def test_nested_cents
@context.merge( "cents" => { 'amount' => HundredCentes.new} ) @context.merge( "cents" => { 'amount' => HundredCentes.new} )
assert_equal 100, @context['cents.amount'] 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'] assert_equal 100, @context['cents.cents.amount']
end end
def test_cents_through_drop def test_cents_through_drop
@context.merge( "cents" => CentsDrop.new ) @context.merge( "cents" => CentsDrop.new )
assert_equal 100, @context['cents.amount'] assert_equal 100, @context['cents.amount']
end end
def test_nested_cents_through_drop 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'] assert_equal 100, @context['vars.cents.amount']
end end
def test_drop_methods_with_question_marks def test_drop_methods_with_question_marks
@context.merge( "cents" => CentsDrop.new ) @context.merge( "cents" => CentsDrop.new )
assert @context['cents.non_zero?'] assert @context['cents.non_zero?']
end end
def test_context_from_within_drop 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'] assert_equal '123', @context['vars.test']
end end
def test_nested_context_from_within_drop 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'] assert_equal '123', @context['vars.local.test']
end end
def test_ranges def test_ranges
@context.merge( "test" => '5' ) @context.merge( "test" => '5' )
assert_equal (1..5), @context['(1..5)'] assert_equal (1..5), @context['(1..5)']
@@ -357,62 +357,62 @@ class ContextTest < Test::Unit::TestCase
def test_cents_through_drop_nestedly 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'] 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'] assert_equal 100, @context['cents.cents.cents.amount']
end end
def test_proc_as_variable def test_proc_as_variable
@context['dynamic'] = Proc.new { 'Hello' } @context['dynamic'] = Proc.new { 'Hello' }
assert_equal 'Hello', @context['dynamic'] assert_equal 'Hello', @context['dynamic']
end end
def test_lambda_as_variable def test_lambda_as_variable
@context['dynamic'] = lambda { 'Hello' } @context['dynamic'] = lambda { 'Hello' }
assert_equal 'Hello', @context['dynamic'] assert_equal 'Hello', @context['dynamic']
end end
def test_nested_lambda_as_variable def test_nested_lambda_as_variable
@context['dynamic'] = { "lambda" => lambda { 'Hello' } } @context['dynamic'] = { "lambda" => lambda { 'Hello' } }
assert_equal 'Hello', @context['dynamic.lambda'] assert_equal 'Hello', @context['dynamic.lambda']
end end
def test_lambda_is_called_once def test_lambda_is_called_once
@context['callcount'] = lambda { @global ||= 0; @global += 1; @global.to_s } @context['callcount'] = lambda { @global ||= 0; @global += 1; @global.to_s }
assert_equal '1', @context['callcount'] assert_equal '1', @context['callcount']
assert_equal '1', @context['callcount'] assert_equal '1', @context['callcount']
assert_equal '1', @context['callcount'] assert_equal '1', @context['callcount']
@global = nil @global = nil
end end
def test_nested_lambda_is_called_once def test_nested_lambda_is_called_once
@context['callcount'] = { "lambda" => lambda { @global ||= 0; @global += 1; @global.to_s } } @context['callcount'] = { "lambda" => lambda { @global ||= 0; @global += 1; @global.to_s } }
assert_equal '1', @context['callcount.lambda']
assert_equal '1', @context['callcount.lambda'] assert_equal '1', @context['callcount.lambda']
assert_equal '1', @context['callcount.lambda'] assert_equal '1', @context['callcount.lambda']
assert_equal '1', @context['callcount.lambda']
@global = nil @global = nil
end end
def test_access_to_context_from_proc def test_access_to_context_from_proc
@context.registers[:magic] = 345392 @context.registers[:magic] = 345392
@context['magic'] = lambda { @context.registers[:magic] } @context['magic'] = lambda { @context.registers[:magic] }
assert_equal 345392, @context['magic'] assert_equal 345392, @context['magic']
end end
def test_to_liquid_and_context_at_first_level def test_to_liquid_and_context_at_first_level
@context['category'] = Category.new("foobar") @context['category'] = Category.new("foobar")
assert_kind_of CategoryDrop, @context['category'] assert_kind_of CategoryDrop, @context['category']
assert_equal @context, @context['category'].context assert_equal @context, @context['category'].context
end end
end end