This repository was archived by the owner on Jun 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathtest_ejs.rb
More file actions
200 lines (156 loc) · 6.69 KB
/
test_ejs.rb
File metadata and controls
200 lines (156 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
require "ejs"
require "test/unit"
FUNCTION_PATTERN = /^function\s*\(.*?\)\s*\{(.*?)\}$/
BRACE_SYNTAX = {
:evaluation_pattern => /\{\{([\s\S]+?)\}\}/,
:interpolation_pattern => /\{\{=([\s\S]+?)\}\}/,
:escape_pattern => /\{\{-([\s\S]+?)\}\}/
}
QUESTION_MARK_SYNTAX = {
:evaluation_pattern => /<\?([\s\S]+?)\?>/,
:interpolation_pattern => /<\?=([\s\S]+?)\?>/,
:escape_pattern => /<\?-([\s\S]+?)\?>/
}
module TestHelper
def test(name, &block)
define_method("test #{name.inspect}", &block)
end
end
class EJSCompilationTest < Test::Unit::TestCase
extend TestHelper
test "compile" do
result = EJS.compile("Hello <%- name %>")
assert_match FUNCTION_PATTERN, result
assert_no_match(/Hello \<%- name %\>/, result)
end
test "compile with custom syntax" do
standard_result = EJS.compile("Hello <%- name %>")
braced_result = EJS.compile("Hello {{= name }}", BRACE_SYNTAX)
assert_match FUNCTION_PATTERN, braced_result
assert_equal standard_result, braced_result
end
end
class EJSCustomPatternTest < Test::Unit::TestCase
extend TestHelper
def setup
@original_evaluation_pattern = EJS.evaluation_pattern
@original_interpolation_pattern = EJS.interpolation_pattern
EJS.evaluation_pattern = BRACE_SYNTAX[:evaluation_pattern]
EJS.interpolation_pattern = BRACE_SYNTAX[:interpolation_pattern]
end
def teardown
EJS.interpolation_pattern = @original_interpolation_pattern
EJS.evaluation_pattern = @original_evaluation_pattern
end
test "compile" do
result = EJS.compile("Hello {{= name }}")
assert_match FUNCTION_PATTERN, result
assert_no_match(/Hello \{\{= name \}\}/, result)
end
test "compile with custom syntax" do
standard_result = EJS.compile("Hello {{= name }}")
question_result = EJS.compile("Hello <?= name ?>", QUESTION_MARK_SYNTAX)
assert_match FUNCTION_PATTERN, question_result
assert_equal standard_result, question_result
end
end
class EJSEvaluationTest < Test::Unit::TestCase
extend TestHelper
test "quotes" do
template = "<%- thing %> is gettin' on my noives!"
assert_equal "This is gettin' on my noives!", EJS.evaluate(template, :thing => "This")
end
test "backslashes" do
template = "<%- thing %> is \\ridanculous"
assert_equal "This is \\ridanculous", EJS.evaluate(template, :thing => "This")
end
test "backslashes into interpolation" do
template = %q{<%- "Hello \"World\"" %>}
assert_equal 'Hello "World"', EJS.evaluate(template)
end
test "implicit semicolon" do
template = "<% var foo = 'bar' %>"
assert_equal '', EJS.evaluate(template)
end
test "iteration" do
template = "<ul><%
for (var i = 0; i < people.length; i++) {
%><li><%- people[i] %></li><% } %></ul>"
result = EJS.evaluate(template, :people => ["Moe", "Larry", "Curly"])
assert_equal "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", result
end
test "without interpolation" do
template = "<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>"
assert_equal template, EJS.evaluate(template)
end
test "two quotes" do
template = "It's its, not it's"
assert_equal template, EJS.evaluate(template)
end
test "quote in statement and body" do
template = "<%
if(foo == 'bar'){
%>Statement quotes and 'quotes'.<% } %>"
assert_equal "Statement quotes and 'quotes'.", EJS.evaluate(template, :foo => "bar")
end
test "newlines and tabs" do
template = "This\n\t\tis: <%- x %>.\n\tok.\nend."
assert_equal "This\n\t\tis: that.\n\tok.\nend.", EJS.evaluate(template, :x => "that")
end
test "braced iteration" do
template = "<ul>{{ for (var i = 0; i < people.length; i++) { }}<li>{{= people[i] }}</li>{{ } }}</ul>"
result = EJS.evaluate(template, { :people => ["Moe", "Larry", "Curly"] }, BRACE_SYNTAX)
assert_equal "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", result
end
test "braced quotes" do
template = "It's its, not it's"
assert_equal template, EJS.evaluate(template, {}, BRACE_SYNTAX)
end
test "braced quotes in statement and body" do
template = "{{ if(foo == 'bar'){ }}Statement quotes and 'quotes'.{{ } }}"
assert_equal "Statement quotes and 'quotes'.", EJS.evaluate(template, { :foo => "bar" }, BRACE_SYNTAX)
end
test "question-marked iteration" do
template = "<ul><? for (var i = 0; i < people.length; i++) { ?><li><?= people[i] ?></li><? } ?></ul>"
result = EJS.evaluate(template, { :people => ["Moe", "Larry", "Curly"] }, QUESTION_MARK_SYNTAX)
assert_equal "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", result
end
test "question-marked quotes" do
template = "It's its, not it's"
assert_equal template, EJS.evaluate(template, {}, QUESTION_MARK_SYNTAX)
end
test "question-marked quote in statement and body" do
template = "<? if(foo == 'bar'){ ?>Statement quotes and 'quotes'.<? } ?>"
assert_equal "Statement quotes and 'quotes'.", EJS.evaluate(template, { :foo => "bar" }, QUESTION_MARK_SYNTAX)
end
test "escaping" do
template = "<%= foobar %>"
assert_equal "<b>Foo Bar</b>", EJS.evaluate(template, { :foobar => "<b>Foo Bar</b>" })
template = "<%= foobar %>"
assert_equal "Foo & Bar", EJS.evaluate(template, { :foobar => "Foo & Bar" })
template = "<%= foobar %>"
assert_equal ""Foo Bar"", EJS.evaluate(template, { :foobar => '"Foo Bar"' })
template = "<%= foobar %>"
assert_equal "'Foo Bar'", EJS.evaluate(template, { :foobar => "'Foo Bar'" })
end
test "braced escaping" do
template = "{{- foobar }}"
assert_equal "<b>Foo Bar</b>", EJS.evaluate(template, { :foobar => "<b>Foo Bar</b>" }, BRACE_SYNTAX)
template = "{{- foobar }}"
assert_equal "Foo & Bar", EJS.evaluate(template, { :foobar => "Foo & Bar" }, BRACE_SYNTAX)
template = "{{- foobar }}"
assert_equal ""Foo Bar"", EJS.evaluate(template, { :foobar => '"Foo Bar"' }, BRACE_SYNTAX)
template = "{{- foobar }}"
assert_equal "'Foo Bar'", EJS.evaluate(template, { :foobar => "'Foo Bar'" }, BRACE_SYNTAX)
end
test "question-mark escaping" do
template = "<?- foobar ?>"
assert_equal "<b>Foo Bar</b>", EJS.evaluate(template, { :foobar => "<b>Foo Bar</b>" }, QUESTION_MARK_SYNTAX)
template = "<?- foobar ?>"
assert_equal "Foo & Bar", EJS.evaluate(template, { :foobar => "Foo & Bar" }, QUESTION_MARK_SYNTAX)
template = "<?- foobar ?>"
assert_equal ""Foo Bar"", EJS.evaluate(template, { :foobar => '"Foo Bar"' }, QUESTION_MARK_SYNTAX)
template = "<?- foobar ?>"
assert_equal "'Foo Bar'", EJS.evaluate(template, { :foobar => "'Foo Bar'" }, QUESTION_MARK_SYNTAX)
end
end