OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Flags: --harmony-templates | |
6 | |
7 var num = 5; | |
8 var str = "str"; | |
9 function fn() { return "result"; } | |
10 var obj = { | |
11 num: num, | |
12 str: str, | |
13 fn: function() { return "result"; } | |
14 }; | |
15 | |
16 // Basic expressions | |
17 assertEquals("foo 5 bar", `foo ${num} bar`); | |
18 assertEquals("foo str bar", `foo ${str} bar`); | |
19 assertEquals("foo [object Object] bar", `foo ${obj} bar`); | |
20 assertEquals("foo result bar", `foo ${fn()} bar`); | |
21 assertEquals("foo 5 bar", `foo ${obj.num} bar`); | |
22 assertEquals("foo str bar", `foo ${obj.str} bar`); | |
23 assertEquals("foo result bar", `foo ${obj.fn()} bar`); | |
24 | |
25 // Expressions containing templates | |
26 assertEquals("foo bar 5", `foo ${`bar ${num}`}`); | |
27 | |
28 // Multi-line templates | |
arv (Not doing code reviews)
2014/11/07 16:55:50
How about testing one with \r\n too?
caitp (gmail)
2014/11/07 17:13:08
the trouble with that (from an mjsunit test) is th
arv (Not doing code reviews)
2014/11/07 17:14:18
You should be able to use eval to be explicit abou
caitp (gmail)
2014/11/07 17:21:00
Ah, you're right, I guess I'm not thinking very cr
| |
29 assertEquals("foo\n bar\n baz", `foo | |
30 bar | |
31 baz`); | |
32 | |
33 // Tagged Templates | |
34 | |
35 // assert tag is invoked | |
36 var called = false; | |
37 (function(s) { | |
38 called = true; | |
39 })`test`; | |
40 assertTrue(called); | |
41 | |
42 called = false; | |
43 // assert tag is invoked in right context | |
44 obj = { | |
45 fn: function() { | |
46 called = true; | |
47 assertEquals(obj, this); | |
48 } | |
49 }; | |
50 | |
51 obj.fn`test`; | |
52 assertTrue(called); | |
53 | |
54 // Simple templates only have a callSiteObj | |
55 (function(s) { | |
56 assertEquals(1, arguments.length); | |
57 })`test`; | |
58 | |
59 // Templates containing expressions have the values of evaluated expressions | |
60 (function(site, n, s, o, f, r) { | |
61 assertEquals(6, arguments.length); | |
62 assertEquals("number", typeof n); | |
63 assertEquals("string", typeof s); | |
64 assertEquals("object", typeof o); | |
65 assertEquals("function", typeof f); | |
66 assertEquals("result", r); | |
67 })`${num}${str}${obj}${fn}${fn()}`; | |
68 | |
69 // The TV and TRV of NoSubstitutionTemplate :: `` is the empty code unit sequenc e. | |
70 (function(s) { | |
71 assertEquals(1, s.length); | |
72 assertEquals(1, s.raw.length); | |
73 assertEquals("", s[0]); | |
74 | |
75 // Failure: expected <""> found <"foo barfoo barfoo foo foo foo testtest"> | |
76 assertEquals("", s.raw[0]); | |
77 })``; | |
78 | |
79 // The TV and TRV of TemplateHead :: `${ is the empty code unit sequence. | |
80 (function(s) { | |
81 assertEquals(2, s.length); | |
82 assertEquals(2, s.raw.length); | |
83 assertEquals("", s[0]); | |
84 | |
85 // Failure: expected <""> found <"foo barfoo barfoo foo foo foo testtest"> | |
86 assertEquals("", s.raw[0]); | |
87 })`${1}`; | |
88 | |
89 // The TV and TRV of TemplateMiddle :: }${ is the empty code unit sequence. | |
90 (function(s) { | |
91 assertEquals(3, s.length); | |
92 assertEquals(3, s.raw.length); | |
93 assertEquals("", s[1]); | |
94 | |
95 // Failure: expected <""> found <"foo barfoo barfoo foo foo foo testtest"> | |
96 // assertEquals("", s.raw[1]); | |
97 })`${1}${2}`; | |
98 | |
99 // The TV and TRV of TemplateTail :: }` is the empty code unit sequence. | |
100 (function(s) { | |
101 assertEquals(2, s.length); | |
102 assertEquals(2, s.raw.length); | |
103 assertEquals("", s[1]); | |
104 | |
105 // Failure: expected <""> found <"foo barfoo barfoo foo foo foo testtest"> | |
106 // assertEquals("", s.raw[1]); | |
107 })`${1}`; | |
108 | |
109 // The TV of NoSubstitutionTemplate :: ` TemplateCharacters ` is the TV of | |
110 // TemplateCharacters. | |
111 (function(s) { assertEquals("foo", s[0]); })`foo`; | |
112 | |
113 // The TV of TemplateHead :: ` TemplateCharacters ${ is the TV of | |
114 // TemplateCharacters. | |
115 (function(s) { assertEquals("foo", s[0]); })`foo${1}`; | |
116 | |
117 // The TV of TemplateMiddle :: } TemplateCharacters ${ is the TV of | |
118 // TemplateCharacters. | |
119 (function(s) { assertEquals("foo", s[1]); })`${1}foo${2}`; | |
120 | |
121 // The TV of TemplateTail :: } TemplateCharacters ` is the TV of | |
122 // TemplateCharacters. | |
123 (function(s) { assertEquals("foo", s[1]); })`${1}foo`; | |
124 | |
125 // The TV of TemplateCharacters :: TemplateCharacter is the TV of | |
126 // TemplateCharacter. | |
127 (function(s) { assertEquals("f", s[0]); })`f`; | |
128 | |
129 // The TV of TemplateCharacter :: $ is the code unit value 0x0024. | |
130 (function(s) { assertEquals("$", s[0]); })`$`; | |
131 | |
132 // The TV of TemplateCharacter :: \ EscapeSequence is the CV of EscapeSequence. | |
133 (function(s) { assertEquals("안녕", s[0]); })`\uc548\uB155`; | |
134 (function(s) { assertEquals("\xff", s[0]); })`\xff`; | |
135 (function(s) { assertEquals("\n", s[0]); })`\n`; | |
136 | |
137 // The TV of TemplateCharacter :: LineContinuation is the TV of | |
138 // LineContinuation. The TV of LineContinuation :: \ LineTerminatorSequence is | |
139 // the empty code unit sequence. | |
140 (function(s) { assertEquals("", s[0]); })`\ | |
141 | |
142 `; | |
143 | |
144 // The TRV of NoSubstitutionTemplate :: ` TemplateCharacters ` is the TRV of | |
145 // TemplateCharacters. | |
146 (function(s) { assertEquals("test", s.raw[0]); })`test`; | |
147 | |
148 // The TRV of TemplateHead :: ` TemplateCharacters ${ is the TRV of | |
149 // TemplateCharacters. | |
150 (function(s) { assertEquals("test", s.raw[0]); })`test${1}`; | |
151 | |
152 // The TRV of TemplateMiddle :: } TemplateCharacters ${ is the TRV of | |
153 // TemplateCharacters. | |
154 (function(s) { assertEquals("test", s.raw[1]); })`${1}test${2}`; | |
155 | |
156 // The TRV of TemplateTail :: } TemplateCharacters ` is the TRV of | |
157 // TemplateCharacters. | |
158 (function(s) { assertEquals("test", s.raw[1]); })`${1}test`; | |
159 | |
160 // The TRV of TemplateCharacters :: TemplateCharacter is the TRV of | |
161 // TemplateCharacter. | |
162 (function(s) { assertEquals("f", s.raw[0]); })`f`; | |
163 | |
164 // The TRV of TemplateCharacter :: $ is the code unit value 0x0024. | |
165 (function(s) { assertEquals("\u0024", s.raw[0]); })`$`; | |
166 | |
167 // The TRV of EscapeSequence :: 0 is the code unit value 0x0030. | |
168 (function(s) { assertEquals("\u005C\u0030", s.raw[0]); })`\0`; | |
169 | |
170 // The TRV of TemplateCharacter :: \ EscapeSequence is the sequence consisting | |
171 // of the code unit value 0x005C followed by the code units of TRV of | |
172 // EscapeSequence. | |
173 | |
174 // The TRV of EscapeSequence :: HexEscapeSequence is the TRV of the | |
175 // HexEscapeSequence. | |
176 (function(s) { assertEquals("\u005Cxff", s.raw[0]); })`\xff`; | |
177 | |
178 // The TRV of EscapeSequence :: UnicodeEscapeSequence is the TRV of the | |
179 // UnicodeEscapeSequence. | |
180 (function(s) { assertEquals("\u005Cuc548", s.raw[0]); })`\uc548`; | |
181 | |
182 // The TRV of CharacterEscapeSequence :: SingleEscapeCharacter is the TRV of | |
183 // the SingleEscapeCharacter. | |
184 (function(s) { assertEquals("\u005C\u0027", s.raw[0]); })`\'`; | |
185 (function(s) { assertEquals("\u005C\u0022", s.raw[0]); })`\"`; | |
186 (function(s) { assertEquals("\u005C\u005C", s.raw[0]); })`\\`; | |
187 (function(s) { assertEquals("\u005Cb", s.raw[0]); })`\b`; | |
188 (function(s) { assertEquals("\u005Cf", s.raw[0]); })`\f`; | |
189 (function(s) { assertEquals("\u005Cn", s.raw[0]); })`\n`; | |
190 (function(s) { assertEquals("\u005Cr", s.raw[0]); })`\r`; | |
191 (function(s) { assertEquals("\u005Ct", s.raw[0]); })`\t`; | |
192 (function(s) { assertEquals("\u005Cv", s.raw[0]); })`\v`; | |
193 | |
194 // The TRV of CharacterEscapeSequence :: NonEscapeCharacter is the CV of the | |
195 // NonEscapeCharacter. | |
196 (function(s) { assertEquals("\u005Cx", s.raw[0]); })`\x`; | |
OLD | NEW |