451 lines
14 KiB
Java
451 lines
14 KiB
Java
|
/*
|
||
|
*
|
||
|
* ***** BEGIN LICENSE BLOCK *****
|
||
|
* Version: MPL 1.1/GPL 2.0
|
||
|
*
|
||
|
* The contents of this file are subject to the Mozilla Public License Version
|
||
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
||
|
* the License. You may obtain a copy of the License at
|
||
|
* http://www.mozilla.org/MPL/
|
||
|
*
|
||
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
||
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
||
|
* for the specific language governing rights and limitations under the
|
||
|
* License.
|
||
|
*
|
||
|
* The Original Code is Rhino code, released
|
||
|
* May 6, 1999.
|
||
|
*
|
||
|
* The Initial Developer of the Original Code is
|
||
|
* Netscape Communications Corporation.
|
||
|
* Portions created by the Initial Developer are Copyright (C) 1997-1999
|
||
|
* the Initial Developer. All Rights Reserved.
|
||
|
*
|
||
|
* Contributor(s):
|
||
|
* Nick Santos
|
||
|
*
|
||
|
* Alternatively, the contents of this file may be used under the terms of
|
||
|
* the GNU General Public License Version 2 or later (the "GPL"), in which
|
||
|
* case the provisions of the GPL are applicable instead of those above. If
|
||
|
* you wish to allow use of your version of this file only under the terms of
|
||
|
* the GPL and not to allow others to use your version of this file under the
|
||
|
* MPL, indicate your decision by deleting the provisions above and replacing
|
||
|
* them with the notice and other provisions required by the GPL. If you do
|
||
|
* not delete the provisions above, a recipient may use your version of this
|
||
|
* file under either the MPL or the GPL.
|
||
|
*
|
||
|
* ***** END LICENSE BLOCK ***** */
|
||
|
|
||
|
package com.google.javascript.rhino;
|
||
|
|
||
|
import static com.google.javascript.rhino.JSDocInfo.Visibility.PRIVATE;
|
||
|
import static com.google.javascript.rhino.JSDocInfo.Visibility.PROTECTED;
|
||
|
import static com.google.javascript.rhino.JSDocInfo.Visibility.PUBLIC;
|
||
|
import static com.google.javascript.rhino.jstype.JSTypeNative.BOOLEAN_TYPE;
|
||
|
import static com.google.javascript.rhino.jstype.JSTypeNative.NUMBER_OBJECT_TYPE;
|
||
|
import static com.google.javascript.rhino.jstype.JSTypeNative.NUMBER_TYPE;
|
||
|
import static com.google.javascript.rhino.jstype.JSTypeNative.STRING_TYPE;
|
||
|
|
||
|
import com.google.common.collect.Sets;
|
||
|
import com.google.javascript.rhino.jstype.JSType;
|
||
|
import com.google.javascript.rhino.jstype.JSTypeNative;
|
||
|
import com.google.javascript.rhino.jstype.JSTypeRegistry;
|
||
|
import com.google.javascript.rhino.testing.Asserts;
|
||
|
import com.google.javascript.rhino.testing.TestErrorReporter;
|
||
|
|
||
|
import junit.framework.TestCase;
|
||
|
|
||
|
public class JSDocInfoTest extends TestCase {
|
||
|
private TestErrorReporter errorReporter = new TestErrorReporter(null, null);
|
||
|
private JSTypeRegistry registry = new JSTypeRegistry(errorReporter);
|
||
|
|
||
|
private JSType getNativeType(JSTypeNative typeId) {
|
||
|
return registry.getNativeType(typeId);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Tests the assigned ordinal of the elements of the
|
||
|
* {@link JSDocInfo.Visibility} enum.
|
||
|
*/
|
||
|
public void testVisibilityOrdinal() {
|
||
|
assertEquals(0, PRIVATE.ordinal());
|
||
|
assertEquals(1, PROTECTED.ordinal());
|
||
|
assertEquals(2, PUBLIC.ordinal());
|
||
|
}
|
||
|
|
||
|
public void testSetType() {
|
||
|
JSDocInfo info = new JSDocInfo();
|
||
|
info.setType(fromString("string"));
|
||
|
|
||
|
assertNull(info.getBaseType());
|
||
|
assertNull(info.getDescription());
|
||
|
assertNull(info.getEnumParameterType());
|
||
|
assertEquals(0, info.getParameterCount());
|
||
|
assertNull(info.getReturnType());
|
||
|
assertTypeEquals(STRING_TYPE, resolve(info.getType()));
|
||
|
assertNull(info.getVisibility());
|
||
|
assertTrue(info.hasType());
|
||
|
assertFalse(info.isConstant());
|
||
|
assertFalse(info.isConstructor());
|
||
|
assertFalse(info.isHidden());
|
||
|
assertFalse(info.shouldPreserveTry());
|
||
|
}
|
||
|
|
||
|
public void testSetTypeAndVisibility() {
|
||
|
JSDocInfo info = new JSDocInfo();
|
||
|
info.setType(fromString("string"));
|
||
|
info.setVisibility(PROTECTED);
|
||
|
|
||
|
assertNull(info.getBaseType());
|
||
|
assertNull(info.getDescription());
|
||
|
assertNull(info.getEnumParameterType());
|
||
|
assertEquals(0, info.getParameterCount());
|
||
|
assertNull(info.getReturnType());
|
||
|
assertTypeEquals(STRING_TYPE, resolve(info.getType()));
|
||
|
assertEquals(PROTECTED, info.getVisibility());
|
||
|
assertTrue(info.hasType());
|
||
|
assertFalse(info.isConstant());
|
||
|
assertFalse(info.isConstructor());
|
||
|
assertFalse(info.isHidden());
|
||
|
assertFalse(info.shouldPreserveTry());
|
||
|
}
|
||
|
|
||
|
public void testSetReturnType() {
|
||
|
JSDocInfo info = new JSDocInfo();
|
||
|
info.setReturnType(fromString("string"));
|
||
|
|
||
|
assertNull(info.getBaseType());
|
||
|
assertNull(info.getDescription());
|
||
|
assertNull(info.getEnumParameterType());
|
||
|
assertEquals(0, info.getParameterCount());
|
||
|
assertTypeEquals(STRING_TYPE, resolve(info.getReturnType()));
|
||
|
assertNull(info.getType());
|
||
|
assertNull(info.getVisibility());
|
||
|
assertFalse(info.hasType());
|
||
|
assertFalse(info.isConstant());
|
||
|
assertFalse(info.isConstructor());
|
||
|
assertFalse(info.isHidden());
|
||
|
assertFalse(info.shouldPreserveTry());
|
||
|
}
|
||
|
|
||
|
public void testSetReturnTypeAndBaseType() {
|
||
|
JSDocInfo info = new JSDocInfo();
|
||
|
info.setBaseType(
|
||
|
new JSTypeExpression(
|
||
|
new Node(Token.BANG, Node.newString("Number")), ""));
|
||
|
info.setReturnType(fromString("string"));
|
||
|
|
||
|
assertTypeEquals(NUMBER_OBJECT_TYPE,
|
||
|
resolve(info.getBaseType()));
|
||
|
assertNull(info.getDescription());
|
||
|
assertNull(info.getEnumParameterType());
|
||
|
assertEquals(0, info.getParameterCount());
|
||
|
assertTypeEquals(STRING_TYPE, resolve(info.getReturnType()));
|
||
|
assertNull(info.getType());
|
||
|
assertNull(info.getVisibility());
|
||
|
assertFalse(info.hasType());
|
||
|
assertFalse(info.isConstant());
|
||
|
assertFalse(info.isConstructor());
|
||
|
assertFalse(info.isHidden());
|
||
|
assertFalse(info.shouldPreserveTry());
|
||
|
}
|
||
|
|
||
|
public void testSetEnumParameterType() {
|
||
|
JSDocInfo info = new JSDocInfo();
|
||
|
info.setEnumParameterType(fromString("string"));
|
||
|
|
||
|
assertNull(info.getBaseType());
|
||
|
assertNull(info.getDescription());
|
||
|
assertTypeEquals(STRING_TYPE,
|
||
|
resolve(info.getEnumParameterType()));
|
||
|
assertEquals(0, info.getParameterCount());
|
||
|
assertNull(info.getReturnType());
|
||
|
assertNull(info.getType());
|
||
|
assertNull(info.getVisibility());
|
||
|
assertFalse(info.hasType());
|
||
|
assertFalse(info.isConstant());
|
||
|
assertFalse(info.isConstructor());
|
||
|
assertFalse(info.isHidden());
|
||
|
assertFalse(info.shouldPreserveTry());
|
||
|
}
|
||
|
|
||
|
public void testMultipleSetType() {
|
||
|
JSDocInfo info = new JSDocInfo();
|
||
|
info.setType(fromString("number"));
|
||
|
|
||
|
try {
|
||
|
info.setReturnType(fromString("boolean"));
|
||
|
fail("Expected exception");
|
||
|
} catch (IllegalStateException e) {}
|
||
|
|
||
|
try {
|
||
|
info.setEnumParameterType(fromString("string"));
|
||
|
fail("Expected exception");
|
||
|
} catch (IllegalStateException e) {}
|
||
|
|
||
|
try {
|
||
|
info.setTypedefType(fromString("string"));
|
||
|
fail("Expected exception");
|
||
|
} catch (IllegalStateException e) {}
|
||
|
|
||
|
assertTypeEquals(NUMBER_TYPE, resolve(info.getType()));
|
||
|
assertNull(info.getReturnType());
|
||
|
assertNull(info.getEnumParameterType());
|
||
|
assertNull(info.getTypedefType());
|
||
|
assertTrue(info.hasType());
|
||
|
}
|
||
|
|
||
|
public void testMultipleSetType2() {
|
||
|
JSDocInfo info = new JSDocInfo();
|
||
|
|
||
|
info.setReturnType(fromString("boolean"));
|
||
|
|
||
|
try {
|
||
|
info.setType(fromString("number"));
|
||
|
fail("Expected exception");
|
||
|
} catch (IllegalStateException e) {}
|
||
|
|
||
|
try {
|
||
|
info.setEnumParameterType(fromString("string"));
|
||
|
fail("Expected exception");
|
||
|
} catch (IllegalStateException e) {}
|
||
|
|
||
|
try {
|
||
|
info.setTypedefType(fromString("string"));
|
||
|
fail("Expected exception");
|
||
|
} catch (IllegalStateException e) {}
|
||
|
|
||
|
assertTypeEquals(BOOLEAN_TYPE,
|
||
|
resolve(info.getReturnType()));
|
||
|
assertNull(info.getEnumParameterType());
|
||
|
assertNull(info.getType());
|
||
|
assertNull(info.getTypedefType());
|
||
|
assertFalse(info.hasType());
|
||
|
}
|
||
|
|
||
|
public void testMultipleSetType3() {
|
||
|
JSDocInfo info = new JSDocInfo();
|
||
|
info.setEnumParameterType(fromString("boolean"));
|
||
|
|
||
|
try {
|
||
|
info.setType(fromString("number"));
|
||
|
fail("Expected exception");
|
||
|
} catch (IllegalStateException e) {}
|
||
|
|
||
|
try {
|
||
|
info.setReturnType(fromString("string"));
|
||
|
fail("Expected exception");
|
||
|
} catch (IllegalStateException e) {}
|
||
|
|
||
|
try {
|
||
|
info.setTypedefType(fromString("string"));
|
||
|
fail("Expected exception");
|
||
|
} catch (IllegalStateException e) {}
|
||
|
|
||
|
assertNull(info.getType());
|
||
|
assertNull(info.getTypedefType());
|
||
|
assertNull(info.getReturnType());
|
||
|
assertTypeEquals(BOOLEAN_TYPE,
|
||
|
resolve(info.getEnumParameterType()));
|
||
|
}
|
||
|
|
||
|
public void testSetTypedefType() {
|
||
|
JSDocInfo info = new JSDocInfo();
|
||
|
info.setTypedefType(fromString("boolean"));
|
||
|
|
||
|
assertTypeEquals(BOOLEAN_TYPE,
|
||
|
resolve(info.getTypedefType()));
|
||
|
assertTrue(info.hasTypedefType());
|
||
|
assertFalse(info.hasType());
|
||
|
assertFalse(info.hasEnumParameterType());
|
||
|
assertFalse(info.hasReturnType());
|
||
|
}
|
||
|
|
||
|
public void testSetConstant() {
|
||
|
JSDocInfo info = new JSDocInfo();
|
||
|
info.setConstant(true);
|
||
|
|
||
|
assertFalse(info.hasType());
|
||
|
assertTrue(info.isConstant());
|
||
|
assertFalse(info.isConstructor());
|
||
|
assertFalse(info.isDefine());
|
||
|
assertFalse(info.isHidden());
|
||
|
assertFalse(info.shouldPreserveTry());
|
||
|
}
|
||
|
|
||
|
public void testSetConstructor() {
|
||
|
JSDocInfo info = new JSDocInfo();
|
||
|
info.setConstructor(true);
|
||
|
|
||
|
assertFalse(info.isConstant());
|
||
|
assertTrue(info.isConstructor());
|
||
|
assertFalse(info.isDefine());
|
||
|
assertFalse(info.isHidden());
|
||
|
assertFalse(info.shouldPreserveTry());
|
||
|
}
|
||
|
|
||
|
public void testSetDefine() {
|
||
|
JSDocInfo info = new JSDocInfo();
|
||
|
info.setDefine(true);
|
||
|
|
||
|
assertTrue(info.isConstant());
|
||
|
assertFalse(info.isConstructor());
|
||
|
assertTrue(info.isDefine());
|
||
|
assertFalse(info.isHidden());
|
||
|
assertFalse(info.shouldPreserveTry());
|
||
|
}
|
||
|
|
||
|
public void testSetHidden() {
|
||
|
JSDocInfo info = new JSDocInfo();
|
||
|
info.setHidden(true);
|
||
|
|
||
|
assertFalse(info.hasType());
|
||
|
assertFalse(info.isConstant());
|
||
|
assertFalse(info.isConstructor());
|
||
|
assertFalse(info.isDefine());
|
||
|
assertTrue(info.isHidden());
|
||
|
assertFalse(info.shouldPreserveTry());
|
||
|
}
|
||
|
|
||
|
public void testSetShouldPreserveTry() {
|
||
|
JSDocInfo info = new JSDocInfo();
|
||
|
info.setShouldPreserveTry(true);
|
||
|
|
||
|
assertFalse(info.isConstant());
|
||
|
assertFalse(info.isConstructor());
|
||
|
assertFalse(info.isDefine());
|
||
|
assertFalse(info.isHidden());
|
||
|
assertTrue(info.shouldPreserveTry());
|
||
|
}
|
||
|
|
||
|
public void testSetNoTypeCheck() {
|
||
|
JSDocInfo info = new JSDocInfo();
|
||
|
info.setNoCheck(true);
|
||
|
|
||
|
assertFalse(info.isDeprecated());
|
||
|
assertFalse(info.isNoAlias());
|
||
|
assertFalse(info.isOverride());
|
||
|
assertTrue(info.isNoTypeCheck());
|
||
|
}
|
||
|
|
||
|
public void testSetOverride() {
|
||
|
JSDocInfo info = new JSDocInfo();
|
||
|
info.setOverride(true);
|
||
|
|
||
|
assertFalse(info.isDeprecated());
|
||
|
assertFalse(info.isNoAlias());
|
||
|
assertTrue(info.isOverride());
|
||
|
}
|
||
|
|
||
|
public void testSetExport() {
|
||
|
JSDocInfo info = new JSDocInfo();
|
||
|
info.setExport(true);
|
||
|
|
||
|
assertTrue(info.isExport());
|
||
|
}
|
||
|
|
||
|
public void testSetNoAlias() {
|
||
|
JSDocInfo info = new JSDocInfo();
|
||
|
info.setNoAlias(true);
|
||
|
|
||
|
assertFalse(info.isDeprecated());
|
||
|
assertFalse(info.isOverride());
|
||
|
assertTrue(info.isNoAlias());
|
||
|
}
|
||
|
|
||
|
public void testSetDeprecated() {
|
||
|
JSDocInfo info = new JSDocInfo();
|
||
|
info.setDeprecated(true);
|
||
|
|
||
|
assertFalse(info.isNoAlias());
|
||
|
assertFalse(info.isOverride());
|
||
|
assertTrue(info.isDeprecated());
|
||
|
}
|
||
|
|
||
|
public void testMultipleSetFlags1() {
|
||
|
JSDocInfo info = new JSDocInfo();
|
||
|
info.setConstant(true);
|
||
|
info.setConstructor(true);
|
||
|
info.setHidden(true);
|
||
|
info.setShouldPreserveTry(true);
|
||
|
|
||
|
assertFalse(info.hasType());
|
||
|
assertTrue(info.isConstant());
|
||
|
assertTrue(info.isConstructor());
|
||
|
assertFalse(info.isDefine());
|
||
|
assertTrue(info.isHidden());
|
||
|
assertTrue(info.shouldPreserveTry());
|
||
|
|
||
|
info.setHidden(false);
|
||
|
|
||
|
assertTrue(info.isConstant());
|
||
|
assertTrue(info.isConstructor());
|
||
|
assertFalse(info.isDefine());
|
||
|
assertFalse(info.isHidden());
|
||
|
assertTrue(info.shouldPreserveTry());
|
||
|
|
||
|
info.setConstant(false);
|
||
|
info.setConstructor(false);
|
||
|
|
||
|
assertFalse(info.isConstant());
|
||
|
assertFalse(info.isConstructor());
|
||
|
assertFalse(info.isDefine());
|
||
|
assertFalse(info.isHidden());
|
||
|
assertTrue(info.shouldPreserveTry());
|
||
|
|
||
|
info.setConstructor(true);
|
||
|
|
||
|
assertFalse(info.isConstant());
|
||
|
assertTrue(info.isConstructor());
|
||
|
assertFalse(info.isDefine());
|
||
|
assertFalse(info.isHidden());
|
||
|
assertTrue(info.shouldPreserveTry());
|
||
|
}
|
||
|
|
||
|
public void testSetFileOverviewWithDocumentationOff() {
|
||
|
JSDocInfo info = new JSDocInfo();
|
||
|
info.documentFileOverview("hi bob");
|
||
|
assertNull(info.getFileOverview());
|
||
|
}
|
||
|
|
||
|
public void testSetFileOverviewWithDocumentationOn() {
|
||
|
JSDocInfo info = new JSDocInfo(true);
|
||
|
info.documentFileOverview("hi bob");
|
||
|
assertEquals("hi bob", info.getFileOverview());
|
||
|
}
|
||
|
|
||
|
public void testSetSuppressions() {
|
||
|
JSDocInfo info = new JSDocInfo(true);
|
||
|
info.setSuppressions(Sets.newHashSet("sam", "bob"));
|
||
|
assertEquals(Sets.newHashSet("bob", "sam"), info.getSuppressions());
|
||
|
}
|
||
|
|
||
|
public void testSetModifies() {
|
||
|
JSDocInfo info = new JSDocInfo(true);
|
||
|
info.setModifies(Sets.newHashSet("this"));
|
||
|
assertEquals(Sets.newHashSet("this"), info.getModifies());
|
||
|
|
||
|
info = new JSDocInfo(true);
|
||
|
info.setModifies(Sets.newHashSet("arguments"));
|
||
|
assertEquals(Sets.newHashSet("arguments"), info.getModifies());
|
||
|
}
|
||
|
|
||
|
/** Gets the type expression for a simple type name. */
|
||
|
private JSTypeExpression fromString(String s) {
|
||
|
return new JSTypeExpression(Node.newString(s), "");
|
||
|
}
|
||
|
|
||
|
private JSType resolve(JSTypeExpression n, String... warnings) {
|
||
|
errorReporter.setWarnings(warnings);
|
||
|
return n.evaluate(null, registry);
|
||
|
}
|
||
|
|
||
|
private void assertTypeEquals(JSTypeNative a, JSType b) {
|
||
|
assertTypeEquals(getNativeType(a), b);
|
||
|
}
|
||
|
|
||
|
private void assertTypeEquals(JSType a, JSType b) {
|
||
|
Asserts.assertTypeEquals(a, b);
|
||
|
}
|
||
|
}
|