utatane.tea@gmail.com | a10944d | 2017-01-28 03:09:12 +0000 | [diff] [blame] | 1 | function dump(callSite) |
| 2 | { |
| 3 | return JSON.stringify({ cooked: callSite, raw: callSite.raw }); |
| 4 | } |
| 5 | |
| 6 | function shouldBe(actual, expected) |
| 7 | { |
| 8 | if (actual !== expected) |
| 9 | throw new Error('bad value: ' + actual); |
| 10 | } |
| 11 | |
| 12 | shouldBe(dump`\newcommand{\fun}{\textbf{Fun!}}`, `{"cooked":["\\newcommand{\\fun}{\\textbf{Fun!}}"],"raw":["\\\\newcommand{\\\\fun}{\\\\textbf{Fun!}}"]}`); |
| 13 | shouldBe(dump`\newcommand{\unicode}{\textbf{Unicode!}}`, `{"cooked":[null],"raw":["\\\\newcommand{\\\\unicode}{\\\\textbf{Unicode!}}"]}`); |
| 14 | shouldBe(dump`\newcommand{\xerxes}{\textbf{King!}}`, `{"cooked":[null],"raw":["\\\\newcommand{\\\\xerxes}{\\\\textbf{King!}}"]}`); |
| 15 | shouldBe(dump`Breve over the h goes \u{h}ere`, `{"cooked":[null],"raw":["Breve over the h goes \\\\u{h}ere"]}`); |
| 16 | |
| 17 | function 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 | |
| 26 | testTag({ |
| 27 | cooked: [ undefined ], |
| 28 | raw: [ "\\unicode and \\u{55}" ], |
| 29 | })`\unicode and \u{55}`; |
| 30 | |
| 31 | testTag({ |
| 32 | cooked: [ undefined, "test" ], |
| 33 | raw: [ "\\unicode and \\u{55}", "test" ], |
| 34 | })`\unicode and \u{55}${42}test`; |
| 35 | |
| 36 | testTag({ |
| 37 | cooked: [ undefined, undefined, "Cocoa" ], |
| 38 | raw: [ "\\unicode and \\u{55}", "\\uhello", "Cocoa" ], |
| 39 | })`\unicode and \u{55}${42}\uhello${42}Cocoa`; |
| 40 | |
| 41 | testTag({ |
| 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 | |
| 46 | testTag({ |
| 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 | |
| 51 | testTag({ |
| 52 | cooked: [ undefined, undefined, undefined ], |
| 53 | raw: [ "\\00", "\\01", "\\1" ] |
| 54 | })`\00${42}\01${42}\1`; |
| 55 | |
| 56 | testTag({ |
| 57 | cooked: [ undefined, undefined ], |
| 58 | raw: [ "\\xo", "\\x0o" ] |
| 59 | })`\xo${42}\x0o`; |
| 60 | |
| 61 | testTag({ |
| 62 | cooked: [ undefined, undefined, undefined, undefined ], |
| 63 | raw: [ "\\uo", "\\u0o", "\\u00o", "\\u000o" ] |
| 64 | })`\uo${42}\u0o${42}\u00o${42}\u000o`; |
| 65 | |
| 66 | testTag({ |
| 67 | cooked: [ undefined, undefined, undefined ], |
| 68 | raw: [ "\\u{o", "\\u{0o", "\\u{110000o" ] |
| 69 | })`\u{o${42}\u{0o${42}\u{110000o`; |