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; | |
|
arv (Not doing code reviews)
2014/11/11 17:15:15
I prefer writing these tests in functions. Makes i
| |
| 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 | |
| 29 assertEquals("foo\n bar\n baz", `foo | |
| 30 bar | |
| 31 baz`); | |
| 32 | |
| 33 assertEquals("foo\n bar\n baz", eval("`foo\r\n bar\r baz`")); | |
| 34 | |
| 35 // Tagged Templates | |
| 36 | |
| 37 // assert tag is invoked | |
| 38 var called = false; | |
| 39 (function(s) { | |
| 40 called = true; | |
| 41 })`test`; | |
| 42 assertTrue(called); | |
| 43 | |
| 44 called = false; | |
| 45 // assert tag is invoked in right context | |
| 46 obj = { | |
| 47 fn: function() { | |
| 48 called = true; | |
| 49 assertEquals(obj, this); | |
| 50 } | |
| 51 }; | |
| 52 | |
| 53 obj.fn`test`; | |
| 54 assertTrue(called); | |
| 55 | |
| 56 // Simple templates only have a callSiteObj | |
| 57 (function(s) { | |
| 58 assertEquals(1, arguments.length); | |
| 59 })`test`; | |
| 60 | |
| 61 // Templates containing expressions have the values of evaluated expressions | |
| 62 (function(site, n, s, o, f, r) { | |
| 63 assertEquals(6, arguments.length); | |
| 64 assertEquals("number", typeof n); | |
| 65 assertEquals("string", typeof s); | |
| 66 assertEquals("object", typeof o); | |
| 67 assertEquals("function", typeof f); | |
| 68 assertEquals("result", r); | |
| 69 })`${num}${str}${obj}${fn}${fn()}`; | |
| 70 | |
| 71 // The TV and TRV of NoSubstitutionTemplate :: `` is the empty code unit sequenc e. | |
| 72 (function(s) { | |
| 73 assertEquals(1, s.length); | |
| 74 assertEquals(1, s.raw.length); | |
| 75 assertEquals("", s[0]); | |
| 76 | |
| 77 // Failure: expected <""> found <"foo barfoo barfoo foo foo foo testtest"> | |
| 78 assertEquals("", s.raw[0]); | |
| 79 })``; | |
| 80 | |
| 81 // The TV and TRV of TemplateHead :: `${ is the empty code unit sequence. | |
| 82 (function(s) { | |
| 83 assertEquals(2, s.length); | |
| 84 assertEquals(2, s.raw.length); | |
| 85 assertEquals("", s[0]); | |
| 86 | |
| 87 // Failure: expected <""> found <"foo barfoo barfoo foo foo foo testtest"> | |
| 88 assertEquals("", s.raw[0]); | |
| 89 })`${1}`; | |
| 90 | |
| 91 // The TV and TRV of TemplateMiddle :: }${ is the empty code unit sequence. | |
| 92 (function(s) { | |
| 93 assertEquals(3, s.length); | |
| 94 assertEquals(3, s.raw.length); | |
| 95 assertEquals("", s[1]); | |
| 96 | |
| 97 // Failure: expected <""> found <"foo barfoo barfoo foo foo foo testtest"> | |
| 98 // assertEquals("", s.raw[1]); | |
| 99 })`${1}${2}`; | |
| 100 | |
| 101 // The TV and TRV of TemplateTail :: }` is the empty code unit sequence. | |
| 102 (function(s) { | |
| 103 assertEquals(2, s.length); | |
| 104 assertEquals(2, s.raw.length); | |
| 105 assertEquals("", s[1]); | |
| 106 | |
| 107 // Failure: expected <""> found <"foo barfoo barfoo foo foo foo testtest"> | |
| 108 // assertEquals("", s.raw[1]); | |
| 109 })`${1}`; | |
| 110 | |
| 111 // The TV of NoSubstitutionTemplate :: ` TemplateCharacters ` is the TV of | |
| 112 // TemplateCharacters. | |
| 113 (function(s) { assertEquals("foo", s[0]); })`foo`; | |
| 114 | |
| 115 // The TV of TemplateHead :: ` TemplateCharacters ${ is the TV of | |
| 116 // TemplateCharacters. | |
| 117 (function(s) { assertEquals("foo", s[0]); })`foo${1}`; | |
| 118 | |
| 119 // The TV of TemplateMiddle :: } TemplateCharacters ${ is the TV of | |
| 120 // TemplateCharacters. | |
| 121 (function(s) { assertEquals("foo", s[1]); })`${1}foo${2}`; | |
| 122 | |
| 123 // The TV of TemplateTail :: } TemplateCharacters ` is the TV of | |
| 124 // TemplateCharacters. | |
| 125 (function(s) { assertEquals("foo", s[1]); })`${1}foo`; | |
| 126 | |
| 127 // The TV of TemplateCharacters :: TemplateCharacter is the TV of | |
| 128 // TemplateCharacter. | |
| 129 (function(s) { assertEquals("f", s[0]); })`f`; | |
| 130 | |
| 131 // The TV of TemplateCharacter :: $ is the code unit value 0x0024. | |
| 132 (function(s) { assertEquals("$", s[0]); })`$`; | |
| 133 | |
| 134 // The TV of TemplateCharacter :: \ EscapeSequence is the CV of EscapeSequence. | |
| 135 (function(s) { assertEquals("안녕", s[0]); })`\uc548\uB155`; | |
| 136 (function(s) { assertEquals("\xff", s[0]); })`\xff`; | |
| 137 (function(s) { assertEquals("\n", s[0]); })`\n`; | |
| 138 | |
| 139 // The TV of TemplateCharacter :: LineContinuation is the TV of | |
| 140 // LineContinuation. The TV of LineContinuation :: \ LineTerminatorSequence is | |
| 141 // the empty code unit sequence. | |
| 142 (function(s) { assertEquals("", s[0]); })`\ | |
| 143 | |
| 144 `; | |
| 145 | |
| 146 // The TRV of NoSubstitutionTemplate :: ` TemplateCharacters ` is the TRV of | |
| 147 // TemplateCharacters. | |
| 148 (function(s) { assertEquals("test", s.raw[0]); })`test`; | |
| 149 | |
| 150 // The TRV of TemplateHead :: ` TemplateCharacters ${ is the TRV of | |
| 151 // TemplateCharacters. | |
| 152 (function(s) { assertEquals("test", s.raw[0]); })`test${1}`; | |
| 153 | |
| 154 // The TRV of TemplateMiddle :: } TemplateCharacters ${ is the TRV of | |
| 155 // TemplateCharacters. | |
| 156 (function(s) { assertEquals("test", s.raw[1]); })`${1}test${2}`; | |
| 157 | |
| 158 // The TRV of TemplateTail :: } TemplateCharacters ` is the TRV of | |
| 159 // TemplateCharacters. | |
| 160 (function(s) { assertEquals("test", s.raw[1]); })`${1}test`; | |
| 161 | |
| 162 // The TRV of TemplateCharacters :: TemplateCharacter is the TRV of | |
| 163 // TemplateCharacter. | |
| 164 (function(s) { assertEquals("f", s.raw[0]); })`f`; | |
| 165 | |
| 166 // The TRV of TemplateCharacter :: $ is the code unit value 0x0024. | |
| 167 (function(s) { assertEquals("\u0024", s.raw[0]); })`$`; | |
| 168 | |
| 169 // The TRV of EscapeSequence :: 0 is the code unit value 0x0030. | |
| 170 (function(s) { assertEquals("\u005C\u0030", s.raw[0]); })`\0`; | |
| 171 | |
| 172 // The TRV of TemplateCharacter :: \ EscapeSequence is the sequence consisting | |
| 173 // of the code unit value 0x005C followed by the code units of TRV of | |
| 174 // EscapeSequence. | |
| 175 | |
| 176 // The TRV of EscapeSequence :: HexEscapeSequence is the TRV of the | |
| 177 // HexEscapeSequence. | |
| 178 (function(s) { assertEquals("\u005Cxff", s.raw[0]); })`\xff`; | |
| 179 | |
| 180 // The TRV of EscapeSequence :: UnicodeEscapeSequence is the TRV of the | |
| 181 // UnicodeEscapeSequence. | |
| 182 (function(s) { assertEquals("\u005Cuc548", s.raw[0]); })`\uc548`; | |
| 183 | |
| 184 // The TRV of CharacterEscapeSequence :: SingleEscapeCharacter is the TRV of | |
| 185 // the SingleEscapeCharacter. | |
| 186 (function(s) { assertEquals("\u005C\u0027", s.raw[0]); })`\'`; | |
| 187 (function(s) { assertEquals("\u005C\u0022", s.raw[0]); })`\"`; | |
| 188 (function(s) { assertEquals("\u005C\u005C", s.raw[0]); })`\\`; | |
| 189 (function(s) { assertEquals("\u005Cb", s.raw[0]); })`\b`; | |
| 190 (function(s) { assertEquals("\u005Cf", s.raw[0]); })`\f`; | |
| 191 (function(s) { assertEquals("\u005Cn", s.raw[0]); })`\n`; | |
| 192 (function(s) { assertEquals("\u005Cr", s.raw[0]); })`\r`; | |
| 193 (function(s) { assertEquals("\u005Ct", s.raw[0]); })`\t`; | |
| 194 (function(s) { assertEquals("\u005Cv", s.raw[0]); })`\v`; | |
| 195 | |
| 196 // The TRV of CharacterEscapeSequence :: NonEscapeCharacter is the CV of the | |
| 197 // NonEscapeCharacter. | |
| 198 (function(s) { assertEquals("\u005Cx", s.raw[0]); })`\x`; | |
| OLD | NEW |