liquor 0.1.1 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (176) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +3 -9
  5. data/Gemfile +7 -0
  6. data/Guardfile +11 -0
  7. data/MIT-LICENSE +6 -2
  8. data/README.md +4 -122
  9. data/Rakefile +20 -23
  10. data/doc/language-spec.html +768 -0
  11. data/doc/language-spec.md +698 -0
  12. data/lib/liquor.rb +39 -68
  13. data/lib/liquor/ast_tools.rb +28 -0
  14. data/lib/liquor/compiler.rb +110 -0
  15. data/lib/liquor/context.rb +76 -254
  16. data/lib/liquor/diagnostics.rb +151 -0
  17. data/lib/liquor/drop/drop.rb +168 -0
  18. data/lib/liquor/drop/drop_delegation.rb +24 -0
  19. data/lib/liquor/drop/drop_scope.rb +118 -0
  20. data/lib/liquor/drop/dropable.rb +17 -0
  21. data/lib/liquor/emitter.rb +313 -0
  22. data/lib/liquor/extensions/kaminari.rb +14 -0
  23. data/lib/liquor/extensions/pagination.rb +235 -0
  24. data/lib/liquor/extensions/rails.rb +97 -0
  25. data/lib/liquor/extensions/thinking_sphinx.rb +14 -0
  26. data/lib/liquor/extensions/tire.rb +30 -0
  27. data/lib/liquor/external.rb +79 -0
  28. data/lib/liquor/function.rb +94 -0
  29. data/lib/liquor/grammar/lexer.rb +1223 -0
  30. data/lib/liquor/grammar/lexer.rl +297 -0
  31. data/lib/liquor/grammar/parser.racc +288 -0
  32. data/lib/liquor/grammar/parser.rb +885 -0
  33. data/lib/liquor/library.rb +41 -0
  34. data/lib/liquor/manager.rb +146 -0
  35. data/lib/liquor/runtime.rb +167 -0
  36. data/lib/liquor/stdlib/builtin_functions.rb +315 -0
  37. data/lib/liquor/stdlib/builtin_tags.rb +228 -0
  38. data/lib/liquor/stdlib/html_truncater.rb +162 -0
  39. data/lib/liquor/stdlib/partial_tags.rb +76 -0
  40. data/lib/liquor/tag.rb +83 -14
  41. data/lib/liquor/version.rb +1 -1
  42. data/liquor.gemspec +29 -6
  43. data/spec/builtins_spec.rb +264 -0
  44. data/spec/compiler_spec.rb +136 -0
  45. data/spec/context_spec.rb +49 -0
  46. data/spec/drop_delegation_spec.rb +21 -0
  47. data/spec/drop_spec.rb +222 -0
  48. data/spec/errors_spec.rb +40 -0
  49. data/spec/external_spec.rb +207 -0
  50. data/spec/function_spec.rb +80 -0
  51. data/spec/lexer_spec.rb +173 -0
  52. data/spec/library_spec.rb +18 -0
  53. data/spec/manager_spec.rb +84 -0
  54. data/spec/parser_spec.rb +381 -0
  55. data/spec/partials_spec.rb +74 -0
  56. data/spec/runtime_spec.rb +97 -0
  57. data/spec/spec_helper.rb +94 -0
  58. data/spec/tag_spec.rb +7 -0
  59. metadata +216 -173
  60. data/AUTHORS +0 -2
  61. data/CHANGELOG +0 -48
  62. data/Gemfile.lock +0 -91
  63. data/History.txt +0 -44
  64. data/LICENSE +0 -23
  65. data/example/server/example_servlet.rb +0 -37
  66. data/example/server/liquid_servlet.rb +0 -28
  67. data/example/server/liquor_servlet.rb +0 -28
  68. data/example/server/server.rb +0 -12
  69. data/example/server/templates/index.liquid +0 -6
  70. data/example/server/templates/index.liquor +0 -6
  71. data/example/server/templates/products.liquid +0 -45
  72. data/example/server/templates/products.liquor +0 -45
  73. data/init.rb +0 -8
  74. data/lib/extras/liquid_view.rb +0 -51
  75. data/lib/extras/liquor_view.rb +0 -51
  76. data/lib/liquor/block.rb +0 -101
  77. data/lib/liquor/condition.rb +0 -120
  78. data/lib/liquor/document.rb +0 -17
  79. data/lib/liquor/drop.rb +0 -256
  80. data/lib/liquor/errors.rb +0 -11
  81. data/lib/liquor/extensions.rb +0 -72
  82. data/lib/liquor/file_system.rb +0 -62
  83. data/lib/liquor/htmltags.rb +0 -74
  84. data/lib/liquor/module_ex.rb +0 -60
  85. data/lib/liquor/standardfilters.rb +0 -315
  86. data/lib/liquor/strainer.rb +0 -58
  87. data/lib/liquor/tags/assign.rb +0 -33
  88. data/lib/liquor/tags/capture.rb +0 -35
  89. data/lib/liquor/tags/case.rb +0 -83
  90. data/lib/liquor/tags/comment.rb +0 -9
  91. data/lib/liquor/tags/content_for.rb +0 -54
  92. data/lib/liquor/tags/cycle.rb +0 -59
  93. data/lib/liquor/tags/for.rb +0 -136
  94. data/lib/liquor/tags/if.rb +0 -80
  95. data/lib/liquor/tags/ifchanged.rb +0 -20
  96. data/lib/liquor/tags/include.rb +0 -56
  97. data/lib/liquor/tags/unless.rb +0 -33
  98. data/lib/liquor/tags/yield.rb +0 -49
  99. data/lib/liquor/template.rb +0 -181
  100. data/lib/liquor/variable.rb +0 -52
  101. data/performance/shopify.rb +0 -92
  102. data/performance/shopify/comment_form.rb +0 -33
  103. data/performance/shopify/database.rb +0 -45
  104. data/performance/shopify/json_filter.rb +0 -7
  105. data/performance/shopify/liquid.rb +0 -18
  106. data/performance/shopify/liquor.rb +0 -18
  107. data/performance/shopify/money_filter.rb +0 -18
  108. data/performance/shopify/paginate.rb +0 -93
  109. data/performance/shopify/shop_filter.rb +0 -98
  110. data/performance/shopify/tag_filter.rb +0 -25
  111. data/performance/shopify/vision.database.yml +0 -945
  112. data/performance/shopify/weight_filter.rb +0 -11
  113. data/performance/tests/dropify/article.liquid +0 -74
  114. data/performance/tests/dropify/blog.liquid +0 -33
  115. data/performance/tests/dropify/cart.liquid +0 -66
  116. data/performance/tests/dropify/collection.liquid +0 -22
  117. data/performance/tests/dropify/index.liquid +0 -47
  118. data/performance/tests/dropify/page.liquid +0 -8
  119. data/performance/tests/dropify/product.liquid +0 -68
  120. data/performance/tests/dropify/theme.liquid +0 -105
  121. data/performance/tests/ripen/article.liquid +0 -74
  122. data/performance/tests/ripen/blog.liquid +0 -13
  123. data/performance/tests/ripen/cart.liquid +0 -54
  124. data/performance/tests/ripen/collection.liquid +0 -29
  125. data/performance/tests/ripen/index.liquid +0 -32
  126. data/performance/tests/ripen/page.liquid +0 -4
  127. data/performance/tests/ripen/product.liquid +0 -75
  128. data/performance/tests/ripen/theme.liquid +0 -85
  129. data/performance/tests/tribble/404.liquid +0 -56
  130. data/performance/tests/tribble/article.liquid +0 -98
  131. data/performance/tests/tribble/blog.liquid +0 -41
  132. data/performance/tests/tribble/cart.liquid +0 -134
  133. data/performance/tests/tribble/collection.liquid +0 -70
  134. data/performance/tests/tribble/index.liquid +0 -94
  135. data/performance/tests/tribble/page.liquid +0 -56
  136. data/performance/tests/tribble/product.liquid +0 -116
  137. data/performance/tests/tribble/search.liquid +0 -51
  138. data/performance/tests/tribble/theme.liquid +0 -90
  139. data/performance/tests/vogue/article.liquid +0 -66
  140. data/performance/tests/vogue/blog.liquid +0 -32
  141. data/performance/tests/vogue/cart.liquid +0 -58
  142. data/performance/tests/vogue/collection.liquid +0 -19
  143. data/performance/tests/vogue/index.liquid +0 -22
  144. data/performance/tests/vogue/page.liquid +0 -3
  145. data/performance/tests/vogue/product.liquid +0 -62
  146. data/performance/tests/vogue/theme.liquid +0 -122
  147. data/test/assign_test.rb +0 -11
  148. data/test/block_test.rb +0 -58
  149. data/test/capture_test.rb +0 -41
  150. data/test/condition_test.rb +0 -115
  151. data/test/content_for_test.rb +0 -15
  152. data/test/context_test.rb +0 -479
  153. data/test/drop_test.rb +0 -162
  154. data/test/error_handling_test.rb +0 -89
  155. data/test/extra/breakpoint.rb +0 -547
  156. data/test/extra/caller.rb +0 -80
  157. data/test/file_system_test.rb +0 -30
  158. data/test/filter_test.rb +0 -147
  159. data/test/helper.rb +0 -24
  160. data/test/html_tag_test.rb +0 -31
  161. data/test/if_else_test.rb +0 -139
  162. data/test/include_tag_test.rb +0 -129
  163. data/test/module_ex_test.rb +0 -89
  164. data/test/output_test.rb +0 -121
  165. data/test/parsing_quirks_test.rb +0 -54
  166. data/test/regexp_test.rb +0 -45
  167. data/test/security_test.rb +0 -41
  168. data/test/standard_filter_test.rb +0 -170
  169. data/test/standard_tag_test.rb +0 -405
  170. data/test/statements_test.rb +0 -137
  171. data/test/strainer_test.rb +0 -27
  172. data/test/template_test.rb +0 -82
  173. data/test/test_helper.rb +0 -28
  174. data/test/unless_else_test.rb +0 -27
  175. data/test/variable_test.rb +0 -173
  176. data/test/yield_test.rb +0 -24
