Debian Codegraph @

test/com/google/javascript/jscomp/ClosureRewriteClassTest.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 package com.google.javascript.jscomp;
17
18 import static com.google.javascript.jscomp.ClosureRewriteClass.GOOG_CLASS_CONSTRUCTOR_MISING;
19 import static com.google.javascript.jscomp.ClosureRewriteClass.GOOG_CLASS_DESCRIPTOR_NOT_VALID;
20 import static com.google.javascript.jscomp.ClosureRewriteClass.GOOG_CLASS_STATICS_NOT_VALID;
21 import static com.google.javascript.jscomp.ClosureRewriteClass.GOOG_CLASS_SUPER_CLASS_NOT_VALID;
22 import static com.google.javascript.jscomp.ClosureRewriteClass.GOOG_CLASS_TARGET_INVALID;
23 import static com.google.javascript.jscomp.ClosureRewriteClass.GOOG_CLASS_UNEXPECTED_PARAMS;
24
25
26 /**
27 * Unit tests for ClosureRewriteGoogClass
28 * @author [email protected] (John Lenz)
29 */
30 public class ClosureRewriteClassTest extends CompilerTestCase {
31
32 @Override
33 protected CompilerPass getProcessor(Compiler compiler) {
34 return new ClosureRewriteClass(compiler);
35 }
36
37 @Override
38 protected void setUp() throws Exception {
39 super.setUp();
40 this.enableEcmaScript5(false);
41 }
42
43 @Override
44 protected int getNumRepetitions() {
45 return 1;
46 }
47
48 public void testBasic1() {
49 test(
50 "var x = goog.defineClass(null, {\n" +
51 " constructor: function(){}\n" +
52 "});",
53
54 "{var x = function() {};}");
55 }
56
57 public void testBasic2() {
58 test(
59 "var x = {};\n" +
60 "x.y = goog.defineClass(null, {\n" +
61 " constructor: function(){}\n" +
62 "});",
63
64 "var x = {};" +
65 "{x.y = function() {};}");
66 }
67
68 public void testBasic3() {
69 test(
70 "var x = goog.labs.classdef.defineClass(null, {\n" +
71 " constructor: function(){}\n" +
72 "});",
73
74 "{var x = function() {};}");
75 }
76
77 public void testInnerClass1() {
78 test(
79 "var x = goog.defineClass(some.Super, {\n" +
80 " constructor: function(){\n" +
81 " this.foo = 1;\n" +
82 " },\n" +
83 " statics: {\n" +
84 " inner: goog.defineClass(x,{\n" +
85 " constructor: function(){\n" +
86 " this.bar = 1;\n" +
87 " }\n" +
88 " })\n" +
89 " }\n" +
90 "});",
91
92 "{" +
93 "var x=function(){this.foo=1};" +
94 "goog.inherits(x,some.Super);" +
95 "{" +
96 "x.inner=function(){this.bar=1};" +
97 "goog.inherits(x.inner,x);" +
98 "}" +
99 "}");
100 }
101
102 public void testComplete1() {
103 test(
104 "var x = goog.defineClass(some.Super, {\n" +
105 " constructor: function(){\n" +
106 " this.foo = 1;\n" +
107 " },\n" +
108 " statics: {\n" +
109 " prop1: 1,\n" +
110 " /** @const */\n" +
111 " PROP2: 2\n" +
112 " },\n" +
113 " anotherProp: 1,\n" +
114 " aMethod: function() {}\n" +
115 "});",
116
117 "{" +
118 "var x=function(){this.foo=1};" +
119 "goog.inherits(x,some.Super);" +
120 "x.prop1=1;" +
121 "x.PROP2=2;" +
122 "x.prototype.anotherProp=1;" +
123 "x.prototype.aMethod=function(){};" +
124 "}");
125 }
126
127 public void testComplete2() {
128 test(
129 "x.y = goog.defineClass(some.Super, {\n" +
130 " constructor: function(){\n" +
131 " this.foo = 1;\n" +
132 " },\n" +
133 " statics: {\n" +
134 " prop1: 1,\n" +
135 " /** @const */\n" +
136 " PROP2: 2\n" +
137 " },\n" +
138 " anotherProp: 1,\n" +
139 " aMethod: function() {}\n" +
140 "});",
141
142 "{\n" +
143 "/** @constructor */\n" +
144 "x.y=function(){this.foo=1};\n" +
145 "goog.inherits(x.y,some.Super);" +
146 "x.y.prop1=1;\n" +
147 "/** @const */\n" +
148 "x.y.PROP2=2;\n" +
149 "x.y.prototype.anotherProp=1;" +
150 "x.y.prototype.aMethod=function(){};" +
151 "}");
152 }
153
154 public void testClassWithStaticInitFn() {
155 test(
156 "x.y = goog.defineClass(some.Super, {\n" +
157 " constructor: function(){\n" +
158 " this.foo = 1;\n" +
159 " },\n" +
160 " statics: function(cls) {\n" +
161 " cls.prop1 = 1;\n" +
162 " /** @const */\n" +
163 " cls.PROP2 = 2;\n" +
164 " },\n" +
165 " anotherProp: 1,\n" +
166 " aMethod: function() {}\n" +
167 "});",
168
169 "{\n" +
170 "/** @constructor */\n" +
171 "x.y=function(){this.foo=1};\n" +
172 "goog.inherits(x.y,some.Super);" +
173 "x.y.prototype.anotherProp=1;" +
174 "x.y.prototype.aMethod=function(){};" +
175 "(function(cls) {" +
176 " cls.prop1=1;\n" +
177 " /** @const */\n" +
178 " cls.PROP2=2;" +
179 "})(x.y);\n" +
180 "}");
181 }
182
183 public void testInvalid1() {
184 testSame(
185 "var x = goog.defineClass();",
186 GOOG_CLASS_SUPER_CLASS_NOT_VALID, true);
187 testSame(
188 "var x = goog.defineClass('foo');",
189 GOOG_CLASS_SUPER_CLASS_NOT_VALID, true);
190 testSame(
191 "var x = goog.defineClass(foo());",
192 GOOG_CLASS_SUPER_CLASS_NOT_VALID, true);
193 testSame(
194 "var x = goog.defineClass({'foo':1});",
195 GOOG_CLASS_SUPER_CLASS_NOT_VALID, true);
196 testSame(
197 "var x = goog.defineClass({1:1});",
198 GOOG_CLASS_SUPER_CLASS_NOT_VALID, true);
199
200 this.enableEcmaScript5(true);
201
202 testSame(
203 "var x = goog.defineClass({get foo() {return 1}});",
204 GOOG_CLASS_SUPER_CLASS_NOT_VALID, true);
205 testSame(
206 "var x = goog.defineClass({set foo(a) {}});",
207 GOOG_CLASS_SUPER_CLASS_NOT_VALID, true);
208 }
209
210 public void testInvalid2() {
211 testSame(
212 "var x = goog.defineClass(null);",
213 GOOG_CLASS_DESCRIPTOR_NOT_VALID, true);
214 testSame(
215 "var x = goog.defineClass(null, null);",
216 GOOG_CLASS_DESCRIPTOR_NOT_VALID, true);
217 testSame(
218 "var x = goog.defineClass(null, foo());",
219 GOOG_CLASS_DESCRIPTOR_NOT_VALID, true);
220 }
221
222 public void testInvalid3() {
223 testSame(
224 "var x = goog.defineClass(null, {});",
225 GOOG_CLASS_CONSTRUCTOR_MISING, true);
226 }
227
228 public void testInvalid4() {
229 testSame(
230 "var x = goog.defineClass(null, {" +
231 " constructor: function(){}," +
232 " statics: null" +
233 "});",
234 GOOG_CLASS_STATICS_NOT_VALID, true);
235 testSame(
236 "var x = goog.defineClass(null, {" +
237 " constructor: function(){}," +
238 " statics: foo" +
239 "});",
240 GOOG_CLASS_STATICS_NOT_VALID, true);
241 testSame(
242 "var x = goog.defineClass(null, {" +
243 " constructor: function(){}," +
244 " statics: {'foo': 1}" +
245 "});",
246 GOOG_CLASS_STATICS_NOT_VALID, true);
247 testSame(
248 "var x = goog.defineClass(null, {" +
249 " constructor: function(){}," +
250 " statics: {1: 1}" +
251 "});",
252 GOOG_CLASS_STATICS_NOT_VALID, true); }
253
254 public void testInvalid5() {
255 testSame(
256 "var x = goog.defineClass(null, {" +
257 " constructor: function(){}" +
258 "}, null);",
259 GOOG_CLASS_UNEXPECTED_PARAMS, true);
260 }
261
262 public void testInvalid6() {
263 testSame(
264 "goog.defineClass();",
265 GOOG_CLASS_TARGET_INVALID, true);
266
267 testSame(
268 "var x = goog.defineClass() || null;",
269 GOOG_CLASS_TARGET_INVALID, true);
270
271 testSame(
272 "({foo: goog.defineClass()});",
273 GOOG_CLASS_TARGET_INVALID, true);
274 }
275 }