forked from gradus/coffeecup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_optimized.coffee
executable file
·220 lines (180 loc) · 6.41 KB
/
test_optimized.coffee
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env coffee
cc = require "./src/coffeecup"
tests =
'Literal text':
template: "text 'Just text'"
expected: 'Just text'
'Default DOCTYPE':
template: "doctype()"
expected: '<!DOCTYPE html>'
'DOCTYPE':
template: "doctype 'xml'"
expected: '<?xml version="1.0" encoding="utf-8" ?>'
'Custom tag':
template: "tag 'custom'"
expected: '<custom></custom>'
'Custom tag with attributes':
template: "tag 'custom', foo: 'bar', ping: 'pong'"
expected: '<custom foo="bar" ping="pong"></custom>'
'Custom tag with attributes and inner content':
template: "tag 'custom', foo: 'bar', ping: 'pong', -> 'zag'"
expected: '<custom foo="bar" ping="pong">zag</custom>'
'Self-closing tags':
template: "img src: 'icon.png', alt: 'Icon'"
expected: '<img src="icon.png" alt="Icon" />'
'Common tag':
template: "p 'hi'"
expected: '<p>hi</p>'
'Attributes':
template: "a href: '/', title: 'Home'"
expected: '<a href="/" title="Home"></a>'
'HereDocs':
template: '''
script """
$(document).ready(function(){
alert('test');
});
"""
'''
expected: "<script>$(document).ready(function(){\n alert('test');\n});</script>"
'CoffeeScript helper (function)':
template: "coffeescript -> alert 'hi'"
expected: "<script>(function() {\n return alert(\"hi\");\n}).call(this);</script>"
'CoffeeScript helper (string)':
template: "coffeescript \"alert 'hi'\""
expected: "<script>\nalert('hi');\n</script>"
'Context vars':
template: "h1 @foo"
expected: '<h1>bar</h1>'
params: {foo: 'bar'}
'Local vars, hardcoded':
template: 'h1 "harcoded: " + obj.foo'
run: ->
obj = {foo: 'bar'}
@compiled = cc.compile(@template, hardcode: {obj})
@expected = '<h1>harcoded: bar</h1>'
@result = @compiled()
@success = @result is @expected
if @success
obj.foo = 'baz'
@result = @compiled()
@success = @result is @expected
'Local vars, hard-coded (functions)':
template: "h1 \"The sum is: \#{sum 1, 2}\""
expected: '<h1>The sum is: 3</h1>'
params: {hardcode: {sum: (a, b) -> a + b}}
'Local vars, hard-coded ("helpers")':
template: "textbox id: 'foo'"
expected: '<input id="foo" name="foo" type="text" />'
params:
hardcode:
textbox: (attrs) ->
tag 'input',
id: attrs.id
name: attrs.id
type: 'text'
'Local vars':
template: 'h1 "dynamic: " + obj.foo'
run: ->
obj = {foo: 'bar'}
@expected = '<h1>dynamic: bar</h1>'
@result = render(@template, locals: {obj: obj})
@success = @result is @expected
if @success
obj.foo = 'baz'
@expected = '<h1>dynamic: baz</h1>'
@result = render(@template, locals: {obj: obj})
@success = @result is @expected
'Comments':
template: "comment 'Comment'"
expected: '<!--Comment-->'
'Escaping':
template: "h1 h(\"<script>alert('\\\"pwned\\\" by c&a ©')</script>\")"
expected: "<h1><script>alert('"pwned" by c&a &copy;')</script></h1>"
'Autoescaping':
template: "h1 \"<script>alert('\\\"pwned\\\" by c&a ©')</script>\""
expected: "<h1><script>alert('"pwned" by c&a &copy;')</script></h1>"
params:
autoescape: yes
'ID/class shortcut (combo)':
template: "div '#myid.myclass1.myclass2', 'foo'"
expected: '<div id="myid" class="myclass1 myclass2">foo</div>'
'ID/class shortcut (ID only)':
template: "div '#myid', 'foo'"
expected: '<div id="myid">foo</div>'
'ID/class shortcut (one class only)':
template: "div '.myclass', 'foo'"
expected: '<div class="myclass">foo</div>'
'ID/class shortcut (multiple classes)':
template: "div '.myclass.myclass2.myclass3', 'foo'"
expected: '<div class="myclass myclass2 myclass3">foo</div>'
'ID/class shortcut (no string contents)':
template: "img '#myid.myclass', src: '/pic.png'"
expected: '<img id="myid" class="myclass" src="/pic.png" />'
'Attribute values':
template: "br vrai: yes, faux: no, undef: @foo, nil: null, str: 'str', num: 42, arr: [1, 2, 3].join(','), obj: {foo: 'bar'}, func: ->"
expected: '<br vrai="vrai" str="str" num="42" arr="1,2,3" obj-foo="bar" func="(function(){}).call(this);" />'
'IE conditionals':
template: """
html ->
head ->
title 'test'
ie 'gte IE8', ->
link href: 'ie.css', rel: 'stylesheet'
"""
expected: '<html><head><title>test</title><!--[if gte IE8]><link href="ie.css" rel="stylesheet" /><![endif]--></head></html>'
'yield':
template: "p \"This text could use \#{yield -> strong -> a href: '/', 'a link'}.\""
expected: '<p>This text could use <strong><a href="/">a link</a></strong>.</p>'
cc = require './src/coffeecup'
render = cc.render
@run = ->
{print} = require 'sys'
colors = {red: "\033[31m", redder: "\033[91m", green: "\033[32m", normal: "\033[0m"}
printc = (color, str) -> print colors[color] + str + colors.normal
[total, passed, failed, errors] = [0, [], [], []]
for name, test of tests
total++
try
if not test.params?
test.params =
optimize: true
cache: on unless total is 1
else
test.params.optimize = true
test.params.cache = true
test.original_params = JSON.stringify test.params
if test.run
test.run()
else
test.result = cc.render(test.template, test.params)
test.success = test.result is test.expected
if test.success
passed.push name
print "[Passed] #{name}\n"
else
failed.push name
printc 'red', "[Failed] #{name}\n"
catch ex
test.result = ex
errors.push name
printc 'redder', "[Error] #{name}\n"
print "\n#{total} tests, #{passed.length} passed, #{failed.length} failed, #{errors.length} errors\n\n"
if failed.length > 0
printc 'red', "FAILED:\n\n"
for name in failed
t = tests[name]
print "- #{name}:\n"
print t.template + "\n"
print t.original_params + "\n" if t.params
printc 'green', t.expected + "\n"
printc 'red', t.result + "\n\n"
if errors.length > 0
printc 'redder', "ERRORS:\n\n"
for name in errors
t = tests[name]
print "- #{name}:\n"
print t.template + "\n"
printc 'green', t.expected + "\n"
printc 'redder', t.result.stack + "\n\n"
@run()