tor@364
|
1 |
/*
|
phrebejk@559
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
tor@364
|
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@422
|
30 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 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@364
|
43 |
*/
|
tor@364
|
44 |
|
tor@364
|
45 |
package org.netbeans.modules.ruby;
|
tor@364
|
46 |
|
tor@2546
|
47 |
import java.util.Collections;
|
emononen@3259
|
48 |
import org.jrubyparser.ast.Node;
|
mkrauskopf@3040
|
49 |
import org.netbeans.modules.csl.api.OffsetRange;
|
mkrauskopf@3040
|
50 |
import org.netbeans.modules.parsing.api.ParserManager;
|
mkrauskopf@3040
|
51 |
import org.netbeans.modules.parsing.api.ResultIterator;
|
mkrauskopf@3040
|
52 |
import org.netbeans.modules.parsing.api.Source;
|
mkrauskopf@3040
|
53 |
import org.netbeans.modules.parsing.api.UserTask;
|
mkrauskopf@3040
|
54 |
import org.netbeans.modules.parsing.spi.Parser;
|
tor@2546
|
55 |
import org.openide.filesystems.FileObject;
|
tor@364
|
56 |
|
tor@364
|
57 |
/**
|
tor@364
|
58 |
*
|
tor@364
|
59 |
* @author Tor Norbye
|
tor@364
|
60 |
*/
|
tor@364
|
61 |
public class RubyParserTest extends RubyTestBase {
|
tor@364
|
62 |
|
tor@364
|
63 |
public RubyParserTest(String testName) {
|
tor@364
|
64 |
super(testName);
|
tor@364
|
65 |
}
|
tor@364
|
66 |
|
tor@1540
|
67 |
@Override
|
tor@364
|
68 |
protected void setUp() throws Exception {
|
tor@364
|
69 |
super.setUp();
|
tor@364
|
70 |
}
|
tor@364
|
71 |
|
tor@1540
|
72 |
@Override
|
tor@364
|
73 |
protected void tearDown() throws Exception {
|
tor@364
|
74 |
super.tearDown();
|
tor@364
|
75 |
}
|
tor@364
|
76 |
|
mkrauskopf@3040
|
77 |
private void checkParseTree(final String file, final String caretLine, final String nodeName) throws Exception {
|
mkrauskopf@3040
|
78 |
Source source = Source.create(getTestFile(file));
|
tor@364
|
79 |
|
mkrauskopf@3040
|
80 |
final int caretOffset;
|
tor@364
|
81 |
if (caretLine != null) {
|
mkrauskopf@3040
|
82 |
caretOffset = getCaretOffset(source.createSnapshot().getText().toString(), caretLine);
|
mkrauskopf@3040
|
83 |
enforceCaretOffset(source, caretOffset);
|
mkrauskopf@3040
|
84 |
} else {
|
mkrauskopf@3040
|
85 |
caretOffset = -1;
|
tor@364
|
86 |
}
|
tor@364
|
87 |
|
mkrauskopf@3040
|
88 |
ParserManager.parse(Collections.singleton(source), new UserTask() {
|
mkrauskopf@3040
|
89 |
@Override
|
mkrauskopf@3040
|
90 |
public void run(ResultIterator resultIterator) throws Exception {
|
mkrauskopf@3040
|
91 |
Parser.Result pr = resultIterator.getParserResult();
|
mkrauskopf@3040
|
92 |
RubyParseResult rpr = AstUtilities.getParseResult(pr);
|
mkrauskopf@3040
|
93 |
|
mkrauskopf@3040
|
94 |
Node root = rpr.getRootNode();
|
mkrauskopf@3040
|
95 |
assertNotNull("Parsing broken input failed for " + file + "; " + rpr.getDiagnostics(), root);
|
mkrauskopf@3040
|
96 |
|
mkrauskopf@3040
|
97 |
// Ensure that we find the node we're looking for
|
mkrauskopf@3040
|
98 |
if (nodeName != null) {
|
mkrauskopf@3040
|
99 |
OffsetRange range = rpr.getSanitizedRange();
|
mkrauskopf@3040
|
100 |
int adjustedOffset;
|
mkrauskopf@3040
|
101 |
if (range.containsInclusive(caretOffset)) {
|
mkrauskopf@3040
|
102 |
adjustedOffset = range.getStart();
|
mkrauskopf@3040
|
103 |
} else {
|
mkrauskopf@3040
|
104 |
adjustedOffset = caretOffset;
|
mkrauskopf@3040
|
105 |
}
|
enebo@4554
|
106 |
Node closest = root.getNodeAt(adjustedOffset);
|
mkrauskopf@3040
|
107 |
assertNotNull(closest);
|
mkrauskopf@3040
|
108 |
String leafName = closest.getClass().getName();
|
mkrauskopf@3040
|
109 |
leafName = leafName.substring(leafName.lastIndexOf('.') + 1);
|
mkrauskopf@3040
|
110 |
assertEquals(nodeName, leafName);
|
mkrauskopf@3040
|
111 |
}
|
tor@364
|
112 |
}
|
mkrauskopf@3040
|
113 |
});
|
mkrauskopf@3040
|
114 |
|
tor@364
|
115 |
}
|
enebo@4542
|
116 |
|
enebo@4542
|
117 |
public void testEmpty1() throws Exception {
|
enebo@4542
|
118 |
checkParseTree("testfiles/empty.rb", "^", "RootNode");
|
enebo@4542
|
119 |
}
|
mkrauskopf@3040
|
120 |
|
tor@364
|
121 |
public void testPartial1() throws Exception {
|
tor@364
|
122 |
checkParseTree("testfiles/broken1.rb", "x.^", "VCallNode");
|
tor@364
|
123 |
}
|
tor@364
|
124 |
|
tor@364
|
125 |
public void testPartial1b() throws Exception {
|
tor@364
|
126 |
// Recover even when the caret is elsewhere
|
tor@364
|
127 |
checkParseTree("testfiles/broken1.rb", null, null);
|
tor@364
|
128 |
}
|
tor@364
|
129 |
|
tor@364
|
130 |
public void testPartial2() throws Exception {
|
emononen@3259
|
131 |
checkParseTree("testfiles/broken2.rb", "Foo.new.^", "CallNode");
|
tor@364
|
132 |
}
|
tor@364
|
133 |
|
tor@364
|
134 |
public void testPartial3() throws Exception {
|
tor@364
|
135 |
checkParseTree("testfiles/broken3.rb", "x = ^", "ClassNode");
|
tor@364
|
136 |
}
|
tor@364
|
137 |
|
tor@364
|
138 |
public void testPartial3b() throws Exception {
|
tor@364
|
139 |
// Recover even when the caret is elsewhere
|
tor@364
|
140 |
checkParseTree("testfiles/broken3.rb", null, null);
|
tor@364
|
141 |
}
|
tor@364
|
142 |
|
tor@364
|
143 |
public void testPartial4() throws Exception {
|
tor@364
|
144 |
checkParseTree("testfiles/broken4.rb", "Test::^", "ConstNode");
|
tor@364
|
145 |
}
|
tor@364
|
146 |
|
tor@364
|
147 |
public void testPartial4b() throws Exception {
|
tor@364
|
148 |
// Recover even when the caret is elsewhere
|
tor@364
|
149 |
checkParseTree("testfiles/broken4.rb", null, null);
|
tor@364
|
150 |
}
|
tor@364
|
151 |
|
tor@364
|
152 |
public void testPartial5() throws Exception {
|
tor@797
|
153 |
checkParseTree("testfiles/broken5.rb", "if true^", "TrueNode");
|
tor@364
|
154 |
}
|
tor@364
|
155 |
|
tor@364
|
156 |
public void testPartial5MissingEnd() throws Exception {
|
tor@364
|
157 |
// An end is missing and we don't have a current line we can simply
|
tor@364
|
158 |
// clip out; try to compensate
|
tor@364
|
159 |
checkParseTree("testfiles/broken5.rb", null, null);
|
tor@364
|
160 |
}
|
tor@364
|
161 |
|
tor@364
|
162 |
public void testPartial6() throws Exception {
|
tor@364
|
163 |
checkParseTree("testfiles/broken6.rb", "def ^", "ClassNode");
|
tor@364
|
164 |
}
|
tor@364
|
165 |
|
tor@797
|
166 |
public void testPartial12() throws Exception {
|
tor@797
|
167 |
checkParseTree("testfiles/broken12.rb", " File.exists?(^)", "ArrayNode");
|
tor@797
|
168 |
}
|
tor@364
|
169 |
|
tor@2501
|
170 |
public void testErrors1() throws Exception {
|
tor@2501
|
171 |
checkErrors("testfiles/colors.rb");
|
tor@2501
|
172 |
}
|
tor@2501
|
173 |
|
tor@2501
|
174 |
public void testErrors2() throws Exception {
|
tor@2501
|
175 |
checkErrors("testfiles/broken1.rb");
|
tor@2501
|
176 |
}
|
tor@2501
|
177 |
|
tor@2501
|
178 |
public void testErrors3() throws Exception {
|
tor@2501
|
179 |
checkErrors("testfiles/broken2.rb");
|
tor@2501
|
180 |
}
|
tor@2501
|
181 |
|
tor@2501
|
182 |
public void testErrors4() throws Exception {
|
tor@2501
|
183 |
checkErrors("testfiles/broken3.rb");
|
tor@2501
|
184 |
}
|
tor@2501
|
185 |
|
tor@2501
|
186 |
public void testErrors5() throws Exception {
|
tor@2501
|
187 |
checkErrors("testfiles/broken4.rb");
|
tor@2501
|
188 |
}
|
tor@2501
|
189 |
|
tor@2501
|
190 |
public void testErrors6() throws Exception {
|
tor@2501
|
191 |
checkErrors("testfiles/broken5.rb");
|
tor@2501
|
192 |
}
|
tor@2501
|
193 |
|
tor@2501
|
194 |
public void testErrors7() throws Exception {
|
tor@2501
|
195 |
checkErrors("testfiles/broken6.rb");
|
tor@2501
|
196 |
}
|
tor@2546
|
197 |
|
tor@2546
|
198 |
public void testValidResult() throws Exception {
|
tor@2546
|
199 |
// Make sure we get a valid parse result out of an aborted parse
|
tor@2546
|
200 |
FileObject fo = getTestFile("testfiles/broken6.rb");
|
mkrauskopf@3040
|
201 |
Source source = Source.create(fo);
|
tor@2546
|
202 |
|
mkrauskopf@3040
|
203 |
ParserManager.parse(Collections.singleton(source), new UserTask() {
|
mkrauskopf@3040
|
204 |
public
|
mkrauskopf@3040
|
205 |
@Override
|
mkrauskopf@3040
|
206 |
void run(ResultIterator resultIterator) throws Exception {
|
mkrauskopf@3040
|
207 |
Parser.Result r = resultIterator.getParserResult();
|
mkrauskopf@3040
|
208 |
RubyParseResult jspr = AstUtilities.getParseResult(r);
|
mkrauskopf@3040
|
209 |
assertNotNull("Expecting JsParseResult, but got " + r, jspr);
|
tor@2546
|
210 |
}
|
mkrauskopf@3040
|
211 |
});
|
tor@2546
|
212 |
}
|
tor@364
|
213 |
}
|