dart-sdk/tests/dartdevc/cast_error/library_uri_in_message_test.dart
Nicholas Shahan 4e25e499c8 [ddc] Add library URIs to cast failure messages
When the two types have the same name but are from different libraries
showing the library URI will help users understand the failure better.

Change-Id: I5ab4412e676272111d41f688ef2d1cc83afbe627
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/194116
Reviewed-by: Mark Zhou <markzipan@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
Commit-Queue: Nicholas Shahan <nshahan@google.com>
2021-04-09 22:24:43 +00:00

67 lines
2.1 KiB
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.
import "../utils.dart";
import "lib_a.dart" as libA;
import "lib_b.dart" as libB;
void main() {
// When the name of the class is the same the error message should include
// library URIs for the two classes.
var a = libA.Animal();
try {
a as libB.Animal;
} on TypeError catch (error) {
var message = error.toString();
expectStringContains(
"Expected a value of type 'Animal' "
"(in org-dartlang-app:/tests/dartdevc/cast_error/lib_b.dart)",
message);
expectStringContains(
"but got one of type 'Animal' "
"(in org-dartlang-app:/tests/dartdevc/cast_error/lib_a.dart)",
message);
}
// Verify the libraries are properly ordered.
var b = libB.Animal();
try {
b as libA.Animal;
} on TypeError catch (error) {
var message = error.toString();
expectStringContains(
"Expected a value of type 'Animal' "
"(in org-dartlang-app:/tests/dartdevc/cast_error/lib_a.dart)",
message);
expectStringContains(
"but got one of type 'Animal' "
"(in org-dartlang-app:/tests/dartdevc/cast_error/lib_b.dart)",
message);
}
// Shows library URIs when one of the types is nullable.
try {
b as libA.Animal?;
} on TypeError catch (error) {
var message = error.toString();
expectStringContains(
"Expected a value of type 'Animal?' "
"(in org-dartlang-app:/tests/dartdevc/cast_error/lib_a.dart)",
message);
expectStringContains(
"but got one of type 'Animal' "
"(in org-dartlang-app:/tests/dartdevc/cast_error/lib_b.dart)",
message);
}
// URIs are not displayed when the class names are different.
try {
a as String;
} on TypeError catch (error) {
var message = error.toString();
expectStringContains(
"Expected a value of type 'String', but got one of type 'Animal'",
message);
}
}