dart-sdk/tests/language/covariant/field_test.dart
Sigmund Cherem 050afd650f [expect] introduce Expect.throwsWhen and Expect.throwsTypeErrorWhen
Today, most tests that touch on a behavior variation end up
skipping expectations or the entirety of a test for some
testing configurations.  Moving forward, we'd like skip less
and try to account for the behavior variations if that's
reasonable.

This CL shows an approach to improve our test coverage for
behavior variations. We introduce two new methods to
[Expect] that allow us to conditionally check that a
function throws, depending on variation predicates.

The CL changes expectations for errors that don't occur
when dart2js omits parameter type checks or implicit
downcasts.

Note: originally I had the intention to introduce a name
parameter to `Expect.throws` and `Expect.throwsTypeError` to
avoid introducing a new API. However, because these APIs are
used for testing core language features, such as function
parameters themselves, we decided to keep the use of
features in these APIs as simple as it can be.

CoreLibraryReviewExempt: no public library semantic change - only improving test coverage under variations
Change-Id: I531657622655778491eaca8b37ba69ffaab559fc
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/351340
Reviewed-by: Lasse Nielsen <lrn@google.com>
Commit-Queue: Sigmund Cherem <sigmund@google.com>
2024-02-14 20:12:05 +00:00

60 lines
1.9 KiB
Dart

// Copyright (c) 2017, 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 "package:expect/expect.dart";
import "package:expect/variations.dart" as v;
class A {}
abstract class B<T> {
// x will be marked genericCovariantInterface, since x's type is covariant in
// the type parameter T.
void set s2(T x);
// x will be marked genericCovariantInterface, since x's type is covariant in
// the type parameter T.
void set s3(T x);
void set s4(Object? x);
void set s5(Object? x) {
s4 = x;
}
}
class C extends B<A> {
A? s1;
// s2 will be marked genericCovariantImpl, since it might be called via
// e.g. B<Object>.
A? s2;
// s3 will be marked genericCovariantImpl, since it might be called via
// e.g. B<Object>.
covariant A? s3;
covariant A? s4;
}
main() {
// Dynamic method calls should always have their arguments type checked.
dynamic d = new C();
Expect.throwsTypeErrorWhen(v.checkedParameters, () => d.s1 = new Object());
// Interface calls should have any arguments marked "genericCovariantImpl"
// type checked provided that the corresponding argument on the interface
// target is marked "genericCovariantInterface".
B<Object> b = new C();
Expect.throwsTypeErrorWhen(v.checkedParameters, () => b.s2 = new Object());
// Interface calls should have any arguments marked "covariant" type checked,
// regardless of whether the corresponding argument on the interface target is
// marked "genericCovariantInterface".
Expect.throwsTypeErrorWhen(v.checkedParameters, () => b.s3 = new Object());
Expect.throwsTypeErrorWhen(v.checkedParameters, () => b.s4 = new Object());
// This calls should have any arguments marked "covariant" type checked.
Expect.throwsTypeErrorWhen(v.checkedParameters, () => b.s5 = new Object());
}