-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.ts
90 lines (84 loc) · 1.98 KB
/
index.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
import { expect, test } from "bun:test";
import dedent from "dedent";
import endent from "endent";
import { aldent } from "./index.ts";
type TagFunction =
// We intentionally ignore the weird dedent type here
typeof endent | typeof aldent;
type Test = (tagFunction: TagFunction) => string;
const compareTagFunctions = (test: Test, solution: string) => {
console.log(
"dedent",
test(
dedent as any /* We intentionally ignore the weird dedent type here */
) === solution
);
console.log("endent", test(endent) === solution);
console.log("aldent", test(aldent) === solution);
expect(test(aldent) === solution);
};
test("Properly indents inner multi-line string without indentation", () => {
compareTagFunctions(
(tagFunction) => tagFunction`
function add() {
${`const a = 1
const b = 2
return a + b`}
}
`,
`function add() {
const a = 1
const b = 2
return a + b
}`
);
});
test("Properly indents inner multi-line string with lots of indentation", () => {
compareTagFunctions(
(tagFunction) => tagFunction`
function add() {
${tagFunction`
const a = 1
const b = 2
return a + b
`}
}
`,
`function add() {
const a = 1
const b = 2
return a + b
}`
);
});
test("Does not strip leading spaces of outer and inner multi-line strings", () => {
compareTagFunctions(
(tagFunction) => tagFunction`
d
c
${tagFunction`
c
b
`}
a
`,
` d
c
c
b
a`
);
});
test("Properly handles escape characters backslash and newline", () => {
compareTagFunctions(
(tagFunction) =>
tagFunction`
This text has a single \\ slash, and does not have a \\n newline here.
It also handles a nested windows path correctly:
${`C:\\Users\\newuser\\Desktop`}
`,
`This text has a single \\ slash, and does not have a \\n newline here.
It also handles a nested windows path correctly:
${`C:\\Users\\newuser\\Desktop`}`
);
});