ruby.project/test/unit/src/org/netbeans/modules/ruby/rubyproject/GotoTestTest.java
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
31 * Microsystems, Inc. All Rights Reserved.
33 * If you wish your version of this file to be governed by only the CDDL
34 * or only the GPL Version 2, indicate your decision by adding
35 * "[Contributor] elects to include this software in this distribution
36 * under the [CDDL or GPL Version 2] license." If you do not indicate a
37 * single choice of license, a recipient has the option to distribute
38 * your version of this file under either the CDDL, the GPL Version 2 or
39 * to extend the choice of license to its licensees as provided above.
40 * However, if you add GPL Version 2 code and therefore, elected the GPL
41 * Version 2 license, then the option applies only if the new code is
42 * made subject to such option by the copyright holder.
45 package org.netbeans.modules.ruby.rubyproject;
48 import org.netbeans.modules.csl.api.DeclarationFinder.DeclarationLocation;
49 import org.netbeans.spi.gototest.TestLocator;
50 import org.netbeans.spi.gototest.TestLocator.LocationResult;
51 import org.openide.filesystems.FileObject;
52 import org.openide.filesystems.FileUtil;
57 public class GotoTestTest extends RubyProjectTestBase {
59 private RubyProject project;
60 private GotoTest gotoTest;
62 public GotoTestTest(String testName) {
67 protected void setUp() throws Exception {
70 project = createTestProject("GotoTest");
71 assertNotNull(project);
72 FileObject dir = project.getProjectDirectory();
74 createFilesFromDesc(dir, "testfiles/gototestfiles");
76 // Create some data files where the class -contents- are used to locate the test
77 createFile(dir, "lib/hello.rb", "class Hello\ndef foo\nend\nend\n");
78 createFile(dir, "test/world.rb", "class HelloTest\ndef foobar\nend\nend\n");
80 createFile(dir, "whatever/donkey.rb", "#foo");
81 createFile(dir, "whatever/donkey_spec.rb", "#foo");
83 touch(dir, "app/controllers/donkey_controller.rb");
84 touch(dir, "spec/controllers/donkey_controller_spec.rb");
85 touch(dir, "test/functional/donkey_controller_test.rb");
87 gotoTest = new GotoTest();
90 private FileObject getProjFile(String file) {
91 return project.getProjectDirectory().getFileObject(file);
94 private void assertIsProjFile(String expectedRelPath, FileObject actualFO) {
95 String relative = getRelative(actualFO);
97 // slashify to the same format, so tests pass on all OSes
98 relative = relative.replace(File.separatorChar, '/');
99 expectedRelPath = expectedRelPath.replace(File.separatorChar, '/');
101 assertEquals(expectedRelPath + " is a project file", relative, expectedRelPath);
104 private String getRelative(FileObject fo) {
106 File path = FileUtil.toFile(fo);
107 File projPath = FileUtil.toFile(project.getProjectDirectory());
108 String relative = path.getAbsolutePath().substring(projPath.getAbsolutePath().length()+1);
113 public void testGotoTestUnit() {
114 assertNotNull(project);
116 DeclarationLocation loc = gotoTest.findTest(getProjFile("lib/foo.rb"), -1);
117 assertNotSame(DeclarationLocation.NONE, loc);
118 assertIsProjFile("test/test_foo.rb", loc.getFileObject());
121 public void testGotoTestUnit2() {
122 assertNotNull(project);
124 DeclarationLocation loc = gotoTest.findTest(getProjFile("lib/bar.rb"), -1);
125 assertNotSame(DeclarationLocation.NONE, loc);
126 assertIsProjFile("test/tc_bar.rb", loc.getFileObject());
129 public void testGotoTestUnit3() {
130 assertNotNull(project);
132 DeclarationLocation loc = gotoTest.findTest(getProjFile("lib/main.rb"), -1);
133 assertNotSame(DeclarationLocation.NONE, loc);
134 assertIsProjFile("test/main_test.rb", loc.getFileObject());
135 assertEquals(-1, loc.getOffset());
138 // The ZenTest patterns are only checked if the ZenTest gem is installed
139 //public void testGotoTestZenTest() {
140 // assertNotNull(project);
142 // DeclarationLocation loc = gotoTest.findTest(getProjFile("app/controllers/my_controller.rb"), -1);
143 // assertNotSame(DeclarationLocation.NONE, loc);
144 // assertIsProjFile("test/controllers/my_controller_test.rb", loc.getFileObject());
148 public void testGotoTestRspec() {
149 assertNotNull(project);
151 DeclarationLocation loc = gotoTest.findTest(getProjFile("app/models/whatever.rb"), -1);
152 assertNotSame(DeclarationLocation.NONE, loc);
153 assertIsProjFile("spec/models/whatever_spec.rb", loc.getFileObject());
154 assertEquals(-1, loc.getOffset());
157 public void testGotoTestRspecRubyProject() {
158 assertNotNull(project);
160 DeclarationLocation loc = gotoTest.findTest(getProjFile("lib/gibbon.rb"), -1);
161 assertNotSame(DeclarationLocation.NONE, loc);
162 assertIsProjFile("spec/gibbon_spec.rb", loc.getFileObject());
163 assertEquals(-1, loc.getOffset());
166 public void testGotoTestRails() {
167 assertNotNull(project);
169 DeclarationLocation loc = gotoTest.findTest(getProjFile("app/models/mymodel.rb"), -1);
170 assertNotSame(DeclarationLocation.NONE, loc);
171 assertIsProjFile("test/unit/mymodel_test.rb", loc.getFileObject());
172 assertEquals(-1, loc.getOffset());
175 public void testEnsureDirection1() {
176 // Make sure that looking for a test when we're in a test doesn't return the opposite
178 assertNotNull(project);
179 DeclarationLocation loc = gotoTest.findTest(getProjFile("test/test_foo.rb"), -1);
180 assertSame(DeclarationLocation.NONE, loc);
183 public void testEnsureDirection2() {
184 assertNotNull(project);
185 DeclarationLocation loc = gotoTest.findTested(getProjFile("lib/foo.rb"), -1);
186 assertSame(DeclarationLocation.NONE, loc);
189 public void testGotoTestedUnit() {
190 assertNotNull(project);
192 DeclarationLocation loc = gotoTest.findTested(getProjFile("test/test_foo.rb"), -1);
193 assertNotSame(DeclarationLocation.NONE, loc);
194 assertIsProjFile("lib/foo.rb", loc.getFileObject());
195 assertEquals(-1, loc.getOffset());
198 public void testGotoTestedUnit2() {
199 assertNotNull(project);
201 DeclarationLocation loc = gotoTest.findTested(getProjFile("test/tc_bar.rb"), -1);
202 assertNotSame(DeclarationLocation.NONE, loc);
203 assertIsProjFile("lib/bar.rb", loc.getFileObject());
204 assertEquals(-1, loc.getOffset());
207 // The ZenTest patterns are only checked if the ZenTest gem is installed
208 //public void testGotoTestedZenTest() {
209 // assertNotNull(project);
211 // DeclarationLocation loc = gotoTest.findTested(getProjFile("test/controllers/my_controller_test.rb"), -1);
212 // assertNotSame(DeclarationLocation.NONE, loc);
213 // assertIsProjFile("app/controllers/my_controller.rb", loc.getFileObject());
217 public void testGotoTestedRspec() {
218 assertNotNull(project);
220 DeclarationLocation loc = gotoTest.findTested(getProjFile("spec/models/whatever_spec.rb"), -1);
221 assertNotSame(DeclarationLocation.NONE, loc);
222 assertIsProjFile("app/models/whatever.rb", loc.getFileObject());
223 assertEquals(-1, loc.getOffset());
226 public void testGotoTestedRails() {
227 assertNotNull(project);
229 DeclarationLocation loc = gotoTest.findTested(getProjFile("test/unit/mymodel_test.rb"), -1);
230 assertNotSame(DeclarationLocation.NONE, loc);
231 assertIsProjFile("app/models/mymodel.rb", loc.getFileObject());
232 assertEquals(-1, loc.getOffset());
235 // The code index doesn't yet work at test time so the index search for HelloTest won't work
236 //public void testGotoTestedClass() {
237 // assertNotNull(project);
239 // DeclarationLocation loc = gotoTest.findTested(getProjFile("lib/hello.rb"), -1);
240 // assertNotSame(DeclarationLocation.NONE, loc);
241 // assertIsProjFile("test/world.rb", loc.getFileObject());
244 private final static String[] FILES = {
251 //"app/controllers/my_controller.rb",
252 //"test/controllers/my_controller_test.rb",
254 "app/models/whatever.rb",
255 "spec/models/whatever_spec.rb",
257 "app/models/mymodel.rb",
258 "test/unit/mymodel_test.rb"
262 public void testFindOpposite() {
264 for (; index < FILES.length; index += 2) {
265 FileObject source = getProjFile(FILES[index]);
266 FileObject test = getProjFile(FILES[index+1]);
268 DeclarationLocation loc = gotoTest.findTest(source, -1);
269 assertEquals(test, loc.getFileObject());
271 loc = gotoTest.findTested(test, -1);
272 assertEquals(source, loc.getFileObject());
274 TestLocator.LocationResult res = gotoTest.findOpposite(test, -1);
275 assertEquals(source, res.getFileObject());
276 assertEquals(-1, res.getOffset());
277 res = gotoTest.findOpposite(source, -1);
278 assertEquals(test, res.getFileObject());
279 assertEquals(-1, res.getOffset());
283 public void testGoto112812() {
284 assertNotNull(project);
286 DeclarationLocation loc = gotoTest.findTest(getProjFile("app/views/user/create.mab"), -1);
287 assertNotSame(DeclarationLocation.NONE, loc);
288 assertIsProjFile("spec/views/user/create_spec.rb", loc.getFileObject());
289 assertEquals(-1, loc.getOffset());
292 public void testGoto112812b() {
293 assertNotNull(project);
295 DeclarationLocation loc = gotoTest.findTest(getProjFile("app/views/user/_partial.mab"), -1);
296 assertNotSame(DeclarationLocation.NONE, loc);
297 assertIsProjFile("spec/views/user/_partial_spec.rb", loc.getFileObject());
298 assertEquals(-1, loc.getOffset());
301 public void testGoto112812c() {
302 assertNotNull(project);
304 DeclarationLocation loc = gotoTest.findTested(getProjFile("spec/views/user/create_spec.rb"), -1);
305 assertNotSame(DeclarationLocation.NONE, loc);
306 assertIsProjFile("app/views/user/create.mab", loc.getFileObject());
307 assertEquals(-1, loc.getOffset());
310 public void testGoto112812d() {
311 assertNotNull(project);
313 DeclarationLocation loc = gotoTest.findTested(getProjFile("spec/views/user/_partial_spec.rb"), -1);
314 assertNotSame(DeclarationLocation.NONE, loc);
315 assertIsProjFile("app/views/user/_partial.mab", loc.getFileObject());
316 assertEquals(-1, loc.getOffset());
319 public void testNegative() {
320 assertNotNull(project);
322 DeclarationLocation loc = gotoTest.findTest(getProjFile("app/controllers/lonely_controller.rb"), -1);
323 assertSame(DeclarationLocation.NONE, loc);
324 assertEquals(-1, loc.getOffset());
328 public void testNegative2() {
329 assertNotNull(project);
331 DeclarationLocation loc = gotoTest.findTest(getProjFile("test/unit/lonesometest.rb"), -1);
332 assertSame(DeclarationLocation.NONE, loc);
333 assertEquals(-1, loc.getOffset());
336 public void testNegative3() {
337 assertNotNull(project);
339 DeclarationLocation loc = gotoTest.findTested(getProjFile("app/controllers/lonely_controller.rb"), -1);
340 assertSame(DeclarationLocation.NONE, loc);
343 public void testGoto119106a() {
344 assertNotNull(project);
346 DeclarationLocation loc = gotoTest.findTest(getProjFile("app/models/rest_phone/phone_call.rb"), -1);
347 assertNotSame(DeclarationLocation.NONE, loc);
348 assertIsProjFile("test/unit/rest_phone/phone_call_test.rb", loc.getFileObject());
349 assertEquals(-1, loc.getOffset());
352 public void testGoto119106b() {
353 assertNotNull(project);
354 LocationResult loc = gotoTest.findOpposite(getProjFile("test/unit/rest_phone/phone_call_test.rb"), -1);
355 assertNotSame(DeclarationLocation.NONE, loc);
356 assertIsProjFile("app/models/rest_phone/phone_call.rb", loc.getFileObject());
357 assertEquals(-1, loc.getOffset());
360 public void testGoto119106c() {
361 assertNotNull(project);
362 DeclarationLocation loc = gotoTest.findTest(getProjFile("test/unit/rest_phone/phone_call_test.rb"), -1);
363 assertSame(DeclarationLocation.NONE, loc);
367 public void testGoto16588a() {
368 assertNotNull(project);
369 DeclarationLocation loc = gotoTest.findTest(getProjFile("lib/widget.rb"), -1);
370 assertNotSame(DeclarationLocation.NONE, loc);
371 assertIsProjFile("spec/lib/widget_spec.rb", loc.getFileObject());
374 public void testGoto16588b() {
375 assertNotNull(project);
376 DeclarationLocation loc = gotoTest.findTested(getProjFile("spec/lib/widget_spec.rb"), -1);
377 assertNotSame(DeclarationLocation.NONE, loc);
378 assertIsProjFile("lib/widget.rb", loc.getFileObject());
381 public void testGoto16588c() {
382 assertNotNull(project);
383 DeclarationLocation loc = gotoTest.findTest(getProjFile("lib/gadget.rb"), -1);
384 assertNotSame(DeclarationLocation.NONE, loc);
385 assertIsProjFile("test/lib/test_gadget.rb", loc.getFileObject());
388 public void testRSpecSingle() {
389 assertNotNull(project);
391 FileObject f = getProjFile("whatever/donkey_spec.rb");
393 DeclarationLocation loc = gotoTest.findTested(f, -1);
394 assertNotSame(DeclarationLocation.NONE, loc);
395 assertIsProjFile("whatever/donkey.rb", loc.getFileObject());
396 assertEquals(-1, loc.getOffset());
399 public void testRSpecSingle2() {
400 assertNotNull(project);
402 FileObject f = getProjFile("whatever/donkey.rb");
404 DeclarationLocation loc = gotoTest.findTest(f, -1);
405 assertNotSame(DeclarationLocation.NONE, loc);
406 assertIsProjFile("whatever/donkey_spec.rb", loc.getFileObject());
407 assertEquals(-1, loc.getOffset());
410 public void testRSpecVsTest() {
411 assertNotNull(project);
413 FileObject f = getProjFile("app/controllers/donkey_controller.rb");
415 DeclarationLocation loc = gotoTest.findTest(f, -1);
416 assertNotSame(DeclarationLocation.NONE, loc);
417 assertIsProjFile("test/functional/donkey_controller_test.rb", loc.getFileObject());
418 assertEquals(-1, loc.getOffset());