-
Notifications
You must be signed in to change notification settings - Fork 28
#2420. Add object-pattern exhaustiveness tests #2454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
LanguageFeatures/Extension-types/exhaustiveness_object_A01_t01.dart
This file contains hidden or 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,67 @@ | ||
// 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 Switch statements and expressions with a sealed class as a | ||
/// matched type are always exhaustive | ||
/// | ||
/// @description Check that it is no compile-time error if the matched value | ||
/// type of a switch expression is an extension type with a sealed class as a | ||
/// representation type and the set of cases is an exhaustive set of object | ||
/// patterns | ||
/// @author [email protected] | ||
|
||
// SharedOptions=--enable-experiment=inline-class | ||
|
||
import "../../Utils/expect.dart"; | ||
import "../Patterns/Exhaustiveness/exhaustiveness_lib.dart"; | ||
|
||
extension type FaceET1<T>(Face<T> _) {} | ||
extension type FaceET2<T>(Face<T> _) implements Face<T> {} | ||
|
||
String test1_1(FaceET1 face) => switch (face) { | ||
Jack() => 'Jack', | ||
Queen() => 'Queen', | ||
King(suit: _) => 'King' | ||
}; | ||
|
||
String test1_2(FaceET2 face) => switch (face) { | ||
Jack() => 'Jack', | ||
Queen() => 'Queen', | ||
King(suit: _) => 'King' | ||
}; | ||
|
||
String test2_1(FaceET1<int> face) => switch (face) { | ||
Jack<num>() => 'Jack', | ||
Queen<int>() => 'Queen', | ||
King<int>(suit: Suit.club || Suit.diamond || Suit.heart || Suit.spade) => | ||
'King' | ||
}; | ||
|
||
String test2_2(FaceET2<int> face) => switch (face) { | ||
Jack<num>() => 'Jack', | ||
Queen<int>() => 'Queen', | ||
King<int>(suit: Suit.club || Suit.diamond || Suit.heart || Suit.spade) => | ||
'King' | ||
}; | ||
|
||
String test3_1<T extends num>(FaceET1<T> face) => switch (face) { | ||
Jack<num>() && Jack<T>(oneEyed: _) => 'Jack', | ||
Queen<num>() => 'Queen', | ||
King<T>() => 'King' | ||
}; | ||
|
||
String test3_2<T extends num>(FaceET2<T> face) => switch (face) { | ||
Jack<num>() && Jack<T>(oneEyed: _) => 'Jack', | ||
Queen<num>() => 'Queen', | ||
King<T>() => 'King' | ||
}; | ||
|
||
main() { | ||
Expect.equals("King", test1_1(FaceET1(King(Suit.club)))); | ||
Expect.equals("King", test1_2(FaceET2(King(Suit.club)))); | ||
Expect.equals("King", test2_1(FaceET1(King(Suit.club)))); | ||
Expect.equals("King", test2_2(FaceET2(King(Suit.club)))); | ||
Expect.equals("King", test3_1<int>(FaceET1(King(Suit.club)))); | ||
Expect.equals("King", test3_2<int>(FaceET2(King(Suit.club)))); | ||
} |
78 changes: 78 additions & 0 deletions
78
LanguageFeatures/Extension-types/exhaustiveness_object_A01_t02.dart
This file contains hidden or 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,78 @@ | ||
// 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 Switch statements and expressions with a sealed class as a | ||
/// matched type are always exhaustive | ||
/// | ||
/// @description Check that it is no compile-time error if the matched value | ||
/// type of a switch statement is an extension type with a sealed class as a | ||
/// representation type and the set of cases is exhaustive | ||
/// @author [email protected] | ||
|
||
// SharedOptions=--enable-experiment=inline-class | ||
|
||
import "../../Utils/expect.dart"; | ||
import "../Patterns/Exhaustiveness/exhaustiveness_lib.dart"; | ||
|
||
extension type FaceET1<T>(Face<T> _) {} | ||
extension type FaceET2<T>(Face<T> _) implements Face<T> {} | ||
|
||
String test1_1(FaceET1 face) { | ||
switch (face) { | ||
case Jack(): return 'Jack'; | ||
case Queen(): return 'Queen'; | ||
case King(suit: _): return 'King'; | ||
} | ||
} | ||
|
||
String test1_2(FaceET2 face) { | ||
switch (face) { | ||
case Jack(): return 'Jack'; | ||
case Queen(): return 'Queen'; | ||
case King(suit: _): return 'King'; | ||
} | ||
} | ||
|
||
String test2_1(FaceET1<int> face) { | ||
switch (face) { | ||
case Jack<num>(): return 'Jack'; | ||
case Queen<int>(): return 'Queen'; | ||
case King<int>(suit: Suit.club || Suit.diamond || Suit.heart || Suit.spade): | ||
return 'King'; | ||
} | ||
} | ||
|
||
String test2_2(FaceET2<int> face) { | ||
switch (face) { | ||
case Jack<num>(): return 'Jack'; | ||
case Queen<int>(): return 'Queen'; | ||
case King<int>(suit: Suit.club || Suit.diamond || Suit.heart || Suit.spade): | ||
return 'King'; | ||
} | ||
} | ||
|
||
String test3_1<T extends num>(FaceET1<T> face) { | ||
switch (face) { | ||
case Jack<num>() && Jack<T>(oneEyed: _): return 'Jack'; | ||
case Queen<num>(): return 'Queen'; | ||
case King<T>(): return 'King'; | ||
} | ||
} | ||
|
||
String test3_2<T extends num>(FaceET2<T> face) { | ||
switch (face) { | ||
case Jack<num>() && Jack<T>(oneEyed: _): return 'Jack'; | ||
case Queen<num>(): return 'Queen'; | ||
case King<T>(): return 'King'; | ||
} | ||
} | ||
|
||
main() { | ||
Expect.equals("King", test1_1(FaceET1(King(Suit.club)))); | ||
Expect.equals("King", test1_2(FaceET2(King(Suit.club)))); | ||
Expect.equals("King", test2_1(FaceET1(King(Suit.club)))); | ||
Expect.equals("King", test2_2(FaceET2(King(Suit.club)))); | ||
Expect.equals("King", test3_1<int>(FaceET1(King(Suit.club)))); | ||
Expect.equals("King", test3_2<int>(FaceET2(King(Suit.club)))); | ||
} |
44 changes: 44 additions & 0 deletions
44
LanguageFeatures/Extension-types/exhaustiveness_object_A01_t03.dart
This file contains hidden or 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,44 @@ | ||
// 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 Switch statements and expressions with a sealed class as a | ||
/// matched type are always exhaustive | ||
/// | ||
/// @description Check that it is no compile-time error if the matched value | ||
/// type of a switch expression is a sealed class as and the set of cases is an | ||
/// exhaustive set of object patterns with extension types | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// @author [email protected] | ||
|
||
// SharedOptions=--enable-experiment=inline-class | ||
|
||
import "../../Utils/expect.dart"; | ||
import "../Patterns/Exhaustiveness/exhaustiveness_lib.dart"; | ||
|
||
extension type JackET<T>(Jack<T> _) implements Jack<T> {} | ||
extension type QueenET<T>(Queen<T> _) implements Queen<T> {} | ||
extension type KingET<T>(King<T> _) implements King<T> {} | ||
|
||
String test1(Face face) => switch (face) { | ||
JackET() || QueenET() => 'Jack or Queen', | ||
KingET(suit: _) => 'King' | ||
}; | ||
|
||
String test2(Face<int> face) => switch (face) { | ||
JackET<num>() || QueenET<int>() => 'Jack or Queen', | ||
KingET<int>( | ||
suit: Suit.club || Suit.diamond || Suit.heart || Suit.spade | ||
) => 'King' | ||
}; | ||
|
||
String test3<T extends num>(Face<T> face) => switch (face) { | ||
JackET<num>() && JackET<T>(oneEyed: _) => 'Jack', | ||
QueenET<num>() => 'Queen', | ||
KingET<T>() => 'King' | ||
}; | ||
|
||
main() { | ||
Expect.equals("Jack or Queen", test1(Jack(Suit.club))); | ||
Expect.equals("Jack or Queen", test2(Jack(Suit.club))); | ||
Expect.equals("King", test3<int>(King(Suit.club))); | ||
} |
54 changes: 54 additions & 0 deletions
54
LanguageFeatures/Extension-types/exhaustiveness_object_A01_t04.dart
This file contains hidden or 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,54 @@ | ||
// 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 Switch statements and expressions with a sealed class as a | ||
/// matched type are always exhaustive | ||
/// | ||
/// @description Check that it is no compile-time error if the matched value | ||
/// type of a switch statement is a sealed class as and the set of cases is an | ||
/// exhaustive set of object patterns with extension types | ||
eernstg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// @author [email protected] | ||
|
||
// SharedOptions=--enable-experiment=inline-class | ||
|
||
import "../../Utils/expect.dart"; | ||
import "../Patterns/Exhaustiveness/exhaustiveness_lib.dart"; | ||
|
||
extension type JackET<T>(Jack<T> _) implements Jack<T> {} | ||
extension type QueenET<T>(Queen<T> _) implements Queen<T> {} | ||
extension type KingET<T>(King<T> _) implements King<T> {} | ||
|
||
String test1(Face face) { | ||
switch (face) { | ||
case JackET() || QueenET(): | ||
return 'Jack or Queen'; | ||
case KingET(suit: _): | ||
return 'King'; | ||
} | ||
} | ||
|
||
String test2(Face<int> face) { | ||
switch (face) { | ||
case JackET<num>() || QueenET<int>(): | ||
return 'Jack or Queen'; | ||
case KingET<int>( | ||
suit: Suit.club || Suit.diamond || Suit.heart || Suit.spade | ||
): | ||
return 'King'; | ||
} | ||
} | ||
|
||
String test3<T extends num>(Face<T> face) { | ||
switch (face) { | ||
case JackET<num>() && JackET<T>(oneEyed: _): return 'Jack'; | ||
case QueenET<num>(): return 'Queen'; | ||
case KingET<T>(): return 'King'; | ||
} | ||
} | ||
|
||
main() { | ||
Expect.equals("Jack or Queen", test1(Jack(Suit.club))); | ||
Expect.equals("Jack or Queen", test2(Jack(Suit.club))); | ||
Expect.equals("King", test3<int>(King(Suit.club))); | ||
} |
69 changes: 69 additions & 0 deletions
69
LanguageFeatures/Extension-types/exhaustiveness_object_A01_t05.dart
This file contains hidden or 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,69 @@ | ||
// 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 Switch statements and expressions with a sealed class as a | ||
/// matched type are always exhaustive | ||
/// | ||
/// @description Check that it is no compile-time error if the matched value | ||
/// type of a switch is an extension type with a sealed class as a | ||
/// representation type and the set of cases is an exhaustive set of object | ||
/// patterns with extension types | ||
/// @author [email protected] | ||
|
||
// SharedOptions=--enable-experiment=inline-class | ||
|
||
import "../../Utils/expect.dart"; | ||
import "../Patterns/Exhaustiveness/exhaustiveness_lib.dart"; | ||
|
||
extension type FaceET1<T>(Face<T> _) {} | ||
extension type FaceET2<T>(Face<T> _) implements Face<T> {} | ||
|
||
extension type JackET<T>(Jack<T> _) implements Jack<T> {} | ||
extension type QueenET<T>(Queen<T> _) implements Queen<T> {} | ||
extension type KingET<T>(King<T> _) implements King<T> {} | ||
|
||
String test1_1(FaceET1 face) => switch (face) { | ||
JackET() || QueenET() => 'Jack or Queen', | ||
KingET(suit: _) => 'King' | ||
}; | ||
|
||
String test1_2(FaceET2 face) => switch (face) { | ||
JackET() || QueenET() => 'Jack or Queen', | ||
KingET(suit: _) => 'King' | ||
}; | ||
|
||
String test2_1(FaceET1<int> face) => switch (face) { | ||
JackET<num>() || QueenET<int>() => 'Jack or Queen', | ||
KingET<int>( | ||
suit: Suit.club || Suit.diamond || Suit.heart || Suit.spade | ||
) => 'King' | ||
}; | ||
|
||
String test2_2(FaceET2<int> face) => switch (face) { | ||
JackET<num>() || QueenET<int>() => 'Jack or Queen', | ||
KingET<int>( | ||
suit: Suit.club || Suit.diamond || Suit.heart || Suit.spade | ||
) => 'King' | ||
}; | ||
|
||
String test3_1<T extends num>(FaceET1<T> face) => switch (face) { | ||
JackET<num>() && JackET<T>(oneEyed: _) => 'Jack', | ||
QueenET<num>() => 'Queen', | ||
KingET<T>() => 'King' | ||
}; | ||
|
||
String test3_2<T extends num>(FaceET2<T> face) => switch (face) { | ||
JackET<num>() && JackET<T>(oneEyed: _) => 'Jack', | ||
QueenET<num>() => 'Queen', | ||
KingET<T>() => 'King' | ||
}; | ||
|
||
main() { | ||
Expect.equals("Jack or Queen", test1_1(FaceET1(Jack(Suit.club)))); | ||
Expect.equals("Jack or Queen", test1_2(FaceET2(Jack(Suit.club)))); | ||
Expect.equals("Jack or Queen", test2_1(FaceET1(Jack(Suit.club)))); | ||
Expect.equals("Jack or Queen", test2_2(FaceET2(Jack(Suit.club)))); | ||
Expect.equals("King", test3_1<int>(FaceET1(King(Suit.club)))); | ||
Expect.equals("King", test3_2<int>(FaceET2(King(Suit.club)))); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.