Skip to content
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

Fixes #2577. Add more chained patterns assignment tests #2580

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions LanguageFeatures/Patterns/pattern_assignment_A02_t02.dart
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);
}
40 changes: 40 additions & 0 deletions LanguageFeatures/Patterns/pattern_assignment_A02_t03.dart
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);
}
32 changes: 32 additions & 0 deletions LanguageFeatures/Patterns/pattern_assignment_A02_t04.dart
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);
}
29 changes: 29 additions & 0 deletions LanguageFeatures/Patterns/pattern_assignment_A02_t05.dart
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);
}
29 changes: 29 additions & 0 deletions LanguageFeatures/Patterns/pattern_assignment_A02_t06.dart
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);
}
30 changes: 30 additions & 0 deletions LanguageFeatures/Patterns/pattern_assignment_A02_t07.dart
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);
}