Skip to content

Commit e8bc7d8

Browse files
committed
tests refactoring
1 parent f2855ef commit e8bc7d8

File tree

6 files changed

+63
-46
lines changed

6 files changed

+63
-46
lines changed

tests/org.eclipse.dltk.javascript.core.tests/src/org/eclipse/dltk/javascript/core/tests/AllTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.eclipse.dltk.javascript.core.tests.search.SearchExternalLibraryTests;
1414
import org.eclipse.dltk.javascript.core.tests.search.SearchFieldTests;
1515
import org.eclipse.dltk.javascript.core.tests.search.SearchFunctionTests;
16+
import org.eclipse.dltk.javascript.core.tests.search.SearchLocalsTests;
1617
import org.eclipse.dltk.javascript.core.tests.search.SearchReferenceTests;
1718
import org.eclipse.dltk.javascript.core.tests.search.SearchTypeReferenceTests;
1819
import org.eclipse.dltk.javascript.core.tests.structure.JSLintModelTests;
@@ -68,6 +69,7 @@ public static Test suite() {
6869
suite.addTest(SearchTypeReferenceTests.suite());
6970
suite.addTest(SelectionTests.suite());
7071
suite.addTest(SearchFieldTests.suite());
72+
suite.addTest(SearchLocalsTests.suite());
7173
suite.addTest(SearchFunctionTests.suite());
7274
suite.addTest(SearchExternalLibraryTests.suite());
7375
// $JUnit-END$

tests/org.eclipse.dltk.javascript.core.tests/src/org/eclipse/dltk/javascript/core/tests/contentassist/SelectionTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
import org.eclipse.dltk.codeassist.ISelectionEngine;
2222
import org.eclipse.dltk.compiler.env.IModuleSource;
2323
import org.eclipse.dltk.core.DLTKLanguageManager;
24-
import org.eclipse.dltk.core.IField;
2524
import org.eclipse.dltk.core.ILocalVariable;
2625
import org.eclipse.dltk.core.IMethod;
2726
import org.eclipse.dltk.core.IModelElement;
2827
import org.eclipse.dltk.core.IScriptProject;
2928
import org.eclipse.dltk.core.ISourceRange;
29+
import org.eclipse.dltk.core.ISourceReference;
3030
import org.eclipse.dltk.core.ModelException;
3131
import org.eclipse.dltk.core.ScriptModelUtil;
3232
import org.eclipse.dltk.core.search.IDLTKSearchConstants;
@@ -185,8 +185,9 @@ public void testField() throws ModelException {
185185
IModelElement[] elements = select(module,
186186
lastPositionInFile("aa", module, false));
187187
assertEquals(1, elements.length);
188-
final IField local = (IField) elements[0];
189-
final ISourceRange nameRange = local.getNameRange();
188+
final IModelElement local = elements[0];
189+
final ISourceRange nameRange = ((ISourceReference) local)
190+
.getNameRange();
190191
final String contents = module.getSourceContents();
191192
assertEquals(contents.indexOf("aa"), nameRange.getOffset());
192193
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2012 NumberFour AG
3+
*
4+
* All rights reserved. This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License v1.0
6+
* which accompanies this distribution, and is available at
7+
* http://www.eclipse.org/legal/epl-v10.html
8+
*
9+
* Contributors:
10+
* NumberFour AG - initial API and Implementation (Alex Panchenko)
11+
*******************************************************************************/
12+
package org.eclipse.dltk.javascript.core.tests.search;
13+
14+
import org.eclipse.dltk.compiler.env.IModuleSource;
15+
import org.eclipse.dltk.core.IModelElement;
16+
import org.eclipse.dltk.core.ModelException;
17+
import org.eclipse.dltk.core.tests.model.AbstractSingleProjectSearchTests;
18+
import org.eclipse.dltk.javascript.core.tests.contentassist.SelectionTests;
19+
20+
public class AbstractSearchTest extends AbstractSingleProjectSearchTests {
21+
22+
protected AbstractSearchTest(String testPluginName, String testName,
23+
String scriptProjectName) {
24+
super(testPluginName, testName, scriptProjectName);
25+
}
26+
27+
protected IModelElement[] select(IModuleSource module, int position) {
28+
return new SelectionTests(null).select(module, position);
29+
}
30+
31+
protected IModuleSource getModule(String path) throws ModelException {
32+
return (IModuleSource) getSourceModule(getProjectName(), "src", path);
33+
}
34+
35+
}

tests/org.eclipse.dltk.javascript.core.tests/src/org/eclipse/dltk/javascript/core/tests/search/SearchFieldTests.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,18 @@
1212
package org.eclipse.dltk.javascript.core.tests.search;
1313

1414
import static org.eclipse.dltk.javascript.core.tests.AllTests.PLUGIN_ID;
15-
import static org.eclipse.dltk.javascript.core.tests.contentassist.AbstractContentAssistTest.lastPositionInFile;
1615
import static org.eclipse.dltk.javascript.core.tests.contentassist.AbstractContentAssistTest.firstPositionInFile;
16+
import static org.eclipse.dltk.javascript.core.tests.contentassist.AbstractContentAssistTest.lastPositionInFile;
1717

1818
import org.eclipse.core.runtime.CoreException;
1919
import org.eclipse.dltk.compiler.env.IModuleSource;
2020
import org.eclipse.dltk.core.IField;
2121
import org.eclipse.dltk.core.IModelElement;
22-
import org.eclipse.dltk.core.ModelException;
2322
import org.eclipse.dltk.core.search.FieldDeclarationMatch;
2423
import org.eclipse.dltk.core.search.FieldReferenceMatch;
25-
import org.eclipse.dltk.core.tests.model.AbstractSingleProjectSearchTests;
2624
import org.eclipse.dltk.core.tests.model.TestSearchResults;
27-
import org.eclipse.dltk.javascript.core.tests.contentassist.SelectionTests;
2825

29-
public class SearchFieldTests extends AbstractSingleProjectSearchTests {
26+
public class SearchFieldTests extends AbstractSearchTest {
3027

3128
public SearchFieldTests(String testName) {
3229
super(PLUGIN_ID, testName, "selection");
@@ -36,13 +33,9 @@ public static Suite suite() {
3633
return new Suite(SearchFieldTests.class);
3734
}
3835

39-
private IModuleSource getModule(String path) throws ModelException {
40-
return (IModuleSource) getSourceModule(getProjectName(), "src", path);
41-
}
42-
4336
public void testFieldAA() throws CoreException {
4437
IModuleSource module = getModule("fields.js");
45-
IModelElement[] elements = new SelectionTests(null).select(module,
38+
IModelElement[] elements = select(module,
4639
lastPositionInFile("aa", module, false));
4740
assertEquals(1, elements.length);
4841
final IField field = (IField) elements[0];
@@ -54,7 +47,7 @@ public void testFieldAA() throws CoreException {
5447

5548
public void testLazyFieldCC() throws CoreException {
5649
IModuleSource module = getModule("fields.js");
57-
IModelElement[] elements = new SelectionTests(null).select(module,
50+
IModelElement[] elements = select(module,
5851
firstPositionInFile("cc", module, false));
5952
assertEquals(1, elements.length);
6053
final IField field = (IField) elements[0];

tests/org.eclipse.dltk.javascript.core.tests/src/org/eclipse/dltk/javascript/core/tests/search/SearchFunctionTests.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@
1818
import org.eclipse.dltk.compiler.env.IModuleSource;
1919
import org.eclipse.dltk.core.IMethod;
2020
import org.eclipse.dltk.core.IModelElement;
21-
import org.eclipse.dltk.core.ModelException;
2221
import org.eclipse.dltk.core.search.MethodDeclarationMatch;
2322
import org.eclipse.dltk.core.search.MethodReferenceMatch;
2423
import org.eclipse.dltk.core.tests.Skip;
25-
import org.eclipse.dltk.core.tests.model.AbstractSingleProjectSearchTests;
2624
import org.eclipse.dltk.core.tests.model.TestSearchResults;
27-
import org.eclipse.dltk.javascript.core.tests.contentassist.SelectionTests;
2825

29-
public class SearchFunctionTests extends AbstractSingleProjectSearchTests {
26+
public class SearchFunctionTests extends AbstractSearchTest {
3027

3128
public SearchFunctionTests(String testName) {
3229
super(PLUGIN_ID, testName, "selection");
@@ -36,13 +33,9 @@ public static Suite suite() {
3633
return new Suite(SearchFunctionTests.class);
3734
}
3835

39-
private IModuleSource getModule(String path) throws ModelException {
40-
return (IModuleSource) getSourceModule(getProjectName(), "src", path);
41-
}
42-
4336
public void testFunctionGlobalField() throws CoreException {
4437
IModuleSource module = getModule("functions.js");
45-
IModelElement[] elements = new SelectionTests(null).select(module,
38+
IModelElement[] elements = select(module,
4639
lastPositionInFile("fun1", module, false));
4740
assertEquals(1, elements.length);
4841
final IMethod method = (IMethod) elements[0];
@@ -54,7 +47,7 @@ public void testFunctionGlobalField() throws CoreException {
5447

5548
public void testFunctionLocalField() throws CoreException {
5649
IModuleSource module = getModule("functions.js");
57-
IModelElement[] elements = new SelectionTests(null).select(module,
50+
IModelElement[] elements = select(module,
5851
lastPositionInFile("fun2", module, false));
5952
assertEquals(1, elements.length);
6053
final IMethod method = (IMethod) elements[0];
@@ -66,15 +59,15 @@ public void testFunctionLocalField() throws CoreException {
6659

6760
public void testFunctionLocalFieldWithDoubleName() throws CoreException {
6861
IModuleSource module = getModule("functions.js");
69-
IModelElement[] elements = new SelectionTests(null).select(module,
62+
IModelElement[] elements = select(module,
7063
lastPositionInFile("fun4", module, false));
7164
assertEquals(0, elements.length);
7265
}
7366

7467
@Skip
7568
public void testFunctionThisField() throws CoreException {
7669
IModuleSource module = getModule("functions.js");
77-
IModelElement[] elements = new SelectionTests(null).select(module,
70+
IModelElement[] elements = select(module,
7871
lastPositionInFile("fun5", module, false));
7972
assertEquals(1, elements.length);
8073
final IMethod method = (IMethod) elements[0];
@@ -86,7 +79,7 @@ public void testFunctionThisField() throws CoreException {
8679

8780
public void testFunctionThisFieldOuterCall() throws CoreException {
8881
IModuleSource module = getModule("functions.js");
89-
IModelElement[] elements = new SelectionTests(null).select(module,
82+
IModelElement[] elements = select(module,
9083
lastPositionInFile("fun6", module, false));
9184
assertEquals(1, elements.length);
9285
final IMethod method = (IMethod) elements[0];
@@ -98,22 +91,22 @@ public void testFunctionThisFieldOuterCall() throws CoreException {
9891

9992
public void testFunctionThisFieldWithInnerFunction() throws CoreException {
10093
IModuleSource module = getModule("functions.js");
101-
IModelElement[] elements = new SelectionTests(null).select(module,
94+
IModelElement[] elements = select(module,
10295
lastPositionInFile("fun8", module, false));
10396
assertEquals(0, elements.length);
10497
}
10598

10699
public void testFunctionThisFieldWithInnerFunctionCall()
107100
throws CoreException {
108101
IModuleSource module = getModule("functions.js");
109-
IModelElement[] elements = new SelectionTests(null).select(module,
102+
IModelElement[] elements = select(module,
110103
lastPositionInFile("funA", module, false));
111104
assertEquals(0, elements.length);
112105
}
113106

114107
public void testGlobalInitializerFunctionField() throws CoreException {
115108
IModuleSource module = getModule("functions.js");
116-
IModelElement[] elements = new SelectionTests(null).select(module,
109+
IModelElement[] elements = select(module,
117110
lastPositionInFile("funB", module, false));
118111
assertEquals(1, elements.length);
119112
final IMethod method = (IMethod) elements[0];
@@ -125,7 +118,7 @@ public void testGlobalInitializerFunctionField() throws CoreException {
125118

126119
public void testLocalInitializerFunctionField() throws CoreException {
127120
IModuleSource module = getModule("functions.js");
128-
IModelElement[] elements = new SelectionTests(null).select(module,
121+
IModelElement[] elements = select(module,
129122
lastPositionInFile("funC", module, false));
130123
assertEquals(1, elements.length);
131124
final IMethod method = (IMethod) elements[0];

tests/org.eclipse.dltk.javascript.core.tests/src/org/eclipse/dltk/javascript/core/tests/search/SearchLocalsTests.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@
1818
import org.eclipse.dltk.compiler.env.IModuleSource;
1919
import org.eclipse.dltk.core.ILocalVariable;
2020
import org.eclipse.dltk.core.IModelElement;
21-
import org.eclipse.dltk.core.ModelException;
2221
import org.eclipse.dltk.core.search.LocalVariableDeclarationMatch;
2322
import org.eclipse.dltk.core.search.LocalVariableReferenceMatch;
24-
import org.eclipse.dltk.core.tests.model.AbstractSingleProjectSearchTests;
2523
import org.eclipse.dltk.core.tests.model.TestSearchResults;
26-
import org.eclipse.dltk.javascript.core.tests.contentassist.SelectionTests;
2724

28-
public class SearchLocalsTests extends AbstractSingleProjectSearchTests {
25+
public class SearchLocalsTests extends AbstractSearchTest {
2926

3027
public SearchLocalsTests(String testName) {
3128
super(PLUGIN_ID, testName, "selection");
@@ -35,17 +32,13 @@ public static Suite suite() {
3532
return new Suite(SearchLocalsTests.class);
3633
}
3734

38-
private IModuleSource getModule(String path) throws ModelException {
39-
return (IModuleSource) getSourceModule(getProjectName(), "src", path);
40-
}
41-
4235
public void testLocalVariableReference() throws CoreException {
4336
IModuleSource module = getModule("locals.js");
44-
IModelElement[] elements = new SelectionTests(null).select(module,
37+
IModelElement[] elements = select(module,
4538
lastPositionInFile("beta", module, false));
4639
assertEquals(1, elements.length);
47-
final ILocalVariable method = (ILocalVariable) elements[0];
48-
final TestSearchResults results = search(method, ALL_OCCURRENCES);
40+
final ILocalVariable var = (ILocalVariable) elements[0];
41+
final TestSearchResults results = search(var, ALL_OCCURRENCES);
4942
assertEquals(3, results.size());
5043
assertTrue(results.getMatch(0) instanceof LocalVariableDeclarationMatch);
5144
assertTrue(results.getMatch(1) instanceof LocalVariableReferenceMatch);
@@ -54,14 +47,14 @@ public void testLocalVariableReference() throws CoreException {
5447

5548
public void testLocalVariableAsArgumentReference() throws CoreException {
5649
IModuleSource module = getModule("locals.js");
57-
IModelElement[] elements = new SelectionTests(null).select(module,
50+
IModelElement[] elements = select(module,
5851
lastPositionInFile("alpha", module, false));
5952
assertEquals(1, elements.length);
60-
final ILocalVariable method = (ILocalVariable) elements[0];
61-
final TestSearchResults results = search(method, ALL_OCCURRENCES);
53+
final ILocalVariable var = (ILocalVariable) elements[0];
54+
final TestSearchResults results = search(var, ALL_OCCURRENCES);
6255
assertEquals(2, results.size());
6356
assertTrue(results.getMatch(0) instanceof LocalVariableDeclarationMatch);
6457
assertTrue(results.getMatch(1) instanceof LocalVariableReferenceMatch);
6558
}
66-
59+
6760
}

0 commit comments

Comments
 (0)