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.
Fixes dart-lang#2577. Add more chained patterns assignment tests
- Loading branch information
Showing
6 changed files
with
202 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// 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 A pattern on the left side of an assignment expression is used to | ||
/// destructure the assigned value. We extend expression: | ||
/// | ||
/// expression ::= patternAssignment | ||
/// | // Existing productions... | ||
/// | ||
/// patternAssignment ::= outerPattern '=' expression | ||
/// | ||
/// This syntax allows chaining pattern assignments and mixing them with other | ||
/// assignments, but does not allow patterns to the left of a compound | ||
/// assignment operator. | ||
/// | ||
/// @description Check that pattern assignments may be chained. Test | ||
/// parenthesized pattern | ||
/// @author sgrekhov22@gmail.com | ||
/// @issue 55232 | ||
import '../../Utils/expect.dart'; | ||
|
||
void main() { | ||
int v1; | ||
int v2; | ||
(v2) = (v1) = 0; | ||
Expect.equals(0, v1); | ||
Expect.equals(0, v2); | ||
|
||
(v2) = v1 = 1; | ||
Expect.equals(1, v1); | ||
Expect.equals(1, v2); | ||
|
||
v2 = (v1) = 2; | ||
Expect.equals(2, v1); | ||
Expect.equals(2, v2); | ||
|
||
(v2) = ((v1)) = (3); | ||
Expect.equals(3, v1); | ||
Expect.equals(3, v2); | ||
} |
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,40 @@ | ||
// 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 A pattern on the left side of an assignment expression is used to | ||
/// destructure the assigned value. We extend expression: | ||
/// | ||
/// expression ::= patternAssignment | ||
/// | // Existing productions... | ||
/// | ||
/// patternAssignment ::= outerPattern '=' expression | ||
/// | ||
/// This syntax allows chaining pattern assignments and mixing them with other | ||
/// assignments, but does not allow patterns to the left of a compound | ||
/// assignment operator. | ||
/// | ||
/// @description Check that pattern assignments may be chained. Test that | ||
/// chaining an object pattern doesn't create an excessive instances | ||
/// @author sgrekhov22@gmail.com | ||
import '../../Utils/expect.dart'; | ||
|
||
var cCounter = 0; | ||
String log = ""; | ||
|
||
class C { | ||
int _x; | ||
final int counter = cCounter++; | ||
C({required int x}) : _x = x + 1; | ||
int get x => _x++; | ||
} | ||
|
||
void main() { | ||
var y = 0; | ||
var z = C(x: y) = C(x: y) = C(x: y) = C(x: y); | ||
Expect.equals(1, cCounter); | ||
Expect.equals(3, y); | ||
Expect.equals(0, z.counter); | ||
Expect.equals(4, z.x); | ||
} |
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,32 @@ | ||
// 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 A pattern on the left side of an assignment expression is used to | ||
/// destructure the assigned value. We extend expression: | ||
/// | ||
/// expression ::= patternAssignment | ||
/// | // Existing productions... | ||
/// | ||
/// patternAssignment ::= outerPattern '=' expression | ||
/// | ||
/// This syntax allows chaining pattern assignments and mixing them with other | ||
/// assignments, but does not allow patterns to the left of a compound | ||
/// assignment operator. | ||
/// | ||
/// @description Check that pattern assignments may be chained. Test record | ||
/// pattern | ||
/// @author sgrekhov22@gmail.com | ||
import '../../Utils/expect.dart'; | ||
|
||
void main() { | ||
var y = 0; | ||
var z1 = (y,) = (y,) = (y,) = (1,); | ||
Expect.equals(1, y); | ||
Expect.equals((1,), z1); | ||
|
||
var z2 = (y: y) = (y: y) = (y: y); | ||
Expect.equals(1, y); | ||
Expect.equals((y: 1), z2); | ||
} |
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,29 @@ | ||
// 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 A pattern on the left side of an assignment expression is used to | ||
/// destructure the assigned value. We extend expression: | ||
/// | ||
/// expression ::= patternAssignment | ||
/// | // Existing productions... | ||
/// | ||
/// patternAssignment ::= outerPattern '=' expression | ||
/// | ||
/// This syntax allows chaining pattern assignments and mixing them with other | ||
/// assignments, but does not allow patterns to the left of a compound | ||
/// assignment operator. | ||
/// | ||
/// @description Check that pattern assignments may be chained. Test list | ||
/// pattern | ||
/// @author sgrekhov22@gmail.com | ||
import '../../Utils/expect.dart'; | ||
|
||
void main() { | ||
var x, y; | ||
var z = [x, y] = [y, x] = [x, y,] = [1, 2]; | ||
Expect.equals(1, x); | ||
Expect.equals(2, y); | ||
Expect.listEquals([1, 2], z); | ||
} |
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,29 @@ | ||
// 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 A pattern on the left side of an assignment expression is used to | ||
/// destructure the assigned value. We extend expression: | ||
/// | ||
/// expression ::= patternAssignment | ||
/// | // Existing productions... | ||
/// | ||
/// patternAssignment ::= outerPattern '=' expression | ||
/// | ||
/// This syntax allows chaining pattern assignments and mixing them with other | ||
/// assignments, but does not allow patterns to the left of a compound | ||
/// assignment operator. | ||
/// | ||
/// @description Check that pattern assignments may be chained. Test map pattern | ||
/// @author sgrekhov22@gmail.com | ||
import '../../Utils/expect.dart'; | ||
|
||
void main() { | ||
const one = "1"; | ||
int y, x; | ||
var z = {"1": x} = {"1": y} = {one: y} = {"1": 2}; | ||
Expect.equals(2, x); | ||
Expect.equals(2, y); | ||
Expect.mapEquals({"1": 2}, z); | ||
} |
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,30 @@ | ||
// 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 A pattern on the left side of an assignment expression is used to | ||
/// destructure the assigned value. We extend expression: | ||
/// | ||
/// expression ::= patternAssignment | ||
/// | // Existing productions... | ||
/// | ||
/// patternAssignment ::= outerPattern '=' expression | ||
/// | ||
/// This syntax allows chaining pattern assignments and mixing them with other | ||
/// assignments, but does not allow patterns to the left of a compound | ||
/// assignment operator. | ||
/// | ||
/// @description Check that it is a run-time error if chained patterns don't | ||
/// match | ||
/// @author sgrekhov22@gmail.com | ||
import '../../Utils/expect.dart'; | ||
|
||
void main() { | ||
const one = "1"; | ||
int y = 0; | ||
Expect.throws(() { | ||
var z = {"1": y} = {"2": y} = {one: y} = {"1": 2}; | ||
}); | ||
Expect.equals(2, y); | ||
} |