Skip to content

Commit c18e243

Browse files
kallentuCommit Queue
authored andcommitted
[tests] Enum shorthands language tests - context types in ??, cascades, and collection literals for simple identifiers.
Follow up tests from Erik's comment here: https://dart-review.googlesource.com/c/sdk/+/393606/comments/16e214b1_4bbfad8f This CL adds language tests for enum shorthands used in expressions where the context type is propagated down, for simple identifiers (no selector chains yet). Once I do make tests for selector chains, I might reorganize the tests, but this will do for now. Bug: #57038 Change-Id: Id4b6924611559c45817ecada491b6a8f05be3561 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/396703 Reviewed-by: Lasse Nielsen <[email protected]> Commit-Queue: Kallen Tu <[email protected]> Reviewed-by: Erik Ernst <[email protected]>
1 parent 817306a commit c18e243

File tree

4 files changed

+223
-7
lines changed

4 files changed

+223
-7
lines changed

tests/language/enum_shorthands/enum_shorthand_helper.dart

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
// SharedOptions=--enable-experiment=enum-shorthands
88

9-
enum Color { blue, red, green }
9+
enum Color { red, green, blue }
1010

1111
class Integer {
1212
static Integer get one => Integer(1);
13+
static Integer get two => Integer(2);
1314
static const Integer constOne = const Integer._(1);
1415
static const Integer constTwo = const Integer._(2);
1516
final int integer;
@@ -19,18 +20,20 @@ class Integer {
1920

2021
extension type IntegerExt(int integer) {
2122
static IntegerExt get one => IntegerExt(1);
23+
static IntegerExt get two => IntegerExt(2);
2224
static const IntegerExt constOne = const IntegerExt._(1);
2325
static const IntegerExt constTwo = const IntegerExt._(2);
2426
const IntegerExt._(this.integer);
2527
}
2628

2729
mixin IntegerMixin on Integer {
28-
static IntegerMixin get mixinOne => IntegerWithMixin(1);
29-
static const IntegerMixin mixinConstOne = const IntegerWithMixin._(1);
30-
static const IntegerMixin mixinConstTwo = const IntegerWithMixin._(2);
30+
static IntegerMixin get mixinOne => _IntegerWithMixin(1);
31+
static IntegerMixin get mixinTwo => _IntegerWithMixin(2);
32+
static const IntegerMixin mixinConstOne = const _IntegerWithMixin._(1);
33+
static const IntegerMixin mixinConstTwo = const _IntegerWithMixin._(2);
3134
}
3235

33-
class IntegerWithMixin extends Integer with IntegerMixin {
34-
const IntegerWithMixin(int integer) : this._(integer);
35-
const IntegerWithMixin._(super.integer) : super._();
36+
class _IntegerWithMixin extends Integer with IntegerMixin {
37+
const _IntegerWithMixin(int integer) : this._(integer);
38+
const _IntegerWithMixin._(super.integer) : super._();
3639
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright (c) 2024, 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+
// Context type is propagated down in cascades.
6+
7+
// SharedOptions=--enable-experiment=enum-shorthands
8+
9+
import '../enum_shorthand_helper.dart';
10+
11+
class Cascade {
12+
late Color color;
13+
late Integer integer;
14+
late IntegerExt integerExt;
15+
late IntegerMixin integerMixin;
16+
}
17+
18+
class CascadeCollection {
19+
late List<Color> colorList;
20+
late Set<Color> colorSet;
21+
late Map<Color, Color> colorMap;
22+
late Map<Color, (Color, Color)> colorMap2;
23+
24+
late List<Integer> integerList;
25+
late Set<Integer> integerSet;
26+
late Map<Integer, Integer> integerMap;
27+
late Map<Integer, (Integer, Integer)> integerMap2;
28+
29+
late List<IntegerExt> integerExtList;
30+
late Set<IntegerExt> integerExtSet;
31+
late Map<IntegerExt, IntegerExt> integerExtMap;
32+
late Map<IntegerExt, (IntegerExt, IntegerExt)> integerExtMap2;
33+
34+
late List<IntegerMixin> integerMixinList;
35+
late Set<IntegerMixin> integerMixinSet;
36+
late Map<IntegerMixin, IntegerMixin> integerMixinMap;
37+
late Map<IntegerMixin, (IntegerMixin, IntegerMixin)> integerMixinMap2;
38+
}
39+
40+
class CascadeMethod {
41+
void color(Color color) => print(color);
42+
void integer(Integer integer) => print(integer);
43+
void integerExt(IntegerExt integer) => print(integer);
44+
void integerMixin(IntegerMixin integer) => print(integer);
45+
}
46+
47+
void main() {
48+
Cascade()
49+
..color = .red
50+
..integer = .one
51+
..integerExt = .one
52+
..integerMixin = .mixinOne;
53+
54+
dynamic mayBeNull = null;
55+
Cascade()
56+
..color = mayBeNull ?? .red
57+
..integer = mayBeNull ?? .one
58+
..integerExt = mayBeNull ?? .one
59+
..integerMixin = mayBeNull ?? .mixinOne;
60+
61+
CascadeCollection()
62+
// Enum
63+
..colorList = [.blue, .green, .red]
64+
..colorSet = {.blue, .red}
65+
..colorMap = {.blue: .blue, .green: .red}
66+
..colorMap2 = {.red: (.blue, .green)}
67+
// Class
68+
..integerList = [.one, .two, .one]
69+
..integerSet = {.one, .two}
70+
..integerMap = {.one: .two, .two: .two}
71+
..integerMap2 = {
72+
.one: (.one, .two),
73+
.two: (.two, .two),
74+
}
75+
// Extension type
76+
..integerExtList = [.one, .two, .one]
77+
..integerExtSet = {.one, .two}
78+
..integerExtMap = {
79+
.one: .two,
80+
.two: .two,
81+
}
82+
..integerExtMap2 = {
83+
.one: (.one, .two),
84+
.two: (.two, .two),
85+
}
86+
// Mixin
87+
..integerMixinList = [
88+
.mixinOne,
89+
.mixinTwo,
90+
.mixinOne,
91+
]
92+
..integerMixinSet = {.mixinOne, .mixinTwo}
93+
..integerMixinMap = {
94+
.mixinOne: .mixinTwo,
95+
.mixinTwo: .mixinTwo,
96+
}
97+
..integerMixinMap2 = {
98+
.mixinOne: (.mixinOne, .mixinTwo),
99+
.mixinTwo: (.mixinTwo, .mixinTwo),
100+
};
101+
102+
CascadeMethod()
103+
..color(.red)
104+
..integer(.one)
105+
..integerExt(.one)
106+
..integerMixin(.mixinOne);
107+
108+
Color color = .blue..toString();
109+
Integer integer = .one..toString();
110+
IntegerExt integerExt = .one..toString();
111+
IntegerMixin integerMixin = .mixinOne..toString();
112+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) 2024, 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+
// Context type is propagated down in collection literals.
6+
7+
// SharedOptions=--enable-experiment=enum-shorthands
8+
9+
import '../enum_shorthand_helper.dart';
10+
11+
void main() {
12+
// Enum
13+
var colorList = <Color>[.blue, .green, .red];
14+
var colorSet = <Color>{.blue, .red};
15+
var colorMap = <Color, Color>{.blue: .blue, .green: .red};
16+
var colorMap2 = <Color, (Color, Color)>{.red: (.blue, .green)};
17+
18+
// Class
19+
var integerList = <Integer>[.one, .two, .one];
20+
var integerSet = <Integer>{.one, .two};
21+
var integerMap = <Integer, Integer>{
22+
.one: .two,
23+
.two: .two,
24+
};
25+
var integerMap2 = <Integer, (Integer, Integer)>{
26+
.one: (.one, .two),
27+
.two: (.two, .two),
28+
};
29+
30+
// Extension type
31+
var integerExtList = <IntegerExt>[
32+
.one,
33+
.two,
34+
.one,
35+
];
36+
var integerExtSet = <IntegerExt>{.one, .two};
37+
var integerExtMap = <IntegerExt, IntegerExt>{
38+
.one: .two,
39+
.two: .two,
40+
};
41+
var integerExtMap2 = <IntegerExt, (IntegerExt, IntegerExt)>{
42+
.one: (.one, .two),
43+
.two: (.two, .two),
44+
};
45+
46+
// Mixin
47+
var integerMixinList = <IntegerMixin>[
48+
.mixinOne,
49+
.mixinTwo,
50+
.mixinOne,
51+
];
52+
var integerMixinSet = <IntegerMixin>{
53+
.mixinOne,
54+
.mixinTwo,
55+
};
56+
var integerMixinMap = <IntegerMixin, IntegerMixin>{
57+
.mixinOne: .mixinTwo,
58+
.mixinTwo: .mixinTwo,
59+
};
60+
var integerMixinMap2 = <IntegerMixin, (IntegerMixin, IntegerMixin)>{
61+
.mixinOne: (.mixinOne, .mixinTwo),
62+
.mixinTwo: (.mixinTwo, .mixinTwo),
63+
};
64+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) 2024, 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+
// Context type is propagated down in an if-null `??` expression.
6+
7+
// SharedOptions=--enable-experiment=enum-shorthands
8+
9+
import '../enum_shorthand_helper.dart';
10+
import 'package:expect/expect.dart';
11+
12+
Color colorTest(Color? color) => color ?? .blue;
13+
14+
Integer integerTest(Integer? integer) => integer ?? .one;
15+
16+
IntegerExt integerExtTest(IntegerExt? integer) => integer ?? .one;
17+
18+
IntegerMixin integerMixinTest(IntegerMixin? integer) =>
19+
integer ?? .mixinOne;
20+
21+
void main() {
22+
// Enum
23+
Expect.equals(colorTest(null), Color.blue);
24+
Expect.equals(colorTest(Color.red), Color.red);
25+
26+
// Class
27+
Expect.equals(integerTest(null), Integer.one);
28+
Expect.equals(integerTest(Integer.two), Integer.two);
29+
30+
// Extension type
31+
Expect.equals(integerExtTest(null), IntegerExt.one);
32+
Expect.equals(integerExtTest(IntegerExt.two), IntegerExt.two);
33+
34+
// Mixin
35+
Expect.equals(integerMixinTest(null), IntegerMixin.mixinOne);
36+
Expect.equals(integerMixinTest(IntegerMixin.mixinTwo), IntegerMixin.mixinTwo);
37+
}

0 commit comments

Comments
 (0)