mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 01:30:32 +00:00
4e25e499c8
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>
67 lines
2.1 KiB
Dart
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);
|
|
}
|
|
}
|