Fix language_2/string_interpolation_and_buffer_test.

It was badly converted to Dart 2, and was both using and testing things
that can no longer happen (like returning a non-null non-String value
from toString).

Change-Id: I085d1172f177e80c3f75af36a2028b7ea241713a
Reviewed-on: https://dart-review.googlesource.com/71144
Reviewed-by: Kevin Millikin <kmillikin@google.com>
Commit-Queue: Lasse R.H. Nielsen <lrn@google.com>
This commit is contained in:
Lasse R.H. Nielsen 2018-08-22 10:35:14 +00:00 committed by commit-bot@chromium.org
parent 8c30475728
commit a6e14ae3e6
3 changed files with 58 additions and 70 deletions

View file

@ -718,7 +718,6 @@ stacktrace_demangle_ctors_test: RuntimeError # Issue 12698
stacktrace_rethrow_error_test/none: RuntimeError # Issue 12698
stacktrace_rethrow_error_test/withtraceparameter: RuntimeError # Issue 12698
stacktrace_rethrow_nonerror_test: RuntimeError # Issue 12698
string_interpolation_and_buffer_test: RuntimeError
string_split_test: CompileTimeError
string_supertype_checked_test: CompileTimeError
super_bound_closure_test/none: CompileTimeError

View file

@ -510,7 +510,6 @@ stack_overflow_stacktrace_test: RuntimeError # Issue 29920; RangeError: Maximum
stack_overflow_test: RuntimeError # Issue 29920; RangeError: Maximum call stack size exceeded
stacktrace_demangle_ctors_test: RuntimeError # Issue 31089; Expect.isTrue(false) fails.
stacktrace_test: RuntimeError # Issue 29920; Expect.isTrue(false) fails.
string_interpolation_and_buffer_test: RuntimeError # NoSuchMethodError: method not found: '<Unexpected Null Value>'
string_literals_test: RuntimeError # Expect.equals(expected: <\x00\x0A\x0D\x7F\xFF\u{FFFF}\u{D800}\u{DC00}\u{DBFF}\u{DFFF}>, actual: <\x00\x0A\x0D\x7F\xFF\u{FFFF}\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}\u{FFFD}>) fails.
super_test: RuntimeError # Expect.equals(expected: <0>, actual: <2>) fails.
switch_label2_test: RuntimeError # Issue 29920; UnimplementedError: node <ShadowContinueSwitchStatement> see https://github.com/dart-lang/sdk/issues/29352 `continue #L1;

View file

@ -2,77 +2,67 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Test to ensure that StringBuffer and string interpolation behaves
// the same and fail fast.
// Interpolation calls `toString`.
// The evaluation of the interpolation fails if `toString` throws or returns
// null. In Dart 2, any method overriding `Object.toString` must return a
// `String` or `null`. In particular, if `object.toString()` returns null, then
// `"$object"` must not evaluate to the string `"null"`.
//
// The specification states that the expression of an interpolation is
// evaluated as follows:
//
// 1. Evaluate $e_i$ to an object $o_i$.
// 2. Invoke the `toString` method on *o<sub>i</sub>* with no arguments,
// and let *r<sub>i</sub>*$ be the returned value.
// 3. If *r<sub>i</sub>* is not an instance of the built-in type `String`,
// throw an `Error`.
//
// (Then the resulting strings are concatenated with the literal string parts).
//
//
// Adding an object to a `StringBuffer` behaves the same as evaluating
// an expression in an interpolation. It must immediately fail if the
// object's toString throws or returns `null`.
//
// This ensures that implementing interpolation via a `StringBuffer`is
// a valid implementation choice.
import "package:expect/expect.dart";
class ToStringWrapper {
final value;
ToStringWrapper(this.value);
toString() => value;
class ToStringString {
String toString() => "String";
}
wrap(value) => new ToStringWrapper(value);
main() {
interpolate(object) {
var result;
try {
result = '${wrap(object)}';
} on ArgumentError {
return 'Error';
}
Expect.isTrue(result is String);
return 'Success';
}
buffer(object) {
var sb;
try {
sb = new StringBuffer()..write(wrap(object));
} on ArgumentError {
return 'Error';
}
if (object == null) {
Expect.isTrue(sb.toString() is String);
}
return 'Success';
}
initBuffer(object) {
var sb;
try {
sb = new StringBuffer(wrap(object));
} on ArgumentError {
return 'Error';
}
if (object == null) {
Expect.isTrue(sb.toString() is String);
}
return 'Success';
}
Expect.equals('Error', interpolate(null));
Expect.equals('Success', interpolate(""));
Expect.equals('Success', interpolate("string"));
Expect.equals('Error', interpolate([]));
Expect.equals('Error', interpolate([1]));
Expect.equals('Error', interpolate(new Object()));
Expect.equals('Error', buffer(null));
Expect.equals('Success', buffer(""));
Expect.equals('Success', buffer("string"));
Expect.equals('Error', buffer([]));
Expect.equals('Error', buffer([1]));
Expect.equals('Error', buffer(new Object()));
Expect.equals('Error', initBuffer(null));
Expect.equals('Success', initBuffer(""));
Expect.equals('Success', initBuffer("string"));
Expect.equals('Error', initBuffer([]));
Expect.equals('Error', initBuffer([1]));
Expect.equals('Error', initBuffer(new Object()));
class ToStringNull {
String toString() => null;
}
class ToStringThrows {
String toString() => throw "Throw";
}
void main() {
var s = ToStringString();
var n = ToStringNull();
var t = ToStringThrows();
Expect.equals("$s$s", "StringString");
// Throws immediately when evaluating the first interpolated expression.
Expect.throws<String>(() => "$t${throw "unreachable"}", (e) => e == "Throw");
Expect.throws<Error>(() => "$n${throw "unreachable"}");
// Throws immediately when adding object that doesn't return a String.
Expect.equals(
(StringBuffer()..write(s)..write(s)).toString(), "StringString");
Expect.throws<String>(
() => StringBuffer()..write(t)..write(throw "unreachable"),
(e) => e == "Throw");
Expect.throws<Error>(
() => StringBuffer()..write(n)..write(throw "unreachable"));
// Same behavior for constructor argument as if adding it to buffer later.
Expect.equals((StringBuffer(s)..write(s)).toString(), "StringString");
Expect.throws<String>(
() => StringBuffer(t)..write(throw "unreachable"), (e) => e == "Throw");
Expect.throws<Error>(() => StringBuffer(n)..write(throw "unreachable"));
}