]>
Commit | Line | Data |
---|---|---|
ed1e77d3 A |
1 | function shouldBe(actual, expected) { |
2 | if (actual !== expected) | |
3 | throw new Error(`bad value: ${actual}`); | |
4 | } | |
5 | ||
6 | function shouldThrow(func, errorMessage) { | |
7 | var errorThrown = false; | |
8 | var error = null; | |
9 | try { | |
10 | func(); | |
11 | } catch (e) { | |
12 | errorThrown = true; | |
13 | error = e; | |
14 | } | |
15 | if (!errorThrown) | |
16 | throw new Error('not thrown'); | |
17 | if (String(error) !== errorMessage) | |
18 | throw new Error(`bad error: ${String(error)}`); | |
19 | } | |
20 | ||
21 | shouldBe(String.raw.name, 'raw'); | |
22 | shouldBe(String.raw.length, 1); | |
23 | ||
24 | shouldThrow(function () { | |
25 | String.raw(); | |
26 | }, "TypeError: String.raw requires template not be null or undefined"); | |
27 | ||
28 | shouldThrow(function () { | |
29 | String.raw(undefined); | |
30 | }, "TypeError: String.raw requires template not be null or undefined"); | |
31 | ||
32 | shouldThrow(function () { | |
33 | String.raw({ raw: undefined }); | |
34 | }, "TypeError: String.raw requires template.raw not be null or undefined"); | |
35 | ||
36 | shouldThrow(function () { | |
37 | String.raw({ raw: null }); | |
38 | }, "TypeError: String.raw requires template.raw not be null or undefined"); | |
39 | ||
40 | shouldThrow(function () { | |
41 | String.raw({ | |
42 | get length() { | |
43 | return new Error('template.length called'); | |
44 | }, | |
45 | ||
46 | raw: { | |
47 | get length() { | |
48 | throw new Error("template.raw.length called"); | |
49 | } | |
50 | } | |
51 | }); | |
52 | }, "Error: template.raw.length called"); | |
53 | ||
54 | shouldBe(String.raw({ | |
55 | raw: { | |
56 | length: -1 | |
57 | } | |
58 | }), ""); | |
59 | ||
60 | shouldBe(String.raw({ | |
61 | raw: { | |
62 | length: -2.5 | |
63 | } | |
64 | }), ""); | |
65 | ||
66 | shouldBe(String.raw({ | |
67 | raw: { | |
68 | length: -Infinity | |
69 | } | |
70 | }), ""); | |
71 | ||
72 | shouldBe(String.raw({ | |
73 | raw: { | |
74 | length: 0 | |
75 | } | |
76 | }), ""); | |
77 | ||
78 | shouldBe(String.raw({ | |
79 | raw: { | |
80 | length: NaN | |
81 | } | |
82 | }), ""); | |
83 | ||
84 | function generateTemplate() { | |
85 | var cooked = []; | |
86 | cooked.raw = Array.from(arguments); | |
87 | return cooked; | |
88 | } | |
89 | ||
90 | shouldBe(String.raw(generateTemplate("", ",", ",", ""), "Cocoa", "Cappuccino", "Matcha"), "Cocoa,Cappuccino,Matcha"); | |
91 | shouldBe(String.raw(generateTemplate("", ",", ",", ""), "Cocoa", "Cappuccino", "Matcha", "Hello"), "Cocoa,Cappuccino,Matcha"); | |
92 | shouldBe(String.raw(generateTemplate("", ",", ",", ""), "Cocoa", "Cappuccino"), "Cocoa,Cappuccino,"); | |
93 | shouldBe(String.raw(generateTemplate("", ",", ",", ""), "Cocoa"), "Cocoa,,"); | |
94 | shouldBe(String.raw(generateTemplate("", ",", ",", "")), ",,"); | |
95 | ||
96 | function Counter(p) { | |
97 | var count = 0; | |
98 | return { | |
99 | toString() { | |
100 | return count++; | |
101 | } | |
102 | }; | |
103 | } | |
104 | ||
105 | var counter = Counter(); | |
106 | shouldBe(String.raw(generateTemplate(counter, counter, counter, counter)), "0123"); | |
107 | var counter = Counter(); | |
108 | shouldBe(String.raw(generateTemplate(counter, counter, counter, counter), counter), "01234"); | |
109 | var counter = Counter(); | |
110 | shouldBe(String.raw(generateTemplate(counter, counter, counter, counter), counter, counter), "012345"); | |
111 | var counter = Counter(); | |
112 | shouldBe(String.raw(generateTemplate(counter, counter, counter, counter), counter, counter, counter), "0123456"); | |
113 | var counter = Counter(); | |
114 | shouldBe(String.raw(generateTemplate(counter, counter, counter, counter), counter, counter, counter, counter), "0123456"); | |
115 | var counter = Counter(); | |
116 | shouldBe(String.raw(generateTemplate(counter, counter, counter, counter), counter, counter, counter, counter), "0123456"); | |
117 | var counter = Counter(); | |
118 | shouldBe(String.raw(generateTemplate(counter, counter, counter, counter), counter, counter, counter, counter, counter), "0123456"); | |
119 | ||
120 | ||
121 | shouldBe(String.raw({ | |
122 | raw: { | |
123 | length: 3.5, | |
124 | 0: "a", | |
125 | 1: "b", | |
126 | 2: "c" | |
127 | } | |
128 | }, "d", "e", "f", "g"), "adbec"); | |
129 | ||
130 | shouldBe(String.raw({ | |
131 | raw: { | |
132 | length: 2.3, | |
133 | 0: "a", | |
134 | 1: "b", | |
135 | 2: "c" | |
136 | } | |
137 | }, "d", "e", "f", "g"), "adb"); | |
138 | ||
139 | shouldBe(String.raw({ | |
140 | raw: { | |
141 | length: 2.3, | |
142 | 0: "a", | |
143 | 2: "c" | |
144 | } | |
145 | }, "d", "e", "f", "g"), "adundefined"); | |
146 | ||
147 | shouldBe(String.raw({ | |
148 | raw: { | |
149 | length: 2.3, | |
150 | 0: "a", | |
151 | 1: "b", | |
152 | 2: "c" | |
153 | } | |
154 | }, undefined, "e", "f", "g"), "aundefinedb"); |