Debian Codegraph @

test/com/google/javascript/jscomp/DefaultCodingConventionTest.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.javascript.rhino.Node;
20 import com.google.javascript.rhino.Token;
21
22 import junit.framework.TestCase;
23
24 /**
25 * Test class for the default {@link CodingConvention}.
26 */
27 public class DefaultCodingConventionTest extends TestCase {
28 private CodingConvention conv = CodingConventions.getDefault();
29
30 public void testVarAndOptionalParams() {
31 Node args = new Node(Token.PARAM_LIST,
32 Node.newString(Token.NAME, "a"),
33 Node.newString(Token.NAME, "b"));
34 Node optArgs = new Node(Token.PARAM_LIST,
35 Node.newString(Token.NAME, "opt_a"),
36 Node.newString(Token.NAME, "opt_b"));
37
38 assertFalse(conv.isVarArgsParameter(args.getFirstChild()));
39 assertFalse(conv.isVarArgsParameter(args.getLastChild()));
40 assertFalse(conv.isVarArgsParameter(optArgs.getFirstChild()));
41 assertFalse(conv.isVarArgsParameter(optArgs.getLastChild()));
42
43 assertFalse(conv.isOptionalParameter(args.getFirstChild()));
44 assertFalse(conv.isOptionalParameter(args.getLastChild()));
45 assertFalse(conv.isOptionalParameter(optArgs.getFirstChild()));
46 assertFalse(conv.isOptionalParameter(optArgs.getLastChild()));
47 }
48
49 public void testInlineName() {
50 assertFalse(conv.isConstant("a"));
51 assertFalse(conv.isConstant("XYZ123_"));
52 assertFalse(conv.isConstant("ABC"));
53 assertFalse(conv.isConstant("ABCdef"));
54 assertFalse(conv.isConstant("aBC"));
55 assertFalse(conv.isConstant("A"));
56 assertFalse(conv.isConstant("_XYZ123"));
57 assertFalse(conv.isConstant("a$b$XYZ123_"));
58 assertFalse(conv.isConstant("a$b$ABC_DEF"));
59 assertFalse(conv.isConstant("a$b$A"));
60 assertFalse(conv.isConstant("a$b$a"));
61 assertFalse(conv.isConstant("a$b$ABCdef"));
62 assertFalse(conv.isConstant("a$b$aBC"));
63 assertFalse(conv.isConstant("a$b$"));
64 assertFalse(conv.isConstant("$"));
65 }
66
67 public void testExportedName() {
68 assertFalse(conv.isExported("_a"));
69 assertFalse(conv.isExported("_a_"));
70 assertFalse(conv.isExported("a"));
71
72 assertFalse(conv.isExported("$super", false));
73 assertTrue(conv.isExported("$super", true));
74 assertTrue(conv.isExported("$super"));
75 }
76
77 public void testPrivateName() {
78 assertFalse(conv.isPrivate("a_"));
79 assertFalse(conv.isPrivate("a"));
80 assertFalse(conv.isPrivate("_a_"));
81 }
82
83 public void testEnumKey() {
84 assertTrue(conv.isValidEnumKey("A"));
85 assertTrue(conv.isValidEnumKey("123"));
86 assertTrue(conv.isValidEnumKey("FOO_BAR"));
87
88 assertTrue(conv.isValidEnumKey("a"));
89 assertTrue(conv.isValidEnumKey("someKeyInCamelCase"));
90 assertTrue(conv.isValidEnumKey("_FOO_BAR"));
91 }
92
93 public void testInheritanceDetection1() {
94 assertNotClassDefining("goog.foo(A, B);");
95 }
96
97 public void testInheritanceDetection2() {
98 assertNotClassDefining("goog.inherits(A, B);");
99 }
100
101 public void testInheritanceDetection3() {
102 assertNotClassDefining("A.inherits(B);");
103 }
104
105 public void testInheritanceDetection4() {
106 assertNotClassDefining("goog.inherits(goog.A, goog.B);");
107 }
108
109 public void testInheritanceDetection5() {
110 assertNotClassDefining("goog.A.inherits(goog.B);");
111 }
112
113 public void testInheritanceDetection6() {
114 assertNotClassDefining("A.inherits(this.B);");
115 }
116
117 public void testInheritanceDetection7() {
118 assertNotClassDefining("this.A.inherits(B);");
119 }
120
121 public void testInheritanceDetection8() {
122 assertNotClassDefining("goog.inherits(A, B, C);");
123 }
124
125 public void testInheritanceDetection9() {
126 assertNotClassDefining("A.mixin(B.prototype);");
127 }
128
129 public void testInheritanceDetection10() {
130 assertNotClassDefining("goog.mixin(A.prototype, B.prototype);");
131 }
132
133 public void testInheritanceDetectionPostCollapseProperties() {
134 assertNotClassDefining("goog$inherits(A, B);");
135 assertNotClassDefining("goog$inherits(A);");
136 }
137
138 public void testFunctionBind() {
139 assertNotFunctionBind("goog.bind(f)");
140 assertNotFunctionBind("goog$bind(f)");
141 assertNotFunctionBind("goog.partial(f)");
142 assertNotFunctionBind("goog$partial(f)");
143
144 assertFunctionBind("(function(){}).bind()");
145 assertFunctionBind("(function(){}).bind(obj)");
146 assertFunctionBind("(function(){}).bind(obj, p1)");
147
148 assertNotFunctionBind("Function.prototype.bind.call()");
149 assertFunctionBind("Function.prototype.bind.call(obj)");
150 assertFunctionBind("Function.prototype.bind.call(obj, p1)");
151 }
152
153 private void assertFunctionBind(String code) {
154 Node n = parseTestCode(code);
155 assertNotNull(conv.describeFunctionBind(n.getFirstChild()));
156 }
157
158 private void assertNotFunctionBind(String code) {
159 Node n = parseTestCode(code);
160 assertNull(conv.describeFunctionBind(n.getFirstChild()));
161 }
162
163 private void assertNotClassDefining(String code) {
164 Node n = parseTestCode(code);
165 assertNull(conv.getClassesDefinedByCall(n.getFirstChild()));
166 }
167
168 private Node parseTestCode(String code) {
169 Compiler compiler = new Compiler();
170 return compiler.parseTestCode(code).getFirstChild();
171 }
172 }