UNPKG

7.27 kBtext/coffeescriptView Raw
1#!/usr/bin/env coffee
2
3tests =
4 'Literal text':
5 template: "text 'Just text'"
6 expected: 'Just text'
7
8 'Default DOCTYPE':
9 template: "doctype()"
10 expected: '<!DOCTYPE html>'
11
12 'DOCTYPE':
13 template: "doctype 'xml'"
14 expected: '<?xml version="1.0" encoding="utf-8" ?>'
15
16 'Custom tag':
17 template: "tag 'custom'"
18 expected: '<custom></custom>'
19
20 'Custom tag with attributes':
21 template: "tag 'custom', foo: 'bar', ping: 'pong'"
22 expected: '<custom foo="bar" ping="pong"></custom>'
23
24 'Custom tag with attributes and inner content':
25 template: "tag 'custom', foo: 'bar', ping: 'pong', -> 'zag'"
26 expected: '<custom foo="bar" ping="pong">zag</custom>'
27
28 'Self-closing tags':
29 template: "img src: 'icon.png', alt: 'Icon'"
30 expected: '<img src="icon.png" alt="Icon" />'
31
32 'Common tag':
33 template: "p 'hi'"
34 expected: '<p>hi</p>'
35
36 'Attributes':
37 template: "a href: '/', title: 'Home'"
38 expected: '<a href="/" title="Home"></a>'
39
40 'HereDocs':
41 template: '''
42 script """
43 $(document).ready(function(){
44 alert('test');
45 });
46 """
47 '''
48 expected: "<script>$(document).ready(function(){\n alert('test');\n});</script>"
49
50 'CoffeeScript helper (function)':
51 template: "coffeescript -> alert 'hi'"
52 expected: "<script>var __slice = Array.prototype.slice;var __hasProp = Object.prototype.hasOwnProperty;var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };var __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };var __indexOf = Array.prototype.indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (this[i] === item) return i; } return -1; };(function () {\n return alert('hi');\n}).call(this);</script>"
53
54 'CoffeeScript helper (string)':
55 template: "coffeescript \"alert 'hi'\""
56 expected: "<script type=\"text/coffeescript\">alert 'hi'</script>"
57
58 'CoffeeScript helper (object)':
59 template: "coffeescript src: 'script.coffee'"
60 expected: "<script src=\"script.coffee\" type=\"text/coffeescript\"></script>"
61
62 'Context vars':
63 template: "h1 @foo"
64 expected: '<h1>bar</h1>'
65 params: {foo: 'bar'}
66
67 'Local vars, hardcoded':
68 template: 'h1 "harcoded: " + obj.foo'
69 run: ->
70 obj = {foo: 'bar'}
71 @compiled = ck.compile(@template, hardcode: {obj})
72 @expected = '<h1>harcoded: bar</h1>'
73 @result = @compiled()
74 @success = @result is @expected
75 if @success
76 obj.foo = 'baz'
77 @result = @compiled()
78 @success = @result is @expected
79
80 'Local vars, hard-coded (functions)':
81 template: "h1 \"The sum is: \#{sum 1, 2}\""
82 expected: '<h1>The sum is: 3</h1>'
83 params: {hardcode: {sum: (a, b) -> a + b}}
84
85 'Local vars, hard-coded ("helpers")':
86 template: "textbox id: 'foo'"
87 expected: '<input id="foo" name="foo" type="text" />'
88 params:
89 hardcode:
90 textbox: (attrs) ->
91 attrs.name = attrs.id
92 attrs.type = 'text'
93 tag 'input', attrs
94
95 'Local vars':
96 template: 'h1 "dynamic: " + obj.foo'
97 run: ->
98 obj = {foo: 'bar'}
99 @expected = '<h1>dynamic: bar</h1>'
100 @result = render(@template, locals: {obj: obj})
101 @success = @result is @expected
102 if @success
103 obj.foo = 'baz'
104 @expected = '<h1>dynamic: baz</h1>'
105 @result = render(@template, locals: {obj: obj})
106 @success = @result is @expected
107
108 'Comments':
109 template: "comment 'Comment'"
110 expected: '<!--Comment-->'
111
112 'Escaping':
113 template: "h1 h(\"<script>alert('\\\"pwned\\\" by c&a &copy;')</script>\")"
114 expected: "<h1>&lt;script&gt;alert('&quot;pwned&quot; by c&amp;a &amp;copy;')&lt;/script&gt;</h1>"
115
116 'Autoescaping':
117 template: "h1 \"<script>alert('\\\"pwned\\\" by c&a &copy;')</script>\""
118 expected: "<h1>&lt;script&gt;alert('&quot;pwned&quot; by c&amp;a &amp;copy;')&lt;/script&gt;</h1>"
119 params: {autoescape: yes}
120
121 'ID/class shortcut (combo)':
122 template: "div '#myid.myclass1.myclass2', 'foo'"
123 expected: '<div id="myid" class="myclass1 myclass2">foo</div>'
124
125 'ID/class shortcut (ID only)':
126 template: "div '#myid', 'foo'"
127 expected: '<div id="myid">foo</div>'
128
129 'ID/class shortcut (one class only)':
130 template: "div '.myclass', 'foo'"
131 expected: '<div class="myclass">foo</div>'
132
133 'ID/class shortcut (multiple classes)':
134 template: "div '.myclass.myclass2.myclass3', 'foo'"
135 expected: '<div class="myclass myclass2 myclass3">foo</div>'
136
137 'ID/class shortcut (no string contents)':
138 template: "img '#myid.myclass', src: '/pic.png'"
139 expected: '<img id="myid" class="myclass" src="/pic.png" />'
140
141 'Attribute values':
142 template: "br vrai: yes, faux: no, undef: @foo, nil: null, str: 'str', num: 42, arr: [1, 2, 3], obj: {foo: 'bar'}, func: ->"
143 expected: '<br vrai="vrai" str="str" num="42" arr="1,2,3" obj-foo="bar" func="(function () {}).call(this);" />'
144
145 'IE conditionals':
146 template: """
147 html ->
148 head ->
149 title 'test'
150 ie 'gte IE8', ->
151 link href: 'ie.css', rel: 'stylesheet'
152 """
153 expected: '''
154 <html>
155 <head>
156 <title>test</title>
157 <!--[if gte IE8]>
158 <link href="ie.css" rel="stylesheet" />
159 <![endif]-->
160 </head>
161 </html>
162
163 '''
164 params: {format: yes}
165
166 'yield':
167 template: "p \"This text could use \#{yield -> strong -> a href: '/', 'a link'}.\""
168 expected: '<p>This text could use <strong><a href="/">a link</a></strong>.</p>'
169
170ck = require './src/coffeekup'
171render = ck.render
172
173@run = ->
174 {print} = require 'sys'
175 colors = {red: "\033[31m", redder: "\033[91m", green: "\033[32m", normal: "\033[0m"}
176 printc = (color, str) -> print colors[color] + str + colors.normal
177
178 [total, passed, failed, errors] = [0, [], [], []]
179
180 for name, test of tests
181 total++
182 try
183 test.original_params = JSON.stringify test.params
184
185 if test.run
186 test.run()
187 else
188 test.result = ck.render(test.template, test.params)
189 test.success = test.result is test.expected
190
191 if test.success
192 passed.push name
193 print "[Passed] #{name}\n"
194 else
195 failed.push name
196 printc 'red', "[Failed] #{name}\n"
197 catch ex
198 test.result = ex
199 errors.push name
200 printc 'redder', "[Error] #{name}\n"
201
202 print "\n#{total} tests, #{passed.length} passed, #{failed.length} failed, #{errors.length} errors\n\n"
203
204 if failed.length > 0
205 printc 'red', "FAILED:\n\n"
206
207 for name in failed
208 t = tests[name]
209 print "- #{name}:\n"
210 print t.template + "\n"
211 print t.original_params + "\n" if t.params
212 printc 'green', t.expected + "\n"
213 printc 'red', t.result + "\n\n"
214
215 if errors.length > 0
216 printc 'redder', "ERRORS:\n\n"
217
218 for name in errors
219 t = tests[name]
220 print "- #{name}:\n"
221 print t.template + "\n"
222 printc 'green', t.expected + "\n"
223 printc 'redder', t.result.stack + "\n\n"
224
225@run()
226