Skip to content

Commit 49b3176

Browse files
authored
Fixes #3049. Add more tests for unary prefix ++ and -- operators. (#3051)
Add more tests for unary prefix ++ and -- operators.
1 parent 7b45a4f commit 49b3176

File tree

6 files changed

+126
-8
lines changed

6 files changed

+126
-8
lines changed

Language/Expressions/Unary_Expressions/variable_decrement_t01.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// @assertion Evaluation of an expression of the form --e is equivalent to
6-
/// e -= 1.
5+
/// @assertion An expression of the form `--e` is treated as `(e-=1)`.
6+
///
77
/// @description Checks that evaluation of an expression of the form --e is
88
/// equivalent to e -= 1.
99
/// @author msyabro

Language/Expressions/Unary_Expressions/variable_decrement_t02.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// @assertion Evaluation of an expression of the form --e is equivalent to
6-
/// e -= 1.
5+
/// @assertion An expression of the form `--e` is treated as `(e-=1)`.
6+
///
77
/// @description Checks that evaluation of an expression of the form --e
88
/// results in invoking operator- on the result of e with the proper argument.
99
/// @author rodionov
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion An expression of the form `--e` is treated as `(e-=1)`.
6+
///
7+
/// @description Checks that evaluation of an expression of the form `--e`
8+
/// results in invoking `operator-` on the result of `e` with the proper
9+
/// argument. Test nullable types.
10+
/// @author [email protected]
11+
/// @issue 56963
12+
13+
import '../../../Utils/expect.dart';
14+
15+
class A {
16+
final int value;
17+
A(this.value);
18+
19+
A? operator -(int? other) {
20+
if (other != null) {
21+
return A(value - other);
22+
}
23+
return null;
24+
}
25+
}
26+
27+
extension on A? {
28+
A? operator -(int other) {
29+
if (this != null) {
30+
return this! - other;
31+
}
32+
return null;
33+
}
34+
}
35+
36+
extension on int? {
37+
int? operator -(int? other) {
38+
if (this != null && other != null) {
39+
return this! - other;
40+
}
41+
return null;
42+
}
43+
}
44+
45+
main() {
46+
A? a = 2 > 1 ? A(42) : null;
47+
var x = --a ?? A(-1);
48+
Expect.equals(41, x.value);
49+
a = 2 > 3 ? A(42) : null;
50+
x = --a ?? A(-1);
51+
Expect.equals(-1, x.value);
52+
53+
int? i = 2 > 1 ? 42 : null;
54+
var y = --i ?? -1;
55+
Expect.equals(41, y);
56+
i = 2 > 3 ? 42 : null;
57+
y = --i ?? -1;
58+
Expect.equals(-1, y);
59+
}

Language/Expressions/Unary_Expressions/variable_increment_t01.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// @assertion Evaluation of an expression of the form ++e is equivalent to
6-
/// e += 1.
5+
/// @assertion An expression of the form `++e` is treated as `(e+=1)`.
6+
///
77
/// @description Checks that evaluation of an expression of the form ++e is
88
/// equivalent to e += 1.
99
/// @author msyabro

Language/Expressions/Unary_Expressions/variable_increment_t02.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// @assertion Evaluation of an expression of the form ++e is equivalent to
6-
/// e += 1.
5+
/// @assertion An expression of the form `++e` is treated as `(e+=1)`.
6+
///
77
/// @description Checks that evaluation of an expression of the form ++e
88
/// results in invoking operator+ on the result of e with the proper argument.
99
/// @author rodionov
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// @assertion An expression of the form `++e` is treated as `(e+=1)`.
6+
///
7+
/// @description Checks that evaluation of an expression of the form `++e`
8+
/// results in invoking `operator+` on the result of e with the proper argument.
9+
/// Test nullable types.
10+
/// @author [email protected]
11+
/// @issue 56963
12+
13+
import '../../../Utils/expect.dart';
14+
15+
class A {
16+
final int value;
17+
A(this.value);
18+
19+
A? operator +(int? other) {
20+
if (other != null) {
21+
return A(value + other);
22+
}
23+
return null;
24+
}
25+
}
26+
27+
extension on A? {
28+
A? operator +(int other) {
29+
if (this != null) {
30+
return this! + other;
31+
}
32+
return null;
33+
}
34+
}
35+
36+
extension on int? {
37+
int? operator +(int? other) {
38+
if (this != null && other != null) {
39+
return this! + other;
40+
}
41+
return null;
42+
}
43+
}
44+
45+
main() {
46+
A? a = 2 > 1 ? A(1) : null;
47+
var x = ++a ?? A(-1);
48+
Expect.equals(2, x.value);
49+
a = 2 > 3 ? A(1) : null;
50+
x = ++a ?? A(-1);
51+
Expect.equals(-1, x.value);
52+
53+
int? i = 2 > 1 ? 42 : null;
54+
var y = ++i ?? -1;
55+
Expect.equals(43, y);
56+
i = 2 > 3 ? 42 : null;
57+
y = ++i ?? -1;
58+
Expect.equals(-1, y);
59+
}

0 commit comments

Comments
 (0)