@@ -1,80 +0,0 @@
1
- class Continuation # :nodoc:
2
- def self.create(*args, &block) # :nodoc:
3
- cc = nil; result = callcc {|c| cc = c; block.call(cc) if block and args.empty?}
4
- result ||= args
5
- return *[cc, *result]
6
- end
7
- end
8
-
9
- class Binding; end # for RDoc
10
- # This method returns the binding of the method that called your
11
- # method. It will raise an Exception when you're not inside a method.
12
- #
13
- # It's used like this:
14
- # def inc_counter(amount = 1)
15
- # Binding.of_caller do |binding|
16
- # # Create a lambda that will increase the variable 'counter'
17
- # # in the caller of this method when called.
18
- # inc = eval("lambda { |arg| counter += arg }", binding)
19
- # # We can refer to amount from inside this block safely.
20
- # inc.call(amount)
21
- # end
22
- # # No other statements can go here. Put them inside the block.
23
- # end
24
- # counter = 0
25
- # 2.times { inc_counter }
26
- # counter # => 2
27
- #
28
- # Binding.of_caller must be the last statement in the method.
29
- # This means that you will have to put everything you want to
30
- # do after the call to Binding.of_caller into the block of it.
31
- # This should be no problem however, because Ruby has closures.
32
- # If you don't do this an Exception will be raised. Because of
33
- # the way that Binding.of_caller is implemented it has to be
34
- # done this way.
35
- def Binding.of_caller(&block)
36
- old_critical = Thread.critical
37
- Thread.critical = true
38
- count = 0
39
- cc, result, error, extra_data = Continuation.create(nil, nil)
40
- error.call if error
41
-
42
- tracer = lambda do |*args|
43
- type, context, extra_data = args[0], args[4], args
44
- if type == "return"
45
- count += 1
46
- # First this method and then calling one will return --
47
- # the trace event of the second event gets the context
48
- # of the method which called the method that called this
49
- # method.
50
- if count == 2
51
- # It would be nice if we could restore the trace_func
52
- # that was set before we swapped in our own one, but
53
- # this is impossible without overloading set_trace_func
54
- # in current Ruby.
55
- set_trace_func(nil)
56
- cc.call(eval("binding", context), nil, extra_data)
57
- end
58
- elsif type == "line" then
59
- nil
60
- elsif type == "c-return" and extra_data[3] == :set_trace_func then
61
- nil
62
- else
63
- set_trace_func(nil)
64
- error_msg = "Binding.of_caller used in non-method context or " +
65
- "trailing statements of method using it aren't in the block."
66
- cc.call(nil, lambda { raise(ArgumentError, error_msg) }, nil)
67
- end
68
- end
69
-
70
- unless result
71
- set_trace_func(tracer)
72
- return nil
73
- else
74
- Thread.critical = old_critical
75
- case block.arity
76
- when 1 then yield(result)
77
- else yield(result, extra_data)
78
- end
79
- end
80
- end
@@ -1,30 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require File.dirname(__FILE__) + '/helper'
3
-
4
- class FileSystemTest < Test::Unit::TestCase
5
- include Liquor
6
-
7
- def test_default
8
- assert_raise(FileSystemError) do
9
- BlankFileSystem.new.read_template_file("dummy")
10
- end
11
- end
12
-
13
- def test_local
14
- file_system = Liquor::LocalFileSystem.new("/some/path")
15
- assert_equal "/some/path/_mypartial.liquor" , file_system.full_path("mypartial")
16
- assert_equal "/some/path/dir/_mypartial.liquor", file_system.full_path("dir/mypartial")
17
-
18
- assert_raise(FileSystemError) do
19
- file_system.full_path("../dir/mypartial")
20
- end
21
-
22
- assert_raise(FileSystemError) do
23
- file_system.full_path("/dir/../../dir/mypartial")
24
- end
25
-
26
- assert_raise(FileSystemError) do
27
- file_system.full_path("/etc/passwd")
28
- end
29
- end
30
- end
@@ -1,147 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require File.dirname(__FILE__) + '/helper'
3
-
4
- module MoneyFilter
5
- def money(input)
6
- sprintf(' %d$ ', input)
7
- end
8
-
9
- def money_with_underscore(input)
10
- sprintf(' %d$ ', input)
11
- end
12
- end
13
-
14
- module CanadianMoneyFilter
15
- def money(input)
16
- sprintf(' %d$ CAD ', input)
17
- end
18
- end
19
-
20
- module FiltersWithArguments
21
- def adjust(input, offset=10)
22
- sprintf('[%d]', input+offset)
23
- end
24
-
25
- def addsub(input, plus, minus=20)
26
- sprintf('[%d]', input+plus-minus)
27
- end
28
- end
29
-
30
- class FiltersTest < Test::Unit::TestCase
31
- include Liquor
32
-
33
- def setup
34
- @context = Context.new
35
- end
36
-
37
- #def test_nonexistent_filter
38
- # @context['var'] = 1000
39
- # assert_match /Error - filter 'money'/, Variable.new("var | money").render(@context)
40
- #end
41
-
42
- def test_nonexistent_filter
43
- @context['var'] = 1000
44
- # assert_raises(FilterNotFound) { Variable.new("var | xyzzy").render(@context) }
45
- assert_equal 1000, Variable.new("var | xyzzy").render(@context)
46
- end
47
-
48
- def test_local_filter
49
- @context['var'] = 1000
50
- @context.add_filters(MoneyFilter)
51
- assert_equal ' 1000$ ', Variable.new("var | money").render(@context)
52
- end
53
-
54
- def test_underscore_in_filter_name
55
- @context['var'] = 1000
56
- @context.add_filters(MoneyFilter)
57
- assert_equal ' 1000$ ', Variable.new("var | money_with_underscore").render(@context)
58
- end
59
-
60
- def test_filter_with_numeric_argument
61
- @context['var'] = 1000
62
- @context.add_filters(FiltersWithArguments)
63
- assert_equal '[1005]', Variable.new("var | adjust: 5").render(@context)
64
- end
65
-
66
- def test_filter_with_negative_argument
67
- @context['var'] = 1000
68
- @context.add_filters(FiltersWithArguments)
69
- assert_equal '[995]', Variable.new("var | adjust: -5").render(@context)
70
- end
71
-
72
- def test_filter_with_default_argument
73
- @context['var'] = 1000
74
- @context.add_filters(FiltersWithArguments)
75
- assert_equal '[1010]', Variable.new("var | adjust").render(@context)
76
- end
77
-
78
- def test_filter_with_two_arguments
79
- @context['var'] = 1000
80
- @context.add_filters(FiltersWithArguments)
81
- assert_equal '[1150]', Variable.new("var | addsub: 200, 50").render(@context)
82
- end
83
-
84
- # ATM the trailing value is silently ignored. Should raise an exception?
85
- #def test_filter_with_two_arguments_no_comma
86
- # @context['var'] = 1000
87
- # @context.add_filters(FiltersWithArguments)
88
- # assert_equal '[1150]', Variable.new("var | addsub: 200 50").render(@context)
89
- #end
90
-
91
- def test_second_filter_overwrites_first
92
- @context['var'] = 1000
93
- @context.add_filters(MoneyFilter)
94
- @context.add_filters(CanadianMoneyFilter)
95
- assert_equal ' 1000$ CAD ', Variable.new("var | money").render(@context)
96
- end
97
-
98
- def test_size
99
- @context['var'] = 'abcd'
100
- @context.add_filters(MoneyFilter)
101
- assert_equal 4, Variable.new("var | size").render(@context)
102
- end
103
-
104
- def test_join
105
- @context['var'] = [1,2,3,4]
106
- assert_equal "1 2 3 4", Variable.new("var | join").render(@context)
107
- end
108
-
109
- def test_sort
110
- @context['value'] = 3
111
- @context['numbers'] = [2,1,4,3]
112
- @context['words'] = ['expected', 'as', 'alphabetic']
113
- @context['arrays'] = [['flattened'], ['are']]
114
- assert_equal [1,2,3,4], Variable.new("numbers | sort").render(@context)
115
- assert_equal ['alphabetic', 'as', 'expected'],
116
- Variable.new("words | sort").render(@context)
117
- assert_equal [3], Variable.new("value | sort").render(@context)
118
- assert_equal ['are', 'flattened'], Variable.new("arrays | sort").render(@context)
119
- end
120
-
121
- def test_strip_html
122
- @context['var'] = "<b>bla blub</a>"
123
- assert_equal "bla blub", Variable.new("var | strip_html").render(@context)
124
- end
125
-
126
- def test_capitalize
127
- @context['var'] = "blub"
128
- assert_equal "Blub", Variable.new("var | capitalize").render(@context)
129
- end
130
- end
131
-
132
- class FiltersInTemplate < Test::Unit::TestCase
133
- include Liquor
134
-
135
- def test_local_global
136
- Template.register_filter(MoneyFilter)
137
-
138
- assert_equal " 1000$ ", Template.parse("{{1000 | money}}").render(nil, nil)
139
- assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render(nil, :filters => CanadianMoneyFilter)
140
- assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render(nil, :filters => [CanadianMoneyFilter])
141
- end
142
-
143
- def test_local_filter_with_deprecated_syntax
144
- assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render(nil, CanadianMoneyFilter)
145
- assert_equal " 1000$ CAD ", Template.parse("{{1000 | money}}").render(nil, [CanadianMoneyFilter])
146
- end
147
- end
@@ -1,24 +0,0 @@
1
- #!/usr/bin/env ruby
2
- $LOAD_PATH.unshift(File.dirname(__FILE__)+ '/extra')
3
-
4
- require 'test/unit'
5
- require 'test/unit/assertions'
6
- require 'caller'
7
- require 'breakpoint'
8
-
9
- require 'rubygems'
10
- require 'active_support'
11
- require 'active_record'
12
- require File.dirname(__FILE__) + '/../lib/liquor'
13
-
14
-
15
- module Test
16
- module Unit
17
- module Assertions
18
- include Liquor
19
- def assert_template_result(expected, template, assigns={}, message=nil)
20
- assert_equal expected, Template.parse(template).render(assigns)
21
- end
22
- end
23
- end
24
- end
@@ -1,31 +0,0 @@
1
- require File.dirname(__FILE__) + '/helper'
2
-
3
- class HtmlTagTest < Test::Unit::TestCase
4
- include Liquor
5
-
6
- def test_html_table
7
-
8
- assert_template_result("<tr class=\"row1\">\n<td class=\"col1\"> 1 </td><td class=\"col2\"> 2 </td><td class=\"col3\"> 3 </td></tr>\n<tr class=\"row2\"><td class=\"col1\"> 4 </td><td class=\"col2\"> 5 </td><td class=\"col3\"> 6 </td></tr>\n",
9
- '{% tablerow n in numbers cols:3%} {{n}} {% endtablerow %}',
10
- 'numbers' => [1,2,3,4,5,6])
11
-
12
- assert_template_result("<tr class=\"row1\">\n</tr>\n",
13
- '{% tablerow n in numbers cols:3%} {{n}} {% endtablerow %}',
14
- 'numbers' => [])
15
- end
16
-
17
- def test_html_table_with_different_cols
18
- assert_template_result("<tr class=\"row1\">\n<td class=\"col1\"> 1 </td><td class=\"col2\"> 2 </td><td class=\"col3\"> 3 </td><td class=\"col4\"> 4 </td><td class=\"col5\"> 5 </td></tr>\n<tr class=\"row2\"><td class=\"col1\"> 6 </td></tr>\n",
19
- '{% tablerow n in numbers cols:5%} {{n}} {% endtablerow %}',
20
- 'numbers' => [1,2,3,4,5,6])
21
-
22
- end
23
-
24
- def test_html_col_counter
25
- assert_template_result("<tr class=\"row1\">\n<td class=\"col1\">1</td><td class=\"col2\">2</td></tr>\n<tr class=\"row2\"><td class=\"col1\">1</td><td class=\"col2\">2</td></tr>\n<tr class=\"row3\"><td class=\"col1\">1</td><td class=\"col2\">2</td></tr>\n",
26
- '{% tablerow n in numbers cols:2%}{{tablerowloop.col}}{% endtablerow %}',
27
- 'numbers' => [1,2,3,4,5,6])
28
-
29
- end
30
-
31
- end
@@ -1,139 +0,0 @@
1
- require File.dirname(__FILE__) + '/helper'
2
-
3
- class IfElseTest < Test::Unit::TestCase
4
- include Liquor
5
-
6
- def test_if
7
- assert_template_result(' ',' {% if false %} this text should not go into the output {% endif %} ')
8
- assert_template_result(' this text should go into the output ',
9
- ' {% if true %} this text should go into the output {% endif %} ')
10
- assert_template_result(' you rock ?','{% if false %} you suck {% endif %} {% if true %} you rock {% endif %}?')
11
- end
12
-
13
- def test_if_else
14
- assert_template_result(' YES ','{% if false %} NO {% else %} YES {% endif %}')
15
- assert_template_result(' YES ','{% if true %} YES {% else %} NO {% endif %}')
16
- assert_template_result(' YES ','{% if "foo" %} YES {% else %} NO {% endif %}')
17
- end
18
-
19
- def test_if_boolean
20
- assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => true)
21
- end
22
-
23
- def test_if_or
24
- assert_template_result(' YES ','{% if a or b %} YES {% endif %}', 'a' => true, 'b' => true)
25
- assert_template_result(' YES ','{% if a or b %} YES {% endif %}', 'a' => true, 'b' => false)
26
- assert_template_result(' YES ','{% if a or b %} YES {% endif %}', 'a' => false, 'b' => true)
27
- assert_template_result('', '{% if a or b %} YES {% endif %}', 'a' => false, 'b' => false)
28
-
29
- assert_template_result(' YES ','{% if a or b or c %} YES {% endif %}', 'a' => false, 'b' => false, 'c' => true)
30
- assert_template_result('', '{% if a or b or c %} YES {% endif %}', 'a' => false, 'b' => false, 'c' => false)
31
- end
32
-
33
- def test_if_or_with_operators
34
- assert_template_result(' YES ','{% if a == true or b == true %} YES {% endif %}', 'a' => true, 'b' => true)
35
- assert_template_result(' YES ','{% if a == true or b == false %} YES {% endif %}', 'a' => true, 'b' => true)
36
- assert_template_result('','{% if a == false or b == false %} YES {% endif %}', 'a' => true, 'b' => true)
37
- end
38
-
39
- def test_comparison_of_strings_containing_and_or_or
40
- assert_nothing_raised do
41
- awful_markup = "a == 'and' and b == 'or' and c == 'foo and bar' and d == 'bar or baz' and e == 'foo' and foo and bar"
42
- assigns = {'a' => 'and', 'b' => 'or', 'c' => 'foo and bar', 'd' => 'bar or baz', 'e' => 'foo', 'foo' => true, 'bar' => true}
43
- assert_template_result(' YES ',"{% if #{awful_markup} %} YES {% endif %}", assigns)
44
- end
45
- end
46
-
47
- def test_if_and
48
- assert_template_result(' YES ','{% if true and true %} YES {% endif %}')
49
- assert_template_result('','{% if false and true %} YES {% endif %}')
50
- assert_template_result('','{% if false and true %} YES {% endif %}')
51
- end
52
-
53
-
54
- def test_hash_miss_generates_false
55
- assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => {})
56
- end
57
-
58
- def test_if_from_variable
59
- assert_template_result('','{% if var %} NO {% endif %}', 'var' => false)
60
- assert_template_result('','{% if var %} NO {% endif %}', 'var' => nil)
61
- assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => {'bar' => false})
62
- assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => {})
63
- assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => nil)
64
- assert_template_result('','{% if foo.bar %} NO {% endif %}', 'foo' => true)
65
-
66
- assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => "text")
67
- assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => true)
68
- assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => 1)
69
- assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => {})
70
- assert_template_result(' YES ','{% if var %} YES {% endif %}', 'var' => [])
71
- assert_template_result(' YES ','{% if "foo" %} YES {% endif %}')
72
- assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => true})
73
- assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => "text"})
74
- assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => 1 })
75
- assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => {} })
76
- assert_template_result(' YES ','{% if foo.bar %} YES {% endif %}', 'foo' => {'bar' => [] })
77
-
78
- assert_template_result(' YES ','{% if var %} NO {% else %} YES {% endif %}', 'var' => false)
79
- assert_template_result(' YES ','{% if var %} NO {% else %} YES {% endif %}', 'var' => nil)
80
- assert_template_result(' YES ','{% if var %} YES {% else %} NO {% endif %}', 'var' => true)
81
- assert_template_result(' YES ','{% if "foo" %} YES {% else %} NO {% endif %}', 'var' => "text")
82
-
83
- assert_template_result(' YES ','{% if foo.bar %} NO {% else %} YES {% endif %}', 'foo' => {'bar' => false})
84
- assert_template_result(' YES ','{% if foo.bar %} YES {% else %} NO {% endif %}', 'foo' => {'bar' => true})
85
- assert_template_result(' YES ','{% if foo.bar %} YES {% else %} NO {% endif %}', 'foo' => {'bar' => "text"})
86
- assert_template_result(' YES ','{% if foo.bar %} NO {% else %} YES {% endif %}', 'foo' => {'notbar' => true})
87
- assert_template_result(' YES ','{% if foo.bar %} NO {% else %} YES {% endif %}', 'foo' => {})
88
- assert_template_result(' YES ','{% if foo.bar %} NO {% else %} YES {% endif %}', 'notfoo' => {'bar' => true})
89
- end
90
-
91
- def test_nested_if
92
- assert_template_result('', '{% if false %}{% if false %} NO {% endif %}{% endif %}')
93
- assert_template_result('', '{% if false %}{% if true %} NO {% endif %}{% endif %}')
94
- assert_template_result('', '{% if true %}{% if false %} NO {% endif %}{% endif %}')
95
- assert_template_result(' YES ', '{% if true %}{% if true %} YES {% endif %}{% endif %}')
96
-
97
- assert_template_result(' YES ', '{% if true %}{% if true %} YES {% else %} NO {% endif %}{% else %} NO {% endif %}')
98
- assert_template_result(' YES ', '{% if true %}{% if false %} NO {% else %} YES {% endif %}{% else %} NO {% endif %}')
99
- assert_template_result(' YES ', '{% if false %}{% if true %} NO {% else %} NONO {% endif %}{% else %} YES {% endif %}')
100
-
101
- end
102
-
103
- def test_comparisons_on_null
104
- assert_template_result('','{% if null < 10 %} NO {% endif %}')
105
- assert_template_result('','{% if null <= 10 %} NO {% endif %}')
106
- assert_template_result('','{% if null >= 10 %} NO {% endif %}')
107
- assert_template_result('','{% if null > 10 %} NO {% endif %}')
108
-
109
- assert_template_result('','{% if 10 < null %} NO {% endif %}')
110
- assert_template_result('','{% if 10 <= null %} NO {% endif %}')
111
- assert_template_result('','{% if 10 >= null %} NO {% endif %}')
112
- assert_template_result('','{% if 10 > null %} NO {% endif %}')
113
- end
114
-
115
- def test_else_if
116
- assert_template_result('0','{% if 0 == 0 %}0{% elsif 1 == 1%}1{% else %}2{% endif %}')
117
- assert_template_result('1','{% if 0 != 0 %}0{% elsif 1 == 1%}1{% else %}2{% endif %}')
118
- assert_template_result('2','{% if 0 != 0 %}0{% elsif 1 != 1%}1{% else %}2{% endif %}')
119
-
120
- assert_template_result('elsif','{% if false %}if{% elsif true %}elsif{% endif %}')
121
- end
122
-
123
- def test_syntax_error_no_variable
124
- assert_raise(SyntaxError){ assert_template_result('', '{% if jerry == 1 %}')}
125
- end
126
-
127
- def test_syntax_error_no_expression
128
- assert_raise(SyntaxError) { assert_template_result('', '{% if %}') }
129
- end
130
-
131
- def test_if_with_custom_condition
132
- Condition.operators['contains'] = :[]
133
-
134
- assert_template_result('yes', %({% if 'bob' contains 'o' %}yes{% endif %}))
135
- assert_template_result('no', %({% if 'bob' contains 'f' %}yes{% else %}no{% endif %}))
136
- ensure
137
- Condition.operators.delete 'contains'
138
- end
139
- end