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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « test/mjsunit/es6/string-raw.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 the V8 project authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Flags: --harmony-unicode 5 // Flags: --harmony-unicode
6 6
7 var num = 5; 7 var num = 5;
8 var str = "str"; 8 var str = "str";
9 function fn() { return "result"; } 9 function fn() { return "result"; }
10 var obj = { 10 var obj = {
(...skipping 568 matching lines...) Expand 10 before | Expand all | Expand 10 after
579 raw.push("raw;" + text); 579 raw.push("raw;" + text);
580 return text; 580 return text;
581 } 581 }
582 assertEquals("test3", tag`test1``test2``test3`); 582 assertEquals("test3", tag`test1``test2``test3`);
583 assertEquals([ 583 assertEquals([
584 "tag;test1", 584 "tag;test1",
585 "tag;test2", 585 "tag;test2",
586 "raw;test3" 586 "raw;test3"
587 ], raw); 587 ], raw);
588 })(); 588 })();
589
590
591 (function testToStringSubstitutions() {
592 var a = {
593 toString: function() { return "a"; },
594 valueOf: function() { return "-a-"; }
595 };
596 var b = {
597 toString: function() { return "b"; },
598 valueOf: function() { return "-b-"; }
599 };
600 assertEquals("a", `${a}`);
601 assertEquals("ab", `${a}${b}`);
602 assertEquals("-a--b-", `${a + b}`);
603 assertEquals("-a-", `${a + ""}`);
604 assertEquals("1a", `1${a}`);
605 assertEquals("1a2", `1${a}2`);
606 assertEquals("1a2b", `1${a}2${b}`);
607 assertEquals("1a2b3", `1${a}2${b}3`);
608 })();
609
610
611 (function testToStringSubstitutionsOrder() {
612 var subs = [];
613 var log = [];
614 function getter(name, value) {
615 return {
616 get: function() {
617 log.push("get" + name);
618 return value;
619 },
620 set: function(v) {
621 log.push("set" + name);
622 }
623 };
624 }
625 Object.defineProperties(subs, {
626 0: getter(0, "a"),
627 1: getter(1, "b"),
628 2: getter(2, "c")
629 });
630
631 assertEquals("-a-b-c-", `-${subs[0]}-${subs[1]}-${subs[2]}-`);
632 assertArrayEquals(["get0", "get1", "get2"], log);
633 })();
634
635
636 (function testTaggedToStringSubstitutionsOrder() {
637 var subs = [];
638 var log = [];
639 var tagged = [];
640 function getter(name, value) {
641 return {
642 get: function() {
643 log.push("get" + name);
644 return value;
645 },
646 set: function(v) {
647 log.push("set" + name);
648 }
649 };
650 }
651 Object.defineProperties(subs, {
652 0: getter(0, 1),
653 1: getter(1, 2),
654 2: getter(2, 3)
655 });
656
657 function tag(cs) {
658 var n_substitutions = arguments.length - 1;
659 var n_cooked = cs.length;
660 var e = cs[0];
661 var i = 0;
662 assertEquals(n_cooked, n_substitutions + 1);
663 while (i < n_substitutions) {
664 var sub = arguments[i++ + 1];
665 var tail = cs[i];
666 tagged.push(sub);
667 e = e.concat(sub, tail);
668 }
669 return e;
670 }
671
672 assertEquals("-1-2-3-", tag`-${subs[0]}-${subs[1]}-${subs[2]}-`);
673 assertArrayEquals(["get0", "get1", "get2"], log);
674 assertArrayEquals([1, 2, 3], tagged);
675
676 tagged.length = 0;
677 log.length = 0;
678 assertEquals("-1-", tag`-${subs[0]}-`);
679 assertArrayEquals(["get0"], log);
680 assertArrayEquals([1], tagged);
681 })();
OLDNEW
« 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