dart-sdk/tests/language/function_subtype/bound_closure7_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

38 lines
1.1 KiB
Dart

// Copyright (c) 2013, 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 test program for constructors and initializers.
// Check function subtyping for bound closures.
import 'package:expect/expect.dart';
import "package:expect/variations.dart" as v;
typedef Foo<T>(T t);
class Class<T> {
foo(Foo<T> o) => o is Foo<T>;
}
bar(int i) {}
baz<T>(Foo<T> o) => o is Foo<T>;
void main() {
dynamic f = new Class<int>().foo;
Expect.isTrue(f(bar));
Expect.isTrue(f is Foo<Foo<int>>);
Expect.isFalse(f is Foo<int>);
Expect.isFalse(f is Foo<Object>);
Expect.throwsTypeErrorWhen(v.checkedParameters, () => f(f));
Expect.throwsTypeErrorWhen(v.checkedParameters, () => f(42));
Foo<Foo<int>> bazInt = baz; // implicit instantiation baz<int>
f = bazInt;
Expect.isTrue(f(bar));
Expect.isFalse(f is Foo<int>);
Expect.throwsTypeErrorWhen(v.checkedParameters, () => f(f));
Expect.throwsTypeErrorWhen(v.checkedParameters, () => f(42));
}