]> git.saurik.com Git - apple/javascriptcore.git/blame - tests/stress/string-raw.js
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / tests / stress / string-raw.js
CommitLineData
ed1e77d3
A
1function shouldBe(actual, expected) {
2 if (actual !== expected)
3 throw new Error(`bad value: ${actual}`);
4}
5
6function 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
21shouldBe(String.raw.name, 'raw');
22shouldBe(String.raw.length, 1);
23
24shouldThrow(function () {
25 String.raw();
26}, "TypeError: String.raw requires template not be null or undefined");
27
28shouldThrow(function () {
29 String.raw(undefined);
30}, "TypeError: String.raw requires template not be null or undefined");
31
32shouldThrow(function () {
33 String.raw({ raw: undefined });
34}, "TypeError: String.raw requires template.raw not be null or undefined");
35
36shouldThrow(function () {
37 String.raw({ raw: null });
38}, "TypeError: String.raw requires template.raw not be null or undefined");
39
40shouldThrow(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
54shouldBe(String.raw({
55 raw: {
56 length: -1
57 }
58}), "");
59
60shouldBe(String.raw({
61 raw: {
62 length: -2.5
63 }
64}), "");
65
66shouldBe(String.raw({
67 raw: {
68 length: -Infinity
69 }
70}), "");
71
72shouldBe(String.raw({
73 raw: {
74 length: 0
75 }
76}), "");
77
78shouldBe(String.raw({
79 raw: {
80 length: NaN
81 }
82}), "");
83
84function generateTemplate() {
85 var cooked = [];
86 cooked.raw = Array.from(arguments);
87 return cooked;
88}
89
90shouldBe(String.raw(generateTemplate("", ",", ",", ""), "Cocoa", "Cappuccino", "Matcha"), "Cocoa,Cappuccino,Matcha");
91shouldBe(String.raw(generateTemplate("", ",", ",", ""), "Cocoa", "Cappuccino", "Matcha", "Hello"), "Cocoa,Cappuccino,Matcha");
92shouldBe(String.raw(generateTemplate("", ",", ",", ""), "Cocoa", "Cappuccino"), "Cocoa,Cappuccino,");
93shouldBe(String.raw(generateTemplate("", ",", ",", ""), "Cocoa"), "Cocoa,,");
94shouldBe(String.raw(generateTemplate("", ",", ",", "")), ",,");
95
96function Counter(p) {
97 var count = 0;
98 return {
99 toString() {
100 return count++;
101 }
102 };
103}
104
105var counter = Counter();
106shouldBe(String.raw(generateTemplate(counter, counter, counter, counter)), "0123");
107var counter = Counter();
108shouldBe(String.raw(generateTemplate(counter, counter, counter, counter), counter), "01234");
109var counter = Counter();
110shouldBe(String.raw(generateTemplate(counter, counter, counter, counter), counter, counter), "012345");
111var counter = Counter();
112shouldBe(String.raw(generateTemplate(counter, counter, counter, counter), counter, counter, counter), "0123456");
113var counter = Counter();
114shouldBe(String.raw(generateTemplate(counter, counter, counter, counter), counter, counter, counter, counter), "0123456");
115var counter = Counter();
116shouldBe(String.raw(generateTemplate(counter, counter, counter, counter), counter, counter, counter, counter), "0123456");
117var counter = Counter();
118shouldBe(String.raw(generateTemplate(counter, counter, counter, counter), counter, counter, counter, counter, counter), "0123456");
119
120
121shouldBe(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
130shouldBe(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
139shouldBe(String.raw({
140 raw: {
141 length: 2.3,
142 0: "a",
143 2: "c"
144 }
145}, "d", "e", "f", "g"), "adundefined");
146
147shouldBe(String.raw({
148 raw: {
149 length: 2.3,
150 0: "a",
151 1: "b",
152 2: "c"
153 }
154}, undefined, "e", "f", "g"), "aundefinedb");