-
Notifications
You must be signed in to change notification settings - Fork 40
/
functional1_test.dart
397 lines (350 loc) · 11.4 KB
/
functional1_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import 'package:dart_eval/dart_eval.dart';
import 'package:dart_eval/dart_eval_bridge.dart';
import 'package:dart_eval/stdlib/core.dart';
import 'package:test/test.dart';
void main() {
group('Functional tests', () {
late Compiler compiler;
setUp(() {
compiler = Compiler();
});
test('Functional test 1', () {
final source = '''
dynamic main() {
var someNumber = 19;
var result = 0;
var a = A(45);
for (var i = someNumber; i < 45; i++) {
final n = a.calculate(i);
result += n;
if ((n % i) > a.number + 18) {
a = B(58);
} else {
if (a.number * i * 2 > B(a.number).calculate(2)) {
a = C(13 + a.number);
}
someNumber = someNumber + 1;
}
}
result += D(21 + a.number).calculate(45);
return result;
}
class A {
final int number;
A(this.number);
int calculate(int other) {
return number + other;
}
}
class B extends A {
B(int number) : super(number);
@override
int calculate(int other) {
var d = 1334;
for (var i = 0; i < 15 + number; i = i + 1) {
if (d > 4000) {
d = d - 14;
}
d += i;
}
return d;
}
}
class C extends A {
C(int number) : super(number);
@override
int calculate(int other) {
var d = 1556;
for (var i = 0; i < 24 - number; i = i + 1) {
if (d > 4000) {
d = d - 14;
} else if (d < 299) {
d = d + 5 + 5;
}
d += i;
}
return d;
}
}
class D extends A {
D(int number) : super(number);
@override
int calculate(int other) {
var d = 1334;
for (var i = 0; i < 15 + number; i = i + 1) {
if (d > 4000) {
d = d - 14;
}
d += super.number;
}
return d;
}
}''';
final runtime = compiler.compileWriteAndLoad({
'example': {'main.dart': source}
});
final result = runtime.executeLib('package:example/main.dart', 'main');
expect(result, $int(45646));
});
test('Sum to', () {
final source = '''
void main() {
print(calc(50));
}
int calc(int sumTo) {
if (sumTo == 13) {
print("unlucky number!");
}
int accum = 0;
for (int i = 0; i < sumTo; i++) {
final b = i < sumTo;
accum = accum + i;
}
return accum;
}''';
final runtime = compiler.compileWriteAndLoad({
'example': {'main.dart': source}
});
expect(() {
runtime.executeLib('package:example/main.dart', 'main');
}, prints('1225\n'));
});
test('Await chain', () async {
final source = '''
import 'dart:async';
Future<int> main() async {
func1();
func2();
func3();
await Future.delayed(Duration(microseconds: 9500));
print("complete");
return func4();
}
void func1() async {
await Future.delayed(Duration(microseconds: 6500));
print("func1");
}
void func2() async {
await Future.delayed(Duration(microseconds: 200));
print("func2");
}
void func3() async {
print("func3");
}
Future<int> func4() async {
print("func4 start");
await Future.delayed(Duration(milliseconds: 20));
print("func4 end");
return 1;
}
''';
final runtime = compiler.compileWriteAndLoad({
'example': {'main.dart': source}
});
expect(() async {
expect(await runtime.executeLib('package:example/main.dart', 'main'),
$int(1));
}, prints('func3\nfunc2\nfunc1\ncomplete\nfunc4 start\nfunc4 end\n'));
});
test('String split loop', () {
final source = r'''
List<String> test() {
const cat = "Fluffy";
var list = cat.split("ff");
/// ------ add Code Start ---------
int length = list.length;
print('length -> $length');
for(var i = 0 ; i < length; i ++){
print('i => $i');
}
/// ------ add Code End ---------
return list;
}
''';
final runtime = compiler.compileWriteAndLoad({
'example': {'main.dart': source}
});
expect(() {
runtime.executeLib('package:example/main.dart', 'test');
}, prints('length -> 2\ni => 0\ni => 1\n'));
});
test('Matches test from Readme', () {
final runtime = compiler.compileWriteAndLoad({
'my_package': {
'main.dart': '''
import 'package:my_package/finder.dart';
void main() {
final finder = Finder('Hello (world)');
final parentheses = finder.findParentheses();
if (parentheses.isNotEmpty) print(parentheses);
}
''',
'finder.dart': r'''
class Finder {
final String string;
Finder(this.string);
List<int> findParentheses() {
final regex = RegExp(r'\((.*?)\)');
final matches = regex.allMatches(string);
return matches.map((match) => match.start).toList();
}
}
'''
}
});
expect(() {
runtime.executeLib('package:my_package/main.dart', 'main');
}, prints('[6]\n'));
});
test('Matches test from Readme using ofProgram', () {
final program = compiler.compile({
'my_package': {
'main.dart': '''
import 'package:my_package/finder.dart';
void main() {
final finder = Finder('Hello (world)');
final parentheses = finder.findParentheses();
if (parentheses.isNotEmpty) print(parentheses);
}
''',
'finder.dart': r'''
class Finder {
final String string;
Finder(this.string);
List<int> findParentheses() {
final regex = RegExp(r'\((.*?)\)');
final matches = regex.allMatches(string);
return matches.map((match) => match.start).toList();
}
}
'''
}
});
final runtime = Runtime.ofProgram(program);
expect(() {
runtime.executeLib('package:my_package/main.dart', 'main');
}, prints('[6]\n'));
});
/// https://github.com/ethanblake4/dart_eval/issues/137
test('Regexp firstMatch bug', () {
final runtime = compiler.compileWriteAndLoad({
'extensions_test': {
'main.dart': '''main() {
var episode = RegExp(r'\\d+').firstMatch('episode 1');
return episode;
}'''
}
});
final value = runtime.executeLib(
'package:extensions_test/main.dart',
'main',
);
expect((value as RegExpMatch).group(0), '1');
});
test('Bridged enum equality ternary assignment', () {
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 media = Media(ShowType.Movie, 'example.com');
final url = media.type == ShowType.Movie ?
media.url = 'movie.com' : null;
print(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');
});
test('Regex replacement loop', () {
final source = r'''
main() {
print(transform(
"ZAAiPZZiZAPZZAiAPZZZZZPZZiiAPZZAiZPZZiAAPZZZZAPZZZZZPZZAZPZAAiPZAAZ"
"PZZAZPZZAAAPZZAZPZZZAAPZZiZZPZZAiZPZZAiAPZZZAAPZZZZZPZZAZPZZiZZPZZAZ"
"iPZZiiiPZZZiAPZAAiPAAAiPZZZAAPZZAAiPZZZAiPZZAiZPZZZZAPZiiZZPZiZZZPAA"
"ZZPZZiZZPZZAiiPZiiZZPZZAiiPZZZZAPZZZZZPAiZiPZZiZiPZZiZZPZZiiAPZZZAAP"
"ZZAAiPZZZAiPZZAiZPZAAiPZiiZZPZiZZZPZZiiAPZZZAAPZZiAAPZZZAiPZZAiZPAii"
"ZPZZAZPZAZAPZZAiZPZiZZZPZiiZiPZZAiiPAAAAPAZZZPZiiiiPZZiAiPZZZAiPZiZZ"
"iPZZZiZPZZAZiPAiAAPZiZiAPZiZiAPZiZiAPZAZAPZAAZPZAAZPAZAZPZZAZPZAiiZP"
"ZAAZPZAAiPZAAZP", 45, "iZAPgKQVv", 11, 3));
}
String transform(String c, int d, String f, int a, int g) {
var e = "";
for (var h = 0, k = c.length; h < k; h++) {
String l = "";
while (c[h] != f[g]) {
l += c[h];
h++;
}
for (var m = 0; m < f.length; m++) {
l = l.replaceAll(RegExp(f[m], multiLine: true), m.toString());
}
print(g);
e += l;
}
return e;
}
''';
final runtime = compiler.compileWriteAndLoad({
'example': {'main.dart': source}
});
expect(() {
runtime.executeLib('package:example/main.dart', 'main');
},
prints('3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n'
'3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n'
'3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n'
'3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n3\n'
'3\n3\n3\n3\n3\n3\n'
'1220110121120211111110021120111022111121111111211220122111211122'
'2112111122110111120111202111221111111211101111210110001110212202'
'2201112211220111201120111112100111011122111101111200100111120011'
'1121111120101101011011110021112211220111201120112201001110111110'
'0211122110221112011201200111211212112011011110010112002222211110'
'0001102011120101101110111210202210102101021010212121221122121211'
'12112001122112201221\n'));
});
test('Default parameter boxing error', () {
final source = r'''
void main() async {
final interceptor = AccessTokenInterceptor();
print(await interceptor.getAccessToken());
}
class AccessTokenInterceptor {
Future<String> getAccessToken([bool force = false]) async {
final String? token = null;
if (!force && token != null) {
return 'decoded';
} else {
return refreshAccessToken();
}
}
Future<String> refreshAccessToken([bool force = true]) async {
return 'refreshed';
}
}
''';
final runtime = compiler.compileWriteAndLoad({
'example': {'main.dart': source}
});
expect(() async {
await runtime.executeLib('package:example/main.dart', 'main');
}, prints('refreshed\n'));
});
});
}