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