Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2012 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 /**
20 * Tests {@link AngularPass}.
21 */
22 public class AngularPassTest extends CompilerTestCase {
23
24 public AngularPassTest() {
25 super();
26 enableLineNumberCheck(false);
27 }
28
29 @Override
30 protected CompilerPass getProcessor(Compiler compiler) {
31 return new AngularPass(compiler);
32 }
33
34 @Override
35 protected int getNumRepetitions() {
36 // This pass only runs once.
37 return 1;
38 }
39
40 @Override
41 protected CompilerOptions getOptions() {
42 CompilerOptions options = new CompilerOptions();
43 // enables angularPass.
44 options.angularPass = true;
45 return getOptions(options);
46 }
47
48 public void testNgInjectAddsInjectToFunctions() throws Exception {
49 test(
50 "/** @ngInject */" +
51 "function fn(a, b) {}",
52
53 "function fn(a, b) {}\n" +
54 "fn['$inject']=['a', 'b']"
55 );
56
57 testSame(
58 "function fn(a, b) {}"
59 );
60 }
61
62 public void testNgInjectAddsInjectToProps() throws Exception {
63 test(
64 "var ns = {};\n" +
65 "/** @ngInject */" +
66 "ns.fn = function (a, b) {}",
67
68 "var ns = {};\n" +
69 "ns.fn = function (a, b) {}\n" +
70 "ns.fn['$inject']=['a', 'b']"
71 );
72
73 testSame(
74 "var ns = {};\n" +
75 "ns.fn = function (a, b) {}"
76 );
77 }
78
79 public void testNgInjectAddsInjectToNestedProps() throws Exception {
80 test(
81 "var ns = {}; ns.subns = {};\n" +
82 "/** @ngInject */" +
83 "ns.subns.fn = function (a, b) {}",
84
85 "var ns = {};ns.subns = {};\n" +
86 "ns.subns.fn = function (a, b) {}\n" +
87 "ns.subns.fn['$inject']=['a', 'b']"
88 );
89
90 testSame(
91 "var ns = {};\n" +
92 "ns.fn = function (a, b) {}"
93 );
94 }
95
96 public void testNgInjectAddsInjectToVars() throws Exception {
97 test(
98 "/** @ngInject */" +
99 "var fn = function (a, b) {}",
100
101 "var fn = function (a, b) {};\n" +
102 "fn['$inject']=['a', 'b']"
103 );
104
105 testSame(
106 "var fn = function (a, b) {}"
107 );
108 }
109
110 public void testNgInjectAddsInjectToVarsWithChainedAssignment()
111 throws Exception {
112 test(
113 "var ns = {};\n" +
114 "/** @ngInject */" +
115 "var fn = ns.func = function (a, b) {}",
116
117 "var ns = {}; var fn = ns.func = function (a, b) {};\n" +
118 "fn['$inject']=['a', 'b']"
119 );
120
121 testSame(
122 "var ns = {};\n" +
123 "var fn = ns.func = function (a, b) {}"
124 );
125 }
126
127 public void testNgInjectInBlock() throws Exception {
128 test(
129 "(function() {" +
130 "var ns = {};\n" +
131 "/** @ngInject */" +
132 "var fn = ns.func = function (a, b) {}" +
133 "})()",
134
135 "(function() {" +
136 "var ns = {}; var fn = ns.func = function (a, b) {};\n" +
137 "fn['$inject']=['a', 'b']" +
138 "})()"
139 );
140
141 testSame(
142 "(function() {" +
143 "var ns = {};\n" +
144 "var fn = ns.func = function (a, b) {}" +
145 "})()"
146 );
147 }
148
149 public void testNgInjectAddsToTheRightBlock() throws Exception {
150 test(
151 "var fn = 10;\n" +
152 "(function() {" +
153 "var ns = {};\n" +
154 "/** @ngInject */" +
155 "var fn = ns.func = function (a, b) {}" +
156 "})()",
157
158 "var fn = 10;" +
159 "(function() {" +
160 "var ns = {}; var fn = ns.func = function (a, b) {};\n" +
161 "fn['$inject']=['a', 'b']" +
162 "})()"
163 );
164 }
165
166 public void testNgInjectInNonBlock() throws Exception {
167 test(
168 "function fake(){}; var ns = {};" +
169 "fake(" +
170 "/** @ngInject */" +
171 "ns.func = function (a, b) {}" +
172 ")",
173 null,
174 AngularPass.INJECT_IN_NON_GLOBAL_OR_BLOCK_ERROR
175 );
176
177 test(
178 "/** @ngInject */(" +
179 "function (a, b) {}" +
180 ")",
181 null,
182 AngularPass.INJECT_IN_NON_GLOBAL_OR_BLOCK_ERROR
183 );
184 }
185
186 public void testNgInjectNonFunction() throws Exception {
187 test(
188 "/** @ngInject */" +
189 "var a = 10",
190 null,
191 AngularPass.INJECT_NON_FUNCTION_ERROR
192 );
193
194 test(
195 "/** @ngInject */" +
196 "var x",
197 null,
198 AngularPass.INJECT_NON_FUNCTION_ERROR
199 );
200 }
201 }