dart-sdk/tests/corelib_2/string_interpolation_error_test.dart
Stephen Adams c74dd07a4a Use interpolation in print and writeAll
Using interpolation ensures that a `null` returned from `o.toString()`
in a non-sound application is treated consistently.

The error message has been upgraded to include the receiver to make
it easier to find the method. Since this error values are reported using
`Error.safeToString`, this will be reported using the class name:

```
ArgumentError (object): toString method returned 'null': Instance of 'BadToString'
```

VM, DDC and dart2js runtimes are updated to make the handling consistent
on VM and web.

TEST=added

Change-Id: I48d8f721dfb3a431dbeacdaa39a9f3a8273e902c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/214741
Commit-Queue: Stephen Adams <sra@google.com>
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
2021-09-29 02:45:32 +00:00

31 lines
727 B
Dart

// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// 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.
// @dart = 2.9
import "package:expect/expect.dart";
class BadToString {
@override
String toString() => null;
}
void test(expected, object) {
var message = '';
if (expected == null) {
Expect.throws(() => '$object',
(error) => '$error'.contains("toString method returned 'null'"));
} else {
Expect.equals(expected, '$object');
}
}
void main() {
test("123", 123);
test("null", null);
test(null, BadToString());
test(null, [BadToString()]);
test(null, {BadToString()});
}