Skip to content

Commit

Permalink
fix #470, string interpolation now respects Dart toString
Browse files Browse the repository at this point in the history
  • Loading branch information
John Messerly committed May 18, 2016
1 parent 9e816ff commit d2b6f53
Show file tree
Hide file tree
Showing 20 changed files with 314 additions and 306 deletions.
443 changes: 225 additions & 218 deletions pkg/dev_compiler/lib/runtime/dart_sdk.js

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions pkg/dev_compiler/lib/src/compiler/code_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4186,11 +4186,9 @@ class CodeGenerator extends GeneralizingAstVisitor
_visitListToBinary(node.strings, '+');

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

@override
Expand Down
9 changes: 0 additions & 9 deletions pkg/dev_compiler/test/browser/language_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,6 @@
'bit_twiddling_bigint_test': fail,
'bool_from_environment_test': fail,
'collection_length_test': skip_timeout,
'collection_to_string_test': fail,
'compare_to2_test': fail,
'const_list_literal_test': fail,
'const_list_remove_range_test': fail,
Expand Down Expand Up @@ -671,14 +670,6 @@
'int64_list_load_store_test': skip_fail,
'typed_data_hierarchy_int64_test': skip_fail,
'typed_data_list_test': fail,

// TODO(vsm): List.toString is different in DDC
// https://github.com/dart-lang/dev_compiler/issues/445
'setRange_1_test': skip_fail,
'setRange_2_test': skip_fail,
'setRange_3_test': skip_fail,
'setRange_4_test': skip_fail,
'setRange_5_test': skip_fail,
},
};

