Debian Codegraph @

test/com/google/javascript/jscomp/GatherRawExportsTest.java

raw symbols SCIP
1 /*
2 * Copyright 2009 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import com.google.common.collect.Sets;
20
21 import java.util.Set;
22
23 /**
24 * Tests for {@link GatherRawExports}.
25 *
26 * @author [email protected] (John Lenz)
27 */
28 public class GatherRawExportsTest extends CompilerTestCase {
29
30 private static final String EXTERNS = "var window;";
31 private GatherRawExports last;
32
33 public GatherRawExportsTest() {
34 super(EXTERNS);
35 super.enableNormalize();
36 }
37
38 @Override
39 protected CompilerPass getProcessor(Compiler compiler) {
40 last = new GatherRawExports(compiler);
41 return last;
42 }
43
44 public void testExportsFound1() {
45 assertExported("var a");
46 }
47
48 public void testExportsFound2() {
49 assertExported("window['a']", "a");
50 }
51
52 public void testExportsFound3() {
53 assertExported("window.a", "a");
54 }
55
56 public void testExportsFound4() {
57 assertExported("this['a']", "a");
58 }
59
60 public void testExportsFound5() {
61 assertExported("this.a", "a");
62 }
63
64 public void testExportsFound6() {
65 assertExported("function f() { this['a'] }");
66 }
67
68 public void testExportsFound7() {
69 assertExported("function f() { this.a }");
70 }
71
72 public void testExportsFound8() {
73 assertExported("window['foo']", "foo");
74 }
75
76 public void testExportsFound9() {
77 assertExported("window['a'] = 1;", "a");
78 }
79
80 public void testExportsFound10() {
81 assertExported("window['a']['b']['c'] = 1;", "a");
82 }
83
84 public void testExportsFound11() {
85 assertExported("if (window['a'] = 1) alert(x);", "a");
86 }
87
88 public void testExportsFound12() {
89 assertExported("function foo() { window['a'] = 1; }", "a");
90 }
91
92 public void testExportsFound13() {
93 assertExported("function foo() {var window; window['a'] = 1; }");
94 }
95
96 public void testExportsFound14() {
97 assertExported("var a={window:{}}; a.window['b']");
98 }
99
100 public void testExportsFound15() {
101 assertExported("window.window['b']", "window");
102 }
103
104 public void testExportsFound16() {
105 // It would be nice to handle this case, hopefully inlining will take care
106 // of it for us.
107 assertExported("var a = window; a['b']");
108 }
109
110 public void testExportOnTopFound1() {
111 assertExported("top['a']", "a");
112 }
113
114 public void testExportOntopFound2() {
115 assertExported("top.a", "a");
116 }
117
118 private void assertExported(String js, String ... names) {
119 Set<String> setNames = Sets.newHashSet(names);
120 testSame(js);
121 assertTrue(last.getExportedVariableNames().equals(setNames));
122 }
123 }