-
Notifications
You must be signed in to change notification settings - Fork 8
/
mod_test.ts
172 lines (165 loc) · 3.91 KB
/
mod_test.ts
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
const { Buffer, copy, cwd } = Deno;
import { assertEquals } from "./vendor/https/deno.land/std/testing/asserts.ts";
import * as dejs from "./mod.ts";
import escape from "./vendor/https/deno.land/x/lodash/escape.js";
const decoder = new TextDecoder("utf-8");
// renderTest
(() => {
interface testCase {
name: string;
body: string;
param?: any;
expected: string;
}
const testCases: Array<testCase> = [
{
name: "Normal",
body: "normal test",
expected: "normal test",
},
{
name: "Escaped",
body: "<%= param %>",
param: "<div>test</div>",
expected: escape("<div>test</div>"),
},
{
name: "Raw",
body: "<%- param %>",
param: "<div>test</div>",
expected: "<div>test</div>",
},
{
name: "Comment",
body: "<%# param %>",
param: "<div>test</div>",
expected: "",
},
{
name: "Evaluate if true",
body: "<% if (param) { %>test<% } %>",
param: true,
expected: "test",
},
{
name: "Evaluate if false",
body: "<% if (param) { %>test<% } %>",
param: false,
expected: "",
},
{
name: "Evaluate for",
body: "<% for (let i = 0; i < 3; i++) { %>Test<% } %>",
expected: "TestTestTest",
},
{
name: "Evaluate nested for",
body:
"<% for (let i = 0; i < 2; i++) { %><% for (let j = 0; j < 2; j++) { %>Test<% } %><% } %>",
expected: "TestTestTestTest",
},
{
name: "Evaluate if true",
body: "<% if (param) { %>test<% } %>",
param: true,
expected: "test",
},
{
name: "Escaped without spacing",
body: "<%=param%>",
param: "<div>test</div>",
expected: escape("<div>test</div>"),
},
{
name: "Raw without spacing",
body: "<%-param%>",
param: "<div>test</div>",
expected: "<div>test</div>",
},
{
name: "Comment without spacing",
body: "<%#param%>",
param: "<div>test</div>",
expected: "",
},
{
name: "Escaped with semi",
body: "<%= param; %>",
param: "<div>test</div>",
expected: escape("<div>test</div>"),
},
{
name: "Raw with semi",
body: "<%- param; %>",
param: "<div>test</div>",
expected: "<div>test</div>",
},
{
name: "Security: Includes JavaScript",
body: "<%= param %>console.log(`${param}`)\\\\",
param: "test",
expected: "testconsole.log(`${param}`)", // Trims backslashes at line end.
},
];
for (const tc of testCases) {
Deno.test({
name: tc.name,
fn: async () => {
const buf = new Buffer();
await copy(await dejs.render(tc.body, { param: tc.param }), buf);
const actual = decoder.decode(await Deno.readAll(buf));
assertEquals(actual, tc.expected);
},
});
}
})();
// renderFileTest
(() => {
interface testCase {
name: string;
fileName: string;
param?: any;
expected: string;
}
const testCases: Array<testCase> = [
{
name: "Normal",
fileName: "normal",
expected: "normal test",
},
{
name: "Raw",
fileName: "raw",
param: "<div>test</div>",
expected: "<div>test</div>",
},
{
name: "Raw Include",
fileName: "raw-include",
param: "<div>test</div>",
expected: "<div>test</div>",
},
{
name: "Escaped Include",
fileName: "escaped-include",
param: "<div>test</div>",
expected: escape("<div>test</div>"),
},
];
for (const tc of testCases) {
Deno.test({
name: tc.name,
fn: async () => {
let buf = new Buffer();
await copy(
await dejs.renderFile(`${cwd()}/testdata/${tc.fileName}.ejs`, {
param: tc.param,
}),
buf,
);
const actual = decoder.decode(await Deno.readAll(buf));
assertEquals(actual, tc.expected);
},
});
}
})();