blob: 12f8f5ee34adde1e466600b988f033849d571957 [file] [log] [blame]
utatane.tea@gmail.coma10944d2017-01-28 03:09:12 +00001function dump(callSite)
2{
3 return JSON.stringify({ cooked: callSite, raw: callSite.raw });
4}
5
6function shouldBe(actual, expected)
7{
8 if (actual !== expected)
9 throw new Error('bad value: ' + actual);
10}
11
12shouldBe(dump`\newcommand{\fun}{\textbf{Fun!}}`, `{"cooked":["\\newcommand{\\fun}{\\textbf{Fun!}}"],"raw":["\\\\newcommand{\\\\fun}{\\\\textbf{Fun!}}"]}`);
13shouldBe(dump`\newcommand{\unicode}{\textbf{Unicode!}}`, `{"cooked":[null],"raw":["\\\\newcommand{\\\\unicode}{\\\\textbf{Unicode!}}"]}`);
14shouldBe(dump`\newcommand{\xerxes}{\textbf{King!}}`, `{"cooked":[null],"raw":["\\\\newcommand{\\\\xerxes}{\\\\textbf{King!}}"]}`);
15shouldBe(dump`Breve over the h goes \u{h}ere`, `{"cooked":[null],"raw":["Breve over the h goes \\\\u{h}ere"]}`);
16
17function testTag(expected) {
18 return function tag(callSite) {
19 shouldBe(callSite.length, expected.cooked.length);
20 shouldBe(callSite.raw.length, expected.raw.length);
21 expected.cooked.forEach((value, index) => shouldBe(callSite[index], value));
22 expected.raw.forEach((value, index) => shouldBe(callSite.raw[index], value));
23 }
24}
25
26testTag({
27 cooked: [ undefined ],
28 raw: [ "\\unicode and \\u{55}" ],
29})`\unicode and \u{55}`;
30
31testTag({
32 cooked: [ undefined, "test" ],
33 raw: [ "\\unicode and \\u{55}", "test" ],
34})`\unicode and \u{55}${42}test`;
35
36testTag({
37 cooked: [ undefined, undefined, "Cocoa" ],
38 raw: [ "\\unicode and \\u{55}", "\\uhello", "Cocoa" ],
39})`\unicode and \u{55}${42}\uhello${42}Cocoa`;
40
41testTag({
42 cooked: [ "Cocoa", undefined, undefined, "Cocoa" ],
43 raw: [ "Cocoa", "\\unicode and \\u{55}", "\\uhello", "Cocoa" ],
44})`Cocoa${42}\unicode and \u{55}${42}\uhello${42}Cocoa`;
45
46testTag({
47 cooked: [ "Cocoa", undefined, undefined, "Cocoa" ],
48 raw: [ "Cocoa", "\\unicode and \\u{55}", "\\uhello", "Cocoa" ],
49})`Cocoa${42}\unicode and \u{55}${42}\uhello${42}Cocoa`;
50
51testTag({
52 cooked: [ undefined, undefined, undefined ],
53 raw: [ "\\00", "\\01", "\\1" ]
54})`\00${42}\01${42}\1`;
55
56testTag({
57 cooked: [ undefined, undefined ],
58 raw: [ "\\xo", "\\x0o" ]
59})`\xo${42}\x0o`;
60
61testTag({
62 cooked: [ undefined, undefined, undefined, undefined ],
63 raw: [ "\\uo", "\\u0o", "\\u00o", "\\u000o" ]
64})`\uo${42}\u0o${42}\u00o${42}\u000o`;
65
66testTag({
67 cooked: [ undefined, undefined, undefined ],
68 raw: [ "\\u{o", "\\u{0o", "\\u{110000o" ]
69})`\u{o${42}\u{0o${42}\u{110000o`;