Debian Codegraph @

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

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