OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2015, 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 import 'dart:async'; | |
6 import 'package:async_helper/async_helper.dart'; | |
7 import 'package:expect/expect.dart'; | |
8 import 'compiler_helper.dart'; | |
9 | |
10 const String TEST_1 = r""" | |
11 import 'dart:_foreign_helper'; | |
12 main() { | |
13 // present: 'Moose' | |
14 JS('', 'Moose'); | |
15 | |
16 // absent: 'Phantom' - pure. | |
17 JS('returns: bool;effects:none;depends:none;throws:never', 'Phantom'); | |
18 | |
19 // present: 'Spider' - unused after constant folding 'is', but unpure. | |
20 print(JS('returns:bool;effects:none;depends:all', 'Spider') is bool); | |
21 | |
22 // absent: 'Wasp' - unused after constant folding 'is', and unpure. | |
23 print(JS('returns:bool;effects:none;depends:all;throws:never', 'Wasp') is bo ol); | |
floitsch
2015/04/13 08:07:14
nit: long line.
sra1
2015/04/13 23:59:05
Done.
| |
24 | |
25 JS('', 'Array'); // absent: "Array" | |
26 } | |
27 """; | |
28 | |
29 const String TEST_2 = r""" | |
30 import 'dart:_foreign_helper'; | |
31 main() { | |
32 var w1 = JS('returns:int;depends:none;effects:none;throws:never', | |
33 'foo(#)', 1); | |
34 var w2 = JS('returns:int;depends:none;effects:none;throws:never', | |
35 'foo(#)', 2); | |
36 | |
37 print([w2, w1]); | |
38 | |
39 // present: '[foo(2), foo(1)]' - since 'foo' is pure, we expect to generate | |
40 // code out-of-order. | |
41 } | |
42 """; | |
43 | |
44 const String TEST_3 = r""" | |
45 import 'dart:_foreign_helper'; | |
46 main() { | |
47 var s = JS('String|Null', '"Hello"'); | |
48 var s1 = JS('returns:String;depends:none;effects:none;throws:null(1)', | |
49 '#.toLowerCase()', s); | |
50 var s2 = JS('returns:String;depends:none;effects:none;throws:null(1)', | |
51 '#.toUpperCase()', s); | |
52 print(s2); | |
53 | |
54 // absent: 'toLowerCase' - removed since s.toUpperCase() generates the same | |
55 // noSuchMethod. | |
56 } | |
57 """; | |
58 | |
59 | |
60 main() { | |
61 RegExp directivePattern = new RegExp( | |
62 // \1 \2 \3 | |
63 r'''// *(present|absent): (?:"([^"]*)"|'([^'']*)')''', | |
64 multiLine: true); | |
65 | |
66 Future check(String test) { | |
67 Uri uri = new Uri(scheme: 'dart', path: 'test'); | |
68 var compiler = compilerFor(test, uri, expectedErrors: 0); | |
69 return compiler.runCompiler(uri).then((_) { | |
70 var element = findElement(compiler, 'main'); | |
71 var backend = compiler.backend; | |
72 String generated = backend.assembleCode(element); | |
73 | |
74 for (Match match in directivePattern.allMatches(test)) { | |
75 String directive = match.group(1); | |
76 String pattern = match.groups([2, 3]).where((s) => s != null).single; | |
77 if (directive == 'present') { | |
78 Expect.isTrue(generated.contains(pattern), | |
79 "Cannot find '$pattern' in:\n$generated"); | |
80 } else { | |
81 assert(directive == 'absent'); | |
82 Expect.isFalse(generated.contains(pattern), | |
83 "Must not find '$pattern' in:\n$generated"); | |
84 } | |
85 } | |
86 }); | |
87 } | |
88 | |
89 asyncTest(() => Future.wait([ | |
90 check(TEST_1), | |
91 check(TEST_2), | |
92 check(TEST_3), | |
93 ])); | |
94 } | |
OLD | NEW |