Migrate language_2/enum to NNBD.

Change-Id: I6d1045d95d0ea1c7e9a1f9fa8524687ed4dd8480
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/142562
Auto-Submit: Bob Nystrom <rnystrom@google.com>
Commit-Queue: Erik Ernst <eernst@google.com>
Reviewed-by: Erik Ernst <eernst@google.com>
This commit is contained in:
Robert Nystrom
2020-04-09 14:47:19 +00:00
committed by commit-bot@chromium.org
parent e5616e7b4c
commit bcf8a7fc51
13 changed files with 450 additions and 0 deletions
+14
View File
@@ -0,0 +1,14 @@
// Copyright (c) 2015, 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.
library enum_duplicate_lib;
enum Enum1 {
A,
B,
}
class Enum2 {
static Iterable get values => ['Enum2.A', 'Enum2.B'];
}
+29
View File
@@ -0,0 +1,29 @@
// Copyright (c) 2015, 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.
// Test for duplicate enums.
library enum_duplicate_test;
import 'package:expect/expect.dart';
import 'duplicate_lib.dart' as lib; //# 01: ok
import 'duplicate_lib.dart' as lib; //# 02: ok
enum Enum1 {
A,
B,
}
enum Enum2 {
A,
B,
}
main() {
Expect.equals('Enum1.A,Enum1.B', Enum1.values.join(','));
Expect.equals('Enum1.A,Enum1.B', lib.Enum1.values.join(',')); //# 01: continued
Expect.equals('Enum2.A,Enum2.B', Enum2.values.join(','));
Expect.equals('Enum2.A,Enum2.B', lib.Enum2.values.join(',')); //# 02: continued
}
+153
View File
@@ -0,0 +1,153 @@
// Copyright (c) 2014, 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.
import 'package:expect/expect.dart';
enum Enum1 { _ }
enum Enum2 { A }
enum Enum3 { B, C }
enum Enum4 {
D,
E,
}
enum Enum5 { F, G, H }
enum _Enum6 { I, _J }
enum _IsNot { IsNot }
// Regression test for https://github.com/dart-lang/sdk/issues/33348
enum JSFunctionPrototype {
length,
prototype,
__proto__,
arguments,
caller,
calee,
name,
constructor,
apply,
bind,
call
}
void expectIs<T>(T t, bool Function(Object?) test) {
Object? obj = t;
Expect.isTrue(test(obj), '$obj is $T');
Expect.isFalse(obj is _IsNot, '$obj is _IsNot');
// test cast
t = obj as T;
Expect.throwsTypeError(() => obj as _IsNot, '$obj as _IsNot');
}
main() {
expectIs(Enum1._, (e) => e is Enum1);
expectIs(Enum2.A, (e) => e is Enum2);
expectIs(Enum3.B, (e) => e is Enum3);
expectIs(Enum4.E, (e) => e is Enum4);
expectIs(Enum5.G, (e) => e is Enum5);
Expect.equals('Enum1._', Enum1._.toString());
Expect.equals(0, Enum1._.index);
Expect.listEquals([Enum1._], Enum1.values);
Enum1.values.forEach(test1);
Expect.equals('Enum2.A', Enum2.A.toString());
Expect.equals(0, Enum2.A.index);
Expect.listEquals([Enum2.A], Enum2.values);
Expect.identical(const <Enum2>[Enum2.A], Enum2.values);
Enum2.values.forEach(test2);
Expect.equals('Enum3.B', Enum3.B.toString());
Expect.equals('Enum3.C', Enum3.C.toString());
Expect.equals(0, Enum3.B.index);
Expect.equals(1, Enum3.C.index);
Expect.listEquals([Enum3.B, Enum3.C], Enum3.values);
Enum3.values.forEach(test3);
Expect.equals('Enum4.D', Enum4.D.toString());
Expect.equals('Enum4.E', Enum4.E.toString());
Expect.equals(0, Enum4.D.index);
Expect.equals(1, Enum4.E.index);
Expect.listEquals([Enum4.D, Enum4.E], Enum4.values);
Enum4.values.forEach(test4);
Expect.equals('Enum5.F', Enum5.F.toString());
Expect.equals('Enum5.G', Enum5.G.toString());
Expect.equals('Enum5.H', Enum5.H.toString());
Expect.equals(0, Enum5.F.index);
Expect.equals(1, Enum5.G.index);
Expect.equals(2, Enum5.H.index);
Expect.listEquals([Enum5.F, Enum5.G, Enum5.H], Enum5.values);
Enum5.values.forEach(test5);
Expect.equals('_Enum6.I', _Enum6.I.toString());
Expect.equals('_Enum6._J', _Enum6._J.toString());
for (var value in JSFunctionPrototype.values) {
expectIs(value, (e) => e is JSFunctionPrototype);
}
Expect.equals(JSFunctionPrototype.length, JSFunctionPrototype.values[0]);
}
test1(Enum1 e) {
int index;
switch (e) {
case Enum1._:
index = 0;
break;
}
Expect.equals(e.index, index);
}
test2(Enum2 e) {
int index;
switch (e) {
case Enum2.A:
index = 0;
break;
}
Expect.equals(e.index, index);
}
test3(Enum3 e) {
int index;
switch (e) {
case Enum3.C:
index = 1;
break;
case Enum3.B:
index = 0;
break;
}
Expect.equals(e.index, index);
}
test4(Enum4 e) {
int index;
switch (e) {
case Enum4.D:
index = 0;
break;
case Enum4.E:
index = 1;
break;
}
Expect.equals(e.index, index);
}
test5(Enum5 e) {
int index;
switch (e) {
case Enum5.H:
index = 2;
break;
case Enum5.F:
index = 0;
break;
case Enum5.G:
index = 1;
break;
}
Expect.equals(e.index, index);
}
+28
View File
@@ -0,0 +1,28 @@
// Copyright (c) 2015, 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.
// Test index access for enums.
library enum_index_test;
import 'package:expect/expect.dart';
enum Enum {
A,
B,
}
class Class {
var index;
}
main() {
test(null, new Class());
test(0, Enum.A);
test(1, Enum.B);
}
test(expected, object) {
Expect.equals(expected, object.index);
}
@@ -0,0 +1,33 @@
// Copyright (c) 2018, 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.
// https://github.com/flutter/flutter/issues/25041
// This test may produce a compile time exception from stack overflow during
// enum initialization or succeed in enum initialization depending on exactly
// how much stack is left and used by the compiler. It should never crash nor
// produce a runtime exception.
enum Fruit {
apple,
banana,
}
getFruit() => Fruit.apple;
recurse() {
try {
recurse();
} catch (e, st) {
print("$e ${getFruit()}");
}
}
main() {
try {
recurse();
} on StackOverflowError catch (e) {
// Swallow.
}
}
@@ -0,0 +1,13 @@
// TODO(multitest): This was automatically migrated from a multitest and may
// contain strange or dead code.
// Copyright (c) 2015, 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.
// Test that `enum` is considered a keyword and therefore invalid as the name of
// declarations.
main() {
}
+13
View File
@@ -0,0 +1,13 @@
// Copyright (c) 2015, 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.
// Test that `enum` is considered a keyword and therefore invalid as the name of
// declarations.
main() {
var enum;
// ^^^^
// [analyzer] SYNTACTIC_ERROR.MISSING_IDENTIFIER
// [cfe] Expected an identifier, but got 'enum'.
}
+10
View File
@@ -0,0 +1,10 @@
// Copyright (c) 2015, 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.
library enum_private_lib;
enum Enum2 {
_A,
_B,
}
@@ -0,0 +1,25 @@
// TODO(multitest): This was automatically migrated from a multitest and may
// contain strange or dead code.
// Copyright (c) 2015, 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.
// Test privacy issue for enums.
library enum_private_test;
import 'package:expect/expect.dart';
import 'private_lib.dart';
enum Enum1 {
_A,
_B,
}
main() {
Expect.equals('Enum1._A,Enum1._B', Enum1.values.join(','));
Expect.equals('Enum2._A,Enum2._B', Enum2.values.join(','));
}
@@ -0,0 +1,25 @@
// TODO(multitest): This was automatically migrated from a multitest and may
// contain strange or dead code.
// Copyright (c) 2015, 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.
// Test privacy issue for enums.
library enum_private_test;
import 'package:expect/expect.dart';
import 'private_lib.dart';
enum Enum1 {
_A,
_B,
}
main() {
Expect.equals('Enum1._A,Enum1._B', Enum1.values.join(','));
}
+25
View File
@@ -0,0 +1,25 @@
// Copyright (c) 2015, 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.
// Test privacy issue for enums.
library enum_private_test;
import 'package:expect/expect.dart';
import 'private_lib.dart';
enum Enum1 {
_A,
_B,
}
main() {
Expect.equals('Enum1._A,Enum1._B', Enum1.values.join(','));
Expect.equals('Enum2._A,Enum2._B', Enum2.values.join(','));
Expect.throwsNoSuchMethodError(() => Enum2._A);
// ^^
// [analyzer] STATIC_TYPE_WARNING.UNDEFINED_ENUM_CONSTANT
// [cfe] Getter not found: '_A'.
}
+71
View File
@@ -0,0 +1,71 @@
// Copyright (c) 2014, 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.
// Basic syntax test for enumeration types
enum Color { red, orange, yellow, green }
// Additional comma at end of list is ok.
enum Veggies {
carrot,
bean,
broccolo,
}
// Need at least one enumeration identifier.
enum Nada {} // //# 01: syntax error
// Duplicate entries are a compile-time error
enum ComeAgain { ahau, knust, zipfel, knust, gupf } // //# 02: compile-time error
// Enum entries must not collide with implicitly defined members.
enum ComeAgain { ahau, knust, zipfel, index } //# 03: compile-time error
enum ComeAgain { ahau, knust, zipfel, values } //# 04: compile-time error
enum ComeAgain { ahau, knust, zipfel, toString } //# 05: compile-time error
// Enum entry must not collide with enum type name.
enum ComeAgain { ahau, knust, zipfel, ComeAgain } //# 06: compile-time error
// Missing comma.
enum Numbers { one, two, three four, five } // //# 07: syntax error
// Missing enum type name.
enum { eins, zwei, drei } // //# 08: syntax error
// Duplicate name in library scope.
topLevelFunction() => null;
enum topLevelFunction { bla, blah } // //# 09: compile-time error
class C {}
enum C { bla, blah } // //# 10: compile-time error
var zzTop;
enum zzTop { Billy, Dusty, Frank } // //# 11: compile-time error
// Enum type cannot be super type or interface type.
class Rainbow extends Color {} // //# 20: compile-time error
class Rainbow implements Color {} // //# 21: compile-time error
class Rainbow extends List with Color {} // //# 22: compile-time error
main() {
Nada x; //# 01: continued
var x = ComeAgain.zipfel; // //# 02: continued
var x = ComeAgain.zipfel; // //# 03: continued
var x = ComeAgain.zipfel; // //# 04: continued
var x = ComeAgain.zipfel; // //# 05: continued
var x = ComeAgain.zipfel; // //# 06: continued
var x = Numbers.four; // //# 07: continued
var x = topLevelFunction.bla; // //# 09: continued
var x = C.bla; // //# 10: continued
var x = zzTop.Frank; // //# 11: continued
var x = new Rainbow(); // //# 20: continued
var x = new Rainbow(); // //# 21: continued
var x = new Rainbow(); // //# 22: continued
// It is a compile-time error to explicitly instantiate an enum instance.
var x = new Color(); // //# 30: compile-time error
}
+11
View File
@@ -0,0 +1,11 @@
// Copyright (c) 2016, 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.
import 'package:expect/expect.dart';
enum ErrorContext { general, name, description, targets }
void main() {
Expect.equals(ErrorContext.name, ErrorContext.name);
}