forked from dart-lang/co19
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dart-lang#2420. Add extension types exhaustiveness tests. Maps (dart-…
…lang#2426) Add extension type exhaustiveness tests about map patterns.
- Loading branch information
Showing
5 changed files
with
230 additions
and
7 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
LanguageFeatures/Extension-types/exhaustiveness_map_A01_t01.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) 2023, 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 Exhaustiveness with map patterns can only be achieved when there | ||
/// is an exhaustive pattern in addition to any map patterns | ||
/// | ||
/// @description Check that a switch statement/expression with map patterns only | ||
/// cannot be exhaustive. Test the case when a matched type is an extension type | ||
/// with a `Map` as a representation type | ||
/// @author sgrekhov22@gmail.com | ||
// SharedOptions=--enable-experiment=inline-class | ||
|
||
extension type ET1<K, V>(Map<K, V> _) {} | ||
extension type ET2<K, V>(Map<K, V> _) implements Map<K, V> {} | ||
|
||
String test1_1(ET1<bool, bool> m) => | ||
switch (m) { | ||
//^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
{true: true} => "case-1", | ||
{true: false} => "case-2", | ||
{false: true} => "case-3", | ||
{false: false} => "case-4", | ||
}; | ||
|
||
String test1_2(ET2<bool, bool> m) => | ||
switch (m) { | ||
//^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
{true: true} => "case-1", | ||
{true: false} => "case-2", | ||
{false: true} => "case-3", | ||
{false: false} => "case-4", | ||
}; | ||
|
||
String test2_1(ET1<bool, bool> m) { | ||
// ^^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
switch (m) { | ||
case {true: true}: | ||
return "case-1"; | ||
case {true: false}: | ||
return "case-2"; | ||
case {false: true}: | ||
return "case-3"; | ||
case {false: false}: | ||
return "case-4"; | ||
} | ||
// There is no return statement here, switch statement is not exhaustive, so an | ||
// error above occurs because function return type cannot be null | ||
} | ||
|
||
String test2_2(ET2<bool, bool> m) { | ||
// ^^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
switch (m) { | ||
case {true: true}: | ||
return "case-1"; | ||
case {true: false}: | ||
return "case-2"; | ||
case {false: true}: | ||
return "case-3"; | ||
case {false: false}: | ||
return "case-4"; | ||
} | ||
} | ||
|
||
main() { | ||
print(test1_1(ET1({true: false}))); | ||
print(test1_2(ET2({true: false}))); | ||
print(test2_1(ET1({true: false}))); | ||
print(test2_2(ET2({true: false}))); | ||
} |
61 changes: 61 additions & 0 deletions
61
LanguageFeatures/Extension-types/exhaustiveness_map_A01_t02.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) 2023, 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 Exhaustiveness with map patterns can only be achieved when there | ||
/// is an exhaustive pattern in addition to any map patterns | ||
/// | ||
/// @description Check that a switch statement/expression with map patterns only | ||
/// cannot be exhaustive. Test the case when a matched type is an extension type | ||
/// with a `Map` as a representation type | ||
/// @author sgrekhov22@gmail.com | ||
// SharedOptions=--enable-experiment=inline-class | ||
|
||
extension type ET1<K, V>(Map<K, V> _) {} | ||
extension type ET2<K, V>(Map<K, V> _) implements Map<K, V> {} | ||
|
||
String test1_1(ET1<bool, bool> m) => | ||
switch (m) { | ||
//^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
Map(length: <= 0) => "match" | ||
}; | ||
|
||
String test1_2(ET2<bool, bool> m) => | ||
switch (m) { | ||
//^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
Map(length: <= 0) => "match" | ||
}; | ||
|
||
String test2_1(ET1<bool, bool> m) { | ||
// ^^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
switch (m) { | ||
case Map(length: <= 0): | ||
return "match"; | ||
} | ||
// There is no return statement here, switch statement is not exhaustive, so an | ||
// error above occurs because function return type cannot be null | ||
} | ||
|
||
String test2_2(ET2<bool, bool> m) { | ||
// ^^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
switch (m) { | ||
case Map(length: <= 0): | ||
return "match"; | ||
} | ||
} | ||
|
||
main() { | ||
print(test1_1(ET1({true: false}))); | ||
print(test1_2(ET2({true: false}))); | ||
print(test2_1(ET1({true: false}))); | ||
print(test2_2(ET2({true: false}))); | ||
} |
81 changes: 81 additions & 0 deletions
81
LanguageFeatures/Extension-types/exhaustiveness_map_A02_t01.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (c) 2023, 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 Exhaustiveness with map patterns can only be achieved when there | ||
/// is an exhaustive pattern in addition to any map patterns | ||
/// | ||
/// @description Check that a switch statement/expression with map patterns and | ||
/// additional exhaustive pattern can be exhaustive. Test the case when a | ||
/// matched type is an extension type with a `Map` as a representation type | ||
/// @author sgrekhov22@gmail.com | ||
// SharedOptions=--enable-experiment=inline-class | ||
|
||
import "../../Utils/expect.dart"; | ||
|
||
extension type ET1<K, V>(Map<K, V> _) {} | ||
extension type ET2<K, V>(Map<K, V> _) implements Map<K, V> {} | ||
|
||
String test1_1(ET1<bool, bool> m) => | ||
switch (m) { | ||
{true: true} => "case-1", | ||
{true: false} => "case-2", | ||
{false: true} => "case-3", | ||
{false: false} => "case-4", | ||
Map() => "other" | ||
}; | ||
|
||
String test1_2(ET2<bool, bool> m) => | ||
switch (m) { | ||
{true: true} => "case-1", | ||
{true: false} => "case-2", | ||
{false: true} => "case-3", | ||
{false: false} => "case-4", | ||
Map() => "other" | ||
}; | ||
|
||
String test2_1(ET1<bool, bool> m) { | ||
switch (m) { | ||
case {true: true}: | ||
return "case-1"; | ||
case {true: false}: | ||
return "case-2"; | ||
case {false: true}: | ||
return "case-3"; | ||
case {false: false}: | ||
return "case-4"; | ||
case Map(): | ||
return "other"; | ||
} | ||
} | ||
|
||
String test2_2(ET2<bool, bool> m) { | ||
switch (m) { | ||
case {true: true}: | ||
return "case-1"; | ||
case {true: false}: | ||
return "case-2"; | ||
case {false: true}: | ||
return "case-3"; | ||
case {false: false}: | ||
return "case-4"; | ||
case Map(): | ||
return "other"; | ||
} | ||
} | ||
|
||
main() { | ||
Expect.equals("case-1", test1_1(ET1({true: true}))); | ||
Expect.equals("case-2", test1_2(ET2({true: false}))); | ||
Expect.equals("case-3", test2_1(ET1({false: true}))); | ||
Expect.equals("case-4", test2_2(ET2({false: false}))); | ||
Expect.equals("other", test1_1(ET1<bool, bool>({}))); | ||
Expect.equals("other", test1_2(ET2<bool, bool>({}))); | ||
Expect.equals("other", test2_1(ET1<bool, bool>({}))); | ||
Expect.equals("other", test2_2(ET2<bool, bool>({}))); | ||
Expect.equals("other", test1_1(ET1({true: true, false: false}))); | ||
Expect.equals("other", test1_2(ET2({true: false, false: false}))); | ||
Expect.equals("other", test2_1(ET1({false: true, true: true}))); | ||
Expect.equals("other", test2_2(ET2({false: false, true: true}))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters