This repository has been archived by the owner on Jun 10, 2018. It is now read-only.
/
test_render.coffee
94 lines (76 loc) · 2.86 KB
/
test_render.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
eco = require "eco"
{fixture} = require "fixtures"
items = [
{ name: "Caprese", price: "5.25"},
{ name: "Artichoke", price: "6.25" }
]
module.exports =
"linking hello.eco": (test) ->
render = eco fixture("hello.eco")
test.same fixture("hello.out.1"), render name: "Sam"
test.done()
"linked templates can be reused": (test) ->
render = eco "Hello <%= @name %>"
test.same "Hello Sam", render name: "Sam"
test.same "Hello Josh", render name: "Josh"
test.done()
"rendering hello.eco": (test) ->
output = eco.render fixture("hello.eco"), name: "Sam"
test.same fixture("hello.out.1"), output
test.done()
"rendering hello.eco without a name throws an exception": (test) ->
test.expect 1
try
eco.render fixture("hello.eco")
catch err
test.ok err
test.done()
"rendering projects.eco with empty projects array": (test) ->
output = eco.render fixture("projects.eco"), projects: []
test.same fixture("projects.out.1"), output
test.done()
"rendering projects.eco with multiple projects": (test) ->
output = eco.render fixture("projects.eco"), projects: [
{ name: "PowerTMS Active Shipments Page Redesign", url: "/projects/1" },
{ name: "SCU Intranet", url: "/projects/2", description: "<p><em>On hold</em></p>" },
{ name: "Sales Template", url: "/projects/3" }
]
test.same fixture("projects.out.2"), output
test.done()
"rendering helpers.eco": (test) ->
output = eco.render fixture("helpers.eco"),
items: items
contentTag: (tagName, attributes, callback) ->
attrs = (" #{name}=\"#{value}\"" for name, value of attributes)
@safe "<#{tagName}#{attrs.join("")}>#{callback()}</#{tagName}>"
test.same fixture("helpers.out.1"), output
test.done()
"rendering capture.eco": (test) ->
output = eco.render fixture("capture.eco"), items: items
test.same fixture("capture.out.1"), output
test.done()
"HTML is escaped by default": (test) ->
output = eco.render "<%= @emailAddress %>",
emailAddress: "<sstephenson@gmail.com>"
test.same "<sstephenson@gmail.com>", output
test.done()
"unescaped HTML can be rendered from a helper": (test) ->
output = eco.render "<%= @helper() %>",
helper: -> @safe "<boo>"
test.same "<boo>", output
test.done()
"escape method can be overridden": (test) ->
output = eco.render "<%= @emailAddress %>",
emailAddress: "<sstephenson@gmail.com>"
escape: (string) ->
string.toUpperCase()
test.same "<SSTEPHENSON@GMAIL.COM>", output
test.done()
"'undefined' is never coerced into a string": (test) ->
test.same "", eco.render "<%= @x %>"
test.same "", eco.render "<%= @safe @x %>"
test.same "", eco.render "<%- @x %>"
test.done()
"rendering an escaped <%": (test) ->
test.same "<%", eco.render "<%%"
test.done()