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..860693eb40d37dea46e3861febde6195ad1fb17f |
| --- /dev/null |
| +++ b/test/mjsunit/es6/templates.js |
| @@ -0,0 +1,146 @@ |
| +// 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; |
| +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`); |
| + |
| +// 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]); |
|
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
|
| +})``; |
| + |
| +// 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]); })`\ |
| + |
| +`; |
| + |
| +// TODO(caitp): add test cases for TRV values once TRV is working correctly |
| +// (see failures commented out above). |
| + |