Skip to content

Commit

Permalink
Rework inference_update_2 tests using expectStaticType.
Browse files Browse the repository at this point in the history
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 <paulberry@google.com>
Reviewed-by: Lasse Nielsen <lrn@google.com>
  • Loading branch information
stereotype441 authored and Commit Queue committed Dec 14, 2023
1 parent 56e81ec commit d6a47a4
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 179 deletions.
20 changes: 6 additions & 14 deletions tests/language/inference_update_2/abstract_field_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Exactly<int>>();
}
}

Expand All @@ -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<Exactly<int?>>();
}
}

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<Exactly<int>>();
}
}

Expand All @@ -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<Exactly<int?>>();
}
}

Expand Down
52 changes: 14 additions & 38 deletions tests/language/inference_update_2/basic_field_promotion_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

// SharedOptions=--enable-experiment=inference-update-2

import '../static_type_helper.dart';

abstract class C {
final int? _privateFinalField;
final int? publicFinalField;
Expand All @@ -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<Exactly<int>>();
}
}
}
Expand All @@ -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<Exactly<int>>();
}
}
}
Expand All @@ -49,87 +47,65 @@ 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<Exactly<int>>();
}
}

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<Exactly<int?>>();
}
}

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<Exactly<int?>>();
}
}

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<Exactly<int?>>();
}
}

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<Exactly<int>>();
}
}

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<Exactly<int?>>();
}
}

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<Exactly<int?>>();
}
}

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<Exactly<int?>>();
}
}

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<Exactly<int>>();
}
}

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<Exactly<int>>();
}
}

Expand Down
6 changes: 3 additions & 3 deletions tests/language/inference_update_2/disabled_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

// @dart=2.18

import '../static_type_helper.dart';

class C {
final int? _privateFinalField;

Expand All @@ -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<Exactly<int?>>();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

// SharedOptions=--enable-experiment=inference-update-2

import '../static_type_helper.dart';

abstract class C {
int? get _f;
}
Expand All @@ -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<Exactly<int>>();
}
}

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<Exactly<int>>();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Exactly<int>>();
}
}
}
Expand All @@ -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<Exactly<int>>();
}
}
}

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.
Expand All @@ -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<Exactly<int>>();
}
}

Expand All @@ -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<Exactly<int>>();
}
}

Expand Down
Loading

0 comments on commit d6a47a4

Please sign in to comment.