Skip to content

Commit

Permalink
dart-lang#2420. Add null-assert pattern exhaustiveness tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrekhov committed Jan 8, 2024
1 parent 86b168a commit a671dcf
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (c) 2024, 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.

/// @assertion Null-assert pattern is exhausted if a of the subpattern and a
/// type Null is exhausted.
///
/// @description Check that a null-assert pattern is exhausted if a of the
/// subpattern and a type `Null` is exhausted. Test switch statement
/// @author sgrekhov22@gmail.com
import "../../Utils/expect.dart";

extension type const BoolET1(bool _) {}
extension type const BoolET2(bool _) implements bool {}

String test1(bool? o) {
switch (o) {
case BoolET1 _!:
return "exhaustive";
}
}

String test2(bool? o) {
switch (o) {
case BoolET2 _!:
return "exhaustive";
}
}

String test3(BoolET1? o) {
switch (o) {
case bool _!:
return "exhaustive";
}
}

String test4(BoolET2? o) {
switch (o) {
case bool _!:
return "exhaustive";
}
}

String test5(BoolET1? o) {
switch (o) {
case BoolET1 _!:
return "exhaustive";
}
}

String test6(BoolET2? o) {
switch (o) {
case BoolET2 _!:
return "exhaustive";
}
}

String test7(BoolET1? o) {
switch (o) {
case BoolET2 _!:
return "exhaustive";
}
}

String test8(BoolET2? o) {
switch (o) {
case BoolET1 _!:
return "exhaustive";
}
}

main() {
Expect.equals("exhaustive" ,test1(true));
Expect.equals("exhaustive" ,test2(false));
Expect.equals("exhaustive" ,test3(BoolET1(true)));
Expect.equals("exhaustive" ,test4(BoolET2(false)));
Expect.equals("exhaustive" ,test5(BoolET1(true)));
Expect.equals("exhaustive" ,test6(BoolET2(false)));
Expect.equals("exhaustive" ,test7(BoolET1(true)));
Expect.equals("exhaustive" ,test8(BoolET2(false)));

Expect.throws(() {
test1(null);
});
Expect.throws(() {
test2(null);
});
Expect.throws(() {
test3(null);
});
Expect.throws(() {
test4(null);
});
Expect.throws(() {
test5(null);
});
Expect.throws(() {
test6(null);
});
Expect.throws(() {
test7(null);
});
Expect.throws(() {
test8(null);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) 2024, 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.

/// @assertion Null-assert pattern is exhausted if a of the subpattern and a
/// type Null is exhausted.
///
/// @description Check that a null-assert pattern is exhausted if a of the
/// subpattern and a type `Null` is exhausted. Test switch expression
/// @author sgrekhov22@gmail.com
import "../../Utils/expect.dart";

extension type const ObjectET1(Object _) {}
extension type const ObjectET2(Object _) implements Object {}

String test1(Object? o) => switch (o) {
ObjectET1 _! => "exhaustive"
};

String test2(Object? o) => switch (o) {
ObjectET2 _! => "exhaustive"
};

String test3(ObjectET1? o) => switch (o) {
Object _! => "exhaustive"
};

String test4(ObjectET2? o) => switch (o) {
Object _! => "exhaustive"
};

String test5(ObjectET1? o) => switch (o) {
ObjectET1 _! => "exhaustive"
};

String test6(ObjectET2? o) => switch (o) {
ObjectET2 _! => "exhaustive"
};

String test7(ObjectET1? o) => switch (o) {
ObjectET2 _! => "exhaustive"
};

String test8(ObjectET2? o) => switch (o) {
ObjectET1 _! => "exhaustive"
};

main() {
Expect.equals("exhaustive" ,test1(true));
Expect.equals("exhaustive" ,test2(42));
Expect.equals("exhaustive" ,test3(ObjectET1(true)));
Expect.equals("exhaustive" ,test4(ObjectET2(42)));
Expect.equals("exhaustive" ,test5(ObjectET1(true)));
Expect.equals("exhaustive" ,test6(ObjectET2(42)));
Expect.equals("exhaustive" ,test7(ObjectET1(true)));
Expect.equals("exhaustive" ,test8(ObjectET2(42)));

Expect.throws(() {
test1(null);
});
Expect.throws(() {
test2(null);
});
Expect.throws(() {
test3(null);
});
Expect.throws(() {
test4(null);
});
Expect.throws(() {
test5(null);
});
Expect.throws(() {
test6(null);
});
Expect.throws(() {
test7(null);
});
Expect.throws(() {
test8(null);
});
}

0 comments on commit a671dcf

Please sign in to comment.