Expand Down
6 changes: 3 additions & 3 deletions pkg/dev_compiler/test/codegen/expect/BenchmarkBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ dart_library.library('BenchmarkBase', null, /* Imports */[
BenchmarkBase$.Expect = class Expect extends core.Object {
static equals(expected, actual) {
if (!dart.equals(expected, actual)) {
dart.throw(`Values not equal: ${expected} vs ${actual}`);
dart.throw(dart.str`Values not equal: ${expected} vs ${actual}`);
}
}
static listEquals(expected, actual) {
if (expected[dartx.length] != actual[dartx.length]) {
dart.throw(`Lists have different lengths: ${expected[dartx.length]} vs ${actual[dartx.length]}`);
dart.throw(dart.str`Lists have different lengths: ${expected[dartx.length]} vs ${actual[dartx.length]}`);
}
for (let i = 0; i < dart.notNull(actual[dartx.length]); i++) {
BenchmarkBase$.Expect.equals(expected[dartx.get](i), actual[dartx.get](i));
Expand Down Expand Up @@ -73,7 +73,7 @@ dart_library.library('BenchmarkBase', null, /* Imports */[
}
report() {
let score = this.measure();
core.print(`${this.name}(RunTime): ${score} us.`);
core.print(dart.str`${this.name}(RunTime): ${score} us.`);
}
};
dart.setSignature(BenchmarkBase$.BenchmarkBase, {
Expand Down
8 changes: 4 additions & 4 deletions pkg/dev_compiler/test/codegen/expect/DeltaBlue.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ dart_library.library('DeltaBlue', null, /* Imports */[
}
report() {
let score = this.measure();
core.print(`${this.name}(RunTime): ${score} us.`);
core.print(dart.str`${this.name}(RunTime): ${score} us.`);
}
};
dart.setSignature(BenchmarkBase$.BenchmarkBase, {
Expand Down Expand Up @@ -532,7 +532,7 @@ dart_library.library('DeltaBlue', null, /* Imports */[
plan.execute();
if (last.value != i) {
core.print("Chain test failed:");
core.print(`Expected last value to be ${i} but it was ${last.value}.`);
core.print(dart.str`Expected last value to be ${i} but it was ${last.value}.`);
}
}
};
Expand Down Expand Up @@ -578,12 +578,12 @@ dart_library.library('DeltaBlue', null, /* Imports */[
BenchmarkBase$.Expect = class Expect extends core.Object {
static equals(expected, actual) {
if (!dart.equals(expected, actual)) {
dart.throw(`Values not equal: ${expected} vs ${actual}`);
dart.throw(dart.str`Values not equal: ${expected} vs ${actual}`);
}
}
static listEquals(expected, actual) {
if (expected[dartx.length] != actual[dartx.length]) {
dart.throw(`Lists have different lengths: ${expected[dartx.length]} vs ${actual[dartx.length]}`);
dart.throw(dart.str`Lists have different lengths: ${expected[dartx.length]} vs ${actual[dartx.length]}`);
}
for (let i = 0; i < dart.notNull(actual[dartx.length]); i++) {
BenchmarkBase$.Expect.equals(expected[dartx.get](i), actual[dartx.get](i));
Expand Down
2 changes: 1 addition & 1 deletion pkg/dev_compiler/test/codegen/expect/async_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dart_library.library('async_helper', null, /* Imports */[
async_helper._onAsyncEnd = null;
async_helper._asyncLevel = 0;
async_helper._buildException = function(msg) {
return core.Exception.new(`Fatal: ${msg}. This is most likely a bug in your test.`);
return core.Exception.new(dart.str`Fatal: ${msg}. This is most likely a bug in your test.`);
};
dart.fn(async_helper._buildException, core.Exception, [core.String]);
async_helper.asyncTestInitialize = function(callback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dart_library.library('async_helper', null, /* Imports */[
async_helper._onAsyncEnd = null;
async_helper._asyncLevel = 0;
async_helper._buildException = function(msg) {
return core.Exception.new(`Fatal: ${msg}. This is most likely a bug in your test.`);
return core.Exception.new(dart.str`Fatal: ${msg}. This is most likely a bug in your test.`);
};
dart.fn(async_helper._buildException, core.Exception, [core.String]);
async_helper.asyncTestInitialize = function(callback) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/dev_compiler/test/codegen/expect/closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export const closure = Object.create(null);
import { core, js, dart, dartx } from 'dart_sdk';
closure.generic_function = function(T) {
return (items: core.List<T>, seed: T): core.List<T> => {
let strings = items[dartx.map](core.String)(dart.fn((i: T): string => `${i}`, core.String, [T]))[dartx.toList]();
let strings = items[dartx.map](core.String)(dart.fn((i: T): string => dart.str`${i}`, core.String, [T]))[dartx.toList]();
return items;
};
};
Expand Down
2 changes: 1 addition & 1 deletion pkg/dev_compiler/test/codegen/expect/covariance.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dart_library.library('covariance', null, /* Imports */[
super.new();
}
add(x) {
core.print(`Bar.add got ${x}`);
core.print(dart.str`Bar.add got ${x}`);
super.add(x);
}
};
Expand Down
54 changes: 27 additions & 27 deletions pkg/dev_compiler/test/codegen/expect/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ dart_library.library('expect', null, /* Imports */[
let end = i;
let truncExpected = expect.Expect._truncateString(expected, start, end, 20);
let truncActual = expect.Expect._truncateString(actual, start, end, 20);
return `at index ${start}: Expected <${truncExpected}>, ` + `Found: <${truncActual}>`;
return dart.str`at index ${start}: Expected <${truncExpected}>, ` + dart.str`Found: <${truncActual}>`;
}
}
return null;
Expand All @@ -59,43 +59,43 @@ dart_library.library('expect', null, /* Imports */[
if (typeof expected == 'string' && typeof actual == 'string') {
let stringDifference = expect.Expect._stringDifference(expected, actual);
if (stringDifference != null) {
expect.Expect._fail(`Expect.equals(${stringDifference}${msg}) fails.`);
expect.Expect._fail(dart.str`Expect.equals(${stringDifference}${msg}) fails.`);
}
}
expect.Expect._fail(`Expect.equals(expected: <${expected}>, actual: <${actual}>${msg}) fails.`);
expect.Expect._fail(dart.str`Expect.equals(expected: <${expected}>, actual: <${actual}>${msg}) fails.`);
}
static isTrue(actual, reason) {
if (reason === void 0) reason = null;
if (dart.notNull(expect._identical(actual, true))) return;
let msg = expect.Expect._getMessage(reason);
expect.Expect._fail(`Expect.isTrue(${actual}${msg}) fails.`);
expect.Expect._fail(dart.str`Expect.isTrue(${actual}${msg}) fails.`);
}
static isFalse(actual, reason) {
if (reason === void 0) reason = null;
if (dart.notNull(expect._identical(actual, false))) return;
let msg = expect.Expect._getMessage(reason);
expect.Expect._fail(`Expect.isFalse(${actual}${msg}) fails.`);
expect.Expect._fail(dart.str`Expect.isFalse(${actual}${msg}) fails.`);
}
static isNull(actual, reason) {
if (reason === void 0) reason = null;
if (null == actual) return;
let msg = expect.Expect._getMessage(reason);
expect.Expect._fail(`Expect.isNull(actual: <${actual}>${msg}) fails.`);
expect.Expect._fail(dart.str`Expect.isNull(actual: <${actual}>${msg}) fails.`);
}
static isNotNull(actual, reason) {
if (reason === void 0) reason = null;
if (null != actual) return;
let msg = expect.Expect._getMessage(reason);
expect.Expect._fail(`Expect.isNotNull(actual: <${actual}>${msg}) fails.`);
expect.Expect._fail(dart.str`Expect.isNotNull(actual: <${actual}>${msg}) fails.`);
}
static identical(expected, actual, reason) {
if (reason === void 0) reason = null;
if (dart.notNull(expect._identical(expected, actual))) return;
let msg = expect.Expect._getMessage(reason);
expect.Expect._fail(`Expect.identical(expected: <${expected}>, actual: <${actual}>${msg}) ` + "fails.");
expect.Expect._fail(dart.str`Expect.identical(expected: <${expected}>, actual: <${actual}>${msg}) ` + "fails.");
}
static fail(msg) {
expect.Expect._fail(`Expect.fail('${msg}')`);
expect.Expect._fail(dart.str`Expect.fail('${msg}')`);
}
static approxEquals(expected, actual, tolerance, reason) {
if (tolerance === void 0) tolerance = null;
Expand All @@ -105,49 +105,49 @@ dart_library.library('expect', null, /* Imports */[
}
if (dart.notNull((dart.notNull(expected) - dart.notNull(actual))[dartx.abs]()) <= dart.notNull(tolerance)) return;
let msg = expect.Expect._getMessage(reason);
expect.Expect._fail(`Expect.approxEquals(expected:<${expected}>, actual:<${actual}>, ` + `tolerance:<${tolerance}>${msg}) fails`);
expect.Expect._fail(dart.str`Expect.approxEquals(expected:<${expected}>, actual:<${actual}>, ` + dart.str`tolerance:<${tolerance}>${msg}) fails`);
}
static notEquals(unexpected, actual, reason) {
if (reason === void 0) reason = null;
if (!dart.equals(unexpected, actual)) return;
let msg = expect.Expect._getMessage(reason);
expect.Expect._fail(`Expect.notEquals(unexpected: <${unexpected}>, actual:<${actual}>${msg}) ` + "fails.");
expect.Expect._fail(dart.str`Expect.notEquals(unexpected: <${unexpected}>, actual:<${actual}>${msg}) ` + "fails.");
}
static listEquals(expected, actual, reason) {
if (reason === void 0) reason = null;
let msg = expect.Expect._getMessage(reason);
let n = dart.notNull(expected[dartx.length]) < dart.notNull(actual[dartx.length]) ? expected[dartx.length] : actual[dartx.length];
for (let i = 0; i < dart.notNull(n); i++) {
if (!dart.equals(expected[dartx.get](i), actual[dartx.get](i))) {
expect.Expect._fail(`Expect.listEquals(at index ${i}, ` + `expected: <${expected[dartx.get](i)}>, actual: <${actual[dartx.get](i)}>${msg}) fails`);
expect.Expect._fail(dart.str`Expect.listEquals(at index ${i}, ` + dart.str`expected: <${expected[dartx.get](i)}>, actual: <${actual[dartx.get](i)}>${msg}) fails`);
}
}
if (expected[dartx.length] != actual[dartx.length]) {
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)}>`);
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)}>`);
}
}
static mapEquals(expected, actual, reason) {
if (reason === void 0) reason = null;
let msg = expect.Expect._getMessage(reason);
for (let key of expected[dartx.keys]) {
if (!dart.notNull(actual[dartx.containsKey](key))) {
expect.Expect._fail(`Expect.mapEquals(missing expected key: <${key}>${msg}) fails`);
expect.Expect._fail(dart.str`Expect.mapEquals(missing expected key: <${key}>${msg}) fails`);
}
expect.Expect.equals(expected[dartx.get](key), actual[dartx.get](key));
}
for (let key of actual[dartx.keys]) {
if (!dart.notNull(expected[dartx.containsKey](key))) {
expect.Expect._fail(`Expect.mapEquals(unexpected key: <${key}>${msg}) fails`);
expect.Expect._fail(dart.str`Expect.mapEquals(unexpected key: <${key}>${msg}) fails`);
}
}
}
static stringEquals(expected, actual, reason) {
if (reason === void 0) reason = null;
if (expected == actual) return;
let msg = expect.Expect._getMessage(reason);
let defaultMessage = `Expect.stringEquals(expected: <${expected}>", <${actual}>${msg}) fails`;
let defaultMessage = dart.str`Expect.stringEquals(expected: <${expected}>", <${actual}>${msg}) fails`;
if (expected == null || actual == null) {
expect.Expect._fail(`${defaultMessage}`);
expect.Expect._fail(dart.str`${defaultMessage}`);
}
let left = 0;
let right = 0;
Expand Down Expand Up @@ -182,8 +182,8 @@ dart_library.library('expect', null, /* Imports */[
let rightTail = "...";
if (left <= 10) leftLead = "";
if (right <= 10) rightTail = "";
let diff = `\nDiff (${left}..${dart.notNull(eLen) - right}/${dart.notNull(aLen) - right}):\n` + `${leftLead}${leftSnippet}[ ${eSnippet} ]${rightSnippet}${rightTail}\n` + `${leftLead}${leftSnippet}[ ${aSnippet} ]${rightSnippet}${rightTail}`;
expect.Expect._fail(`${defaultMessage}${diff}`);
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}`;
expect.Expect._fail(dart.str`${defaultMessage}${diff}`);
}
static setEquals(expected, actual, reason) {
if (reason === void 0) reason = null;
Expand All @@ -193,44 +193,44 @@ dart_library.library('expect', null, /* Imports */[
extraSet.removeAll(expected);
if (dart.notNull(extraSet.isEmpty) && dart.notNull(missingSet.isEmpty)) return;
let msg = expect.Expect._getMessage(reason);
let sb = new core.StringBuffer(`Expect.setEquals(${msg}) fails`);
let sb = new core.StringBuffer(dart.str`Expect.setEquals(${msg}) fails`);
if (!dart.notNull(missingSet.isEmpty)) {
sb.write('\nExpected collection does not contain: ');
}
for (let val of missingSet) {
sb.write(`${val} `);
sb.write(dart.str`${val} `);
}
if (!dart.notNull(extraSet.isEmpty)) {
sb.write('\nExpected collection should not contain: ');
}
for (let val of extraSet) {
sb.write(`${val} `);
sb.write(dart.str`${val} `);
}
expect.Expect._fail(sb.toString());
}
static throws(f, check, reason) {
if (check === void 0) check = null;
if (reason === void 0) reason = null;
let msg = reason == null ? "" : `(${reason})`;
let msg = reason == null ? "" : dart.str`(${reason})`;
if (!dart.is(f, expect._Nullary)) {
expect.Expect._fail(`Expect.throws${msg}: Function f not callable with zero arguments`);
expect.Expect._fail(dart.str`Expect.throws${msg}: Function f not callable with zero arguments`);
}
try {
f();
} catch (e) {
let s = dart.stackTrace(e);
if (check != null) {
if (!dart.notNull(dart.dcall(check, e))) {
expect.Expect._fail(`Expect.throws${msg}: Unexpected '${e}'\n${s}`);
expect.Expect._fail(dart.str`Expect.throws${msg}: Unexpected '${e}'\n${s}`);
}
}
return;
}

expect.Expect._fail(`Expect.throws${msg} fails: Did not throw`);
expect.Expect._fail(dart.str`Expect.throws${msg} fails: Did not throw`);
}
static _getMessage(reason) {
return reason == null ? "" : `, '${reason}'`;
return reason == null ? "" : dart.str`, '${reason}'`;
}
static _fail(message) {
dart.throw(new expect.ExpectException(message));
Expand Down
Loading

0 comments on commit d2b6f53

Please sign in to comment.