Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1530)

Unified Diff: test/mjsunit/es6/templates.js

Issue 1027183002: [es6] call ToString() on template substitutions (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Test ToString behaviour for tagged templates + String.raw Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « test/mjsunit/es6/string-raw.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/mjsunit/es6/templates.js
diff --git a/test/mjsunit/es6/templates.js b/test/mjsunit/es6/templates.js
index a2b77b13c531560a71a2e584711cdba6923e3818..15296e87227cc963e4fa7c98fb4c5d6d024aab5e 100644
--- a/test/mjsunit/es6/templates.js
+++ b/test/mjsunit/es6/templates.js
@@ -586,3 +586,96 @@ var global = this;
"raw;test3"
], raw);
})();
+
+
+(function testToStringSubstitutions() {
+ var a = {
+ toString: function() { return "a"; },
+ valueOf: function() { return "-a-"; }
+ };
+ var b = {
+ toString: function() { return "b"; },
+ valueOf: function() { return "-b-"; }
+ };
+ assertEquals("a", `${a}`);
+ assertEquals("ab", `${a}${b}`);
+ assertEquals("-a--b-", `${a + b}`);
+ assertEquals("-a-", `${a + ""}`);
+ assertEquals("1a", `1${a}`);
+ assertEquals("1a2", `1${a}2`);
+ assertEquals("1a2b", `1${a}2${b}`);
+ assertEquals("1a2b3", `1${a}2${b}3`);
+})();
+
+
+(function testToStringSubstitutionsOrder() {
+ var subs = [];
+ var log = [];
+ function getter(name, value) {
+ return {
+ get: function() {
+ log.push("get" + name);
+ return value;
+ },
+ set: function(v) {
+ log.push("set" + name);
+ }
+ };
+ }
+ Object.defineProperties(subs, {
+ 0: getter(0, "a"),
+ 1: getter(1, "b"),
+ 2: getter(2, "c")
+ });
+
+ assertEquals("-a-b-c-", `-${subs[0]}-${subs[1]}-${subs[2]}-`);
+ assertArrayEquals(["get0", "get1", "get2"], log);
+})();
+
+
+(function testTaggedToStringSubstitutionsOrder() {
+ var subs = [];
+ var log = [];
+ var tagged = [];
+ function getter(name, value) {
+ return {
+ get: function() {
+ log.push("get" + name);
+ return value;
+ },
+ set: function(v) {
+ log.push("set" + name);
+ }
+ };
+ }
+ Object.defineProperties(subs, {
+ 0: getter(0, 1),
+ 1: getter(1, 2),
+ 2: getter(2, 3)
+ });
+
+ function tag(cs) {
+ var n_substitutions = arguments.length - 1;
+ var n_cooked = cs.length;
+ var e = cs[0];
+ var i = 0;
+ assertEquals(n_cooked, n_substitutions + 1);
+ while (i < n_substitutions) {
+ var sub = arguments[i++ + 1];
+ var tail = cs[i];
+ tagged.push(sub);
+ e = e.concat(sub, tail);
+ }
+ return e;
+ }
+
+ assertEquals("-1-2-3-", tag`-${subs[0]}-${subs[1]}-${subs[2]}-`);
+ assertArrayEquals(["get0", "get1", "get2"], log);
+ assertArrayEquals([1, 2, 3], tagged);
+
+ tagged.length = 0;
+ log.length = 0;
+ assertEquals("-1-", tag`-${subs[0]}-`);
+ assertArrayEquals(["get0"], log);
+ assertArrayEquals([1], tagged);
+})();
« no previous file with comments | « test/mjsunit/es6/string-raw.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698