Skip to content

Commit d2b6f53

Browse files
author
John Messerly
committed
fix #470, string interpolation now respects Dart toString
[email protected] Review URL: https://codereview.chromium.org/1989663002 .
1 parent 9e816ff commit d2b6f53

File tree

20 files changed

+314
-306
lines changed

20 files changed

+314
-306
lines changed

pkg/dev_compiler/lib/runtime/dart_sdk.js

Lines changed: 225 additions & 218 deletions
Large diffs are not rendered by default.

pkg/dev_compiler/lib/src/compiler/code_generator.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4186,11 +4186,9 @@ class CodeGenerator extends GeneralizingAstVisitor
41864186
_visitListToBinary(node.strings, '+');
41874187

41884188
@override
4189-
JS.TemplateString visitStringInterpolation(StringInterpolation node) {
4190-
// Assuming we implement toString() on our objects, we can avoid calling it
4191-
// in most cases. Builtin types may differ though. We could handle this with
4192-
// a tagged template.
4193-
return new JS.TemplateString(_visitList(node.elements));
4189+
JS.Expression visitStringInterpolation(StringInterpolation node) {
4190+
return new JS.TaggedTemplate(
4191+
js.call('dart.str'), new JS.TemplateString(_visitList(node.elements)));
41944192
}
41954193

41964194
@override

pkg/dev_compiler/test/browser/language_tests.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,6 @@
386386
'bit_twiddling_bigint_test': fail,
387387
'bool_from_environment_test': fail,
388388
'collection_length_test': skip_timeout,
389-
'collection_to_string_test': fail,
390389
'compare_to2_test': fail,
391390
'const_list_literal_test': fail,
392391
'const_list_remove_range_test': fail,
@@ -671,14 +670,6 @@
671670
'int64_list_load_store_test': skip_fail,
672671
'typed_data_hierarchy_int64_test': skip_fail,
673672
'typed_data_list_test': fail,
674-
675-
// TODO(vsm): List.toString is different in DDC
676-
// https://github.com/dart-lang/dev_compiler/issues/445
677-
'setRange_1_test': skip_fail,
678-
'setRange_2_test': skip_fail,
679-
'setRange_3_test': skip_fail,
680-
'setRange_4_test': skip_fail,
681-
'setRange_5_test': skip_fail,
682673
},
683674
};
684675

