Skip to content

Commit

Permalink
dart-lang#1401. If- and for-elements tests for maps and sets added
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrekhov committed Mar 24, 2023
1 parent c1aef80 commit 150b776
Show file tree
Hide file tree
Showing 43 changed files with 2,278 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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
/// Likewise, a collection element of the form:
///
/// for (<patternVariableDeclaration>; <condition>; <increment>) <element>
/// Is executed like a traditional for loop though is more likely to declare
/// multiple variables
///
/// @description Check that a pattern-for element is executed like a traditional
/// for loop. Test variable and identifier subpatterns of a parenthesized
/// pattern. Test pattern-for element in a map literal
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns

import "../../Utils/expect.dart";

main() {
var map1 = {
"k0": 0,
for (var (int i) = 1; i <= 3; (i) = (i + 1)) "k$i": i,
"k4": 4
};
Expect.mapEquals({"k0": 0, "k1": 1, "k2": 2, "k3": 3, "k4": 4}, map1);
int j = -1;
var map2 = {"k0": 0, for ((j) = 1; j < 4; (j) = (j + 1)) "k$j": j, "k4": 4};
Expect.mapEquals({"k0": 0, "k1": 1, "k2": 2, "k3": 3, "k4": 4}, map2);
}
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
/// Likewise, a collection element of the form:
///
/// for (<patternVariableDeclaration>; <condition>; <increment>) <element>
/// Is executed like a traditional for loop though is more likely to declare
/// multiple variables
///
/// @description Check that a pattern-for element is executed like a traditional
/// for loop. Test a list pattern in a map literal
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns

import "../../Utils/expect.dart";

