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

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: Make predicate positional 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
« no previous file with comments | « pkg/compiler/lib/src/tree_ir/optimization/statement_rewriter.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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('(# || 1, #)', 1); // Could also be 2.
43
44 test('(#, null.a, #)', 1);
45 test('(#, undefined.a, #)', 1);
46 test('(#, (void 0).a, #)', 1);
47 test('(#, "a".a, #)', 2);
48 test('((#, "a").a, #)', 2);
49
50 test('#[#][#][#][#]', 2);
51 test('#[#][#][#][#]', 3, notNull: [0]);
52 test('#[#][#][#][#]', 3, notNull: [0, 1, 2, 3]);
53
54 test('#.a = #', 2);
55 test('#.a.b = #', 1);
56 test('#[1] = #', 2);
57 test('#[1][1] = #', 1);
58
59 test('#.a = #.a + #.a + #.a', 2);
60 test('#.a = #.a + #.a + #.a', 2, notNull: [0]);
61 test('#.a = #.a + #.a + #.a', 3, notNull: [1]);
62 test('#.a = #.a + #.a + #.a', 4, notNull: [1, 2]);
63
64 test('#()', 1);
65 test('#(#, #)', 3);
66 test('#.f(#, #)', 1);
67 test('#.f(#, #)', 3, notNull: [0]);
68
69 test('(#.a+=1, #)', 1);
70 test('(#.a++, #)', 1);
71 test('(++#.a, #)', 1);
72
73 test('new Array(#)', 1);
74 test('new Date(#)', 1);
75 test('new Function(#)', 1);
76 test('new RegExp(#)', 1);
77 test('new xxx(#)', 0);
78 test('String(#)', 1);
79 test('# in #', 2);
80
81 test('Object.keys(#)', 1);
82
83 test('typeof #', 1);
84 test('typeof #.foo', 1);
85 test('typeof foo.#', 0);
86 test('typeof Array.#', 1);
87
88 test('throw #', 1);
89 test('throw #.x', 1);
90
91 test('(function(){})()', 0);
92 test('(function(a,b){#})(#, #)', 0);
93 // Placeholders in an immediate call are ok.
94 test('(function(a,b){a++;b++;return a+b})(#, #)', 2);
95
96 test('# ? # : #', 1);
97 test('(# ? 1 : #, #)', 1);
98 test('(# ? # : 2, #)', 1);
99 test('(# ? 1 : 2, #)', 1); // Could also be 4.
100
101 test('{A:#, B:#, C:#}', 3);
102 test('[#,#,#,#]', 4);
103 test('[,,,,#,#,,,]', 2);
104 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/tree_ir/optimization/statement_rewriter.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698