Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: tests/compiler/dart2js/js_safety_test.dart

Issue 1510633003: Safety analysis for JS template placeholders. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 'package:expect/expect.dart';
6 import 'package:compiler/src/js/js.dart' as js;
7 import 'package:compiler/src/js/placeholder_safety.dart';
8
9 void test(String source, int expected, {List notNull: const[]}) {
10 var predicate = (int pos) => ! notNull.contains(pos);
11 js.Template template = js.js.parseForeignJS(source);
12 int actual = PlaceholderSafetyAnalysis.analyze(template.ast, predicate);
13 Expect.equals(expected, actual, 'source: "$source", notNull: $notNull');
14 }
15
16
17 void main() {
18 test('0', 0);
19
20 test('#.x', 1);
21 test('!!#.x', 1);
22 test('#.x + 1', 1);
23 test('1 + #.x', 1);
24 test('#[#] + 2', 2);
25 test('2 + #[#]', 2);
26 test('(# & #) >>> 0', 2);
27
28 test('#.a + #.b + #.c', 1);
29 test('#.b + #.b + #.c', 2, notNull: [0]);
30 test('#.c + #.b + #.c', 3, notNull: [0, 1]);
31 test('#.d + #.b + #.c', 1, notNull: [1]);
32
33 test('typeof(#) == "string"', 1);
34 test('"object" === typeof #', 1);
35
36 test('# == 1 || # == 2 || # == 3', 1);
37 test('# != 1 && # != 2 && # != 3', 1);
38
39 test('#.x == 1 || # == 1', 1);
40 test('# == 1 || #.x == 1', 1);
41
42 test('(#, null.a, #)', 1);
43 test('(#, undefined.a, #)', 1);
44 test('(#, (void 0).a, #)', 1);
45 test('(#, "a".a, #)', 2);
46
47 test('#[#][#][#][#]', 2);
48 test('#[#][#][#][#]', 3, notNull: [0]);
49 test('#[#][#][#][#]', 3, notNull: [0, 1, 2, 3]);
50
51 test('#.a = #', 2);
52 test('#.a.b = #', 1);
53 test('#[1] = #', 2);
54 test('#[1][1] = #', 1);
55
56 test('#.a = #.a + #.a + #.a', 2);
57 test('#.a = #.a + #.a + #.a', 2, notNull: [0]);
58 test('#.a = #.a + #.a + #.a', 3, notNull: [1]);
59 test('#.a = #.a + #.a + #.a', 4, notNull: [1, 2]);
60
61 test('#()', 1);
62 test('#(#, #)', 3);
63 test('#.f(#, #)', 1);
64 test('#.f(#, #)', 3, notNull: [0]);
65
66 test('(#.a+=1, #)', 1);
67 test('(#.a++, #)', 1);
68 test('(++#.a, #)', 1);
69
70 test('new Array(#)', 1);
71 test('new Date(#)', 1);
72 test('new Function(#)', 1);
73 test('new xxx(#)', 0);
74 test('# in #', 2);
75
76 test('Object.keys(#)', 1);
77
78 test('typeof #', 1);
79 test('typeof #.foo', 1);
80 test('typeof foo.#', 0);
81 test('typeof Array.#', 1);
82
83 test('throw #', 1);
84 test('throw #.x', 1);
85
86 test('(function(){})()', 0);
87 test('(function(a,b){#})(#, #)', 0);
88 // Placeholders in an immediate call are ok.
89 test('(function(a,b){a++;b++;return a+b})(#, #)', 2);
90 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698