-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tests] Enum shorthands language tests - context types in ??, cascade…
…s, 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 <lrn@google.com> Commit-Queue: Kallen Tu <kallentu@google.com> Reviewed-by: Erik Ernst <eernst@google.com>
- Loading branch information
Showing
4 changed files
with
223 additions
and
7 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
112 changes: 112 additions & 0 deletions
112
tests/language/enum_shorthands/simple/simple_identifier_cascade_test.dart
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,112 @@ | ||
// 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. | ||
|
||
// Context type is propagated down in cascades. | ||
|
||
// SharedOptions=--enable-experiment=enum-shorthands | ||
|
||
import '../enum_shorthand_helper.dart'; | ||
|
||
class Cascade { | ||
late Color color; | ||
late Integer integer; | ||
late IntegerExt integerExt; | ||
late IntegerMixin integerMixin; | ||
} | ||
|
||
class CascadeCollection { | ||
late List<Color> colorList; | ||
late Set<Color> colorSet; | ||
late Map<Color, Color> colorMap; | ||
late Map<Color, (Color, Color)> colorMap2; | ||
|
||
late List<Integer> integerList; | ||
late Set<Integer> integerSet; | ||
late Map<Integer, Integer> integerMap; | ||
late Map<Integer, (Integer, Integer)> integerMap2; | ||
|
||
late List<IntegerExt> integerExtList; | ||
late Set<IntegerExt> integerExtSet; | ||
late Map<IntegerExt, IntegerExt> integerExtMap; | ||
late Map<IntegerExt, (IntegerExt, IntegerExt)> integerExtMap2; | ||
|
||
late List<IntegerMixin> integerMixinList; | ||
late Set<IntegerMixin> integerMixinSet; | ||
late Map<IntegerMixin, IntegerMixin> integerMixinMap; | ||
late Map<IntegerMixin, (IntegerMixin, IntegerMixin)> integerMixinMap2; | ||
} | ||
|
||
class CascadeMethod { | ||
void color(Color color) => print(color); | ||
void integer(Integer integer) => print(integer); | ||
void integerExt(IntegerExt integer) => print(integer); | ||
void integerMixin(IntegerMixin integer) => print(integer); | ||
} | ||
|
||
void main() { | ||
Cascade() | ||
..color = .red | ||
..integer = .one | ||
..integerExt = .one | ||
..integerMixin = .mixinOne; | ||
|
||
dynamic mayBeNull = null; | ||
Cascade() | ||
..color = mayBeNull ?? .red | ||
..integer = mayBeNull ?? .one | ||
..integerExt = mayBeNull ?? .one | ||
..integerMixin = mayBeNull ?? .mixinOne; | ||
|
||
CascadeCollection() | ||
// Enum | ||
..colorList = [.blue, .green, .red] | ||
..colorSet = {.blue, .red} | ||
..colorMap = {.blue: .blue, .green: .red} | ||
..colorMap2 = {.red: (.blue, .green)} | ||
// Class | ||
..integerList = [.one, .two, .one] | ||
..integerSet = {.one, .two} | ||
..integerMap = {.one: .two, .two: .two} | ||
..integerMap2 = { | ||
.one: (.one, .two), | ||
.two: (.two, .two), | ||
} | ||
// Extension type | ||
..integerExtList = [.one, .two, .one] | ||
..integerExtSet = {.one, .two} | ||
..integerExtMap = { | ||
.one: .two, | ||
.two: .two, | ||
} | ||
..integerExtMap2 = { | ||
.one: (.one, .two), | ||
.two: (.two, .two), | ||
} | ||
// Mixin | ||
..integerMixinList = [ | ||
.mixinOne, | ||
.mixinTwo, | ||
.mixinOne, | ||
] | ||
..integerMixinSet = {.mixinOne, .mixinTwo} | ||
..integerMixinMap = { | ||
.mixinOne: .mixinTwo, | ||
.mixinTwo: .mixinTwo, | ||
} | ||
..integerMixinMap2 = { | ||
.mixinOne: (.mixinOne, .mixinTwo), | ||
.mixinTwo: (.mixinTwo, .mixinTwo), | ||
}; | ||
|
||
CascadeMethod() | ||
..color(.red) | ||
..integer(.one) | ||
..integerExt(.one) | ||
..integerMixin(.mixinOne); | ||
|
||
Color color = .blue..toString(); | ||
Integer integer = .one..toString(); | ||
IntegerExt integerExt = .one..toString(); | ||
IntegerMixin integerMixin = .mixinOne..toString(); | ||
} |
64 changes: 64 additions & 0 deletions
64
tests/language/enum_shorthands/simple/simple_identifier_collection_literal_test.dart
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,64 @@ | ||
// 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. | ||
|
||
// Context type is propagated down in collection literals. | ||
|
||
// SharedOptions=--enable-experiment=enum-shorthands | ||
|
||
import '../enum_shorthand_helper.dart'; | ||
|
||
void main() { | ||
// Enum | ||
var colorList = <Color>[.blue, .green, .red]; | ||
var colorSet = <Color>{.blue, .red}; | ||
var colorMap = <Color, Color>{.blue: .blue, .green: .red}; | ||
var colorMap2 = <Color, (Color, Color)>{.red: (.blue, .green)}; | ||
|
||
// Class | ||
var integerList = <Integer>[.one, .two, .one]; | ||
var integerSet = <Integer>{.one, .two}; | ||
var integerMap = <Integer, Integer>{ | ||
.one: .two, | ||
.two: .two, | ||
}; | ||
var integerMap2 = <Integer, (Integer, Integer)>{ | ||
.one: (.one, .two), | ||
.two: (.two, .two), | ||
}; | ||
|
||
// Extension type | ||
var integerExtList = <IntegerExt>[ | ||
.one, | ||
.two, | ||
.one, | ||
]; | ||
var integerExtSet = <IntegerExt>{.one, .two}; | ||
var integerExtMap = <IntegerExt, IntegerExt>{ | ||
.one: .two, | ||
.two: .two, | ||
}; | ||
var integerExtMap2 = <IntegerExt, (IntegerExt, IntegerExt)>{ | ||
.one: (.one, .two), | ||
.two: (.two, .two), | ||
}; | ||
|
||
// Mixin | ||
var integerMixinList = <IntegerMixin>[ | ||
.mixinOne, | ||
.mixinTwo, | ||
.mixinOne, | ||
]; | ||
var integerMixinSet = <IntegerMixin>{ | ||
.mixinOne, | ||
.mixinTwo, | ||
}; | ||
var integerMixinMap = <IntegerMixin, IntegerMixin>{ | ||
.mixinOne: .mixinTwo, | ||
.mixinTwo: .mixinTwo, | ||
}; | ||
var integerMixinMap2 = <IntegerMixin, (IntegerMixin, IntegerMixin)>{ | ||
.mixinOne: (.mixinOne, .mixinTwo), | ||
.mixinTwo: (.mixinTwo, .mixinTwo), | ||
}; | ||
} |
37 changes: 37 additions & 0 deletions
37
tests/language/enum_shorthands/simple/simple_identifier_if_null_test.dart
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,37 @@ | ||
// 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. | ||
|
||
// Context type is propagated down in an if-null `??` expression. | ||
|
||
// SharedOptions=--enable-experiment=enum-shorthands | ||
|
||
import '../enum_shorthand_helper.dart'; | ||
import 'package:expect/expect.dart'; | ||
|
||
Color colorTest(Color? color) => color ?? .blue; | ||
|
||
Integer integerTest(Integer? integer) => integer ?? .one; | ||
|
||
IntegerExt integerExtTest(IntegerExt? integer) => integer ?? .one; | ||
|
||
IntegerMixin integerMixinTest(IntegerMixin? integer) => | ||
integer ?? .mixinOne; | ||
|
||
void main() { | ||
// Enum | ||
Expect.equals(colorTest(null), Color.blue); | ||
Expect.equals(colorTest(Color.red), Color.red); | ||
|
||
// Class | ||
Expect.equals(integerTest(null), Integer.one); | ||
Expect.equals(integerTest(Integer.two), Integer.two); | ||
|
||
// Extension type | ||
Expect.equals(integerExtTest(null), IntegerExt.one); | ||
Expect.equals(integerExtTest(IntegerExt.two), IntegerExt.two); | ||
|
||
// Mixin | ||
Expect.equals(integerMixinTest(null), IntegerMixin.mixinOne); | ||
Expect.equals(integerMixinTest(IntegerMixin.mixinTwo), IntegerMixin.mixinTwo); | ||
} |