Chromium Code Reviews| 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 (function testBasicExpressions() { | |
| 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 | |
| 26 (function testExpressionsContainingTemplates() { | |
| 27 assertEquals("foo bar 5", `foo ${`bar ${num}`}`); | |
| 28 })(); | |
| 29 | |
| 30 (function testMultilineTemplates() { | |
| 31 assertEquals("foo\n bar\n baz", `foo | |
| 32 bar | |
| 33 baz`); | |
| 34 | |
| 35 assertEquals("foo\n bar\n baz", eval("`foo\r\n bar\r baz`")); | |
| 36 })(); | |
| 37 | |
| 38 (function testLineContinuation() { | |
| 39 assertEquals("\n", `\ | |
| 40 | |
| 41 `); | |
| 42 })(); | |
| 43 | |
| 44 (function testTaggedTemplates() { | |
| 45 var calls = 0; | |
| 46 (function(s) { | |
| 47 calls++; | |
| 48 })`test`; | |
| 49 assertEquals(1, calls); | |
| 50 | |
| 51 calls = 0; | |
| 52 // assert tag is invoked in right context | |
| 53 obj = { | |
| 54 fn: function() { | |
| 55 calls++; | |
| 56 assertEquals(obj, this); | |
| 57 } | |
| 58 }; | |
| 59 | |
| 60 obj.fn`test`; | |
| 61 assertEquals(1, calls); | |
| 62 | |
| 63 calls = 0; | |
| 64 // Simple templates only have a callSiteObj | |
| 65 (function(s) { | |
| 66 calls++; | |
| 67 assertEquals(1, arguments.length); | |
| 68 })`test`; | |
| 69 assertEquals(1, calls); | |
| 70 | |
| 71 // Templates containing expressions have the values of evaluated expressions | |
| 72 calls = 0; | |
| 73 (function(site, n, s, o, f, r) { | |
| 74 calls++; | |
| 75 assertEquals(6, arguments.length); | |
| 76 assertEquals("number", typeof n); | |
| 77 assertEquals("string", typeof s); | |
| 78 assertEquals("object", typeof o); | |
| 79 assertEquals("function", typeof f); | |
| 80 assertEquals("result", r); | |
| 81 })`${num}${str}${obj}${fn}${fn()}`; | |
| 82 assertEquals(1, calls); | |
| 83 | |
| 84 // The TV and TRV of NoSubstitutionTemplate :: `` is the empty code unit | |
| 85 // sequence. | |
| 86 calls = 0; | |
| 87 (function(s) { | |
| 88 calls++; | |
| 89 assertEquals(1, s.length); | |
| 90 assertEquals(1, s.raw.length); | |
| 91 assertEquals("", s[0]); | |
| 92 | |
| 93 // Failure: expected <""> found <"foo barfoo barfoo foo foo foo testtest"> | |
| 94 assertEquals("", s.raw[0]); | |
| 95 })``; | |
| 96 assertEquals(1, calls); | |
| 97 | |
| 98 // The TV and TRV of TemplateHead :: `${ is the empty code unit sequence. | |
| 99 calls = 0; | |
| 100 (function(s) { | |
| 101 calls++; | |
| 102 assertEquals(2, s.length); | |
| 103 assertEquals(2, s.raw.length); | |
| 104 assertEquals("", s[0]); | |
| 105 assertEquals("", s.raw[0]); | |
| 106 })`${1}`; | |
| 107 assertEquals(1, calls); | |
| 108 | |
| 109 // The TV and TRV of TemplateMiddle :: }${ is the empty code unit sequence. | |
| 110 calls = 0; | |
| 111 (function(s) { | |
| 112 calls++; | |
| 113 assertEquals(3, s.length); | |
| 114 assertEquals(3, s.raw.length); | |
| 115 assertEquals("", s[1]); | |
| 116 assertEquals("", s.raw[1]); | |
| 117 })`${1}${2}`; | |
| 118 assertEquals(1, calls); | |
| 119 | |
| 120 // The TV and TRV of TemplateTail :: }` is the empty code unit sequence. | |
| 121 calls = 0; | |
| 122 (function(s) { | |
| 123 calls++; | |
| 124 assertEquals(2, s.length); | |
| 125 assertEquals(2, s.raw.length); | |
| 126 assertEquals("", s[1]); | |
| 127 assertEquals("", s.raw[1]); | |
| 128 })`${1}`; | |
| 129 assertEquals(1, calls); | |
| 130 | |
| 131 // The TV of NoSubstitutionTemplate :: ` TemplateCharacters ` is the TV of | |
| 132 // TemplateCharacters. | |
| 133 calls = 0; | |
| 134 (function(s) { calls++; assertEquals("foo", s[0]); })`foo`; | |
| 135 assertEquals(1, calls); | |
| 136 | |
| 137 // The TV of TemplateHead :: ` TemplateCharacters ${ is the TV of | |
| 138 // TemplateCharacters. | |
| 139 calls = 0; | |
| 140 (function(s) { calls++; assertEquals("foo", s[0]); })`foo${1}`; | |
| 141 assertEquals(1, calls); | |
| 142 | |
| 143 // The TV of TemplateMiddle :: } TemplateCharacters ${ is the TV of | |
| 144 // TemplateCharacters. | |
| 145 calls = 0; | |
| 146 (function(s) { calls++; assertEquals("foo", s[1]); })`${1}foo${2}`; | |
| 147 assertEquals(1, calls); | |
| 148 | |
| 149 // The TV of TemplateTail :: } TemplateCharacters ` is the TV of | |
| 150 // TemplateCharacters. | |
| 151 calls = 0; | |
| 152 (function(s) { calls++; assertEquals("foo", s[1]); })`${1}foo`; | |
| 153 assertEquals(1, calls); | |
| 154 | |
| 155 // The TV of TemplateCharacters :: TemplateCharacter is the TV of | |
| 156 // TemplateCharacter. | |
| 157 calls = 0; | |
| 158 (function(s) { calls++; assertEquals("f", s[0]); })`f`; | |
| 159 assertEquals(1, calls); | |
| 160 | |
| 161 // The TV of TemplateCharacter :: $ is the code unit value 0x0024. | |
| 162 calls = 0; | |
| 163 (function(s) { calls++; assertEquals("$", s[0]); })`$`; | |
| 164 assertEquals(1, calls); | |
| 165 | |
| 166 // The TV of TemplateCharacter :: \ EscapeSequence is the CV of | |
| 167 // EscapeSequence. | |
| 168 calls = 0; | |
| 169 (function(s) { calls++; assertEquals("안녕", s[0]); })`\uc548\uB155`; | |
| 170 (function(s) { calls++; assertEquals("\xff", s[0]); })`\xff`; | |
| 171 (function(s) { calls++; assertEquals("\n", s[0]); })`\n`; | |
| 172 assertEquals(3, calls); | |
| 173 | |
| 174 // The TV of TemplateCharacter :: LineContinuation is the TV of | |
| 175 // LineContinuation. The TV of LineContinuation :: \ LineTerminatorSequence is | |
| 176 // the empty code unit sequence. | |
| 177 calls = 0; | |
| 178 (function(s) { calls++; assertEquals("", s[0]); })`\ | |
| 179 `; | |
| 180 assertEquals(1, calls); | |
| 181 | |
| 182 // The TRV of NoSubstitutionTemplate :: ` TemplateCharacters ` is the TRV of | |
| 183 // TemplateCharacters. | |
| 184 calls = 0; | |
| 185 (function(s) { calls++; assertEquals("test", s.raw[0]); })`test`; | |
| 186 assertEquals(1, calls); | |
| 187 | |
| 188 // The TRV of TemplateHead :: ` TemplateCharacters ${ is the TRV of | |
| 189 // TemplateCharacters. | |
| 190 calls = 0; | |
| 191 (function(s) { calls++; assertEquals("test", s.raw[0]); })`test${1}`; | |
| 192 assertEquals(1, calls); | |
| 193 | |
| 194 // The TRV of TemplateMiddle :: } TemplateCharacters ${ is the TRV of | |
| 195 // TemplateCharacters. | |
| 196 calls = 0; | |
| 197 (function(s) { calls++; assertEquals("test", s.raw[1]); })`${1}test${2}`; | |
| 198 assertEquals(1, calls); | |
| 199 | |
| 200 // The TRV of TemplateTail :: } TemplateCharacters ` is the TRV of | |
| 201 // TemplateCharacters. | |
| 202 calls = 0; | |
| 203 (function(s) { calls++; assertEquals("test", s.raw[1]); })`${1}test`; | |
| 204 assertEquals(1, calls); | |
| 205 | |
| 206 // The TRV of TemplateCharacters :: TemplateCharacter is the TRV of | |
| 207 // TemplateCharacter. | |
| 208 calls = 0; | |
| 209 (function(s) { calls++; assertEquals("f", s.raw[0]); })`f`; | |
| 210 assertEquals(1, calls); | |
| 211 | |
| 212 // The TRV of TemplateCharacter :: $ is the code unit value 0x0024. | |
| 213 calls = 0; | |
| 214 (function(s) { calls++; assertEquals("\u0024", s.raw[0]); })`$`; | |
| 215 assertEquals(1, calls); | |
| 216 | |
| 217 // The TRV of EscapeSequence :: 0 is the code unit value 0x0030. | |
| 218 calls = 0; | |
| 219 (function(s) { calls++; assertEquals("\u005C\u0030", s.raw[0]); })`\0`; | |
| 220 assertEquals(1, calls); | |
| 221 | |
| 222 // The TRV of TemplateCharacter :: \ EscapeSequence is the sequence consisting | |
| 223 // of the code unit value 0x005C followed by the code units of TRV of | |
| 224 // EscapeSequence. | |
| 225 | |
| 226 // The TRV of EscapeSequence :: HexEscapeSequence is the TRV of the | |
| 227 // HexEscapeSequence. | |
| 228 calls = 0; | |
| 229 (function(s) { calls++; assertEquals("\u005Cxff", s.raw[0]); })`\xff`; | |
| 230 assertEquals(1, calls); | |
| 231 | |
| 232 // The TRV of EscapeSequence :: UnicodeEscapeSequence is the TRV of the | |
| 233 // UnicodeEscapeSequence. | |
| 234 calls = 0; | |
| 235 (function(s) { calls++; assertEquals("\u005Cuc548", s.raw[0]); })`\uc548`; | |
| 236 assertEquals(1, calls); | |
| 237 | |
| 238 // The TRV of CharacterEscapeSequence :: SingleEscapeCharacter is the TRV of | |
| 239 // the SingleEscapeCharacter. | |
| 240 calls = 0; | |
| 241 (function(s) { calls++; assertEquals("\u005C\u0027", s.raw[0]); })`\'`; | |
| 242 (function(s) { calls++; assertEquals("\u005C\u0022", s.raw[0]); })`\"`; | |
| 243 (function(s) { calls++; assertEquals("\u005C\u005C", s.raw[0]); })`\\`; | |
| 244 (function(s) { calls++; assertEquals("\u005Cb", s.raw[0]); })`\b`; | |
| 245 (function(s) { calls++; assertEquals("\u005Cf", s.raw[0]); })`\f`; | |
| 246 (function(s) { calls++; assertEquals("\u005Cn", s.raw[0]); })`\n`; | |
| 247 (function(s) { calls++; assertEquals("\u005Cr", s.raw[0]); })`\r`; | |
| 248 (function(s) { calls++; assertEquals("\u005Ct", s.raw[0]); })`\t`; | |
| 249 (function(s) { calls++; assertEquals("\u005Cv", s.raw[0]); })`\v`; | |
| 250 (function(s) { calls++; assertEquals("\u005C`", s.raw[0]); })`\``; | |
| 251 assertEquals(10, calls); | |
| 252 | |
| 253 // The TRV of CharacterEscapeSequence :: NonEscapeCharacter is the CV of the | |
| 254 // NonEscapeCharacter. | |
| 255 calls = 0; | |
| 256 (function(s) { calls++; assertEquals("\u005Cx", s.raw[0]); })`\x`; | |
| 257 assertEquals(1, calls); | |
| 258 | |
| 259 // The TRV of LineTerminatorSequence :: <LF> is the code unit value 0x000A. | |
| 260 // The TRV of LineTerminatorSequence :: <CR> is the code unit value 0x000A. | |
| 261 // The TRV of LineTerminatorSequence :: <CR><LF> is the sequence consisting of | |
| 262 // the code unit value 0x000A. | |
| 263 calls = 0; | |
| 264 function testRawLineNormalization(cs) { | |
| 265 calls++; | |
| 266 assertEquals(cs.raw[0], "\n\n\n"); | |
| 267 assertEquals(cs.raw[1], "\n\n\n"); | |
| 268 } | |
| 269 eval("testRawLineNormalization`\r\n\n\r${1}\r\n\n\r`"); | |
| 270 assertEquals(1, calls); | |
| 271 | |
| 272 // The TRV of LineContinuation :: \ LineTerminatorSequence is the sequence | |
| 273 // consisting of the code unit value 0x005C followed by the code units of TRV | |
| 274 // of LineTerminatorSequence. | |
| 275 calls = 0; | |
| 276 function testRawLineContinuation(cs) { | |
| 277 calls++; | |
| 278 assertEquals(cs.raw[0], "\u005C\n\u005C\n\u005C\n"); | |
| 279 assertEquals(cs.raw[1], "\u005C\n\u005C\n\u005C\n"); | |
| 280 } | |
| 281 eval("testRawLineContinuation`\\\r\n\\\n\\\r${1}\\\r\n\\\n\\\r`"); | |
| 282 assertEquals(1, calls); | |
| 283 })(); | |
| 284 | |
| 285 | |
| 286 (function testCallSiteObj() { | |
| 287 var calls = 0; | |
| 288 function tag(cs) { | |
| 289 calls++; | |
| 290 assertTrue(cs.hasOwnProperty("raw")); | |
| 291 assertTrue(Object.isFrozen(cs)); | |
| 292 assertTrue(Object.isFrozen(cs.raw)); | |
| 293 var raw = Object.getOwnPropertyDescriptor(cs, "raw"); | |
| 294 assertFalse(raw.writable); | |
| 295 assertFalse(raw.configurable); | |
| 296 assertFalse(raw.enumerable); | |
| 297 assertTrue(cs.raw instanceof Array); | |
|
arv (Not doing code reviews)
2014/11/12 17:03:38
Similar issue with instanceof as in. In test you a
| |
| 298 assertTrue(cs instanceof Array); | |
| 299 | |
| 300 var cooked0 = Object.getOwnPropertyDescriptor(cs, "0"); | |
| 301 assertFalse(cooked0.writable); | |
| 302 assertFalse(cooked0.configurable); | |
| 303 assertTrue(cooked0.enumerable); | |
| 304 | |
| 305 var raw0 = Object.getOwnPropertyDescriptor(cs.raw, "0"); | |
| 306 assertFalse(cooked0.writable); | |
| 307 assertFalse(cooked0.configurable); | |
| 308 assertTrue(cooked0.enumerable); | |
| 309 | |
| 310 var length = Object.getOwnPropertyDescriptor(cs, "length"); | |
| 311 assertFalse(length.writable); | |
| 312 assertFalse(length.configurable); | |
| 313 assertFalse(length.enumerable); | |
| 314 | |
| 315 length = Object.getOwnPropertyDescriptor(cs.raw, "length"); | |
| 316 assertFalse(length.writable); | |
| 317 assertFalse(length.configurable); | |
| 318 assertFalse(length.enumerable); | |
| 319 } | |
| 320 tag`${1}`; | |
| 321 assertEquals(1, calls); | |
| 322 })(); | |
| 323 | |
| 324 | |
| 325 (function testUTF16ByteOrderMark() { | |
| 326 assertEquals("\uFEFFtest", `\uFEFFtest`); | |
| 327 assertEquals("\uFEFFtest", eval("`\uFEFFtest`")); | |
| 328 })(); | |
| 329 | |
| 330 | |
| 331 (function testExtendedArrayPrototype() { | |
| 332 Object.defineProperty(Array.prototype, 0, { | |
| 333 set: function() { | |
| 334 assertUnreachable(); | |
| 335 } | |
| 336 }); | |
| 337 function tag(){} | |
| 338 tag`a${1}b`; | |
| 339 })(); | |
| OLD | NEW |