-
Notifications
You must be signed in to change notification settings - Fork 34
/
enum_test.dart
101 lines (91 loc) · 2.72 KB
/
enum_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import 'package:dart_eval/dart_eval.dart';
import 'package:dart_eval/dart_eval_bridge.dart';
import 'package:dart_eval/src/eval/shared/stdlib/core/num.dart';
import 'package:test/test.dart';
void main() {
group('Enum tests', () {
late Compiler compiler;
setUp(() {
compiler = Compiler();
});
test('Basic enum', () {
final runtime = compiler.compileWriteAndLoad({
'example': {
'main.dart': '''
enum MyEnum {
A, B, C
}
int main() {
return MyEnum.B.index + MyEnum.C.index;
}
'''
}
});
expect(runtime.executeLib('package:example/main.dart', 'main'), 3);
});
test('Enum with field', () {
final runtime = compiler.compileWriteAndLoad({
'example': {
'main.dart': '''
enum MyEnum {
A(1), B(2);
final int x;
const MyEnum(this.x);
}
int main() {
return MyEnum.B.index + MyEnum.A.x;
}
'''
}
});
expect(runtime.executeLib('package:example/main.dart', 'main'), 2);
});
test('Enum equality', () {
final runtime = compiler.compileWriteAndLoad({
'example': {
'main.dart': '''
enum MyEnum {
A, B, C
}
void main() {
print(MyEnum.B == MyEnum.C);
print(MyEnum.B == MyEnum.B);
}
'''
}
});
expect(() => runtime.executeLib('package:example/main.dart', 'main'),
prints('false\ntrue\n'));
});
test('Enum boxing error', () {
final compiler2 = Compiler();
compiler2.defineBridgeEnum(BridgeEnumDef(
BridgeTypeRef(
BridgeTypeSpec('package:my_package/show.dart', 'ShowType')),
values: ['Movie', 'Series']));
final program = compiler2.compile({
'my_package': {
'main.dart': r'''
import 'show.dart';
class Media {
Media(this.type, this.url);
final ShowType type;
String? url;
}
void main() {
final requiredType = 'movie';
final type = (requiredType == 'movie') ?
ShowType.Movie : ShowType.Series;
final media = Media(type, 'example.com');
print(media.url);
}
'''
}
});
final runtime = Runtime.ofProgram(program);
runtime.registerBridgeEnumValues('package:my_package/show.dart',
'ShowType', {'Movie': $int(0), 'Series': $int(1)});
runtime.executeLib('package:my_package/main.dart', 'main');
});
});
}