001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.javascript.editing;
043:
044: import org.mozilla.javascript.Node;
045: import org.netbeans.editor.BaseDocument;
046: import org.openide.filesystems.FileObject;
047:
048: /**
049: * @todo Test compound assignment: x = File::Stat.new
050: *
051: * @author Tor Norbye
052: */
053: public class JsTypeAnalyzerTest extends JsTestBase {
054:
055: public JsTypeAnalyzerTest(String testName) {
056: super (testName);
057: }
058:
059: private JsTypeAnalyzer getAnalyzer(String file, String caretLine,
060: boolean findMethod) throws Exception {
061: FileObject fo = getTestFile(file);
062: BaseDocument doc = getDocument(fo);
063: TestCompilationInfo info = getInfo(fo);
064: Node root = AstUtilities.getRoot(info);
065: initializeRegistry();
066: JsIndex index = JsIndex.get(info
067: .getIndex(JsMimeResolver.JAVASCRIPT_MIME_TYPE));
068:
069: int caretOffset = -1;
070: if (caretLine != null) {
071: int caretDelta = caretLine.indexOf("^");
072: assertTrue(caretDelta != -1);
073: caretLine = caretLine.substring(0, caretDelta)
074: + caretLine.substring(caretDelta + 1);
075: int lineOffset = info.getText().indexOf(caretLine);
076: assertTrue(lineOffset != -1);
077: caretOffset = lineOffset + caretDelta;
078: }
079:
080: AstPath path = new AstPath(root, caretOffset);
081: Node node = path.leaf();
082:
083: if (findMethod) {
084: Node method = AstUtilities.findMethodAtOffset(root,
085: caretOffset);
086: assertNotNull(method);
087:
088: root = method;
089: }
090:
091: JsTypeAnalyzer instance = new JsTypeAnalyzer(info, index, root,
092: node, caretOffset, caretOffset, doc, fo);
093:
094: return instance;
095: }
096:
097: public void testGetType() throws Exception {
098: JsTypeAnalyzer instance = getAnalyzer("testfiles/types1.js",
099: "// E^ND", false);
100:
101: assertEquals("String", instance.getType("a"));
102: assertEquals("Number", instance.getType("b"));
103: assertEquals("Number", instance.getType("c"));
104: assertEquals("String", instance.getType("d"));
105: assertEquals("RegExp", instance.getType("e"));
106: assertEquals("Function", instance.getType("f"));
107: assertEquals("String", instance.getType("g"));
108: assertEquals("RegExp", instance.getType("h"));
109: assertEquals("MyObj", instance.getType("i"));
110: assertEquals("MyObj", instance.getType("j"));
111: assertEquals("String", instance.getType("k"));
112: assertNull(instance.getType("l"));
113: assertEquals("Boolean", instance.getType("m"));
114: assertEquals("Boolean", instance.getType("n"));
115: assertEquals("Spry.Effect.Foo", instance.getType("o"));
116: assertEquals("Spry.Effect.AnimatedElement", instance
117: .getType("p"));
118: }
119:
120: public void testGetType2() throws Exception {
121: JsTypeAnalyzer instance = getAnalyzer("testfiles/types2.js",
122: "// I^nitial", true);
123:
124: assertEquals("Mixed", instance.getType("el"));
125: assertEquals("Object|Array", instance.getType("values"));
126: assertEquals("Boolean", instance.getType("returnElement"));
127: assertEquals(null, instance.getType("unknown"));
128: }
129:
130: //public void testGetReturnType1() throws Exception {
131: // JsTypeAnalyzer instance = getAnalyzer("testfiles/types2.js", "^alert(mycall)", true);
132: //
133: // assertEquals("HTMLElement|Axt.Element", instance.getType("mycall"));
134: //}
135:
136: // TODO - test intermediate state, e.g. asking for the value of k in the middle of the file before
137: // reassignment should yield earlier value
138: }
|