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#1401. Exhaustiveness tests fixed and new ones added (dart-l…
…ang#2019) Update and fix exhaustiveness tests. In particular, test exhaustiveness of custom classes that implement `List`, including implementations whose `length` can be negative.
- Loading branch information
Showing
21 changed files
with
450 additions
and
27 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/// always exhaustive | ||
/// | ||
/// @description Check that it is no compile-time error if a matched type of a | ||
/// switch expression is an enum and all cases are exhaustive | ||
/// switch expression is an enum and the set of cases is exhaustive | ||
/// @author [email protected] | ||
// SharedOptions=--enable-experiment=patterns | ||
|
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/// always exhaustive | ||
/// | ||
/// @description Check that it is no compile-time error if a matched type of a | ||
/// switch expression is an enum and all cases are exhaustive | ||
/// switch expression is an enum and the set of cases is exhaustive | ||
/// @author [email protected] | ||
// SharedOptions=--enable-experiment=patterns | ||
|
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/// always exhaustive | ||
/// | ||
/// @description Check that it is no compile-time error if a matched type of a | ||
/// switch expression is an enum and all cases are exhaustive | ||
/// switch expression is an enum and the set of cases is exhaustive | ||
/// @author [email protected] | ||
// SharedOptions=--enable-experiment=patterns | ||
|
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/// always exhaustive | ||
/// | ||
/// @description Check that it is a compile-time error if a matched type of a | ||
/// switch expression is an enum and not all cases are exhaustive | ||
/// switch expression is an enum and the set of cases is not exhaustive | ||
/// @author [email protected] | ||
// SharedOptions=--enable-experiment=patterns | ||
|
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
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
54 changes: 54 additions & 0 deletions
54
LanguageFeatures/Patterns/Exhaustiveness/exhaustiveness_list_A04_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,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 expression with a list as a matched type can be exhaustive | ||
/// | ||
/// @description Check that a custom implementation of `List` can be exhaustive | ||
/// @author [email protected] | ||
// SharedOptions=--enable-experiment=patterns | ||
|
||
import "dart:collection"; | ||
import "../../../Utils/expect.dart"; | ||
|
||
class MyList<T> extends ListBase<T> { | ||
List<T> _inner = []; | ||
|
||
MyList(this._inner); | ||
|
||
@override | ||
int get length { | ||
return _inner.length; | ||
} | ||
|
||
@override | ||
void set length(int newLength) { | ||
_inner.length = newLength; | ||
} | ||
|
||
@override | ||
T operator [](int index) { | ||
return _inner[index]; | ||
} | ||
|
||
@override | ||
void operator []=(int index, T value) { | ||
_inner[index] = value; | ||
} | ||
} | ||
|
||
String test(MyList<int> l) => | ||
switch (l) { | ||
[] => "0", | ||
[_] => "1", | ||
[_, _] => "2", | ||
[_, _, ...] => "2+" | ||
}; | ||
|
||
main() { | ||
Expect.equals("0", test(MyList<int>([]))); | ||
Expect.equals("1", test(MyList<int>([1]))); | ||
Expect.equals("2", test(MyList<int>([1, 2]))); | ||
Expect.equals("2+", test(MyList<int>([1, 2, 3]))); | ||
} |
54 changes: 54 additions & 0 deletions
54
LanguageFeatures/Patterns/Exhaustiveness/exhaustiveness_list_A04_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,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 expression with a list as a matched type can be exhaustive | ||
/// | ||
/// @description Check that a custom implementation of `List` can be exhaustive | ||
/// @author [email protected] | ||
// SharedOptions=--enable-experiment=patterns | ||
|
||
import "dart:collection"; | ||
import "../../../Utils/expect.dart"; | ||
|
||
class MyList<T> with ListMixin<T> implements List<T> { | ||
List<T> _inner = []; | ||
|
||
MyList(this._inner); | ||
|
||
@override | ||
int get length { | ||
return _inner.length; | ||
} | ||
|
||
@override | ||
void set length(int newLength) { | ||
_inner.length = newLength; | ||
} | ||
|
||
@override | ||
T operator [](int index) { | ||
return _inner[index]; | ||
} | ||
|
||
@override | ||
void operator []=(int index, T value) { | ||
_inner[index] = value; | ||
} | ||
} | ||
|
||
String test(MyList<int> l) => | ||
switch (l) { | ||
[] => "0", | ||
[_] => "1", | ||
[_, _] => "2", | ||
[_, _, ...] => "2+" | ||
}; | ||
|
||
main() { | ||
Expect.equals("0", test(MyList<int>([]))); | ||
Expect.equals("1", test(MyList<int>([1]))); | ||
Expect.equals("2", test(MyList<int>([1, 2]))); | ||
Expect.equals("2+", test(MyList<int>([1, 2, 3]))); | ||
} |
55 changes: 55 additions & 0 deletions
55
LanguageFeatures/Patterns/Exhaustiveness/exhaustiveness_list_A04_t03.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,55 @@ | ||
// 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 expression with a list as a matched type can be exhaustive | ||
/// | ||
/// @description Check that an implementation of `List` with a negative length | ||
/// can be exhaustive | ||
/// @author [email protected] | ||
// SharedOptions=--enable-experiment=patterns | ||
|
||
import "dart:collection"; | ||
import "../../../Utils/expect.dart"; | ||
|
||
class MisbehavingList<T> extends ListBase<T> { | ||
List<T> _inner = []; | ||
|
||
MisbehavingList(this._inner); | ||
|
||
@override | ||
int get length { | ||
return (-1) * _inner.length; | ||
} | ||
|
||
@override | ||
void set length(int newLength) { | ||
_inner.length = newLength; | ||
} | ||
|
||
@override | ||
T operator [](int index) { | ||
return _inner[index]; | ||
} | ||
|
||
@override | ||
void operator []=(int index, T value) { | ||
_inner[index] = value; | ||
} | ||
} | ||
|
||
String test(MisbehavingList<int> ml) => | ||
switch (ml) { | ||
[] => "0", | ||
[_] => "1", | ||
[_, _] => "2", | ||
[_, _, ...] => "2+" | ||
}; | ||
|
||
main() { | ||
Expect.equals("0", test(MisbehavingList<int>([]))); | ||
Expect.equals("0", test(MisbehavingList<int>([1]))); | ||
Expect.equals("0", test(MisbehavingList<int>([1, 2]))); | ||
Expect.equals("0", test(MisbehavingList<int>([1, 2, 3]))); | ||
} |
55 changes: 55 additions & 0 deletions
55
LanguageFeatures/Patterns/Exhaustiveness/exhaustiveness_list_A04_t04.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,55 @@ | ||
// 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 expression with a list as a matched type can be exhaustive | ||
/// | ||
/// @description Check that an implementation of `List` with a negative length | ||
/// can be exhaustive | ||
/// @author [email protected] | ||
// SharedOptions=--enable-experiment=patterns | ||
|
||
import "dart:collection"; | ||
import "../../../Utils/expect.dart"; | ||
|
||
class MisbehavingList<T> with ListMixin<T> implements List<T> { | ||
List<T> _inner = []; | ||
|
||
MisbehavingList(this._inner); | ||
|
||
@override | ||
int get length { | ||
return (-1) * _inner.length; | ||
} | ||
|
||
@override | ||
void set length(int newLength) { | ||
_inner.length = newLength; | ||
} | ||
|
||
@override | ||
T operator [](int index) { | ||
return _inner[index]; | ||
} | ||
|
||
@override | ||
void operator []=(int index, T value) { | ||
_inner[index] = value; | ||
} | ||
} | ||
|
||
String test(MisbehavingList<int> ml) => | ||
switch (ml) { | ||
[] => "0", | ||
[_] => "1", | ||
[_, _] => "2", | ||
[_, _, ...] => "2+" | ||
}; | ||
|
||
main() { | ||
Expect.equals("0", test(MisbehavingList<int>([]))); | ||
Expect.equals("0", test(MisbehavingList<int>([1]))); | ||
Expect.equals("0", test(MisbehavingList<int>([1, 2]))); | ||
Expect.equals("0", test(MisbehavingList<int>([1, 2, 3]))); | ||
} |
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
36 changes: 36 additions & 0 deletions
36
LanguageFeatures/Patterns/Exhaustiveness/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,36 @@ | ||
// 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 map as a matched type can | ||
/// never be exhaustive | ||
/// | ||
/// @description Check that map pattern cannot be exhaustive | ||
/// @author [email protected] | ||
// SharedOptions=--enable-experiment=patterns | ||
|
||
String test1(Map<bool, bool> m) => | ||
switch (m) { | ||
//^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
Map(length: <= 0) => "match" | ||
}; | ||
|
||
String test2(Map<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 | ||
} | ||
|
||
main() { | ||
print(test1({true: false})); | ||
print(test2({true: false})); | ||
} |
35 changes: 35 additions & 0 deletions
35
LanguageFeatures/Patterns/Exhaustiveness/exhaustiveness_sealed_A02_t03.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,35 @@ | ||
// 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 a compile-time error if matched type of a | ||
/// switch expression is a sealed class and cases are not exhaustive | ||
/// @author [email protected] | ||
// SharedOptions=--enable-experiment=patterns,class-modifiers | ||
|
||
import "exhaustiveness_lib.dart"; | ||
|
||
String test1(Face face) => switch (face) { | ||
// ^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
LastPersonOnEarth _ => 'LastPersonOnEarth' | ||
}; | ||
|
||
String test2(Face face) => switch (face) { | ||
// ^^^^^^ | ||
// [analyzer] unspecified | ||
// [cfe] unspecified | ||
LastPersonOnEarth _ => 'Jack', | ||
Queen _ => 'Queen', | ||
King _ => 'King' | ||
}; | ||
|
||
main() { | ||
test1(King(Suit.club)); | ||
test2(King(Suit.club)); | ||
} |
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
Oops, something went wrong.