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 // 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 // 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]); | |
|
caitp (gmail)
2014/11/01 03:33:55
If I could get some help with this, that would be
caitp (gmail)
2014/11/01 04:34:21
Nevermind, it was a mistake in the scanner, easily
| |
| 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 // TODO(caitp): add test cases for TRV values once TRV is working correctly | |
| 145 // (see failures commented out above). | |
| 146 | |
| OLD | NEW |