From d6a47a4a23804ceede9982f9c80ce0a057934529 Mon Sep 17 00:00:00 2001 From: Paul Berry Date: Thu, 14 Dec 2023 14:00:35 +0000 Subject: [PATCH] Rework inference_update_2 tests using expectStaticType. Using `expectStaticType` is a more effective test, because it verifies that the type is _exactly_ what is expected. It also makes the test easier to read. Change-Id: I4b0a22f32601a5dca830a182ff4d37cddd657776 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/340580 Commit-Queue: Paul Berry Reviewed-by: Lasse Nielsen --- .../abstract_field_test.dart | 20 ++----- .../basic_field_promotion_test.dart | 52 +++++----------- .../inference_update_2/disabled_test.dart | 6 +- .../do_promote_abstract_getters_test.dart | 12 ++-- .../field_invocation_promotion_test.dart | 24 ++++---- ...eld_promotion_and_no_such_method_test.dart | 44 ++++---------- .../field_promotion_name_conflicts_test.dart | 60 +++++-------------- .../top_level_type_inference_test.dart | 36 ++++------- 8 files changed, 75 insertions(+), 179 deletions(-) diff --git a/tests/language/inference_update_2/abstract_field_test.dart b/tests/language/inference_update_2/abstract_field_test.dart index ecb723fe51ef..60d24a1c4332 100644 --- a/tests/language/inference_update_2/abstract_field_test.dart +++ b/tests/language/inference_update_2/abstract_field_test.dart @@ -6,6 +6,8 @@ // SharedOptions=--enable-experiment=inference-update-2 +import '../static_type_helper.dart'; + abstract class C { abstract final int? _f1; abstract int? _f2; @@ -20,13 +22,9 @@ class D { _f2 = i; } -void acceptsInt(int x) {} - void testAbstractFinalFieldIsPromotable(C c) { if (c._f1 != null) { - var x = c._f1; - // `x` has type `int` so this is ok - acceptsInt(x); + c._f1.expectStaticType>(); } } @@ -37,17 +35,13 @@ void testAbstractNonFinalFieldIsNotPromotable(C c) { // we might as well prevent promotion even in the absence of an // implementation. if (c._f2 != null) { - var x = c._f2; - // `x` has type `int?` so this is ok - x = null; + c._f2.expectStaticType>(); } } void testAbstractFinalFieldDoesNotBlockPromotionElsewhere(D d) { if (d._f1 != null) { - var x = d._f1; - // `x` has type `int` so this is ok - acceptsInt(x); + d._f1.expectStaticType>(); } } @@ -58,9 +52,7 @@ void testAbstractNonFinalFieldBlocksPromotionElsewhere(D d) { // promotion. So we might as well block promotion even in the absence of an // implementation. if (d._f2 != null) { - var x = d._f2; - // `x` has type `int?` so this is ok - x = null; + d._f2.expectStaticType>(); } } diff --git a/tests/language/inference_update_2/basic_field_promotion_test.dart b/tests/language/inference_update_2/basic_field_promotion_test.dart index 761b7daf996e..6f2aa41f0ac4 100644 --- a/tests/language/inference_update_2/basic_field_promotion_test.dart +++ b/tests/language/inference_update_2/basic_field_promotion_test.dart @@ -6,6 +6,8 @@ // SharedOptions=--enable-experiment=inference-update-2 +import '../static_type_helper.dart'; + abstract class C { final int? _privateFinalField; final int? publicFinalField; @@ -22,9 +24,7 @@ abstract class C { testPrivateFinalFieldThisAccess() { if (_privateFinalField != null) { - var x = _privateFinalField; - // `x` has type `int` so this is ok - acceptsInt(x); + _privateFinalField.expectStaticType>(); } } } @@ -34,9 +34,7 @@ abstract class D extends C { testPrivateFinalFieldSuperAccess() { if (super._privateFinalField != null) { - var x = super._privateFinalField; - // `x` has type `int` so this is ok - acceptsInt(x); + super._privateFinalField.expectStaticType>(); } } } @@ -49,77 +47,57 @@ enum E { const E(this._privateFinalFieldInEnum); } -void acceptsInt(int x) {} - void testPrivateFinalField(C c) { if (c._privateFinalField != null) { - var x = c._privateFinalField; - // `x` has type `int` so this is ok - acceptsInt(x); + c._privateFinalField.expectStaticType>(); } } void testPublicFinalField(C c) { if (c.publicFinalField != null) { - var x = c.publicFinalField; - // `x` has type `int?` so this is ok - x = null; + c.publicFinalField.expectStaticType>(); } } void testPrivateField(C c) { if (c._privateField != null) { - var x = c._privateField; - // `x` has type `int?` so this is ok - x = null; + c._privateField.expectStaticType>(); } } void testPublicField(C c) { if (c.publicField != null) { - var x = c.publicField; - // `x` has type `int?` so this is ok - x = null; + c.publicField.expectStaticType>(); } } void testPrivateAbstractGetter(C c) { if (c._privateAbstractGetter != null) { - var x = c._privateAbstractGetter; - // `x` has type `int` so this is ok - acceptsInt(x); + c._privateAbstractGetter.expectStaticType>(); } } void testPublicAbstractGetter(C c) { if (c.publicAbstractGetter != null) { - var x = c.publicAbstractGetter; - // `x` has type `int?` so this is ok - x = null; + c.publicAbstractGetter.expectStaticType>(); } } void testPrivateConcreteGetter(C c) { if (c._privateConcreteGetter != null) { - var x = c._privateConcreteGetter; - // `x` has type `int?` so this is ok - x = null; + c._privateConcreteGetter.expectStaticType>(); } } void testPublicConcreteGetter(C c) { if (c.publicConcreteGetter != null) { - var x = c.publicConcreteGetter; - // `x` has type `int?` so this is ok - x = null; + c.publicConcreteGetter.expectStaticType>(); } } void testPrivateFinalFieldInEnum(E e) { if (e._privateFinalFieldInEnum != null) { - var x = e._privateFinalFieldInEnum; - // `x` has type `int` so this is ok - acceptsInt(x); + e._privateFinalFieldInEnum.expectStaticType>(); } } @@ -127,9 +105,7 @@ void testPrivateFinalFieldGeneralPropertyAccess(C c) { // The analyzer uses a special data structure for `IDENTIFIER.IDENTIFIER`, so // we need to test the general case of property accesses as well. if ((c)._privateFinalField != null) { - var x = (c)._privateFinalField; - // `x` has type `int` so this is ok - acceptsInt(x); + (c)._privateFinalField.expectStaticType>(); } } diff --git a/tests/language/inference_update_2/disabled_test.dart b/tests/language/inference_update_2/disabled_test.dart index 6467fba2381c..2e946f67843b 100644 --- a/tests/language/inference_update_2/disabled_test.dart +++ b/tests/language/inference_update_2/disabled_test.dart @@ -6,6 +6,8 @@ // @dart=2.18 +import '../static_type_helper.dart'; + class C { final int? _privateFinalField; @@ -14,9 +16,7 @@ class C { void testPrivateFinalField(C c) { if (c._privateFinalField != null) { - var x = c._privateFinalField; - // `x` has type `int?` so this is ok - x = null; + c._privateFinalField.expectStaticType>(); } } diff --git a/tests/language/inference_update_2/do_promote_abstract_getters_test.dart b/tests/language/inference_update_2/do_promote_abstract_getters_test.dart index 5231182f2a90..5ed3757a7263 100644 --- a/tests/language/inference_update_2/do_promote_abstract_getters_test.dart +++ b/tests/language/inference_update_2/do_promote_abstract_getters_test.dart @@ -9,6 +9,8 @@ // SharedOptions=--enable-experiment=inference-update-2 +import '../static_type_helper.dart'; + abstract class C { int? get _f; } @@ -19,21 +21,15 @@ class D extends C { D(this._f); } -void acceptsInt(int x) {} - void testBaseClass(C c) { if (c._f != null) { - var x = c._f; - // `x` has type `int` so this is ok - acceptsInt(x); + c._f.expectStaticType>(); } } void testDerivedClass(D d) { if (d._f != null) { - var x = d._f; - // `x` has type `int` so this is ok - acceptsInt(x); + d._f.expectStaticType>(); } } diff --git a/tests/language/inference_update_2/field_invocation_promotion_test.dart b/tests/language/inference_update_2/field_invocation_promotion_test.dart index 8c94f2f44d6d..e70f9968fc90 100644 --- a/tests/language/inference_update_2/field_invocation_promotion_test.dart +++ b/tests/language/inference_update_2/field_invocation_promotion_test.dart @@ -12,6 +12,8 @@ // SharedOptions=--enable-experiment=inference-update-2 +import '../static_type_helper.dart'; + class C { final void Function()? _nullablePrivateFunction; final int? Function() _privateFunctionWithNullableReturnType; @@ -27,9 +29,7 @@ class C { void testPrivateFunctionWithNullableReturnTypeThisAccess() { if (_privateFunctionWithNullableReturnType is int Function()) { - var x = _privateFunctionWithNullableReturnType(); - // `x` has type `int` so this is ok - acceptsInt(x); + _privateFunctionWithNullableReturnType().expectStaticType>(); } } } @@ -48,15 +48,13 @@ class D extends C { void testPrivateFunctionWithNullableReturnTypeSuperAccess() { if (super._privateFunctionWithNullableReturnType is int Function()) { - var x = super._privateFunctionWithNullableReturnType(); - // `x` has type `int` so this is ok - acceptsInt(x); + super + ._privateFunctionWithNullableReturnType() + .expectStaticType>(); } } } -void acceptsInt(int x) {} - void testNullablePrivateFunction(C c) { if (c._nullablePrivateFunction != null) { // `c._nullablePrivateFunction` has been shown to be non-null so this is ok. @@ -66,9 +64,7 @@ void testNullablePrivateFunction(C c) { void testPrivateFunctionWithNullableReturnType(C c) { if (c._privateFunctionWithNullableReturnType is int Function()) { - var x = c._privateFunctionWithNullableReturnType(); - // `x` has type `int` so this is ok - acceptsInt(x); + c._privateFunctionWithNullableReturnType().expectStaticType>(); } } @@ -86,9 +82,9 @@ void testPrivateFunctionWithNullableReturnTypeGeneralPropertyAccess(C c) { // The analyzer uses a special data structure for `IDENTIFIER.IDENTIFIER`, so // we need to test the general case of property accesses as well. if ((c)._privateFunctionWithNullableReturnType is int Function()) { - var x = (c)._privateFunctionWithNullableReturnType(); - // `x` has type `int` so this is ok - acceptsInt(x); + (c) + ._privateFunctionWithNullableReturnType() + .expectStaticType>(); } } diff --git a/tests/language/inference_update_2/field_promotion_and_no_such_method_test.dart b/tests/language/inference_update_2/field_promotion_and_no_such_method_test.dart index 72ba6fe4695c..7dfc6608bae3 100644 --- a/tests/language/inference_update_2/field_promotion_and_no_such_method_test.dart +++ b/tests/language/inference_update_2/field_promotion_and_no_such_method_test.dart @@ -7,6 +7,8 @@ // SharedOptions=--enable-experiment=inference-update-2 +import '../static_type_helper.dart'; + import 'field_promotion_and_no_such_method_lib.dart' as otherLib; class C { @@ -131,85 +133,63 @@ mixin M4 implements K { dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); } -void acceptsInt(int x) {} - void testConflictsWithNoSuchMethodForwarder(C c) { if (c._f1 != null) { - var x = c._f1; - // `x` has type `int?` so this is ok - x = null; + c._f1.expectStaticType>(); } } void testNoConflictWithNoSuchMethodForwarderForDifferentLib(C c) { if (c._f2 != null) { - var x = c._f2; - // `x` has type `int` so this is ok - acceptsInt(x); + c._f2.expectStaticType>(); } } void testConflictsWithNoSuchMethodForwarderViaClassTypeAlias(C c) { if (c._f3 != null) { - var x = c._f3; - // `x` has type `int?` so this is ok - x = null; + c._f3.expectStaticType>(); } } void testNoConflictWithNoSuchMethodForwarderIfImplementedInMixin(C c) { if (c._f4 != null) { - var x = c._f4; - // `x` has type `int` so this is ok - acceptsInt(x); + c._f4.expectStaticType>(); } } void testNoConflictWithNoSuchMethodForwarderIfImplementedInSuperclass(C c) { if (c._f5 != null) { - var x = c._f5; - // `x` has type `int` so this is ok - acceptsInt(x); + c._f5.expectStaticType>(); } } void testConflictsWithNoSuchMethodForwarderInEnum(C c) { if (c._f6 != null) { - var x = c._f6; - // `x` has type `int?` so this is ok - x = null; + c._f6.expectStaticType>(); } } void testConflictsWithNoSuchMethodForwarderThroughInheritedInterface(C c) { if (c._f7 != null) { - var x = c._f7; - // `x` has type `int?` so this is ok - x = null; + c._f7.expectStaticType>(); } } void testConflictsWithNoSuchMethodForwarderThroughMixedInInterface(C c) { if (c._f8 != null) { - var x = c._f8; - // `x` has type `int?` so this is ok - x = null; + c._f8.expectStaticType>(); } } void testNoConflictWithNoSuchMethodForwarderInUnusedMixin(C c) { if (c._f9 != null) { - var x = c._f9; - // `x` has type `int` so this is ok - acceptsInt(x); + c._f9.expectStaticType>(); } } void testConflictsWithNoSuchMethodForwarderBasedOnAbstractGetter(C c) { if (c._f10 != null) { - var x = c._f10; - // `x` has type `int?` so this is ok - x = null; + c._f10.expectStaticType>(); } } diff --git a/tests/language/inference_update_2/field_promotion_name_conflicts_test.dart b/tests/language/inference_update_2/field_promotion_name_conflicts_test.dart index d1b9bf9b70e5..115d2b6f4489 100644 --- a/tests/language/inference_update_2/field_promotion_name_conflicts_test.dart +++ b/tests/language/inference_update_2/field_promotion_name_conflicts_test.dart @@ -8,6 +8,8 @@ // SharedOptions=--enable-experiment=inference-update-2 +import '../static_type_helper.dart'; + part 'field_promotion_name_conflicts_part.dart'; class C { @@ -74,117 +76,87 @@ enum E { int? get _f14 => 0; } -void acceptsInt(int x) {} - void testFinalField(C c) { if (c._f1 != null) { - var x = c._f1; - // `x` has type `int` so this is ok - acceptsInt(x); + c._f1.expectStaticType>(); } } void testNonFinalField(C c) { if (c._f2 != null) { - var x = c._f2; - // `x` has type `int?` so this is ok - x = null; + c._f2.expectStaticType>(); } } void testAbstractGetter(C c) { if (c._f3 != null) { - var x = c._f3; - // `x` has type `int` so this is ok - acceptsInt(x); + c._f3.expectStaticType>(); } } void testConcreteGetter(C c) { if (c._f4 != null) { - var x = c._f4; - // `x` has type `int?` so this is ok - x = null; + c._f4.expectStaticType>(); } } void testSetter(C c) { if (c._f5 != null) { - var x = c._f5; - // `x` has type `int` so this is ok - acceptsInt(x); + c._f5.expectStaticType>(); } } void testStaticField(C c) { if (c._f6 != null) { - var x = c._f6; - // `x` has type `int` so this is ok - acceptsInt(x); + c._f6.expectStaticType>(); } } void testStaticGetter(C c) { if (c._f7 != null) { - var x = c._f7; - // `x` has type `int` so this is ok - acceptsInt(x); + c._f7.expectStaticType>(); } } void testTopLevelField(C c) { if (c._f8 != null) { - var x = c._f8; - // `x` has type `int` so this is ok - acceptsInt(x); + c._f8.expectStaticType>(); } } void testTopLevelGetter(C c) { if (c._f9 != null) { - var x = c._f9; - // `x` has type `int` so this is ok - acceptsInt(x); + c._f9.expectStaticType>(); } } void testExtensionGetter(C c) { if (c._f10 != null) { - var x = c._f10; - // `x` has type `int` so this is ok - acceptsInt(x); + c._f10.expectStaticType>(); } } void testGetterInPart(C c) { if (c._f11 != null) { - var x = c._f11; - // `x` has type `int?` so this is ok - x = null; + c._f11.expectStaticType>(); } } void testFieldInMixin(C c) { if (c._f12 != null) { - var x = c._f12; - // `x` has type `int?` so this is ok - x = null; + c._f12.expectStaticType>(); } } void testGetterInMixin(C c) { if (c._f13 != null) { - var x = c._f13; - // `x` has type `int?` so this is ok - x = null; + c._f13.expectStaticType>(); } } void testGetterInEnum(C c) { if (c._f14 != null) { - var x = c._f14; - // `x` has type `int?` so this is ok - x = null; + c._f14.expectStaticType>(); } } diff --git a/tests/language/inference_update_2/top_level_type_inference_test.dart b/tests/language/inference_update_2/top_level_type_inference_test.dart index 31a4e58ead40..130eba35c662 100644 --- a/tests/language/inference_update_2/top_level_type_inference_test.dart +++ b/tests/language/inference_update_2/top_level_type_inference_test.dart @@ -9,6 +9,8 @@ // SharedOptions=--enable-experiment=inference-update-2 +import '../static_type_helper.dart'; + class C { final int? _promotable; final int? _notPromotable; // due to D._notPromotable @@ -45,54 +47,36 @@ final topLevelPromotable = final topLevelNotPromotable = ((C c) => c._notPromotable != null ? c._notPromotable : 0)(new C(0)); -void acceptsInt(int x) {} - void testTopLevelPromotable() { - var x = topLevelPromotable; - // `x` has type `int` so this is ok - acceptsInt(x); + topLevelPromotable.expectStaticType>(); } void testTopLevelNotPromotable() { - var x = topLevelNotPromotable; - // `x` has type `int?` so this is ok - x = null; + topLevelNotPromotable.expectStaticType>(); } void testStaticPromotable() { - var x = C.staticPromotable; - // `x` has type `int` so this is ok - acceptsInt(x); + C.staticPromotable.expectStaticType>(); } void testStaticNotPromotable() { - var x = C.staticNotPromotable; - // `x` has type `int?` so this is ok - x = null; + C.staticNotPromotable.expectStaticType>(); } void testInstancePromotable(C c) { - var x = c.instancePromotable; - // `x` has type `int` so this is ok - acceptsInt(x); + c.instancePromotable.expectStaticType>(); } void testInstanceNotPromotable(C c) { - var x = c.instanceNotPromotable; - // `x` has type `int?` so this is ok - x = null; + c.instanceNotPromotable.expectStaticType>(); } void testInstancePromotableViaThis(C c) { - var x = c.instancePromotableViaThis; - // `x` has type `int` so this is ok - acceptsInt(x); + c.instancePromotableViaThis.expectStaticType>(); } void testInstanceNotPromotableViaThis(C c) { - var x = c.instanceNotPromotableViaThis; - // `x` has type `int?` so this is ok - x = null; + c.instanceNotPromotableViaThis.expectStaticType>(); } main() {}