main() {
var map = {
"k-1": -1,
for (var [a, b] = [0, 1]; a <= 8; [a, b] = [b, a + b]) "k$a": a,
"k13": 13
};
Expect.mapEquals({
"k-1": -1,
"k0": 0,
"k1": 1,
"k2": 2,
"k3": 3,
"k5": 5,
"k8": 8,
"k13": 13
}, map);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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
/// Likewise, a collection element of the form:
///
/// for (<patternVariableDeclaration>; <condition>; <increment>) <element>
/// Is executed like a traditional for loop though is more likely to declare
/// multiple variables
///
/// @description Check that a pattern-for element is executed like a traditional
/// for loop. Test a map pattern in a map literal
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns

import "../../Utils/expect.dart";

main() {
var map = {
"k-1": -1,
for (var {"k1": a, "k2": b} = {"k1": 0, "k2": 1};
a <= 8;
{"k1": a, "k2": b} = {"k1": b, "k2": a + b})
"k$a": a,
"k13": 13
};
Expect.mapEquals({
"k-1": -1,
"k0": 0,
"k1": 1,
"k2": 2,
"k3": 3,
"k5": 5,
"k8": 8,
"k13": 13
}, map);
}
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
/// Likewise, a collection element of the form:
///
/// for (<patternVariableDeclaration>; <condition>; <increment>) <element>
/// Is executed like a traditional for loop though is more likely to declare
/// multiple variables
///
/// @description Check that a pattern-for element is executed like a traditional
/// for loop. Test a record pattern in a map literal
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns,records

import "../../Utils/expect.dart";

main() {
var map = {
"k-1": -1,
for (var (a, b) = (0, 1); a <= 8; (a, b) = (b, a + b)) "k$a": a,
"k13": 13
};
Expect.mapEquals({
"k-1": -1,
"k0": 0,
"k1": 1,
"k2": 2,
"k3": 3,
"k5": 5,
"k8": 8,
"k13": 13
}, map);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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
/// Likewise, a collection element of the form:
///
/// for (<patternVariableDeclaration>; <condition>; <increment>) <element>
/// Is executed like a traditional for loop though is more likely to declare
/// multiple variables
///
/// @description Check that a pattern-for element is executed like a traditional
/// for loop. Test an object pattern in a map literal
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns,records

import "../../Utils/expect.dart";
import "patterns_lib.dart";

main() {
var map = {
"k0": 0,
for (var Square(:areaAsInt) = Square(1);
areaAsInt <= 9;
Square(:areaAsInt) = Square((++areaAsInt).toDouble()))
"k$areaAsInt": areaAsInt,
"k42": 42
};
Expect.mapEquals({"k0": 0, "k1": 1, "k4": 4, "k42": 42}, map);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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
/// Likewise, a collection element of the form:
///
/// for (<patternVariableDeclaration>; <condition>; <increment>) <element>
/// Is executed like a traditional for loop though is more likely to declare
/// multiple variables
///
/// @description Check that a pattern-for element is executed like a traditional
/// for loop. Test variable and identifier subpatterns of a parenthesized
/// pattern. Test pattern-for element in a set literal
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns

import "../../Utils/expect.dart";

main() {
var set1 = {0, for (var (int i) = 1; i <= 3; (i) = (i + 1)) i, 4};
Expect.setEquals({0, 1, 2, 3, 4}, set1);
int j = -1;
var set2 = {0, for ((j) = 1; j < 4; (j) = (j + 1)) j, 4};
Expect.setEquals({0, 1, 2, 3, 4}, set2);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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
/// Likewise, a collection element of the form:
///
/// for (<patternVariableDeclaration>; <condition>; <increment>) <element>
/// Is executed like a traditional for loop though is more likely to declare
/// multiple variables
///
/// @description Check that a pattern-for element is executed like a traditional
/// for loop. Test a list pattern in a set literal
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns

import "../../Utils/expect.dart";

main() {
var set = {
-1,
for (var [a, b] = [0, 1]; a <= 8; [a, b] = [b, a + b]) a,
13
};
Expect.setEquals({-1, 0, 1, 2, 3, 5, 8, 13}, set);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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
/// Likewise, a collection element of the form:
///
/// for (<patternVariableDeclaration>; <condition>; <increment>) <element>
/// Is executed like a traditional for loop though is more likely to declare
/// multiple variables
///
/// @description Check that a pattern-for element is executed like a traditional
/// for loop. Test a map pattern in a set literal
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns

import "../../Utils/expect.dart";

main() {
var set = {
-1,
for (var {"k1": a, "k2": b} = {"k1": 0, "k2": 1};
a <= 8;
{"k1": a, "k2": b} = {"k1": b, "k2": a + b})
a,
13
};
Expect.setEquals({-1, 0, 1, 2, 3, 5, 8, 13}, set);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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
/// Likewise, a collection element of the form:
///
/// for (<patternVariableDeclaration>; <condition>; <increment>) <element>
/// Is executed like a traditional for loop though is more likely to declare
/// multiple variables
///
/// @description Check that a pattern-for element is executed like a traditional
/// for loop. Test a record pattern in a set literal
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns,records

import "../../Utils/expect.dart";

main() {
var set = {-1, for (var (a, b) = (0, 1); a <= 8; (a, b) = (b, a + b)) a, 13};
Expect.setEquals({-1, 0, 1, 2, 3, 5, 8, 13}, set);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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
/// Likewise, a collection element of the form:
///
/// for (<patternVariableDeclaration>; <condition>; <increment>) <element>
/// Is executed like a traditional for loop though is more likely to declare
/// multiple variables
///
/// @description Check that a pattern-for element is executed like a traditional
/// for loop. Test an object pattern in a set literal
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns,records

import "../../Utils/expect.dart";
import "patterns_lib.dart";

main() {
var set = {
0,
for (var Square(:areaAsInt) = Square(1);
areaAsInt <= 9;
Square(:areaAsInt) = Square((++areaAsInt).toDouble()))
areaAsInt,
42
};
Expect.setEquals({0, 1, 4, 42}, set);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
///
/// @description Check that a pattern-for element is executed like a traditional
/// for loop. Test that it is a compile-time error if a final variable is
/// assigned in a pattern-for element
/// assigned in a pattern-for element in a list literal
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns,records
Expand Down
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
/// A statement of the form:
///
/// for (<patternVariableDeclaration>; <condition>; <increment>) <statement>
///
/// Is executed like a traditional for loop though is more likely to declare
/// multiple variables.
///
/// @description Check that a pattern-for element is executed like a traditional
/// for loop. Test that it is a compile-time error if a final variable is
/// assigned in a pattern-for element in a map literal
/// @author [email protected]
// SharedOptions=--enable-experiment=patterns,records

import "patterns_lib.dart";

main() {
var m1 = {
for (final (int i) = 0; i < 3; i++) "k$i": i,
// ^
// [analyzer] unspecified
// [cfe] unspecified
};
var m2 = {
for (final [a] = [0]; a < 3; [a] = [a + 1]) "k$a": a,
// ^
// [analyzer] unspecified
// [cfe] unspecified
};
var m3 = {
for (final {"k1": a} = {"k1": 0}; a < 3; {"k1": a} = {"k1": a + 1}) "k$a": a,
// ^
// [analyzer] unspecified
// [cfe] unspecified
};
var m4 = {
for (final (a,) = (0,); a < 3; (a,) = (a + 1,)) "k$a": a,
// ^
// [analyzer] unspecified
// [cfe] unspecified
};
var m5 = {
for (final Square(:areaAsInt) = Square(1); areaAsInt < 2;
Square(:areaAsInt) = Square(areaAsInt + 1)) "k$areaAsInt": areaAsInt,
// ^^^^^^^^^
// [analyzer] unspecified
// [cfe] unspecified
};
}
Loading

0 comments on commit 150b776

Please sign in to comment.