tor@373
|
1 |
/*
|
phrebejk@559
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
tor@373
|
3 |
*
|
jglick@4117
|
4 |
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
|
jglick@4117
|
5 |
*
|
jglick@4117
|
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
|
jglick@4117
|
7 |
* Other names may be trademarks of their respective owners.
|
mkrauskopf@422
|
8 |
*
|
phrebejk@559
|
9 |
* The contents of this file are subject to the terms of either the GNU
|
phrebejk@559
|
10 |
* General Public License Version 2 only ("GPL") or the Common
|
phrebejk@559
|
11 |
* Development and Distribution License("CDDL") (collectively, the
|
phrebejk@559
|
12 |
* "License"). You may not use this file except in compliance with the
|
phrebejk@559
|
13 |
* License. You can obtain a copy of the License at
|
phrebejk@559
|
14 |
* http://www.netbeans.org/cddl-gplv2.html
|
phrebejk@559
|
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
phrebejk@559
|
16 |
* specific language governing permissions and limitations under the
|
phrebejk@559
|
17 |
* License. When distributing the software, include this License Header
|
phrebejk@559
|
18 |
* Notice in each file and include the License file at
|
jglick@4117
|
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
|
phrebejk@559
|
20 |
* particular file as subject to the "Classpath" exception as provided
|
jglick@4117
|
21 |
* by Oracle in the GPL Version 2 section of the License file that
|
phrebejk@559
|
22 |
* accompanied this code. If applicable, add the following below the
|
phrebejk@559
|
23 |
* License Header, with the fields enclosed by brackets [] replaced by
|
phrebejk@559
|
24 |
* your own identifying information:
|
mkrauskopf@422
|
25 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
mkrauskopf@422
|
26 |
*
|
phrebejk@559
|
27 |
* Contributor(s):
|
phrebejk@559
|
28 |
*
|
mkrauskopf@422
|
29 |
* The Original Software is NetBeans. The Initial Developer of the Original
|
mkrauskopf@2738
|
30 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
|
mkrauskopf@422
|
31 |
* Microsystems, Inc. All Rights Reserved.
|
phrebejk@559
|
32 |
*
|
phrebejk@559
|
33 |
* If you wish your version of this file to be governed by only the CDDL
|
phrebejk@559
|
34 |
* or only the GPL Version 2, indicate your decision by adding
|
phrebejk@559
|
35 |
* "[Contributor] elects to include this software in this distribution
|
phrebejk@559
|
36 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a
|
phrebejk@559
|
37 |
* single choice of license, a recipient has the option to distribute
|
phrebejk@559
|
38 |
* your version of this file under either the CDDL, the GPL Version 2 or
|
phrebejk@559
|
39 |
* to extend the choice of license to its licensees as provided above.
|
phrebejk@559
|
40 |
* However, if you add GPL Version 2 code and therefore, elected the GPL
|
phrebejk@559
|
41 |
* Version 2 license, then the option applies only if the new code is
|
phrebejk@559
|
42 |
* made subject to such option by the copyright holder.
|
tor@373
|
43 |
*/
|
tor@373
|
44 |
|
tor@373
|
45 |
package org.netbeans.modules.ruby;
|
tor@373
|
46 |
|
tor@581
|
47 |
import java.util.ArrayList;
|
tor@546
|
48 |
import java.util.Arrays;
|
mkrauskopf@2864
|
49 |
import java.util.Iterator;
|
mkrauskopf@2864
|
50 |
import java.util.LinkedHashSet;
|
tor@546
|
51 |
import java.util.List;
|
tor@2094
|
52 |
import java.util.Map;
|
tor@546
|
53 |
import java.util.Set;
|
emononen@3259
|
54 |
import org.jrubyparser.ast.ArrayNode;
|
emononen@3781
|
55 |
import org.jrubyparser.ast.CallNode;
|
emononen@3259
|
56 |
import org.jrubyparser.ast.DefnNode;
|
emononen@3259
|
57 |
import org.jrubyparser.ast.FCallNode;
|
emononen@3781
|
58 |
import org.jrubyparser.ast.FalseNode;
|
emononen@3781
|
59 |
import org.jrubyparser.ast.FixnumNode;
|
emononen@3259
|
60 |
import org.jrubyparser.ast.IterNode;
|
emononen@3259
|
61 |
import org.jrubyparser.ast.MethodDefNode;
|
emononen@3259
|
62 |
import org.jrubyparser.ast.Node;
|
emononen@3259
|
63 |
import org.jrubyparser.ast.NodeType;
|
emononen@3259
|
64 |
import org.jrubyparser.ast.ReturnNode;
|
emononen@3259
|
65 |
import org.jrubyparser.ast.StrNode;
|
emononen@3259
|
66 |
import org.jrubyparser.ast.INameNode;
|
emononen@3781
|
67 |
import org.jrubyparser.ast.NilNode;
|
emononen@3781
|
68 |
import org.jrubyparser.ast.TrueNode;
|
emononen@3781
|
69 |
import org.jrubyparser.ast.VCallNode;
|
vstejskal@3085
|
70 |
import org.netbeans.api.java.classpath.ClassPath;
|
mkrauskopf@3040
|
71 |
import org.netbeans.modules.csl.api.OffsetRange;
|
mkrauskopf@3040
|
72 |
import org.netbeans.modules.parsing.spi.Parser;
|
tor@546
|
73 |
import org.openide.filesystems.FileObject;
|
tor@373
|
74 |
|
tor@373
|
75 |
/**
|
tor@373
|
76 |
* @todo Lots of other methods to test!
|
tor@373
|
77 |
*
|
tor@373
|
78 |
* @author Tor Norbye
|
tor@373
|
79 |
*/
|
tor@373
|
80 |
public class AstUtilitiesTest extends RubyTestBase {
|
tor@373
|
81 |
|
tor@373
|
82 |
public AstUtilitiesTest(String testName) {
|
tor@373
|
83 |
super(testName);
|
tor@373
|
84 |
}
|
tor@373
|
85 |
|
vstejskal@3085
|
86 |
@Override
|
emononen@3450
|
87 |
protected void setUp() throws Exception {
|
emononen@3459
|
88 |
super.setUp();
|
emononen@3450
|
89 |
// makes the indexer to index also top level methods in tests
|
emononen@3459
|
90 |
RubyIndexer.userSourcesTest = true;
|
emononen@3450
|
91 |
}
|
emononen@3450
|
92 |
|
emononen@3450
|
93 |
|
emononen@3450
|
94 |
@Override
|
vstejskal@3085
|
95 |
protected Map<String, ClassPath> createClassPathsForTest() {
|
vstejskal@3085
|
96 |
return rubyTestsClassPath();
|
vstejskal@3085
|
97 |
}
|
vstejskal@3085
|
98 |
|
mkrauskopf@2765
|
99 |
public void testFindBySignature1() throws Exception {
|
tor@373
|
100 |
// Test top level methods
|
tor@373
|
101 |
Node root = getRootNode("testfiles/top_level.rb");
|
tor@373
|
102 |
Node node = AstUtilities.findBySignature(root, "Object#bar(baz)");
|
tor@373
|
103 |
assertNotNull(node);
|
tor@373
|
104 |
assertEquals("bar", ((INameNode)node).getName());
|
tor@373
|
105 |
}
|
tor@373
|
106 |
|
mkrauskopf@2765
|
107 |
public void testFindBySignature2() throws Exception {
|
tor@373
|
108 |
Node root = getRootNode("testfiles/ape.rb");
|
tor@373
|
109 |
Node node = AstUtilities.findBySignature(root, "Ape#test_sorting(coll)");
|
tor@373
|
110 |
assertNotNull(node);
|
tor@373
|
111 |
assertEquals("test_sorting", ((INameNode)node).getName());
|
tor@373
|
112 |
}
|
tor@411
|
113 |
|
mkrauskopf@2765
|
114 |
public void testFindMethodBySignatureNested() throws Exception {
|
tor@581
|
115 |
Node root = getRootNode("testfiles/resolv.rb");
|
tor@2566
|
116 |
Node node = AstUtilities.findBySignature(root, "Resolv::DNS#lazy_initialize");
|
tor@581
|
117 |
assertNotNull(node);
|
tor@581
|
118 |
assertEquals("lazy_initialize", ((INameNode)node).getName());
|
tor@581
|
119 |
}
|
tor@2498
|
120 |
|
mkrauskopf@2765
|
121 |
public void testFindConstantBySignatureNested() throws Exception {
|
mkrauskopf@2765
|
122 |
Node root = getRootNode("testfiles/constants.rb");
|
mkrauskopf@2765
|
123 |
Node node = AstUtilities.findBySignature(root, "Colors::Converter#VERSION");
|
mkrauskopf@2765
|
124 |
assertNotNull(node);
|
mkrauskopf@2765
|
125 |
assertEquals("VERSION", ((INameNode)node).getName());
|
mkrauskopf@2765
|
126 |
}
|
mkrauskopf@2765
|
127 |
|
mkrauskopf@2765
|
128 |
public void testFindConstantBySignature() throws Exception {
|
mkrauskopf@2765
|
129 |
Node root = getRootNode("testfiles/constants.rb");
|
mkrauskopf@2765
|
130 |
Node node = AstUtilities.findBySignature(root, "Colors#RED");
|
mkrauskopf@2765
|
131 |
assertNotNull(node);
|
mkrauskopf@2765
|
132 |
assertEquals("RED", ((INameNode)node).getName());
|
mkrauskopf@2765
|
133 |
}
|
mkrauskopf@2765
|
134 |
|
mkrauskopf@2765
|
135 |
public void testFindBySignatureInstance() throws Exception {
|
tor@411
|
136 |
Node root = getRootNode("testfiles/ape.rb");
|
tor@411
|
137 |
Node node = AstUtilities.findBySignature(root, "Ape#@dialogs");
|
tor@411
|
138 |
assertNotNull(node);
|
emononen@3259
|
139 |
assertEquals(node.getNodeType(), NodeType.INSTASGNNODE);
|
enebo@4542
|
140 |
assertEquals("@dialogs", ((INameNode)node).getLexicalName());
|
tor@411
|
141 |
}
|
tor@411
|
142 |
|
mkrauskopf@2765
|
143 |
public void testFindBySignatureClassVar() throws Exception {
|
tor@411
|
144 |
Node root = getRootNode("testfiles/ape.rb");
|
tor@411
|
145 |
Node node = AstUtilities.findBySignature(root, "Ape#@@debugging");
|
tor@411
|
146 |
assertNotNull(node);
|
emononen@3259
|
147 |
assertEquals(node.getNodeType(), NodeType.CLASSVARASGNNODE);
|
enebo@4542
|
148 |
assertEquals("@@debugging", ((INameNode)node).getLexicalName());
|
tor@411
|
149 |
}
|
tor@546
|
150 |
|
tor@546
|
151 |
public void testFindRequires() throws Exception {
|
tor@546
|
152 |
Node root = getRootNode("testfiles/ape.rb");
|
tor@546
|
153 |
Set<String> requires = AstUtilities.getRequires(root);
|
tor@546
|
154 |
List<String> expected = Arrays.asList(new String[] {
|
tor@546
|
155 |
"rexml/document",
|
tor@546
|
156 |
"rubygems",
|
tor@546
|
157 |
"builder",
|
tor@546
|
158 |
"getter",
|
tor@546
|
159 |
"service",
|
tor@546
|
160 |
"samples",
|
tor@546
|
161 |
"entry",
|
tor@546
|
162 |
"poster",
|
tor@546
|
163 |
"collection",
|
tor@546
|
164 |
"deleter",
|
tor@546
|
165 |
"putter",
|
tor@546
|
166 |
"feed",
|
tor@546
|
167 |
"html",
|
tor@546
|
168 |
"crumbs",
|
tor@2498
|
169 |
"escaper",
|
tor@546
|
170 |
"categories",
|
tor@546
|
171 |
"names",
|
tor@546
|
172 |
"validator",
|
tor@546
|
173 |
"authent"
|
tor@546
|
174 |
});
|
tor@546
|
175 |
assertEquals(expected, requires);
|
tor@546
|
176 |
}
|
tor@2498
|
177 |
|
mkrauskopf@2864
|
178 |
public void testExitPointsFoo() {
|
mkrauskopf@2864
|
179 |
Node root = getRootNode("testfiles/exit_points.rb");
|
mkrauskopf@2864
|
180 |
MethodDefNode methodDef = (MethodDefNode) AstUtilities.findBySignature(root, "Dummy#foo");
|
mkrauskopf@2864
|
181 |
Set<Node> exits = new LinkedHashSet<Node>();
|
mkrauskopf@2864
|
182 |
AstUtilities.findExitPoints(methodDef, exits);
|
mkrauskopf@2864
|
183 |
assertEquals("two exit points, was: " + exits, 2, exits.size());
|
mkrauskopf@2864
|
184 |
Iterator<Node> it = exits.iterator();
|
mkrauskopf@2864
|
185 |
assertTrue("return node", it.next() instanceof ReturnNode);
|
mkrauskopf@2864
|
186 |
assertTrue("return node", it.next() instanceof ArrayNode);
|
mkrauskopf@2864
|
187 |
}
|
mkrauskopf@2864
|
188 |
|
mkrauskopf@2864
|
189 |
public void testExitPointsBoo() {
|
mkrauskopf@2864
|
190 |
Node root = getRootNode("testfiles/exit_points.rb");
|
mkrauskopf@2864
|
191 |
MethodDefNode methodDef = (MethodDefNode) AstUtilities.findBySignature(root, "Dummy#boo(unusedparam,unusedparam2,usedparam)");
|
mkrauskopf@2864
|
192 |
Set<Node> exits = new LinkedHashSet<Node>();
|
mkrauskopf@2864
|
193 |
AstUtilities.findExitPoints(methodDef, exits);
|
mkrauskopf@2864
|
194 |
assertEquals("one exit point, was: " + exits, 1, exits.size());
|
mkrauskopf@2864
|
195 |
Iterator<Node> it = exits.iterator();
|
mkrauskopf@2864
|
196 |
assertTrue("return node", it.next() instanceof FCallNode);
|
mkrauskopf@2864
|
197 |
}
|
mkrauskopf@2864
|
198 |
|
emononen@3781
|
199 |
public void testExitPointsBaz() {
|
emononen@3781
|
200 |
Node root = getRootNode("testfiles/exit_points.rb");
|
emononen@3781
|
201 |
MethodDefNode methodDef = (MethodDefNode) AstUtilities.findBySignature(root, "Dummy#baz");
|
emononen@3781
|
202 |
List<Node> exits = new ArrayList<Node>();
|
emononen@3781
|
203 |
AstUtilities.findExitPoints(methodDef, exits);
|
emononen@3781
|
204 |
assertEquals("two exit points, was: " + exits, 2, exits.size());
|
emononen@3781
|
205 |
assertTrue(exits.get(0) instanceof StrNode);
|
emononen@3781
|
206 |
assertTrue(exits.get(1) instanceof FixnumNode);
|
emononen@3781
|
207 |
}
|
emononen@3781
|
208 |
|
emononen@3781
|
209 |
public void testExitPointsBar() {
|
emononen@3781
|
210 |
Node root = getRootNode("testfiles/exit_points.rb");
|
emononen@3781
|
211 |
MethodDefNode methodDef = (MethodDefNode) AstUtilities.findBySignature(root, "Dummy#bar");
|
emononen@3781
|
212 |
List<Node> exits = new ArrayList<Node>();
|
emononen@3781
|
213 |
AstUtilities.findExitPoints(methodDef, exits);
|
emononen@3781
|
214 |
assertEquals("Was: " + exits, 3, exits.size());
|
emononen@3781
|
215 |
assertEquals("if", AstUtilities.getNameOrValue(exits.get(0)));
|
emononen@3781
|
216 |
assertEquals("elsif", AstUtilities.getNameOrValue(exits.get(1)));
|
emononen@3781
|
217 |
assertEquals("else", AstUtilities.getNameOrValue(exits.get(2)));
|
emononen@3781
|
218 |
}
|
emononen@3781
|
219 |
|
emononen@3781
|
220 |
public void testExitPointsQux() {
|
emononen@3781
|
221 |
Node root = getRootNode("testfiles/exit_points.rb");
|
emononen@3781
|
222 |
MethodDefNode methodDef = (MethodDefNode) AstUtilities.findBySignature(root, "Dummy#qux");
|
emononen@3781
|
223 |
List<Node> exits = new ArrayList<Node>();
|
emononen@3781
|
224 |
AstUtilities.findExitPoints(methodDef, exits);
|
emononen@3781
|
225 |
assertEquals("Was: " + exits, 3, exits.size());
|
emononen@3781
|
226 |
assertEquals("1", AstUtilities.getNameOrValue(exits.get(0)));
|
emononen@3781
|
227 |
assertEquals("2", AstUtilities.getNameOrValue(exits.get(1)));
|
emononen@3786
|
228 |
assertTrue(exits.get(2) instanceof FixnumNode);
|
emononen@3781
|
229 |
}
|
emononen@3781
|
230 |
|
emononen@3781
|
231 |
public void testExitPointsThud() {
|
emononen@3781
|
232 |
Node root = getRootNode("testfiles/exit_points.rb");
|
emononen@3781
|
233 |
MethodDefNode methodDef = (MethodDefNode) AstUtilities.findBySignature(root, "Dummy#thud");
|
emononen@3781
|
234 |
List<Node> exits = new ArrayList<Node>();
|
emononen@3781
|
235 |
AstUtilities.findExitPoints(methodDef, exits);
|
emononen@3781
|
236 |
assertEquals("Was: " + exits, 1, exits.size());
|
emononen@3781
|
237 |
assertEquals("2", AstUtilities.getNameOrValue(exits.get(0)));
|
emononen@3781
|
238 |
}
|
emononen@3781
|
239 |
|
emononen@3781
|
240 |
public void testExitPointsCorge() {
|
emononen@3781
|
241 |
Node root = getRootNode("testfiles/exit_points.rb");
|
emononen@3781
|
242 |
MethodDefNode methodDef = (MethodDefNode) AstUtilities.findBySignature(root, "Dummy#corge(z)");
|
emononen@3781
|
243 |
List<Node> exits = new ArrayList<Node>();
|
emononen@3781
|
244 |
AstUtilities.findExitPoints(methodDef, exits);
|
emononen@3781
|
245 |
assertEquals("Was: " + exits, 2, exits.size());
|
emononen@3781
|
246 |
assertEquals("a", AstUtilities.getNameOrValue(exits.get(0)));
|
emononen@3781
|
247 |
assertTrue(exits.get(1) instanceof NilNode);
|
emononen@3781
|
248 |
}
|
emononen@3781
|
249 |
|
emononen@3781
|
250 |
public void testExitPointsQuux() {
|
emononen@3781
|
251 |
Node root = getRootNode("testfiles/exit_points.rb");
|
emononen@3781
|
252 |
MethodDefNode methodDef = (MethodDefNode) AstUtilities.findBySignature(root, "Dummy#quux");
|
emononen@3781
|
253 |
List<Node> exits = new ArrayList<Node>();
|
emononen@3781
|
254 |
AstUtilities.findExitPoints(methodDef, exits);
|
emononen@3781
|
255 |
assertEquals("Was: " + exits, 4, exits.size());
|
emononen@3781
|
256 |
assertTrue(exits.get(0) instanceof TrueNode);
|
emononen@3781
|
257 |
assertTrue(exits.get(1) instanceof FalseNode);
|
emononen@3781
|
258 |
assertTrue(exits.get(2) instanceof VCallNode);
|
emononen@3781
|
259 |
assertTrue(exits.get(3) instanceof CallNode);
|
emononen@3781
|
260 |
}
|
emononen@3781
|
261 |
|
emononen@3781
|
262 |
public void testExitPointsFred() {
|
emononen@3781
|
263 |
Node root = getRootNode("testfiles/exit_points.rb");
|
emononen@3781
|
264 |
MethodDefNode methodDef = (MethodDefNode) AstUtilities.findBySignature(root, "Dummy#fred");
|
emononen@3781
|
265 |
List<Node> exits = new ArrayList<Node>();
|
emononen@3781
|
266 |
AstUtilities.findExitPoints(methodDef, exits);
|
emononen@3781
|
267 |
assertEquals("Was: " + exits, 1, exits.size());
|
emononen@3781
|
268 |
Node node = exits.get(0);
|
emononen@3781
|
269 |
assertTrue("Was " + node, node instanceof CallNode);
|
emononen@3781
|
270 |
}
|
emononen@3781
|
271 |
|
emononen@4042
|
272 |
public void testExitPointsBarry() {
|
emononen@4042
|
273 |
Node root = getRootNode("testfiles/exit_points.rb");
|
emononen@4042
|
274 |
MethodDefNode methodDef = (MethodDefNode) AstUtilities.findBySignature(root, "Dummy#barry(p)");
|
emononen@4042
|
275 |
List<Node> exits = new ArrayList<Node>();
|
emononen@4042
|
276 |
AstUtilities.findExitPoints(methodDef, exits);
|
emononen@4042
|
277 |
assertEquals("Was: " + exits, 3, exits.size());
|
emononen@4042
|
278 |
assertEquals("raise", AstUtilities.getNameOrValue(exits.get(0)));
|
enebo@4542
|
279 |
assertTrue(exits.get(2) instanceof FixnumNode);
|
enebo@4542
|
280 |
assertEquals("77", AstUtilities.getNameOrValue(exits.get(1)));
|
emononen@4042
|
281 |
}
|
emononen@4042
|
282 |
|
tor@546
|
283 |
public void testGetMethodName() {
|
tor@546
|
284 |
String testFile = "testfiles/ape.rb";
|
tor@546
|
285 |
FileObject fileObject = getTestFile(testFile);
|
tor@546
|
286 |
String text = readFile(fileObject);
|
tor@2498
|
287 |
|
tor@546
|
288 |
int offset = 0;
|
tor@546
|
289 |
String method = AstUtilities.getMethodName(fileObject, offset);
|
tor@546
|
290 |
assertNull(method);
|
tor@2498
|
291 |
|
tor@546
|
292 |
offset = text.indexOf("@w.text! lines[-1]");
|
tor@546
|
293 |
method = AstUtilities.getMethodName(fileObject, offset);
|
tor@546
|
294 |
assertEquals("report_li", method);
|
tor@2498
|
295 |
|
tor@546
|
296 |
offset = text.indexOf("step[1 .. -1].each { |li| report_li(nil, nil, li) }");
|
tor@546
|
297 |
method = AstUtilities.getMethodName(fileObject, offset);
|
tor@546
|
298 |
assertEquals("report_html", method);
|
tor@546
|
299 |
}
|
tor@2081
|
300 |
|
tor@2081
|
301 |
public void testGetTestName() {
|
tor@2081
|
302 |
String testFile = "testfiles/new_test.rb";
|
tor@2081
|
303 |
FileObject fileObject = getTestFile(testFile);
|
tor@2081
|
304 |
String text = readFile(fileObject);
|
tor@2498
|
305 |
|
tor@2081
|
306 |
int offset = 0;
|
tor@2081
|
307 |
String test = null;
|
tor@2498
|
308 |
|
tor@2081
|
309 |
offset = text.indexOf("something should happen to me okay?");
|
tor@2081
|
310 |
test = AstUtilities.getTestName(fileObject, offset);
|
tor@2081
|
311 |
assertEquals("test_something_should_happen_to_me_okay?", test);
|
tor@2081
|
312 |
|
tor@2081
|
313 |
offset = text.indexOf("something else should happen to me okay?");
|
tor@2081
|
314 |
test = AstUtilities.getTestName(fileObject, offset);
|
tor@2081
|
315 |
assertEquals("test_something_else_should_happen_to_me_okay?", test);
|
tor@2081
|
316 |
|
tor@2081
|
317 |
offset = text.indexOf("test \"something ");
|
tor@2081
|
318 |
test = AstUtilities.getTestName(fileObject, offset);
|
tor@2081
|
319 |
assertEquals("test_something_should_happen_to_me_okay?", test);
|
tor@2081
|
320 |
}
|
tor@2498
|
321 |
|
emononen@2899
|
322 |
public void testGetTestNameForShouldaTest() {
|
emononen@2899
|
323 |
String testFile = "testfiles/shoulda_test.rb";
|
emononen@2899
|
324 |
FileObject fileObject = getTestFile(testFile);
|
emononen@2899
|
325 |
String text = readFile(fileObject);
|
emononen@2899
|
326 |
|
emononen@2899
|
327 |
int offset = 0;
|
emononen@2899
|
328 |
String test = null;
|
emononen@2899
|
329 |
|
emononen@2912
|
330 |
offset = text.indexOf("be empty");
|
emononen@2912
|
331 |
test = AstUtilities.getTestName(fileObject, offset);
|
emononen@2912
|
332 |
assertEquals("Queue should be empty", test);
|
emononen@2912
|
333 |
|
emononen@2912
|
334 |
offset = text.indexOf("work ");
|
emononen@2912
|
335 |
test = AstUtilities.getTestName(fileObject, offset);
|
emononen@2912
|
336 |
assertEquals("A Queue instance with a space at the end should work ", test);
|
emononen@2912
|
337 |
|
emononen@2899
|
338 |
offset = text.indexOf("respond to :push");
|
emononen@2899
|
339 |
test = AstUtilities.getTestName(fileObject, offset);
|
emononen@2899
|
340 |
assertEquals("A Queue instance should respond to :push", test);
|
emononen@2899
|
341 |
|
emononen@2899
|
342 |
offset = text.indexOf("return that element on :pop");
|
emononen@2899
|
343 |
test = AstUtilities.getTestName(fileObject, offset);
|
emononen@2899
|
344 |
assertEquals("A Queue instance with a single element should return that element on :pop", test);
|
emononen@2899
|
345 |
}
|
emononen@2899
|
346 |
|
tor@581
|
347 |
public void testAddNodesByType() {
|
tor@581
|
348 |
Node root = getRootNode("testfiles/unused.rb");
|
tor@581
|
349 |
List<Node> result = new ArrayList<Node>();
|
tor@1291
|
350 |
AstUtilities.addNodesByType(root, new NodeType[] { NodeType.ITERNODE }, result);
|
tor@581
|
351 |
assertEquals(1, result.size());
|
tor@581
|
352 |
assertTrue(result.get(0) instanceof IterNode);
|
tor@581
|
353 |
}
|
tor@2498
|
354 |
|
tor@581
|
355 |
public void testAddNodesByType2() {
|
tor@581
|
356 |
Node root = getRootNode("testfiles/top_level.rb");
|
tor@581
|
357 |
List<Node> result = new ArrayList<Node>();
|
tor@1291
|
358 |
AstUtilities.addNodesByType(root, new NodeType[] { NodeType.DEFNNODE }, result);
|
tor@581
|
359 |
assertEquals(2, result.size());
|
tor@581
|
360 |
assertTrue(result.get(0) instanceof DefnNode);
|
tor@546
|
361 |
}
|
tor@2498
|
362 |
|
tor@2094
|
363 |
private void addAllNodes(Node node, List<Node> list, Node parent, Map<Node,Node> parents) {
|
tor@2094
|
364 |
try {
|
tor@2094
|
365 |
node.getPosition().getStartOffset();
|
tor@2094
|
366 |
node.getPosition().getEndOffset();
|
tor@2094
|
367 |
} catch (UnsupportedOperationException uoe) {
|
tor@2094
|
368 |
OffsetRange parentOffset = parent != null ? AstUtilities.getRange(parent) : OffsetRange.NONE;
|
tor@2094
|
369 |
fail(uoe.getMessage() + " node=" + node + " with parent" + parent + " at offset " + parentOffset.toString());
|
tor@2094
|
370 |
}
|
tor@2094
|
371 |
|
tor@2094
|
372 |
list.add(node);
|
tor@2094
|
373 |
parents.put(node, parent);
|
tor@2498
|
374 |
|
enebo@4542
|
375 |
for (Node child : node.childNodes()) {
|
tor@2094
|
376 |
addAllNodes(child, list, node, parents);
|
tor@2094
|
377 |
}
|
tor@2094
|
378 |
}
|
tor@2498
|
379 |
|
tor@2502
|
380 |
public void testGuessName() throws Exception {
|
mkrauskopf@3040
|
381 |
//public static String guessName(ParserResult info, OffsetRange lexRange, OffsetRange astRange) {
|
mkrauskopf@3040
|
382 |
Parser.Result parserResult = getParserResult("testfiles/arguments.rb");
|
mkrauskopf@3040
|
383 |
String text = getText(parserResult);
|
tor@2502
|
384 |
|
tor@2502
|
385 |
int caretOffset = getCaretOffset(text, "call1(^x)");
|
tor@2502
|
386 |
OffsetRange range = new OffsetRange(caretOffset, caretOffset);
|
mkrauskopf@3040
|
387 |
String name = AstUtilities.guessName(parserResult, range, range);
|
tor@2502
|
388 |
assertEquals("foo", name);
|
tor@2502
|
389 |
|
tor@2502
|
390 |
caretOffset = getCaretOffset(text, "call2(^y)");
|
tor@2502
|
391 |
range = new OffsetRange(caretOffset, caretOffset);
|
mkrauskopf@3040
|
392 |
name = AstUtilities.guessName(parserResult, range, range);
|
tor@2502
|
393 |
assertEquals("foo", name);
|
tor@2502
|
394 |
|
tor@2502
|
395 |
caretOffset = getCaretOffset(text, "call3(^x,y,z)");
|
tor@2502
|
396 |
range = new OffsetRange(caretOffset, caretOffset);
|
mkrauskopf@3040
|
397 |
name = AstUtilities.guessName(parserResult, range, range);
|
tor@2502
|
398 |
assertEquals("a", name);
|
tor@2502
|
399 |
|
tor@2502
|
400 |
caretOffset = getCaretOffset(text, "call3(x,^y,z)");
|
tor@2502
|
401 |
range = new OffsetRange(caretOffset, caretOffset);
|
mkrauskopf@3040
|
402 |
name = AstUtilities.guessName(parserResult, range, range);
|
tor@2502
|
403 |
assertEquals("b", name);
|
tor@2502
|
404 |
|
tor@2502
|
405 |
caretOffset = getCaretOffset(text, "call4(^x,y,z,w)");
|
tor@2502
|
406 |
range = new OffsetRange(caretOffset, caretOffset);
|
mkrauskopf@3040
|
407 |
name = AstUtilities.guessName(parserResult, range, range);
|
tor@2502
|
408 |
assertEquals("a", name);
|
tor@2502
|
409 |
|
tor@2502
|
410 |
caretOffset = getCaretOffset(text, "call4(x,^y,z,w)");
|
tor@2502
|
411 |
range = new OffsetRange(caretOffset, caretOffset);
|
mkrauskopf@3040
|
412 |
name = AstUtilities.guessName(parserResult, range, range);
|
tor@2502
|
413 |
assertEquals("b", name);
|
tor@2502
|
414 |
|
tor@2502
|
415 |
caretOffset = getCaretOffset(text, "call4(x,y,^z,w)");
|
tor@2502
|
416 |
range = new OffsetRange(caretOffset, caretOffset);
|
mkrauskopf@3040
|
417 |
name = AstUtilities.guessName(parserResult, range, range);
|
tor@2502
|
418 |
assertEquals("c", name);
|
tor@2502
|
419 |
|
tor@2502
|
420 |
caretOffset = getCaretOffset(text, "call4(x,y,z,^w)");
|
tor@2502
|
421 |
range = new OffsetRange(caretOffset, caretOffset);
|
mkrauskopf@3040
|
422 |
name = AstUtilities.guessName(parserResult, range, range);
|
tor@2502
|
423 |
assertEquals("d", name);
|
tor@2502
|
424 |
}
|
tor@2502
|
425 |
|
tor@2502
|
426 |
|
enebo@4522
|
427 |
// FIXME: 19UPGRADE - single error in rspec_core for utf8_encoded.rb
|
tor@2094
|
428 |
// Make sure we don't bomb out analyzing any of these files
|
enebo@4522
|
429 |
/*
|
tor@2498
|
430 |
public void testStress() throws Throwable {
|
tor@2094
|
431 |
List<FileObject> files = findJRubyRubyFiles();
|
tor@2094
|
432 |
for (FileObject file : files) {
|
mkrauskopf@3040
|
433 |
Parser.Result parserResult = getParserResult(file);
|
enebo@4522
|
434 |
BaseDocument doc = null;
|
enebo@4522
|
435 |
try {
|
enebo@4522
|
436 |
doc = RubyUtils.getDocument(parserResult, true);
|
enebo@4522
|
437 |
} catch (NullPointerException e) {
|
enebo@4522
|
438 |
assertFalse("File could not be retrieved: " + file, true);
|
enebo@4522
|
439 |
}
|
emononen@3096
|
440 |
assertNotNull("Document for file: " + file.getPath() + " must not be null.", doc);
|
tor@2094
|
441 |
List<Node> allNodes = new ArrayList<Node>();
|
mkrauskopf@3040
|
442 |
Node root = AstUtilities.getRoot(parserResult);
|
tor@2094
|
443 |
if (root == null || root.isInvisible()) {
|
tor@2094
|
444 |
continue;
|
tor@2094
|
445 |
}
|
tor@2094
|
446 |
assertNotNull(file + " had unexpected parsing errors", root);
|
tor@2094
|
447 |
Map<Node,Node> parents = new IdentityHashMap<Node,Node>(1000);
|
tor@2094
|
448 |
addAllNodes(root, allNodes, null, parents);
|
tor@2094
|
449 |
if (root == null) {
|
tor@2094
|
450 |
continue;
|
tor@2094
|
451 |
}
|
tor@2498
|
452 |
|
tor@2498
|
453 |
AstUtilities.getClasses(root);
|
tor@2498
|
454 |
AstUtilities.getRequires(root);
|
tor@2498
|
455 |
|
tor@2094
|
456 |
for (Node node : allNodes) {
|
tor@2094
|
457 |
try {
|
tor@2094
|
458 |
node.getPosition().getStartOffset();
|
tor@2094
|
459 |
node.getPosition().getEndOffset();
|
tor@2498
|
460 |
|
tor@2094
|
461 |
// Known exceptions - broken for getNameRange/getRange
|
tor@2094
|
462 |
if (node instanceof StrNode || node instanceof DStrNode) {
|
tor@2094
|
463 |
// See AstOffsetTest.testStringOffset1
|
tor@2094
|
464 |
continue;
|
tor@2094
|
465 |
}
|
tor@2498
|
466 |
|
tor@2498
|
467 |
AstUtilities.getFunctionNameRange(node);
|
tor@2094
|
468 |
AstUtilities.getNameRange(node);
|
tor@2498
|
469 |
OffsetRange nodeRange = AstUtilities.getRange(node);
|
tor@2498
|
470 |
|
tor@2498
|
471 |
// 147800
|
tor@2498
|
472 |
if (AstUtilities.isCall(node)) {
|
tor@2498
|
473 |
AstUtilities.getCallRange(node);
|
tor@2498
|
474 |
AstUtilities.getCallName(node);
|
tor@2498
|
475 |
|
tor@2498
|
476 |
for (int offset = nodeRange.getStart(); offset <= nodeRange.getEnd(); offset++) {
|
tor@2498
|
477 |
AstUtilities.findArgumentIndex(node, offset);
|
tor@2498
|
478 |
}
|
tor@2498
|
479 |
}
|
tor@2498
|
480 |
|
tor@2498
|
481 |
if (node instanceof AliasNode) {
|
tor@2498
|
482 |
AliasNode an = (AliasNode)node;
|
tor@2498
|
483 |
AstUtilities.getAliasOldRange(an);
|
tor@2498
|
484 |
AstUtilities.getAliasNewRange(an);
|
tor@2498
|
485 |
}
|
tor@2498
|
486 |
|
enebo@4518
|
487 |
if (AstUtilities.isAttr(node)) AstUtilities.getAttrSymbols(node);
|
enebo@4518
|
488 |
if (node instanceof MethodDefNode) AstUtilities.getDefName(node);
|
enebo@4518
|
489 |
if (node instanceof Colon2Node) AstUtilities.getFqn((Colon2Node)node);
|
tor@2498
|
490 |
|
tor@2498
|
491 |
if (node instanceof AssignableNode) {
|
enebo@4518
|
492 |
AstUtilities.offsetRangeFor(((AssignableNode)node).getLeftHandSidePosition());
|
tor@2498
|
493 |
}
|
tor@2498
|
494 |
|
tor@2498
|
495 |
if (node instanceof ClassNode) {
|
tor@2498
|
496 |
AstUtilities.getSuperclass((ClassNode)node);
|
tor@2498
|
497 |
}
|
tor@2094
|
498 |
} catch (Throwable t) {
|
tor@2094
|
499 |
Node parent = parents.get(node);
|
tor@2094
|
500 |
OffsetRange parentOffset = parent != null ? AstUtilities.getRange(parent) : OffsetRange.NONE;
|
tor@2094
|
501 |
fail(t.getMessage() + " while parsing " + FileUtil.getFileDisplayName(file) + " and node=" + node + " with parent" + parent + " at offset " + parentOffset.toString());
|
tor@2094
|
502 |
}
|
tor@2094
|
503 |
}
|
tor@2566
|
504 |
//ParserResult result = info.getEmbeddedResult(RubyInstallation.RUBY_MIME_TYPE, 0);
|
tor@2566
|
505 |
//LinkedList<AstElement> list = new LinkedList<AstElement>();
|
tor@2566
|
506 |
//RubyParseResult rpr = (RubyParseResult)result;
|
tor@2566
|
507 |
//list.addAll(rpr.getStructure().getElements());
|
tor@2566
|
508 |
//while (!list.isEmpty()) {
|
tor@2566
|
509 |
// AstElement el = list.removeFirst();
|
tor@2566
|
510 |
// String signature = el.getFqn();
|
tor@2566
|
511 |
// if (signature == null) {
|
tor@2566
|
512 |
// signature = el.getIn() + "." + el.getName();
|
tor@2566
|
513 |
// }
|
tor@2566
|
514 |
// if (signature.length() > 0 && !".".equals(signature)) {
|
tor@2566
|
515 |
// assertNotNull(el.toString(), signature);
|
tor@2566
|
516 |
// AstUtilities.findBySignature(root, signature);
|
tor@2566
|
517 |
// }
|
tor@2566
|
518 |
// list.addAll(el.getChildren());
|
tor@2566
|
519 |
//}
|
tor@2094
|
520 |
}
|
enebo@4522
|
521 |
}*/
|
tor@2498
|
522 |
|
tor@2498
|
523 |
public void testFindArguments1() throws Exception {
|
emononen@3096
|
524 |
Parser.Result parserResult = getParserResult("testfiles/ape.rb");
|
mkrauskopf@3040
|
525 |
String text = getText(parserResult);
|
tor@2498
|
526 |
int caretOffset = getCaretOffset(text, "might_^fail(uri, requested_e_coll, requested_m_coll)");
|
enebo@4554
|
527 |
Node call = AstUtilities.getRoot(parserResult).getNodeAt(caretOffset);
|
tor@2498
|
528 |
assertTrue(AstUtilities.isCall(call));
|
tor@2498
|
529 |
|
tor@2498
|
530 |
caretOffset = getCaretOffset(text, "might_fail(^uri, requested_e_coll, requested_m_coll)");
|
tor@2498
|
531 |
assertEquals(0, AstUtilities.findArgumentIndex(call, caretOffset));
|
tor@2498
|
532 |
|
tor@2498
|
533 |
caretOffset = getCaretOffset(text, "might_fail(uri^, requested_e_coll, requested_m_coll)");
|
tor@2498
|
534 |
assertEquals(0, AstUtilities.findArgumentIndex(call, caretOffset));
|
tor@2498
|
535 |
|
tor@2498
|
536 |
caretOffset = getCaretOffset(text, "might_fail(uri,^ requested_e_coll, requested_m_coll)");
|
tor@2498
|
537 |
assertEquals(1, AstUtilities.findArgumentIndex(call, caretOffset));
|
tor@2498
|
538 |
|
tor@2498
|
539 |
caretOffset = getCaretOffset(text, "might_fail(uri, requested_e_coll^, requested_m_coll)");
|
tor@2498
|
540 |
assertEquals(1, AstUtilities.findArgumentIndex(call, caretOffset));
|
tor@2498
|
541 |
|
tor@2498
|
542 |
caretOffset = getCaretOffset(text, "might_fail(uri, requested_e_coll,^ requested_m_coll)");
|
tor@2498
|
543 |
assertEquals(2, AstUtilities.findArgumentIndex(call, caretOffset));
|
tor@2498
|
544 |
|
tor@2498
|
545 |
caretOffset = getCaretOffset(text, "might_fail(uri, requested_e_coll, requested_m_coll^)");
|
tor@2498
|
546 |
assertEquals(2, AstUtilities.findArgumentIndex(call, caretOffset));
|
tor@2498
|
547 |
|
tor@2498
|
548 |
caretOffset = getCaretOffset(text, "might_fail(uri, requested_e_coll, requested_m_coll)^");
|
tor@2498
|
549 |
assertEquals(-1, AstUtilities.findArgumentIndex(call, caretOffset));
|
tor@2498
|
550 |
}
|
tor@2498
|
551 |
|
tor@2498
|
552 |
public void testFindArguments2() throws Exception {
|
emononen@3096
|
553 |
Parser.Result parserResult = getParserResult("testfiles/rubygems.rb");
|
mkrauskopf@3040
|
554 |
String text = getText(parserResult);
|
tor@2498
|
555 |
//int caretOffset = 2755; // "new" call from failed earlier test
|
tor@2498
|
556 |
int caretOffset = getCaretOffset(text, "MUTEX = Mutex.^new");
|
tor@2498
|
557 |
|
enebo@4554
|
558 |
Node call = AstUtilities.getRoot(parserResult).getNodeAt(caretOffset);
|
tor@2498
|
559 |
assertTrue(AstUtilities.isCall(call));
|
tor@2498
|
560 |
|
tor@2498
|
561 |
assertEquals(-1, AstUtilities.findArgumentIndex(call, caretOffset));
|
tor@2498
|
562 |
assertEquals(-1, AstUtilities.findArgumentIndex(call, caretOffset+3));
|
tor@2498
|
563 |
}
|
tor@2498
|
564 |
|
tor@2498
|
565 |
public void testFindArguments3() throws Exception {
|
emononen@3096
|
566 |
Parser.Result parserResult = getParserResult("testfiles/rubygems.rb");
|
mkrauskopf@3040
|
567 |
String text = getText(parserResult);
|
tor@2498
|
568 |
//int caretOffset = 2755; // "new" call from failed earlier test
|
tor@2498
|
569 |
int caretOffset = getCaretOffset(text, "Gem.ac^tivate(gem_name, *version_requirements)");
|
enebo@4554
|
570 |
Node call = AstUtilities.getRoot(parserResult).getNodeAt(caretOffset);
|
tor@2498
|
571 |
assertTrue(AstUtilities.isCall(call));
|
tor@2498
|
572 |
|
tor@2498
|
573 |
caretOffset = getCaretOffset(text, "Gem.^activate(gem_name, *version_requirements)");
|
tor@2498
|
574 |
assertEquals(-1, AstUtilities.findArgumentIndex(call, caretOffset));
|
tor@2498
|
575 |
|
tor@2498
|
576 |
caretOffset = getCaretOffset(text, "Gem.activate(^gem_name, *version_requirements)");
|
tor@2498
|
577 |
assertEquals(0, AstUtilities.findArgumentIndex(call, caretOffset));
|
tor@2498
|
578 |
|
tor@2498
|
579 |
caretOffset = getCaretOffset(text, "Gem.activate(gem_name^, *version_requirements)");
|
tor@2498
|
580 |
assertEquals(0, AstUtilities.findArgumentIndex(call, caretOffset));
|
tor@2498
|
581 |
|
tor@2498
|
582 |
caretOffset = getCaretOffset(text, "Gem.activate(gem_name,^ *version_requirements)");
|
tor@2498
|
583 |
assertEquals(1, AstUtilities.findArgumentIndex(call, caretOffset));
|
tor@2498
|
584 |
|
tor@2498
|
585 |
caretOffset = getCaretOffset(text, "Gem.activate(gem_name, ^*version_requirements)");
|
tor@2498
|
586 |
assertEquals(1, AstUtilities.findArgumentIndex(call, caretOffset));
|
tor@2498
|
587 |
|
tor@2498
|
588 |
caretOffset = getCaretOffset(text, "Gem.activate(gem_name, *^version_requirements)");
|
tor@2498
|
589 |
assertEquals(1, AstUtilities.findArgumentIndex(call, caretOffset));
|
tor@2498
|
590 |
|
tor@2498
|
591 |
caretOffset = getCaretOffset(text, "Gem.activate(gem_name, *version_requirements^)");
|
tor@2498
|
592 |
assertEquals(1, AstUtilities.findArgumentIndex(call, caretOffset));
|
tor@2498
|
593 |
|
tor@2498
|
594 |
|
tor@2498
|
595 |
caretOffset = getCaretOffset(text, "Gem::Dependency.^new(gem, version_requirements)");
|
tor@2498
|
596 |
caretOffset = getCaretOffset(text, "Gem::ConfigFile.^new []");
|
tor@2498
|
597 |
caretOffset = getCaretOffset(text, "StringIO.^new data");
|
tor@2498
|
598 |
caretOffset = getCaretOffset(text, "Gem::Dependency.^new gem, requirements");
|
tor@2498
|
599 |
|
tor@2498
|
600 |
// TODO - make sure I add up nested calls correctly, e.g.
|
tor@2498
|
601 |
// foo(bar, baz(boo(bazy)))
|
tor@2498
|
602 |
// TODO - test argscatnode - missing fallthrough node!
|
tor@2498
|
603 |
}
|
tor@373
|
604 |
}
|