dart-sdk/tests/corelib/apply_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

81 lines
2.3 KiB
Dart

// Copyright (c) 2012, 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;
// Testing Function.apply calls correctly.
// This test is not testing error handling, only that correct parameters
// cause a correct call.
int test0() => 42;
int test0a({required int a}) => 37 + a;
int test1(int i) => i + 1;
int test1a(int i, {required int a}) => i + a;
int test2(int i, int j) => i + j;
int test2a(int i, int j, {required int a}) => i + j + a;
class C {
int x = 10;
int foo(int y) => this.x + y;
}
class Callable {
int call(int x, int y) => x + y;
}
@pragma('dart2js:noInline')
@pragma('dart2js:assumeDynamic')
confuse(x) => x;
main() {
testMap(res, func, map) {
Expect.equals(res, Function.apply(func, null, map));
Expect.equals(res, Function.apply(func, [], map));
}
testList(res, func, list) {
Expect.equals(res, Function.apply(func, list));
Expect.equals(res, Function.apply(func, list, null));
Expect.equals(res, Function.apply(func, list, new Map<Symbol, dynamic>()));
}
testListTyped(res, Function func, list) => testList(res, func, list);
test(res, func, list, map) {
Expect.equals(res, Function.apply(func, list, map));
}
testList(42, test0, null);
testList(42, test0, []);
testMap(42, test0a, {#a: 5});
testList(42, test1, [41]);
test(42, test1a, [20], {#a: 22});
testList(42, test2, [20, 22]);
test(42, test2a, [10, 15], {#a: 17});
// Test that "this" is correct when calling closurized functions.
var cfoo = new C().foo;
testList(42, cfoo, [32]);
// Test that apply works even with a different name.
var app = confuse(Function.apply);
Expect.equals(42, app(test2, [22, 20]));
// Test that apply can itself be applied.
Expect.equals(
42,
Function.apply(Function.apply, [
test2,
[17, 25]
]));
// Test that apply works on callable objects when it is passed to a method
// that expects Function (and not dynamic).
Expect.throwsWhen(
v.checkedImplicitDowncasts, () => testList(42, new Callable(), [13, 29]));
testListTyped(42, new Callable(), [13, 29]);
}