Chromium Code Reviews| Index: test/mjsunit/es6/templates.js |
| diff --git a/test/mjsunit/es6/templates.js b/test/mjsunit/es6/templates.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2606b70e3bbba0553e24a3b008eddba5d9adbc3d |
| --- /dev/null |
| +++ b/test/mjsunit/es6/templates.js |
| @@ -0,0 +1,198 @@ |
| +// Copyright 2014 the V8 project authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// Flags: --harmony-templates |
| + |
| +var num = 5; |
|
arv (Not doing code reviews)
2014/11/11 17:15:15
I prefer writing these tests in functions. Makes i
|
| +var str = "str"; |
| +function fn() { return "result"; } |
| +var obj = { |
| + num: num, |
| + str: str, |
| + fn: function() { return "result"; } |
| +}; |
| + |
| +// Basic expressions |
| +assertEquals("foo 5 bar", `foo ${num} bar`); |
| +assertEquals("foo str bar", `foo ${str} bar`); |
| +assertEquals("foo [object Object] bar", `foo ${obj} bar`); |
| +assertEquals("foo result bar", `foo ${fn()} bar`); |
| +assertEquals("foo 5 bar", `foo ${obj.num} bar`); |
| +assertEquals("foo str bar", `foo ${obj.str} bar`); |
| +assertEquals("foo result bar", `foo ${obj.fn()} bar`); |
| + |
| +// Expressions containing templates |
| +assertEquals("foo bar 5", `foo ${`bar ${num}`}`); |
| + |
| +// Multi-line templates |
| +assertEquals("foo\n bar\n baz", `foo |
| + bar |
| + baz`); |
| + |
| +assertEquals("foo\n bar\n baz", eval("`foo\r\n bar\r baz`")); |
| + |
| +// Tagged Templates |
| + |
| +// assert tag is invoked |
| +var called = false; |
| +(function(s) { |
| + called = true; |
| +})`test`; |
| +assertTrue(called); |
| + |
| +called = false; |
| +// assert tag is invoked in right context |
| +obj = { |
| + fn: function() { |
| + called = true; |
| + assertEquals(obj, this); |
| + } |
| +}; |
| + |
| +obj.fn`test`; |
| +assertTrue(called); |
| + |
| +// Simple templates only have a callSiteObj |
| +(function(s) { |
| + assertEquals(1, arguments.length); |
| +})`test`; |
| + |
| +// Templates containing expressions have the values of evaluated expressions |
| +(function(site, n, s, o, f, r) { |
| + assertEquals(6, arguments.length); |
| + assertEquals("number", typeof n); |
| + assertEquals("string", typeof s); |
| + assertEquals("object", typeof o); |
| + assertEquals("function", typeof f); |
| + assertEquals("result", r); |
| +})`${num}${str}${obj}${fn}${fn()}`; |
| + |
| +// The TV and TRV of NoSubstitutionTemplate :: `` is the empty code unit sequence. |
| +(function(s) { |
| + assertEquals(1, s.length); |
| + assertEquals(1, s.raw.length); |
| + assertEquals("", s[0]); |
| + |
| + // Failure: expected <""> found <"foo barfoo barfoo foo foo foo testtest"> |
| + assertEquals("", s.raw[0]); |
| +})``; |
| + |
| +// The TV and TRV of TemplateHead :: `${ is the empty code unit sequence. |
| +(function(s) { |
| + assertEquals(2, s.length); |
| + assertEquals(2, s.raw.length); |
| + assertEquals("", s[0]); |
| + |
| + // Failure: expected <""> found <"foo barfoo barfoo foo foo foo testtest"> |
| + assertEquals("", s.raw[0]); |
| +})`${1}`; |
| + |
| +// The TV and TRV of TemplateMiddle :: }${ is the empty code unit sequence. |
| +(function(s) { |
| + assertEquals(3, s.length); |
| + assertEquals(3, s.raw.length); |
| + assertEquals("", s[1]); |
| + |
| + // Failure: expected <""> found <"foo barfoo barfoo foo foo foo testtest"> |
| + // assertEquals("", s.raw[1]); |
| +})`${1}${2}`; |
| + |
| +// The TV and TRV of TemplateTail :: }` is the empty code unit sequence. |
| +(function(s) { |
| + assertEquals(2, s.length); |
| + assertEquals(2, s.raw.length); |
| + assertEquals("", s[1]); |
| + |
| + // Failure: expected <""> found <"foo barfoo barfoo foo foo foo testtest"> |
| + // assertEquals("", s.raw[1]); |
| +})`${1}`; |
| + |
| +// The TV of NoSubstitutionTemplate :: ` TemplateCharacters ` is the TV of |
| +// TemplateCharacters. |
| +(function(s) { assertEquals("foo", s[0]); })`foo`; |
| + |
| +// The TV of TemplateHead :: ` TemplateCharacters ${ is the TV of |
| +// TemplateCharacters. |
| +(function(s) { assertEquals("foo", s[0]); })`foo${1}`; |
| + |
| +// The TV of TemplateMiddle :: } TemplateCharacters ${ is the TV of |
| +// TemplateCharacters. |
| +(function(s) { assertEquals("foo", s[1]); })`${1}foo${2}`; |
| + |
| +// The TV of TemplateTail :: } TemplateCharacters ` is the TV of |
| +// TemplateCharacters. |
| +(function(s) { assertEquals("foo", s[1]); })`${1}foo`; |
| + |
| +// The TV of TemplateCharacters :: TemplateCharacter is the TV of |
| +// TemplateCharacter. |
| +(function(s) { assertEquals("f", s[0]); })`f`; |
| + |
| +// The TV of TemplateCharacter :: $ is the code unit value 0x0024. |
| +(function(s) { assertEquals("$", s[0]); })`$`; |
| + |
| +// The TV of TemplateCharacter :: \ EscapeSequence is the CV of EscapeSequence. |
| +(function(s) { assertEquals("안녕", s[0]); })`\uc548\uB155`; |
| +(function(s) { assertEquals("\xff", s[0]); })`\xff`; |
| +(function(s) { assertEquals("\n", s[0]); })`\n`; |
| + |
| +// The TV of TemplateCharacter :: LineContinuation is the TV of |
| +// LineContinuation. The TV of LineContinuation :: \ LineTerminatorSequence is |
| +// the empty code unit sequence. |
| +(function(s) { assertEquals("", s[0]); })`\ |
| + |
| +`; |
| + |
| +// The TRV of NoSubstitutionTemplate :: ` TemplateCharacters ` is the TRV of |
| +// TemplateCharacters. |
| +(function(s) { assertEquals("test", s.raw[0]); })`test`; |
| + |
| +// The TRV of TemplateHead :: ` TemplateCharacters ${ is the TRV of |
| +// TemplateCharacters. |
| +(function(s) { assertEquals("test", s.raw[0]); })`test${1}`; |
| + |
| +// The TRV of TemplateMiddle :: } TemplateCharacters ${ is the TRV of |
| +// TemplateCharacters. |
| +(function(s) { assertEquals("test", s.raw[1]); })`${1}test${2}`; |
| + |
| +// The TRV of TemplateTail :: } TemplateCharacters ` is the TRV of |
| +// TemplateCharacters. |
| +(function(s) { assertEquals("test", s.raw[1]); })`${1}test`; |
| + |
| +// The TRV of TemplateCharacters :: TemplateCharacter is the TRV of |
| +// TemplateCharacter. |
| +(function(s) { assertEquals("f", s.raw[0]); })`f`; |
| + |
| +// The TRV of TemplateCharacter :: $ is the code unit value 0x0024. |
| +(function(s) { assertEquals("\u0024", s.raw[0]); })`$`; |
| + |
| +// The TRV of EscapeSequence :: 0 is the code unit value 0x0030. |
| +(function(s) { assertEquals("\u005C\u0030", s.raw[0]); })`\0`; |
| + |
| +// The TRV of TemplateCharacter :: \ EscapeSequence is the sequence consisting |
| +// of the code unit value 0x005C followed by the code units of TRV of |
| +// EscapeSequence. |
| + |
| +// The TRV of EscapeSequence :: HexEscapeSequence is the TRV of the |
| +// HexEscapeSequence. |
| +(function(s) { assertEquals("\u005Cxff", s.raw[0]); })`\xff`; |
| + |
| +// The TRV of EscapeSequence :: UnicodeEscapeSequence is the TRV of the |
| +// UnicodeEscapeSequence. |
| +(function(s) { assertEquals("\u005Cuc548", s.raw[0]); })`\uc548`; |
| + |
| +// The TRV of CharacterEscapeSequence :: SingleEscapeCharacter is the TRV of |
| +// the SingleEscapeCharacter. |
| +(function(s) { assertEquals("\u005C\u0027", s.raw[0]); })`\'`; |
| +(function(s) { assertEquals("\u005C\u0022", s.raw[0]); })`\"`; |
| +(function(s) { assertEquals("\u005C\u005C", s.raw[0]); })`\\`; |
| +(function(s) { assertEquals("\u005Cb", s.raw[0]); })`\b`; |
| +(function(s) { assertEquals("\u005Cf", s.raw[0]); })`\f`; |
| +(function(s) { assertEquals("\u005Cn", s.raw[0]); })`\n`; |
| +(function(s) { assertEquals("\u005Cr", s.raw[0]); })`\r`; |
| +(function(s) { assertEquals("\u005Ct", s.raw[0]); })`\t`; |
| +(function(s) { assertEquals("\u005Cv", s.raw[0]); })`\v`; |
| + |
| +// The TRV of CharacterEscapeSequence :: NonEscapeCharacter is the CV of the |
| +// NonEscapeCharacter. |
| +(function(s) { assertEquals("\u005Cx", s.raw[0]); })`\x`; |