liquid 1.9.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest.txt +0 -31
- data/Rakefile +1 -1
- data/lib/extras/liquid_view.rb +15 -2
- data/lib/liquid.rb +15 -15
- data/lib/liquid/block.rb +1 -2
- data/lib/liquid/context.rb +89 -99
- data/lib/liquid/drop.rb +6 -4
- data/lib/liquid/errors.rb +1 -0
- data/lib/liquid/standardfilters.rb +56 -11
- data/lib/liquid/strainer.rb +1 -1
- data/lib/liquid/tags/assign.rb +1 -1
- data/lib/liquid/tags/case.rb +2 -2
- data/lib/liquid/tags/cycle.rb +3 -4
- data/lib/liquid/tags/for.rb +53 -35
- data/lib/liquid/tags/if.rb +3 -3
- data/lib/liquid/template.rb +8 -7
- data/lib/liquid/variable.rb +10 -11
- metadata +5 -35
- data/example/server/example_servlet.rb +0 -37
- data/example/server/liquid_servlet.rb +0 -28
- data/example/server/server.rb +0 -12
- data/example/server/templates/index.liquid +0 -6
- data/example/server/templates/products.liquid +0 -45
- data/test/block_test.rb +0 -58
- data/test/condition_test.rb +0 -109
- data/test/context_test.rb +0 -418
- data/test/drop_test.rb +0 -141
- data/test/error_handling_test.rb +0 -78
- data/test/extra/breakpoint.rb +0 -547
- data/test/extra/caller.rb +0 -80
- data/test/file_system_test.rb +0 -30
- data/test/filter_test.rb +0 -98
- data/test/helper.rb +0 -20
- data/test/html_tag_test.rb +0 -31
- data/test/if_else_test.rb +0 -127
- data/test/include_tag_test.rb +0 -114
- data/test/module_ex_test.rb +0 -89
- data/test/output_test.rb +0 -121
- data/test/parsing_quirks_test.rb +0 -29
- data/test/regexp_test.rb +0 -40
- data/test/security_test.rb +0 -41
- data/test/standard_filter_test.rb +0 -126
- data/test/standard_tag_test.rb +0 -383
- data/test/statements_test.rb +0 -137
- data/test/strainer_test.rb +0 -16
- data/test/template_test.rb +0 -26
- data/test/unless_else_test.rb +0 -27
- data/test/variable_test.rb +0 -135
@@ -1,37 +0,0 @@
|
|
1
|
-
module ProductsFilter
|
2
|
-
def price(integer)
|
3
|
-
sprintf("$%.2d USD", integer / 100.0)
|
4
|
-
end
|
5
|
-
|
6
|
-
def prettyprint(text)
|
7
|
-
text.gsub( /\*(.*)\*/, '<b>\1</b>' )
|
8
|
-
end
|
9
|
-
|
10
|
-
def count(array)
|
11
|
-
array.size
|
12
|
-
end
|
13
|
-
|
14
|
-
def paragraph(p)
|
15
|
-
"<p>#{p}</p>"
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
class Servlet < LiquidServlet
|
20
|
-
|
21
|
-
def index
|
22
|
-
{ 'date' => Time.now }
|
23
|
-
end
|
24
|
-
|
25
|
-
def products
|
26
|
-
{ 'products' => products_list, 'section' => 'Snowboards', 'cool_products' => true}
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
30
|
-
|
31
|
-
def products_list
|
32
|
-
[{'name' => 'Arbor Draft', 'price' => 39900, 'description' => 'the *arbor draft* is a excellent product' },
|
33
|
-
{'name' => 'Arbor Element', 'price' => 40000, 'description' => 'the *arbor element* rocks for freestyling'},
|
34
|
-
{'name' => 'Arbor Diamond', 'price' => 59900, 'description' => 'the *arbor diamond* is a made up product because im obsessed with arbor and have no creativity'}]
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
class LiquidServlet < WEBrick::HTTPServlet::AbstractServlet
|
2
|
-
|
3
|
-
def do_GET(req, res)
|
4
|
-
handle(:get, req, res)
|
5
|
-
end
|
6
|
-
|
7
|
-
def do_POST(req, res)
|
8
|
-
handle(:post, req, res)
|
9
|
-
end
|
10
|
-
|
11
|
-
private
|
12
|
-
|
13
|
-
def handle(type, req, res)
|
14
|
-
@request, @response = req, res
|
15
|
-
|
16
|
-
@request.path_info =~ /(\w+)$/
|
17
|
-
@action = $1 || 'index'
|
18
|
-
@assigns = send(@action) if respond_to?(@action)
|
19
|
-
|
20
|
-
@response['Content-Type'] = "text/html"
|
21
|
-
@response.status = 200
|
22
|
-
@response.body = Liquid::Template.parse(read_template).render(@assigns, :filters => [ProductsFilter])
|
23
|
-
end
|
24
|
-
|
25
|
-
def read_template(filename = @action)
|
26
|
-
File.read( File.dirname(__FILE__) + "/templates/#{filename}.liquid" )
|
27
|
-
end
|
28
|
-
end
|
data/example/server/server.rb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
require 'webrick'
|
2
|
-
require 'rexml/document'
|
3
|
-
|
4
|
-
require File.dirname(__FILE__) + '/../../lib/liquid'
|
5
|
-
require File.dirname(__FILE__) + '/liquid_servlet'
|
6
|
-
require File.dirname(__FILE__) + '/example_servlet'
|
7
|
-
|
8
|
-
# Setup webrick
|
9
|
-
server = WEBrick::HTTPServer.new( :Port => ARGV[1] || 3000 )
|
10
|
-
server.mount('/', Servlet)
|
11
|
-
trap("INT"){ server.shutdown }
|
12
|
-
server.start
|
@@ -1,45 +0,0 @@
|
|
1
|
-
<?xml version="1.0" encoding="utf-8"?>
|
2
|
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
3
|
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
4
|
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
5
|
-
<head>
|
6
|
-
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
|
7
|
-
<meta http-equiv="Content-Language" content="en-us" />
|
8
|
-
|
9
|
-
<title>products</title>
|
10
|
-
|
11
|
-
<meta name="ROBOTS" content="ALL" />
|
12
|
-
<meta http-equiv="imagetoolbar" content="no" />
|
13
|
-
<meta name="MSSmartTagsPreventParsing" content="true" />
|
14
|
-
<meta name="Copyright" content="(c) 2005 Copyright content: Copyright design: Tobias Luetke" />
|
15
|
-
<!-- (c) Copyright 2005 by Tobias Luetke All Rights Reserved. -->
|
16
|
-
</head>
|
17
|
-
|
18
|
-
<body>
|
19
|
-
|
20
|
-
<h1>There are currently {{products | count}} products in the {{section}} catalog</h1>
|
21
|
-
|
22
|
-
{% if cool_products %}
|
23
|
-
Cool products :)
|
24
|
-
{% else %}
|
25
|
-
Uncool products :(
|
26
|
-
{% endif %}
|
27
|
-
|
28
|
-
<ul id="products">
|
29
|
-
|
30
|
-
{% for product in products %}
|
31
|
-
<li>
|
32
|
-
<h2>{{product.name}}</h2>
|
33
|
-
Only {{product.price | price }}
|
34
|
-
|
35
|
-
{{product.description | prettyprint | paragraph }}
|
36
|
-
|
37
|
-
{{ 'it rocks!' | paragraph }}
|
38
|
-
|
39
|
-
</li>
|
40
|
-
{% endfor %}
|
41
|
-
|
42
|
-
</ul>
|
43
|
-
|
44
|
-
</body>
|
45
|
-
</html>
|
data/test/block_test.rb
DELETED
@@ -1,58 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/helper'
|
2
|
-
|
3
|
-
class VariableTest < Test::Unit::TestCase
|
4
|
-
include Liquid
|
5
|
-
|
6
|
-
def test_blankspace
|
7
|
-
template = Liquid::Template.parse(" ")
|
8
|
-
assert_equal [" "], template.root.nodelist
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_variable_beginning
|
12
|
-
template = Liquid::Template.parse("{{funk}} ")
|
13
|
-
assert_equal 2, template.root.nodelist.size
|
14
|
-
assert_equal Variable, template.root.nodelist[0].class
|
15
|
-
assert_equal String, template.root.nodelist[1].class
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_variable_end
|
19
|
-
template = Liquid::Template.parse(" {{funk}}")
|
20
|
-
assert_equal 2, template.root.nodelist.size
|
21
|
-
assert_equal String, template.root.nodelist[0].class
|
22
|
-
assert_equal Variable, template.root.nodelist[1].class
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_variable_middle
|
26
|
-
template = Liquid::Template.parse(" {{funk}} ")
|
27
|
-
assert_equal 3, template.root.nodelist.size
|
28
|
-
assert_equal String, template.root.nodelist[0].class
|
29
|
-
assert_equal Variable, template.root.nodelist[1].class
|
30
|
-
assert_equal String, template.root.nodelist[2].class
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_variable_many_embedded_fragments
|
34
|
-
template = Liquid::Template.parse(" {{funk}} {{so}} {{brother}} ")
|
35
|
-
assert_equal 7, template.root.nodelist.size
|
36
|
-
assert_equal [String, Variable, String, Variable, String, Variable, String], block_types(template.root.nodelist)
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_with_block
|
40
|
-
template = Liquid::Template.parse(" {% comment %} {% endcomment %} ")
|
41
|
-
assert_equal [String, Comment, String], block_types(template.root.nodelist)
|
42
|
-
assert_equal 3, template.root.nodelist.size
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_with_custom_tag
|
46
|
-
Liquid::Template.register_tag("testtag", Block)
|
47
|
-
|
48
|
-
assert_nothing_thrown do
|
49
|
-
template = Liquid::Template.parse( "{% testtag %} {% endtesttag %}")
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
private
|
54
|
-
|
55
|
-
def block_types(nodelist)
|
56
|
-
nodelist.collect { |node| node.class }
|
57
|
-
end
|
58
|
-
end
|
data/test/condition_test.rb
DELETED
@@ -1,109 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/helper'
|
2
|
-
|
3
|
-
class ConditionTest < Test::Unit::TestCase
|
4
|
-
include Liquid
|
5
|
-
|
6
|
-
def test_basic_condition
|
7
|
-
assert_equal false, Condition.new('1', '==', '2').evaluate
|
8
|
-
assert_equal true, Condition.new('1', '==', '1').evaluate
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_default_operators_evalute_true
|
12
|
-
assert_evalutes_true '1', '==', '1'
|
13
|
-
assert_evalutes_true '1', '!=', '2'
|
14
|
-
assert_evalutes_true '1', '<>', '2'
|
15
|
-
assert_evalutes_true '1', '<', '2'
|
16
|
-
assert_evalutes_true '2', '>', '1'
|
17
|
-
assert_evalutes_true '1', '>=', '1'
|
18
|
-
assert_evalutes_true '2', '>=', '1'
|
19
|
-
assert_evalutes_true '1', '<=', '2'
|
20
|
-
assert_evalutes_true '1', '<=', '1'
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_default_operators_evalute_false
|
24
|
-
assert_evalutes_false '1', '==', '2'
|
25
|
-
assert_evalutes_false '1', '!=', '1'
|
26
|
-
assert_evalutes_false '1', '<>', '1'
|
27
|
-
assert_evalutes_false '1', '<', '0'
|
28
|
-
assert_evalutes_false '2', '>', '4'
|
29
|
-
assert_evalutes_false '1', '>=', '3'
|
30
|
-
assert_evalutes_false '2', '>=', '4'
|
31
|
-
assert_evalutes_false '1', '<=', '0'
|
32
|
-
assert_evalutes_false '1', '<=', '0'
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_contains_works_on_strings
|
36
|
-
assert_evalutes_true "'bob'", 'contains', "'o'"
|
37
|
-
assert_evalutes_true "'bob'", 'contains', "'b'"
|
38
|
-
assert_evalutes_true "'bob'", 'contains', "'bo'"
|
39
|
-
assert_evalutes_true "'bob'", 'contains', "'ob'"
|
40
|
-
assert_evalutes_true "'bob'", 'contains', "'bob'"
|
41
|
-
|
42
|
-
assert_evalutes_false "'bob'", 'contains', "'bob2'"
|
43
|
-
assert_evalutes_false "'bob'", 'contains', "'a'"
|
44
|
-
assert_evalutes_false "'bob'", 'contains', "'---'"
|
45
|
-
end
|
46
|
-
|
47
|
-
def test_contains_works_on_arrays
|
48
|
-
@context = Liquid::Context.new
|
49
|
-
@context['array'] = [1,2,3,4,5]
|
50
|
-
|
51
|
-
assert_evalutes_false "array", 'contains', '0'
|
52
|
-
assert_evalutes_true "array", 'contains', '1'
|
53
|
-
assert_evalutes_true "array", 'contains', '2'
|
54
|
-
assert_evalutes_true "array", 'contains', '3'
|
55
|
-
assert_evalutes_true "array", 'contains', '4'
|
56
|
-
assert_evalutes_true "array", 'contains', '5'
|
57
|
-
assert_evalutes_false "array", 'contains', '6'
|
58
|
-
|
59
|
-
assert_evalutes_false "array", 'contains', '"1"'
|
60
|
-
|
61
|
-
end
|
62
|
-
|
63
|
-
def test_or_condition
|
64
|
-
condition = Condition.new('1', '==', '2')
|
65
|
-
|
66
|
-
assert_equal false, condition.evaluate
|
67
|
-
|
68
|
-
condition.or Condition.new('2', '==', '1')
|
69
|
-
|
70
|
-
assert_equal false, condition.evaluate
|
71
|
-
|
72
|
-
condition.or Condition.new('1', '==', '1')
|
73
|
-
|
74
|
-
assert_equal true, condition.evaluate
|
75
|
-
end
|
76
|
-
|
77
|
-
def test_and_condition
|
78
|
-
condition = Condition.new('1', '==', '1')
|
79
|
-
|
80
|
-
assert_equal true, condition.evaluate
|
81
|
-
|
82
|
-
condition.and Condition.new('2', '==', '2')
|
83
|
-
|
84
|
-
assert_equal true, condition.evaluate
|
85
|
-
|
86
|
-
condition.and Condition.new('2', '==', '1')
|
87
|
-
|
88
|
-
assert_equal false, condition.evaluate
|
89
|
-
end
|
90
|
-
|
91
|
-
|
92
|
-
def test_should_allow_custom_proc_operator
|
93
|
-
Condition.operators['starts_with'] = Proc.new { |cond, left, right| left =~ %r{^#{right}}}
|
94
|
-
|
95
|
-
assert_evalutes_true "'bob'", 'starts_with', "'b'"
|
96
|
-
assert_evalutes_false "'bob'", 'starts_with', "'o'"
|
97
|
-
ensure
|
98
|
-
Condition.operators.delete 'starts_with'
|
99
|
-
end
|
100
|
-
|
101
|
-
private
|
102
|
-
def assert_evalutes_true(left, op, right)
|
103
|
-
assert Condition.new(left, op, right).evaluate(@context || Liquid::Context.new), "Evaluated false: #{left} #{op} #{right}"
|
104
|
-
end
|
105
|
-
|
106
|
-
def assert_evalutes_false(left, op, right)
|
107
|
-
assert !Condition.new(left, op, right).evaluate(@context || Liquid::Context.new), "Evaluated true: #{left} #{op} #{right}"
|
108
|
-
end
|
109
|
-
end
|
data/test/context_test.rb
DELETED
@@ -1,418 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/helper'
|
2
|
-
class HundredCentes
|
3
|
-
def to_liquid
|
4
|
-
100
|
5
|
-
end
|
6
|
-
end
|
7
|
-
|
8
|
-
class CentsDrop < Liquid::Drop
|
9
|
-
def amount
|
10
|
-
HundredCentes.new
|
11
|
-
end
|
12
|
-
|
13
|
-
def non_zero?
|
14
|
-
true
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
class ContextSensitiveDrop < Liquid::Drop
|
19
|
-
def test
|
20
|
-
@context['test']
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
class Category < Liquid::Drop
|
25
|
-
attr_accessor :name
|
26
|
-
|
27
|
-
def initialize(name)
|
28
|
-
@name = name
|
29
|
-
end
|
30
|
-
|
31
|
-
def to_liquid
|
32
|
-
CategoryDrop.new(self)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
class CategoryDrop
|
37
|
-
attr_accessor :category, :context
|
38
|
-
def initialize(category)
|
39
|
-
@category = category
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
|
44
|
-
class ContextTest < Test::Unit::TestCase
|
45
|
-
include Liquid
|
46
|
-
|
47
|
-
def setup
|
48
|
-
@template = Liquid::Template.new
|
49
|
-
@context = Liquid::Context.new(@template.assigns, @template.registers)
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_variables
|
53
|
-
@context['string'] = 'string'
|
54
|
-
assert_equal 'string', @context['string']
|
55
|
-
|
56
|
-
@context['num'] = 5
|
57
|
-
assert_equal 5, @context['num']
|
58
|
-
|
59
|
-
@context['time'] = Time.parse('2006-06-06 12:00:00')
|
60
|
-
assert_equal Time.parse('2006-06-06 12:00:00'), @context['time']
|
61
|
-
|
62
|
-
@context['date'] = Date.today
|
63
|
-
assert_equal Date.today, @context['date']
|
64
|
-
|
65
|
-
now = DateTime.now
|
66
|
-
@context['datetime'] = now
|
67
|
-
assert_equal now, @context['datetime']
|
68
|
-
|
69
|
-
@context['bool'] = true
|
70
|
-
assert_equal true, @context['bool']
|
71
|
-
|
72
|
-
@context['bool'] = false
|
73
|
-
assert_equal false, @context['bool']
|
74
|
-
|
75
|
-
@context['nil'] = nil
|
76
|
-
assert_equal nil, @context['nil']
|
77
|
-
assert_equal nil, @context['nil']
|
78
|
-
end
|
79
|
-
|
80
|
-
def test_variables_not_existing
|
81
|
-
assert_equal nil, @context['does_not_exist']
|
82
|
-
end
|
83
|
-
|
84
|
-
def test_scoping
|
85
|
-
assert_nothing_raised do
|
86
|
-
@context.push
|
87
|
-
@context.pop
|
88
|
-
end
|
89
|
-
|
90
|
-
assert_raise(Liquid::ContextError) do
|
91
|
-
@context.pop
|
92
|
-
end
|
93
|
-
|
94
|
-
assert_raise(Liquid::ContextError) do
|
95
|
-
@context.push
|
96
|
-
@context.pop
|
97
|
-
@context.pop
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
def test_length_query
|
102
|
-
|
103
|
-
@context['numbers'] = [1,2,3,4]
|
104
|
-
|
105
|
-
assert_equal 4, @context['numbers.size']
|
106
|
-
|
107
|
-
@context['numbers'] = {1 => 1,2 => 2,3 => 3,4 => 4}
|
108
|
-
|
109
|
-
assert_equal 4, @context['numbers.size']
|
110
|
-
|
111
|
-
@context['numbers'] = {1 => 1,2 => 2,3 => 3,4 => 4, 'size' => 1000}
|
112
|
-
|
113
|
-
assert_equal 1000, @context['numbers.size']
|
114
|
-
|
115
|
-
end
|
116
|
-
|
117
|
-
def test_hyphenated_variable
|
118
|
-
|
119
|
-
@context['oh-my'] = 'godz'
|
120
|
-
assert_equal 'godz', @context['oh-my']
|
121
|
-
|
122
|
-
end
|
123
|
-
|
124
|
-
def test_add_filter
|
125
|
-
|
126
|
-
filter = Module.new do
|
127
|
-
def hi(output)
|
128
|
-
output + ' hi!'
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
context = Context.new(@template)
|
133
|
-
context.add_filters(filter)
|
134
|
-
assert_equal 'hi? hi!', context.invoke(:hi, 'hi?')
|
135
|
-
|
136
|
-
context = Context.new(@template)
|
137
|
-
assert_equal 'hi?', context.invoke(:hi, 'hi?')
|
138
|
-
|
139
|
-
context.add_filters(filter)
|
140
|
-
assert_equal 'hi? hi!', context.invoke(:hi, 'hi?')
|
141
|
-
|
142
|
-
end
|
143
|
-
|
144
|
-
def test_override_global_filter
|
145
|
-
global = Module.new do
|
146
|
-
def notice(output)
|
147
|
-
"Global #{output}"
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
|
-
local = Module.new do
|
152
|
-
def notice(output)
|
153
|
-
"Local #{output}"
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
Template.register_filter(global)
|
158
|
-
assert_equal 'Global test', Template.parse("{{'test' | notice }}").render
|
159
|
-
assert_equal 'Local test', Template.parse("{{'test' | notice }}").render({}, :filters => [local])
|
160
|
-
end
|
161
|
-
|
162
|
-
def test_only_intended_filters_make_it_there
|
163
|
-
|
164
|
-
filter = Module.new do
|
165
|
-
def hi(output)
|
166
|
-
output + ' hi!'
|
167
|
-
end
|
168
|
-
end
|
169
|
-
|
170
|
-
context = Context.new(@template)
|
171
|
-
methods = context.strainer.methods
|
172
|
-
context.add_filters(filter)
|
173
|
-
assert_equal (methods + ['hi']).sort, context.strainer.methods.sort
|
174
|
-
end
|
175
|
-
|
176
|
-
def test_add_item_in_outer_scope
|
177
|
-
@context['test'] = 'test'
|
178
|
-
@context.push
|
179
|
-
assert_equal 'test', @context['test']
|
180
|
-
@context.pop
|
181
|
-
assert_equal 'test', @context['test']
|
182
|
-
end
|
183
|
-
|
184
|
-
def test_add_item_in_inner_scope
|
185
|
-
@context.push
|
186
|
-
@context['test'] = 'test'
|
187
|
-
assert_equal 'test', @context['test']
|
188
|
-
@context.pop
|
189
|
-
assert_equal nil, @context['test']
|
190
|
-
end
|
191
|
-
|
192
|
-
def test_hierachical_data
|
193
|
-
@context['hash'] = {"name" => 'tobi'}
|
194
|
-
assert_equal 'tobi', @context['hash.name']
|
195
|
-
end
|
196
|
-
|
197
|
-
def test_keywords
|
198
|
-
assert_equal true, @context['true']
|
199
|
-
assert_equal false, @context['false']
|
200
|
-
end
|
201
|
-
|
202
|
-
def test_digits
|
203
|
-
assert_equal 100, @context['100']
|
204
|
-
assert_equal 100.00, @context['100.00']
|
205
|
-
end
|
206
|
-
|
207
|
-
def test_strings
|
208
|
-
assert_equal "hello!", @context['"hello!"']
|
209
|
-
assert_equal "hello!", @context["'hello!'"]
|
210
|
-
end
|
211
|
-
|
212
|
-
def test_merge
|
213
|
-
@context.merge({ "test" => "test" })
|
214
|
-
assert_equal 'test', @context['test']
|
215
|
-
@context.merge({ "test" => "newvalue", "foo" => "bar" })
|
216
|
-
assert_equal 'newvalue', @context['test']
|
217
|
-
assert_equal 'bar', @context['foo']
|
218
|
-
end
|
219
|
-
|
220
|
-
def test_array_notation
|
221
|
-
@context['test'] = [1,2,3,4,5]
|
222
|
-
|
223
|
-
assert_equal 1, @context['test[0]']
|
224
|
-
assert_equal 2, @context['test[1]']
|
225
|
-
assert_equal 3, @context['test[2]']
|
226
|
-
assert_equal 4, @context['test[3]']
|
227
|
-
assert_equal 5, @context['test[4]']
|
228
|
-
end
|
229
|
-
|
230
|
-
def test_recoursive_array_notation
|
231
|
-
@context['test'] = {'test' => [1,2,3,4,5]}
|
232
|
-
|
233
|
-
assert_equal 1, @context['test.test[0]']
|
234
|
-
|
235
|
-
@context['test'] = [{'test' => 'worked'}]
|
236
|
-
|
237
|
-
assert_equal 'worked', @context['test[0].test']
|
238
|
-
end
|
239
|
-
|
240
|
-
def test_hash_to_array_transition
|
241
|
-
@context['colors'] = {
|
242
|
-
'Blue' => ['003366','336699', '6699CC', '99CCFF'],
|
243
|
-
'Green' => ['003300','336633', '669966', '99CC99'],
|
244
|
-
'Yellow' => ['CC9900','FFCC00', 'FFFF99', 'FFFFCC'],
|
245
|
-
'Red' => ['660000','993333', 'CC6666', 'FF9999']
|
246
|
-
}
|
247
|
-
|
248
|
-
assert_equal '003366', @context['colors.Blue[0]']
|
249
|
-
assert_equal 'FF9999', @context['colors.Red[3]']
|
250
|
-
end
|
251
|
-
|
252
|
-
def test_try_first
|
253
|
-
@context['test'] = [1,2,3,4,5]
|
254
|
-
|
255
|
-
assert_equal 1, @context['test.first']
|
256
|
-
assert_equal 5, @context['test.last']
|
257
|
-
|
258
|
-
@context['test'] = {'test' => [1,2,3,4,5]}
|
259
|
-
|
260
|
-
assert_equal 1, @context['test.test.first']
|
261
|
-
assert_equal 5, @context['test.test.last']
|
262
|
-
|
263
|
-
@context['test'] = [1]
|
264
|
-
assert_equal 1, @context['test.first']
|
265
|
-
assert_equal 1, @context['test.last']
|
266
|
-
end
|
267
|
-
|
268
|
-
def test_access_hashes_with_hash_notation
|
269
|
-
|
270
|
-
@context['products'] = {'count' => 5, 'tags' => ['deepsnow', 'freestyle'] }
|
271
|
-
@context['product'] = {'variants' => [ {'title' => 'draft151cm'}, {'title' => 'element151cm'} ]}
|
272
|
-
|
273
|
-
|
274
|
-
assert_equal 5, @context['products["count"]']
|
275
|
-
assert_equal 'deepsnow', @context['products["tags"][0]']
|
276
|
-
assert_equal 'deepsnow', @context['products["tags"].first']
|
277
|
-
assert_equal 'draft151cm', @context['product["variants"][0]["title"]']
|
278
|
-
assert_equal 'element151cm', @context['product["variants"][1]["title"]']
|
279
|
-
assert_equal 'draft151cm', @context['product["variants"][0]["title"]']
|
280
|
-
assert_equal 'element151cm', @context['product["variants"].last["title"]']
|
281
|
-
end
|
282
|
-
|
283
|
-
def test_access_variable_with_hash_notation
|
284
|
-
@context['foo'] = 'baz'
|
285
|
-
@context['bar'] = 'foo'
|
286
|
-
|
287
|
-
assert_equal 'baz', @context['["foo"]']
|
288
|
-
assert_equal 'baz', @context['[bar]']
|
289
|
-
end
|
290
|
-
|
291
|
-
def test_access_hashes_with_hash_access_variables
|
292
|
-
|
293
|
-
@context['var'] = 'tags'
|
294
|
-
@context['nested'] = {'var' => 'tags'}
|
295
|
-
@context['products'] = {'count' => 5, 'tags' => ['deepsnow', 'freestyle'] }
|
296
|
-
|
297
|
-
assert_equal 'deepsnow', @context['products[var].first']
|
298
|
-
assert_equal 'freestyle', @context['products[nested.var].last']
|
299
|
-
end
|
300
|
-
|
301
|
-
|
302
|
-
def test_first_can_appear_in_middle_of_callchain
|
303
|
-
|
304
|
-
@context['product'] = {'variants' => [ {'title' => 'draft151cm'}, {'title' => 'element151cm'} ]}
|
305
|
-
|
306
|
-
assert_equal 'draft151cm', @context['product.variants[0].title']
|
307
|
-
assert_equal 'element151cm', @context['product.variants[1].title']
|
308
|
-
assert_equal 'draft151cm', @context['product.variants.first.title']
|
309
|
-
assert_equal 'element151cm', @context['product.variants.last.title']
|
310
|
-
|
311
|
-
end
|
312
|
-
|
313
|
-
def test_cents
|
314
|
-
@context.merge( "cents" => HundredCentes.new )
|
315
|
-
assert_equal 100, @context['cents']
|
316
|
-
end
|
317
|
-
|
318
|
-
def test_nested_cents
|
319
|
-
@context.merge( "cents" => { 'amount' => HundredCentes.new} )
|
320
|
-
assert_equal 100, @context['cents.amount']
|
321
|
-
|
322
|
-
@context.merge( "cents" => { 'cents' => { 'amount' => HundredCentes.new} } )
|
323
|
-
assert_equal 100, @context['cents.cents.amount']
|
324
|
-
end
|
325
|
-
|
326
|
-
def test_cents_through_drop
|
327
|
-
@context.merge( "cents" => CentsDrop.new )
|
328
|
-
assert_equal 100, @context['cents.amount']
|
329
|
-
end
|
330
|
-
|
331
|
-
def test_nested_cents_through_drop
|
332
|
-
@context.merge( "vars" => {"cents" => CentsDrop.new} )
|
333
|
-
assert_equal 100, @context['vars.cents.amount']
|
334
|
-
end
|
335
|
-
|
336
|
-
def test_drop_methods_with_question_marks
|
337
|
-
@context.merge( "cents" => CentsDrop.new )
|
338
|
-
assert @context['cents.non_zero?']
|
339
|
-
end
|
340
|
-
|
341
|
-
def test_context_from_within_drop
|
342
|
-
@context.merge( "test" => '123', "vars" => ContextSensitiveDrop.new )
|
343
|
-
assert_equal '123', @context['vars.test']
|
344
|
-
end
|
345
|
-
|
346
|
-
def test_nested_context_from_within_drop
|
347
|
-
@context.merge( "test" => '123', "vars" => {"local" => ContextSensitiveDrop.new } )
|
348
|
-
assert_equal '123', @context['vars.local.test']
|
349
|
-
end
|
350
|
-
|
351
|
-
def test_ranges
|
352
|
-
@context.merge( "test" => '5' )
|
353
|
-
assert_equal (1..5), @context['(1..5)']
|
354
|
-
assert_equal (1..5), @context['(1..test)']
|
355
|
-
assert_equal (5..5), @context['(test..test)']
|
356
|
-
end
|
357
|
-
|
358
|
-
def test_cents_through_drop_nestedly
|
359
|
-
@context.merge( "cents" => {"cents" => CentsDrop.new} )
|
360
|
-
assert_equal 100, @context['cents.cents.amount']
|
361
|
-
|
362
|
-
@context.merge( "cents" => { "cents" => {"cents" => CentsDrop.new}} )
|
363
|
-
assert_equal 100, @context['cents.cents.cents.amount']
|
364
|
-
end
|
365
|
-
|
366
|
-
def test_proc_as_variable
|
367
|
-
@context['dynamic'] = Proc.new { 'Hello' }
|
368
|
-
|
369
|
-
assert_equal 'Hello', @context['dynamic']
|
370
|
-
end
|
371
|
-
|
372
|
-
def test_lambda_as_variable
|
373
|
-
@context['dynamic'] = lambda { 'Hello' }
|
374
|
-
|
375
|
-
assert_equal 'Hello', @context['dynamic']
|
376
|
-
end
|
377
|
-
|
378
|
-
def test_nested_lambda_as_variable
|
379
|
-
@context['dynamic'] = { "lambda" => lambda { 'Hello' } }
|
380
|
-
|
381
|
-
assert_equal 'Hello', @context['dynamic.lambda']
|
382
|
-
end
|
383
|
-
|
384
|
-
def test_lambda_is_called_once
|
385
|
-
@context['callcount'] = lambda { @global ||= 0; @global += 1; @global.to_s }
|
386
|
-
|
387
|
-
assert_equal '1', @context['callcount']
|
388
|
-
assert_equal '1', @context['callcount']
|
389
|
-
assert_equal '1', @context['callcount']
|
390
|
-
|
391
|
-
@global = nil
|
392
|
-
end
|
393
|
-
|
394
|
-
def test_nested_lambda_is_called_once
|
395
|
-
@context['callcount'] = { "lambda" => lambda { @global ||= 0; @global += 1; @global.to_s } }
|
396
|
-
|
397
|
-
assert_equal '1', @context['callcount.lambda']
|
398
|
-
assert_equal '1', @context['callcount.lambda']
|
399
|
-
assert_equal '1', @context['callcount.lambda']
|
400
|
-
|
401
|
-
@global = nil
|
402
|
-
end
|
403
|
-
|
404
|
-
def test_access_to_context_from_proc
|
405
|
-
@context.registers[:magic] = 345392
|
406
|
-
|
407
|
-
@context['magic'] = lambda { @context.registers[:magic] }
|
408
|
-
|
409
|
-
assert_equal 345392, @context['magic']
|
410
|
-
end
|
411
|
-
|
412
|
-
def test_to_liquid_and_context_at_first_level
|
413
|
-
@context['category'] = Category.new("foobar")
|
414
|
-
assert_kind_of CategoryDrop, @context['category']
|
415
|
-
assert_equal @context, @context['category'].context
|
416
|
-
end
|
417
|
-
|
418
|
-
end
|