| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.javascript.jscomp.CodingConvention.SubclassRelationship; |
| 20 |
import com.google.javascript.jscomp.CodingConvention.SubclassType; |
| 21 |
import com.google.javascript.rhino.Node; |
| 22 |
import com.google.javascript.rhino.Token; |
| 23 |
import com.google.javascript.rhino.jstype.FunctionType; |
| 24 |
import com.google.javascript.rhino.jstype.JSTypeRegistry; |
| 25 |
|
| 26 |
import junit.framework.TestCase; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class ClosureCodingConventionTest extends TestCase { |
| 32 |
private ClosureCodingConvention conv = new ClosureCodingConvention(); |
| 33 |
|
| 34 |
public void testVarAndOptionalParams() { |
| 35 |
Node args = new Node(Token.PARAM_LIST, |
| 36 |
Node.newString(Token.NAME, "a"), |
| 37 |
Node.newString(Token.NAME, "b")); |
| 38 |
Node optArgs = new Node(Token.PARAM_LIST, |
| 39 |
Node.newString(Token.NAME, "opt_a"), |
| 40 |
Node.newString(Token.NAME, "opt_b")); |
| 41 |
|
| 42 |
assertFalse(conv.isVarArgsParameter(args.getFirstChild())); |
| 43 |
assertFalse(conv.isVarArgsParameter(args.getLastChild())); |
| 44 |
assertFalse(conv.isVarArgsParameter(optArgs.getFirstChild())); |
| 45 |
assertFalse(conv.isVarArgsParameter(optArgs.getLastChild())); |
| 46 |
|
| 47 |
assertFalse(conv.isOptionalParameter(args.getFirstChild())); |
| 48 |
assertFalse(conv.isOptionalParameter(args.getLastChild())); |
| 49 |
assertFalse(conv.isOptionalParameter(optArgs.getFirstChild())); |
| 50 |
assertFalse(conv.isOptionalParameter(optArgs.getLastChild())); |
| 51 |
} |
| 52 |
|
| 53 |
public void testInlineName() { |
| 54 |
assertFalse(conv.isConstant("a")); |
| 55 |
assertFalse(conv.isConstant("XYZ123_")); |
| 56 |
assertFalse(conv.isConstant("ABC")); |
| 57 |
assertFalse(conv.isConstant("ABCdef")); |
| 58 |
assertFalse(conv.isConstant("aBC")); |
| 59 |
assertFalse(conv.isConstant("A")); |
| 60 |
assertFalse(conv.isConstant("_XYZ123")); |
| 61 |
assertFalse(conv.isConstant("a$b$XYZ123_")); |
| 62 |
assertFalse(conv.isConstant("a$b$ABC_DEF")); |
| 63 |
assertFalse(conv.isConstant("a$b$A")); |
| 64 |
assertFalse(conv.isConstant("a$b$a")); |
| 65 |
assertFalse(conv.isConstant("a$b$ABCdef")); |
| 66 |
assertFalse(conv.isConstant("a$b$aBC")); |
| 67 |
assertFalse(conv.isConstant("a$b$")); |
| 68 |
assertFalse(conv.isConstant("$")); |
| 69 |
} |
| 70 |
|
| 71 |
public void testExportedName() { |
| 72 |
assertFalse(conv.isExported("_a")); |
| 73 |
assertFalse(conv.isExported("_a_")); |
| 74 |
assertFalse(conv.isExported("a")); |
| 75 |
|
| 76 |
assertFalse(conv.isExported("$super", false)); |
| 77 |
assertTrue(conv.isExported("$super", true)); |
| 78 |
assertTrue(conv.isExported("$super")); |
| 79 |
} |
| 80 |
|
| 81 |
public void testPrivateName() { |
| 82 |
assertFalse(conv.isPrivate("a_")); |
| 83 |
assertFalse(conv.isPrivate("a")); |
| 84 |
assertFalse(conv.isPrivate("_a_")); |
| 85 |
} |
| 86 |
|
| 87 |
public void testEnumKey() { |
| 88 |
assertTrue(conv.isValidEnumKey("A")); |
| 89 |
assertTrue(conv.isValidEnumKey("123")); |
| 90 |
assertTrue(conv.isValidEnumKey("FOO_BAR")); |
| 91 |
|
| 92 |
assertTrue(conv.isValidEnumKey("a")); |
| 93 |
assertTrue(conv.isValidEnumKey("someKeyInCamelCase")); |
| 94 |
assertTrue(conv.isValidEnumKey("_FOO_BAR")); |
| 95 |
} |
| 96 |
|
| 97 |
public void testInheritanceDetection1() { |
| 98 |
assertNotClassDefining("goog.foo(A, B);"); |
| 99 |
} |
| 100 |
|
| 101 |
public void testInheritanceDetection2() { |
| 102 |
assertDefinesClasses("goog.inherits(A, B);", "A", "B"); |
| 103 |
} |
| 104 |
|
| 105 |
public void testInheritanceDetection3() { |
| 106 |
assertDefinesClasses("A.inherits(B);", "A", "B"); |
| 107 |
} |
| 108 |
|
| 109 |
public void testInheritanceDetection4() { |
| 110 |
assertDefinesClasses("goog.inherits(goog.A, goog.B);", "goog.A", "goog.B"); |
| 111 |
} |
| 112 |
|
| 113 |
public void testInheritanceDetection5() { |
| 114 |
assertDefinesClasses("goog.A.inherits(goog.B);", "goog.A", "goog.B"); |
| 115 |
} |
| 116 |
|
| 117 |
public void testInheritanceDetection6() { |
| 118 |
assertNotClassDefining("A.inherits(this.B);"); |
| 119 |
} |
| 120 |
|
| 121 |
public void testInheritanceDetection7() { |
| 122 |
assertNotClassDefining("this.A.inherits(B);"); |
| 123 |
} |
| 124 |
|
| 125 |
public void testInheritanceDetection8() { |
| 126 |
assertNotClassDefining("goog.inherits(A, B, C);"); |
| 127 |
} |
| 128 |
|
| 129 |
public void testInheritanceDetection9() { |
| 130 |
assertDefinesClasses("A.mixin(B.prototype);", |
| 131 |
"A", "B"); |
| 132 |
} |
| 133 |
|
| 134 |
public void testInheritanceDetection10() { |
| 135 |
assertDefinesClasses("goog.mixin(A.prototype, B.prototype);", |
| 136 |
"A", "B"); |
| 137 |
} |
| 138 |
|
| 139 |
public void testInheritanceDetection11() { |
| 140 |
assertNotClassDefining("A.mixin(B)"); |
| 141 |
} |
| 142 |
|
| 143 |
public void testInheritanceDetection12() { |
| 144 |
assertNotClassDefining("goog.mixin(A.prototype, B)"); |
| 145 |
} |
| 146 |
|
| 147 |
public void testInheritanceDetection13() { |
| 148 |
assertNotClassDefining("goog.mixin(A, B)"); |
| 149 |
} |
| 150 |
|
| 151 |
public void testInheritanceDetection14() { |
| 152 |
assertNotClassDefining("goog$mixin((function(){}).prototype)"); |
| 153 |
} |
| 154 |
|
| 155 |
public void testInheritanceDetectionPostCollapseProperties() { |
| 156 |
assertDefinesClasses("goog$inherits(A, B);", "A", "B"); |
| 157 |
assertNotClassDefining("goog$inherits(A);"); |
| 158 |
} |
| 159 |
|
| 160 |
public void testObjectLiteralCast() { |
| 161 |
assertNotObjectLiteralCast("goog.reflect.object();"); |
| 162 |
assertNotObjectLiteralCast("goog.reflect.object(A);"); |
| 163 |
assertNotObjectLiteralCast("goog.reflect.object(1, {});"); |
| 164 |
assertObjectLiteralCast("goog.reflect.object(A, {});"); |
| 165 |
} |
| 166 |
|
| 167 |
public void testFunctionBind() { |
| 168 |
assertNotFunctionBind("goog.bind()"); |
| 169 |
assertFunctionBind("goog.bind(f)"); |
| 170 |
assertFunctionBind("goog.bind(f, obj)"); |
| 171 |
assertFunctionBind("goog.bind(f, obj, p1)"); |
| 172 |
|
| 173 |
assertNotFunctionBind("goog$bind()"); |
| 174 |
assertFunctionBind("goog$bind(f)"); |
| 175 |
assertFunctionBind("goog$bind(f, obj)"); |
| 176 |
assertFunctionBind("goog$bind(f, obj, p1)"); |
| 177 |
|
| 178 |
assertNotFunctionBind("goog.partial()"); |
| 179 |
assertFunctionBind("goog.partial(f)"); |
| 180 |
assertFunctionBind("goog.partial(f, obj)"); |
| 181 |
assertFunctionBind("goog.partial(f, obj, p1)"); |
| 182 |
|
| 183 |
assertNotFunctionBind("goog$partial()"); |
| 184 |
assertFunctionBind("goog$partial(f)"); |
| 185 |
assertFunctionBind("goog$partial(f, obj)"); |
| 186 |
assertFunctionBind("goog$partial(f, obj, p1)"); |
| 187 |
|
| 188 |
assertFunctionBind("(function(){}).bind()"); |
| 189 |
assertFunctionBind("(function(){}).bind(obj)"); |
| 190 |
assertFunctionBind("(function(){}).bind(obj, p1)"); |
| 191 |
|
| 192 |
assertNotFunctionBind("Function.prototype.bind.call()"); |
| 193 |
assertFunctionBind("Function.prototype.bind.call(obj)"); |
| 194 |
assertFunctionBind("Function.prototype.bind.call(obj, p1)"); |
| 195 |
} |
| 196 |
|
| 197 |
public void testRequire() { |
| 198 |
assertRequire("goog.require('foo')"); |
| 199 |
assertNotRequire("goog.require(foo)"); |
| 200 |
assertNotRequire("goog.require()"); |
| 201 |
assertNotRequire("foo()"); |
| 202 |
} |
| 203 |
|
| 204 |
public void testApplySubclassRelationship() { |
| 205 |
JSTypeRegistry registry = new JSTypeRegistry(null); |
| 206 |
|
| 207 |
Node nodeA = new Node(Token.FUNCTION); |
| 208 |
FunctionType ctorA = registry.createConstructorType("A", nodeA, |
| 209 |
new Node(Token.PARAM_LIST), null, null); |
| 210 |
|
| 211 |
Node nodeB = new Node(Token.FUNCTION); |
| 212 |
FunctionType ctorB = registry.createConstructorType("B", nodeB, |
| 213 |
new Node(Token.PARAM_LIST), null, null); |
| 214 |
|
| 215 |
conv.applySubclassRelationship(ctorA, ctorB, SubclassType.INHERITS); |
| 216 |
|
| 217 |
assertTrue(ctorB.getPrototype().hasOwnProperty("constructor")); |
| 218 |
assertEquals(nodeB, ctorB.getPrototype().getPropertyNode("constructor")); |
| 219 |
|
| 220 |
assertTrue(ctorB.hasOwnProperty("superClass_")); |
| 221 |
assertEquals(nodeB, ctorB.getPropertyNode("superClass_")); |
| 222 |
} |
| 223 |
|
| 224 |
private void assertFunctionBind(String code) { |
| 225 |
Node n = parseTestCode(code); |
| 226 |
assertNotNull(conv.describeFunctionBind(n.getFirstChild())); |
| 227 |
} |
| 228 |
|
| 229 |
private void assertNotFunctionBind(String code) { |
| 230 |
Node n = parseTestCode(code); |
| 231 |
assertNull(conv.describeFunctionBind(n.getFirstChild())); |
| 232 |
} |
| 233 |
|
| 234 |
private void assertRequire(String code) { |
| 235 |
Node n = parseTestCode(code); |
| 236 |
assertNotNull(conv.extractClassNameIfRequire(n.getFirstChild(), n)); |
| 237 |
} |
| 238 |
|
| 239 |
private void assertNotRequire(String code) { |
| 240 |
Node n = parseTestCode(code); |
| 241 |
assertNull(conv.extractClassNameIfRequire(n.getFirstChild(), n)); |
| 242 |
} |
| 243 |
|
| 244 |
private void assertNotObjectLiteralCast(String code) { |
| 245 |
Node n = parseTestCode(code); |
| 246 |
assertNull(conv.getObjectLiteralCast(n.getFirstChild())); |
| 247 |
} |
| 248 |
|
| 249 |
private void assertObjectLiteralCast(String code) { |
| 250 |
Node n = parseTestCode(code); |
| 251 |
assertNotNull(conv.getObjectLiteralCast(n.getFirstChild())); |
| 252 |
} |
| 253 |
|
| 254 |
private void assertNotClassDefining(String code) { |
| 255 |
Node n = parseTestCode(code); |
| 256 |
assertNull(conv.getClassesDefinedByCall(n.getFirstChild())); |
| 257 |
} |
| 258 |
|
| 259 |
private void assertDefinesClasses(String code, String subclassName, |
| 260 |
String superclassName) { |
| 261 |
Node n = parseTestCode(code); |
| 262 |
SubclassRelationship classes = |
| 263 |
conv.getClassesDefinedByCall(n.getFirstChild()); |
| 264 |
assertNotNull(classes); |
| 265 |
assertEquals(subclassName, classes.subclassName); |
| 266 |
assertEquals(superclassName, classes.superclassName); |
| 267 |
} |
| 268 |
|
| 269 |
private Node parseTestCode(String code) { |
| 270 |
Compiler compiler = new Compiler(); |
| 271 |
return compiler.parseTestCode(code).getFirstChild(); |
| 272 |
} |
| 273 |
} |