pkg/dev_compiler/test/codegen/expect/BenchmarkBase.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ dart_library.library('BenchmarkBase', null, /* Imports */[
99
BenchmarkBase$.Expect = class Expect extends core.Object {
1010
static equals(expected, actual) {
1111
if (!dart.equals(expected, actual)) {
12-
dart.throw(`Values not equal: ${expected} vs ${actual}`);
12+
dart.throw(dart.str`Values not equal: ${expected} vs ${actual}`);
1313
}
1414
}
1515
static listEquals(expected, actual) {
1616
if (expected[dartx.length] != actual[dartx.length]) {
17-
dart.throw(`Lists have different lengths: ${expected[dartx.length]} vs ${actual[dartx.length]}`);
17+
dart.throw(dart.str`Lists have different lengths: ${expected[dartx.length]} vs ${actual[dartx.length]}`);
1818
}
1919
for (let i = 0; i < dart.notNull(actual[dartx.length]); i++) {
2020
BenchmarkBase$.Expect.equals(expected[dartx.get](i), actual[dartx.get](i));
@@ -73,7 +73,7 @@ dart_library.library('BenchmarkBase', null, /* Imports */[
7373
}
7474
report() {
7575
let score = this.measure();
76-
core.print(`${this.name}(RunTime): ${score} us.`);
76+
core.print(dart.str`${this.name}(RunTime): ${score} us.`);
7777
}
7878
};
7979
dart.setSignature(BenchmarkBase$.BenchmarkBase, {

pkg/dev_compiler/test/codegen/expect/DeltaBlue.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ dart_library.library('DeltaBlue', null, /* Imports */[
5252
}
5353
report() {
5454
let score = this.measure();
55-
core.print(`${this.name}(RunTime): ${score} us.`);
55+
core.print(dart.str`${this.name}(RunTime): ${score} us.`);
5656
}
5757
};
5858
dart.setSignature(BenchmarkBase$.BenchmarkBase, {
@@ -532,7 +532,7 @@ dart_library.library('DeltaBlue', null, /* Imports */[
532532
plan.execute();
533533
if (last.value != i) {
534534
core.print("Chain test failed:");
535-
core.print(`Expected last value to be ${i} but it was ${last.value}.`);
535+
core.print(dart.str`Expected last value to be ${i} but it was ${last.value}.`);
536536
}
537537
}
538538
};
@@ -578,12 +578,12 @@ dart_library.library('DeltaBlue', null, /* Imports */[
578578
BenchmarkBase$.Expect = class Expect extends core.Object {
579579
static equals(expected, actual) {
580580
if (!dart.equals(expected, actual)) {
581-
dart.throw(`Values not equal: ${expected} vs ${actual}`);
581+
dart.throw(dart.str`Values not equal: ${expected} vs ${actual}`);
582582
}
583583
}
584584
static listEquals(expected, actual) {
585585
if (expected[dartx.length] != actual[dartx.length]) {
586-
dart.throw(`Lists have different lengths: ${expected[dartx.length]} vs ${actual[dartx.length]}`);
586+
dart.throw(dart.str`Lists have different lengths: ${expected[dartx.length]} vs ${actual[dartx.length]}`);
587587
}
588588
for (let i = 0; i < dart.notNull(actual[dartx.length]); i++) {
589589
BenchmarkBase$.Expect.equals(expected[dartx.get](i), actual[dartx.get](i));

pkg/dev_compiler/test/codegen/expect/async_helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dart_library.library('async_helper', null, /* Imports */[
1111
async_helper._onAsyncEnd = null;
1212
async_helper._asyncLevel = 0;
1313
async_helper._buildException = function(msg) {
14-
return core.Exception.new(`Fatal: ${msg}. This is most likely a bug in your test.`);
14+
return core.Exception.new(dart.str`Fatal: ${msg}. This is most likely a bug in your test.`);
1515
};
1616
dart.fn(async_helper._buildException, core.Exception, [core.String]);
1717
async_helper.asyncTestInitialize = function(callback) {

pkg/dev_compiler/test/codegen/expect/async_helper/async_helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dart_library.library('async_helper', null, /* Imports */[
1111
async_helper._onAsyncEnd = null;
1212
async_helper._asyncLevel = 0;
1313
async_helper._buildException = function(msg) {
14-
return core.Exception.new(`Fatal: ${msg}. This is most likely a bug in your test.`);
14+
return core.Exception.new(dart.str`Fatal: ${msg}. This is most likely a bug in your test.`);
1515
};
1616
dart.fn(async_helper._buildException, core.Exception, [core.String]);
1717
async_helper.asyncTestInitialize = function(callback) {

pkg/dev_compiler/test/codegen/expect/closure.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export const closure = Object.create(null);
22
import { core, js, dart, dartx } from 'dart_sdk';
33
closure.generic_function = function(T) {
44
return (items: core.List<T>, seed: T): core.List<T> => {
5-
let strings = items[dartx.map](core.String)(dart.fn((i: T): string => `${i}`, core.String, [T]))[dartx.toList]();
5+
let strings = items[dartx.map](core.String)(dart.fn((i: T): string => dart.str`${i}`, core.String, [T]))[dartx.toList]();
66
return items;
77
};
88
};

pkg/dev_compiler/test/codegen/expect/covariance.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ dart_library.library('covariance', null, /* Imports */[
3434
super.new();
3535
}
3636
add(x) {
37-
core.print(`Bar.add got ${x}`);
37+
core.print(dart.str`Bar.add got ${x}`);
3838
super.add(x);
3939
}
4040
};

pkg/dev_compiler/test/codegen/expect/expect.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ dart_library.library('expect', null, /* Imports */[
4747
let end = i;
4848
let truncExpected = expect.Expect._truncateString(expected, start, end, 20);
4949
let truncActual = expect.Expect._truncateString(actual, start, end, 20);
50-
return `at index ${start}: Expected <${truncExpected}>, ` + `Found: <${truncActual}>`;
50+
return dart.str`at index ${start}: Expected <${truncExpected}>, ` + dart.str`Found: <${truncActual}>`;
5151
}
5252
}
5353
return null;
@@ -59,43 +59,43 @@ dart_library.library('expect', null, /* Imports */[
5959
if (typeof expected == 'string' && typeof actual == 'string') {
6060
let stringDifference = expect.Expect._stringDifference(expected, actual);
6161
if (stringDifference != null) {
62-
expect.Expect._fail(`Expect.equals(${stringDifference}${msg}) fails.`);
62+
expect.Expect._fail(dart.str`Expect.equals(${stringDifference}${msg}) fails.`);
6363
}
6464
}
65-
expect.Expect._fail(`Expect.equals(expected: <${expected}>, actual: <${actual}>${msg}) fails.`);
65+
expect.Expect._fail(dart.str`Expect.equals(expected: <${expected}>, actual: <${actual}>${msg}) fails.`);
6666
}
6767
static isTrue(actual, reason) {
6868
if (reason === void 0) reason = null;
6969
if (dart.notNull(expect._identical(actual, true))) return;
7070
let msg = expect.Expect._getMessage(reason);
71-
expect.Expect._fail(`Expect.isTrue(${actual}${msg}) fails.`);
71+
expect.Expect._fail(dart.str`Expect.isTrue(${actual}${msg}) fails.`);
7272
}
7373
static isFalse(actual, reason) {
7474
if (reason === void 0) reason = null;
7575
if (dart.notNull(expect._identical(actual, false))) return;
7676
let msg = expect.Expect._getMessage(reason);
77-
expect.Expect._fail(`Expect.isFalse(${actual}${msg}) fails.`);
77+
expect.Expect._fail(dart.str`Expect.isFalse(${actual}${msg}) fails.`);
7878
}
7979
static isNull(actual, reason) {
8080
if (reason === void 0) reason = null;
8181
if (null == actual) return;
8282
let msg = expect.Expect._getMessage(reason);
83-
expect.Expect._fail(`Expect.isNull(actual: <${actual}>${msg}) fails.`);
83+
expect.Expect._fail(dart.str`Expect.isNull(actual: <${actual}>${msg}) fails.`);
8484
}
8585
static isNotNull(actual, reason) {
8686
if (reason === void 0) reason = null;
8787
if (null != actual) return;
8888
let msg = expect.Expect._getMessage(reason);
89-
expect.Expect._fail(`Expect.isNotNull(actual: <${actual}>${msg}) fails.`);
89+
expect.Expect._fail(dart.str`Expect.isNotNull(actual: <${actual}>${msg}) fails.`);
9090
}
9191
static identical(expected, actual, reason) {
9292
if (reason === void 0) reason = null;
9393
if (dart.notNull(expect._identical(expected, actual))) return;
9494
let msg = expect.Expect._getMessage(reason);
95-
expect.Expect._fail(`Expect.identical(expected: <${expected}>, actual: <${actual}>${msg}) ` + "fails.");
95+
expect.Expect._fail(dart.str`Expect.identical(expected: <${expected}>, actual: <${actual}>${msg}) ` + "fails.");
9696
}
9797
static fail(msg) {
98-
expect.Expect._fail(`Expect.fail('${msg}')`);
98+
expect.Expect._fail(dart.str`Expect.fail('${msg}')`);
9999
}
100100
static approxEquals(expected, actual, tolerance, reason) {
101101
if (tolerance === void 0) tolerance = null;
@@ -105,49 +105,49 @@ dart_library.library('expect', null, /* Imports */[
105105
}
106106
if (dart.notNull((dart.notNull(expected) - dart.notNull(actual))[dartx.abs]()) <= dart.notNull(tolerance)) return;
107107
let msg = expect.Expect._getMessage(reason);
108-
expect.Expect._fail(`Expect.approxEquals(expected:<${expected}>, actual:<${actual}>, ` + `tolerance:<${tolerance}>${msg}) fails`);
108+
expect.Expect._fail(dart.str`Expect.approxEquals(expected:<${expected}>, actual:<${actual}>, ` + dart.str`tolerance:<${tolerance}>${msg}) fails`);
109109
}
110110
static notEquals(unexpected, actual, reason) {
111111
if (reason === void 0) reason = null;
112112
if (!dart.equals(unexpected, actual)) return;
113113
let msg = expect.Expect._getMessage(reason);
114-
expect.Expect._fail(`Expect.notEquals(unexpected: <${unexpected}>, actual:<${actual}>${msg}) ` + "fails.");
114+
expect.Expect._fail(dart.str`Expect.notEquals(unexpected: <${unexpected}>, actual:<${actual}>${msg}) ` + "fails.");
115115
}
116116
static listEquals(expected, actual, reason) {
117117
if (reason === void 0) reason = null;
118118
let msg = expect.Expect._getMessage(reason);
119119
let n = dart.notNull(expected[dartx.length]) < dart.notNull(actual[dartx.length]) ? expected[dartx.length] : actual[dartx.length];
120120
for (let i = 0; i < dart.notNull(n); i++) {
121121
if (!dart.equals(expected[dartx.get](i), actual[dartx.get](i))) {
122-
expect.Expect._fail(`Expect.listEquals(at index ${i}, ` + `expected: <${expected[dartx.get](i)}>, actual: <${actual[dartx.get](i)}>${msg}) fails`);
122+
expect.Expect._fail(dart.str`Expect.listEquals(at index ${i}, ` + dart.str`expected: <${expected[dartx.get](i)}>, actual: <${actual[dartx.get](i)}>${msg}) fails`);
123123
}
124124
}
125125
if (expected[dartx.length] != actual[dartx.length]) {
126-
expect.Expect._fail('Expect.listEquals(list length, ' + `expected: <${expected[dartx.length]}>, actual: <${actual[dartx.length]}>${msg}) ` + 'fails: Next element <' + `${dart.notNull(expected[dartx.length]) > dart.notNull(n) ? expected[dartx.get](n) : actual[dartx.get](n)}>`);
126+
expect.Expect._fail('Expect.listEquals(list length, ' + dart.str`expected: <${expected[dartx.length]}>, actual: <${actual[dartx.length]}>${msg}) ` + 'fails: Next element <' + dart.str`${dart.notNull(expected[dartx.length]) > dart.notNull(n) ? expected[dartx.get](n) : actual[dartx.get](n)}>`);
127127
}
128128
}
129129
static mapEquals(expected, actual, reason) {
130130
if (reason === void 0) reason = null;
131131
let msg = expect.Expect._getMessage(reason);
132132
for (let key of expected[dartx.keys]) {
133133
if (!dart.notNull(actual[dartx.containsKey](key))) {
134-
expect.Expect._fail(`Expect.mapEquals(missing expected key: <${key}>${msg}) fails`);
134+
expect.Expect._fail(dart.str`Expect.mapEquals(missing expected key: <${key}>${msg}) fails`);
135135
}
136136
expect.Expect.equals(expected[dartx.get](key), actual[dartx.get](key));
137137
}
138138
for (let key of actual[dartx.keys]) {
139139
if (!dart.notNull(expected[dartx.containsKey](key))) {
140-
expect.Expect._fail(`Expect.mapEquals(unexpected key: <${key}>${msg}) fails`);
140+
expect.Expect._fail(dart.str`Expect.mapEquals(unexpected key: <${key}>${msg}) fails`);
141141
}
142142
}
143143
}
144144
static stringEquals(expected, actual, reason) {
145145
if (reason === void 0) reason = null;
146146
if (expected == actual) return;
147147
let msg = expect.Expect._getMessage(reason);
148-
let defaultMessage = `Expect.stringEquals(expected: <${expected}>", <${actual}>${msg}) fails`;
148+
let defaultMessage = dart.str`Expect.stringEquals(expected: <${expected}>", <${actual}>${msg}) fails`;
149149
if (expected == null || actual == null) {
150-
expect.Expect._fail(`${defaultMessage}`);
150+
expect.Expect._fail(dart.str`${defaultMessage}`);
151151
}
152152
let left = 0;
153153
let right = 0;
@@ -182,8 +182,8 @@ dart_library.library('expect', null, /* Imports */[
182182
let rightTail = "...";
183183
if (left <= 10) leftLead = "";
184184
if (right <= 10) rightTail = "";
185-
let diff = `\nDiff (${left}..${dart.notNull(eLen) - right}/${dart.notNull(aLen) - right}):\n` + `${leftLead}${leftSnippet}[ ${eSnippet} ]${rightSnippet}${rightTail}\n` + `${leftLead}${leftSnippet}[ ${aSnippet} ]${rightSnippet}${rightTail}`;
186-
expect.Expect._fail(`${defaultMessage}${diff}`);
185+
let diff = dart.str`\nDiff (${left}..${dart.notNull(eLen) - right}/${dart.notNull(aLen) - right}):\n` + dart.str`${leftLead}${leftSnippet}[ ${eSnippet} ]${rightSnippet}${rightTail}\n` + dart.str`${leftLead}${leftSnippet}[ ${aSnippet} ]${rightSnippet}${rightTail}`;
186+
expect.Expect._fail(dart.str`${defaultMessage}${diff}`);
187187
}
188188
static setEquals(expected, actual, reason) {
189189
if (reason === void 0) reason = null;
@@ -193,44 +193,44 @@ dart_library.library('expect', null, /* Imports */[
193193
extraSet.removeAll(expected);
194194
if (dart.notNull(extraSet.isEmpty) && dart.notNull(missingSet.isEmpty)) return;
195195
let msg = expect.Expect._getMessage(reason);
196-
let sb = new core.StringBuffer(`Expect.setEquals(${msg}) fails`);
196+
let sb = new core.StringBuffer(dart.str`Expect.setEquals(${msg}) fails`);
197197
if (!dart.notNull(missingSet.isEmpty)) {
198198
sb.write('\nExpected collection does not contain: ');
199199
}
200200
for (let val of missingSet) {
201-
sb.write(`${val} `);
201+
sb.write(dart.str`${val} `);
202202
}
203203
if (!dart.notNull(extraSet.isEmpty)) {
204204
sb.write('\nExpected collection should not contain: ');
205205
}
206206
for (let val of extraSet) {
207-
sb.write(`${val} `);
207+
sb.write(dart.str`${val} `);
208208
}
209209
expect.Expect._fail(sb.toString());
210210
}
211211
static throws(f, check, reason) {
212212
if (check === void 0) check = null;
213213
if (reason === void 0) reason = null;
214-
let msg = reason == null ? "" : `(${reason})`;
214+
let msg = reason == null ? "" : dart.str`(${reason})`;
215215
if (!dart.is(f, expect._Nullary)) {
216-
expect.Expect._fail(`Expect.throws${msg}: Function f not callable with zero arguments`);
216+
expect.Expect._fail(dart.str`Expect.throws${msg}: Function f not callable with zero arguments`);
217217
}
218218
try {
219219
f();
220220
} catch (e) {
221221
let s = dart.stackTrace(e);
222222
if (check != null) {
223223
if (!dart.notNull(dart.dcall(check, e))) {
224-
expect.Expect._fail(`Expect.throws${msg}: Unexpected '${e}'\n${s}`);
224+
expect.Expect._fail(dart.str`Expect.throws${msg}: Unexpected '${e}'\n${s}`);
225225
}
226226
}
227227
return;
228228
}
229229

230-
expect.Expect._fail(`Expect.throws${msg} fails: Did not throw`);
230+
expect.Expect._fail(dart.str`Expect.throws${msg} fails: Did not throw`);
231231
}
232232
static _getMessage(reason) {
233-
return reason == null ? "" : `, '${reason}'`;
233+
return reason == null ? "" : dart.str`, '${reason}'`;
234234
}
235235
static _fail(message) {
236236
dart.throw(new expect.ExpectException(message));

0 commit comments

Comments
 (0)