-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
parse.spec.ts
113 lines (93 loc) · 2.94 KB
/
parse.spec.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
/* global it, expect, describe */
import { Eta } from "../src/index";
const eta = new Eta();
const fs = require("fs");
const path = require("path");
const filePath = path.join(__dirname, "templates/complex.eta");
const complexTemplate = fs.readFileSync(filePath, "utf8");
describe("parse test", () => {
it("parses a simple template", () => {
const buff = eta.parse("hi <%= hey %>");
expect(buff).toEqual(["hi ", { val: "hey", t: "i" }]);
});
it("parses a raw tag", () => {
const buff = eta.parse("hi <%~ hey %>");
expect(buff).toEqual(["hi ", { val: "hey", t: "r" }]);
});
it("works with whitespace trimming", () => {
const buff = eta.parse("hi\n<%- = hey-%> <%_ = hi _%>");
expect(buff).toEqual(["hi", { val: "hey", t: "i" }, { val: "hi", t: "i" }]);
});
it("works with multiline comments", () => {
const buff = eta.parse("hi <% /* comment contains delimiter %> */ %>");
expect(buff).toEqual(["hi ", { val: "/* comment contains delimiter %> */", t: "e" }]);
});
it("parses with simple template literal", () => {
const buff = eta.parse("hi <%= `template %> ${value}` %>");
expect(buff).toEqual(["hi ", { val: "`template %> ${value}`", t: "i" }]);
});
it("compiles complex template", () => {
const buff = eta.parse(complexTemplate);
expect(buff).toEqual([
"Hi\\n",
{ t: "e", val: 'console.log("Hope you like Eta!")' },
{ t: "i", val: "it.htmlstuff" },
"\\n",
{ t: "e", val: "for (var key in it.obj) {" },
"Value: ",
{ t: "i", val: "it.obj[key]" },
", Key: ",
{ t: "i", val: "key" },
"\\n",
{ t: "e", val: "if (key === 'thirdchild') {" },
" ",
{
t: "e",
val: "for (var i = 0, arr = it.obj[key]; i < arr.length; i++) {",
},
" Salutations! Index: ",
{ t: "i", val: "i" },
", parent key: ",
{ t: "i", val: "key" },
" \\n ",
{ t: "e", val: "}" },
{ t: "e", val: "}" },
{ t: "e", val: "}" },
"\\nThis is a partial: ",
{ t: "r", val: 'include("mypartial")' },
]);
});
test("throws with unclosed tag", () => {
expect(() => {
eta.parse('<%hi("hey")');
}).toThrowError("hi");
});
test("throws with unclosed single-quote string", () => {
expect(() => {
eta.parse("<%= ' %>");
}).toThrowError(`unclosed string at line 1 col 5:
<%= ' %>
^`);
});
test("throws with unclosed double-quote string", () => {
expect(() => {
eta.parse('<%= " %>');
}).toThrowError(`unclosed string at line 1 col 5:
<%= " %>
^`);
});
test("throws with unclosed template literal", () => {
expect(() => {
eta.parse("<%= ` %>");
}).toThrowError(`unclosed string at line 1 col 5:
<%= \` %>
^`);
});
test("throws with unclosed multi-line comment", () => {
expect(() => {
eta.parse("<%= /* %>");
}).toThrowError(`unclosed comment at line 1 col 5:
<%= /* %>
^`);
});
});