Skip to content

Commit

Permalink
Normalize behavior between the two parsers
Browse files Browse the repository at this point in the history
- handling for some array types
- handling for implements types
- checking for script elements

#412
  • Loading branch information
eric-milles committed Mar 16, 2018
1 parent 0882873 commit b5aac4b
Show file tree
Hide file tree
Showing 13 changed files with 314 additions and 394 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,147 +19,145 @@

public final class LocalVariableReferenceSearchTests extends SearchTestSuite {

private static final String XXX = "xxx";

@Test
public void testvarReference1() throws Exception {
public void testVarReference1() throws Exception {
String contents = "def xxx";
int nameStart = contents.indexOf(XXX);
int nameStart = contents.indexOf("xxx");
doTestForReferencesInScript(contents, createRegions(nameStart));
}

@Test
public void testvarReference2() throws Exception {
public void testVarReference2() throws Exception {
String contents = "def xxx\nxxx";
int nameStart = contents.indexOf(XXX);
int nameStart2 = contents.indexOf(XXX, nameStart+1);
int nameStart = contents.indexOf("xxx");
int nameStart2 = contents.indexOf("xxx", nameStart + 1);
doTestForReferencesInScript(contents, createRegions(nameStart, nameStart2));
}

@Test
public void testvarReference3() throws Exception {
public void testVarReference3() throws Exception {
String contents = "def xxx() { \ndef xxx\n xxx\n xxx() }";
int nameStart = contents.indexOf(XXX, contents.indexOf('('));
int nameStart2 = contents.indexOf(XXX, nameStart+1);
int nameStart3 = contents.indexOf(XXX, nameStart2+1);
int nameStart = contents.indexOf("xxx", contents.indexOf('('));
int nameStart2 = contents.indexOf("xxx", nameStart + 1);
int nameStart3 = contents.indexOf("xxx", nameStart2 + 1);
doTestForReferences(contents, 4, createRegions(nameStart, nameStart2, nameStart3));
}

@Test
public void testvarReference4() throws Exception {
public void testVarReference4() throws Exception {
String contents = "def xxx(xxx) { \n xxx\n xxx() }";
int nameStart = contents.indexOf(XXX, contents.indexOf('('));
int nameStart2 = contents.indexOf(XXX, nameStart+1);
int nameStart3 = contents.indexOf(XXX, nameStart2+1);
int nameStart = contents.indexOf("xxx", contents.indexOf('('));
int nameStart2 = contents.indexOf("xxx", nameStart + 1);
int nameStart3 = contents.indexOf("xxx", nameStart2 + 1);
doTestForReferences(contents, 4, createRegions(nameStart, nameStart2, nameStart3));
}

@Test
public void testvarReference5() throws Exception {
public void testVarReference5() throws Exception {
String contents = "def xxx(int xxx) { \n xxx\n xxx() }";
int nameStart = contents.indexOf(XXX, contents.indexOf('('));
int nameStart2 = contents.indexOf(XXX, nameStart+1);
int nameStart3 = contents.indexOf(XXX, nameStart2+1);
int nameStart = contents.indexOf("xxx", contents.indexOf('('));
int nameStart2 = contents.indexOf("xxx", nameStart + 1);
int nameStart3 = contents.indexOf("xxx", nameStart2 + 1);
doTestForReferences(contents, 4, createRegions(nameStart, nameStart2, nameStart3));
}

@Test
public void testvarReference6() throws Exception {
public void testVarReference6() throws Exception {
String contents = "def xxx = {int xxx -> \n xxx\n xxx() }";
int nameStart = contents.indexOf(XXX, contents.indexOf('{'));
int nameStart2 = contents.indexOf(XXX, nameStart+1);
int nameStart3 = contents.indexOf(XXX, nameStart2+1);
int nameStart = contents.indexOf("xxx", contents.indexOf('{'));
int nameStart2 = contents.indexOf("xxx", nameStart + 1);
int nameStart3 = contents.indexOf("xxx", nameStart2 + 1);
doTestForReferencesInScript(contents, createRegions(nameStart, nameStart2, nameStart3));
}

@Test
public void testvarReference7() throws Exception {
public void testVarReference7() throws Exception {
String contents = "def xxx = {int xxx \n xxx\n xxx() }";
int nameStart = contents.indexOf(XXX, contents.indexOf('{'));
int nameStart2 = contents.indexOf(XXX, nameStart+1);
int nameStart3 = contents.indexOf(XXX, nameStart2+1);
int nameStart = contents.indexOf("xxx", contents.indexOf('{'));
int nameStart2 = contents.indexOf("xxx", nameStart + 1);
int nameStart3 = contents.indexOf("xxx", nameStart2 + 1);
doTestForReferencesInScript(contents, createRegions(nameStart, nameStart2, nameStart3));
}

@Test
public void testvarReference8() throws Exception {
public void testVarReference8() throws Exception {
String contents = "for (xxx in 0..7) \n { xxx }";
int nameStart = contents.indexOf(XXX, contents.indexOf('('));
int nameStart2 = contents.indexOf(XXX, nameStart+1);
int nameStart = contents.indexOf("xxx", contents.indexOf('('));
int nameStart2 = contents.indexOf("xxx", nameStart + 1);
doTestForReferencesInScript(contents, createRegions(nameStart, nameStart2));
}

@Test
public void testvarReference9() throws Exception {
public void testVarReference9() throws Exception {
String contents = "def x1(xxx) { xxx }\ndef x2(xxx) { xxx }";
int nameStart = contents.indexOf(XXX, contents.indexOf('}'));
int nameStart2 = contents.indexOf(XXX, nameStart+1);
int nameStart = contents.indexOf("xxx", contents.indexOf('}'));
int nameStart2 = contents.indexOf("xxx", nameStart + 1);
doTestForReferences(contents, 5, createRegions(nameStart, nameStart2));
}

@Test
public void testvarReference10() throws Exception {
public void testVarReference10() throws Exception {
String contents = "class First {\n def xxx \ndef x2(xxx) { xxx } }";
int nameStart = contents.indexOf(XXX, contents.indexOf('('));
int nameStart2 = contents.indexOf(XXX, nameStart+1);
int nameStart = contents.indexOf("xxx", contents.indexOf('('));
int nameStart2 = contents.indexOf("xxx", nameStart + 1);
doTestForReferences(contents, 1, createRegions(nameStart, nameStart2));
}

@Test
public void testvarReferenceInGString1() throws Exception {
public void testVarReferenceInGString1() throws Exception {
String contents = "def xxx\n\"${xxx}\"";
int nameStart = contents.indexOf(XXX);
int nameStart2 = contents.indexOf(XXX, nameStart+1);
int nameStart = contents.indexOf("xxx");
int nameStart2 = contents.indexOf("xxx", nameStart + 1);
doTestForReferences(contents, 3, createRegions(nameStart, nameStart2));
}

@Test
public void testvarReferenceInGString2() throws Exception {
public void testVarReferenceInGString2() throws Exception {
String contents = "def xxx\n\"${xxx.toString()}\"";
int nameStart = contents.indexOf(XXX);
int nameStart2 = contents.indexOf(XXX, nameStart+1);
int nameStart = contents.indexOf("xxx");
int nameStart2 = contents.indexOf("xxx", nameStart + 1);
doTestForReferences(contents, 3, createRegions(nameStart, nameStart2));
}

@Test
public void testvarReferenceInGString3() throws Exception {
public void testVarReferenceInGString3() throws Exception {
String contents = "def xxx\n\"${blah(xxx)}\"";
int nameStart = contents.indexOf(XXX);
int nameStart2 = contents.indexOf(XXX, nameStart+1);
int nameStart = contents.indexOf("xxx");
int nameStart2 = contents.indexOf("xxx", nameStart + 1);
doTestForReferences(contents, 3, createRegions(nameStart, nameStart2));
}

@Test
public void testvarReferenceInGString4() throws Exception {
public void testVarReferenceInGString4() throws Exception {
String contents = "def xxx\n\"${xxx} \"";
int nameStart = contents.indexOf(XXX);
int nameStart2 = contents.indexOf(XXX, nameStart+1);
int nameStart = contents.indexOf("xxx");
int nameStart2 = contents.indexOf("xxx", nameStart + 1);
doTestForReferences(contents, 3, createRegions(nameStart, nameStart2));
}

@Test
public void testvarReferenceInGString5() throws Exception {
public void testVarReferenceInGString5() throws Exception {
String contents = "def xxx\n\"${xxx }\"";
int nameStart = contents.indexOf(XXX);
int nameStart2 = contents.indexOf(XXX, nameStart+1);
int nameStart = contents.indexOf("xxx");
int nameStart2 = contents.indexOf("xxx", nameStart + 1);
doTestForReferences(contents, 3, createRegions(nameStart, nameStart2));
}

//--------------------------------------------------------------------------

private MatchRegion[] createRegions(int...nameStarts) {
private MatchRegion[] createRegions(int... nameStarts) {
MatchRegion[] regions = new MatchRegion[nameStarts.length];
for (int i = 0; i < nameStarts.length; i++) {
regions[i] = new MatchRegion(nameStarts[i], XXX.length());
for (int i = 0, n = nameStarts.length; i < n; i += 1) {
regions[i] = new MatchRegion(nameStarts[i], "xxx".length());
}
return regions;
}

private void doTestForReferencesInScript(String contents, MatchRegion[] matchLocations) throws Exception {
doTestForVarReferences(contents, 3, XXX, matchLocations[0].offset, matchLocations);
doTestForVarReferences(contents, 3, "xxx", matchLocations[0].offset, matchLocations);
}

private void doTestForReferences(String contents, int locationInParent, MatchRegion[] matchLocations) throws Exception {
doTestForVarReferences(contents, locationInParent, XXX, matchLocations[0].offset, matchLocations);
doTestForVarReferences(contents, locationInParent, "xxx", matchLocations[0].offset, matchLocations);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -1526,7 +1526,7 @@ public void testImplementingInterface_GroovyGenericsIncorrectlyExtendingJavaGene
"----------\n" +
"1. ERROR in p\\C.groovy (at line 2)\n" +
"\tpublic class C implements Iii<String> {\n" +
"\t ^^^^^^^^^^^^\n" +
"\t ^^^\n" +
"Groovy:The type String is not a valid substitute for the bounded parameter <T extends java.lang.Number>\n" +
"----------\n");
}
Expand Down Expand Up @@ -1638,10 +1638,9 @@ public void testHalfFinishedGenericsProgram() {
public void testHalfFinishedGenericsProgramWithCorrectSuppression() {
String[] sources = {
"Demo.groovy",
"public class Demo {\n"+
"\n"+
"@SuppressWarnings(\"rawtypes\")\n"+ // should cause no warnings
"List myList;\n"+
"class Demo {\n" +
" @SuppressWarnings('rawtypes')\n" + // should suppress the warning
" List myList\n" +
"}",
};

Expand All @@ -1652,10 +1651,9 @@ public void testHalfFinishedGenericsProgramWithCorrectSuppression() {
public void testHalfFinishedGenericsProgramWithCorrectSuppressionAtTheTypeLevel() {
String[] sources = {
"Demo.groovy",
"@SuppressWarnings(\"rawtypes\")\n"+ // should cause no warnings
"public class Demo {\n"+
"\n"+
"List myList;\n"+
"@SuppressWarnings('rawtypes')\n" + // should suppress the warning
"class Demo {\n"+
" List myList\n"+
"}",
};

Expand All @@ -1666,17 +1664,16 @@ public void testHalfFinishedGenericsProgramWithCorrectSuppressionAtTheTypeLevel(
public void testHalfFinishedGenericsProgramWithUnnecessarySuppression() {
String[] sources = {
"Demo.groovy",
"public class Demo {\n"+
"\n"+
"@SuppressWarnings(\"unchecked\")\n"+ // unnecessary suppression
"List<String> myList;\n"+
"class Demo {\n" +
" @SuppressWarnings('unchecked')\n" + // unnecessary suppression
" List<String> myList\n" +
"}",
};

runNegativeTest(sources,
"----------\n" +
"1. WARNING in Demo.groovy (at line 3)\n" +
"\t@SuppressWarnings(\"unchecked\")\n" +
"1. WARNING in Demo.groovy (at line 2)\n" +
"\t@SuppressWarnings('unchecked')\n" +
"\t ^^^^^^^^^^^\n" +
"Unnecessary @SuppressWarnings(\"unchecked\")\n" +
"----------\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ public void testRecursion_GR531_4() {
"----------\n" +
"2. ERROR in XXX.groovy (at line 1)\n" +
"\tinterface XXX extends XXX {\n" +
"\t ^^^^\n" +
"\t ^^^\n" +
"Cycle detected: the type XXX cannot extend/implement itself or one of its own member types\n" +
"----------\n");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -894,7 +894,7 @@ public void testTraits40() {
"----------\n" +
"2. ERROR in Sample.groovy (at line 6)\n" +
"\tclass MyClass implements MyTrait {\n" +
"\t ^^^^^^^^\n" +
"\t ^^^^^^^\n" +
"The type MyTrait cannot be a superinterface of MyClass; a superinterface must be an interface\n" +
"----------\n");
}
Expand Down Expand Up @@ -928,7 +928,7 @@ public void testTraits41() {
"----------\n" +
"2. ERROR in Sample.groovy (at line 7)\n" +
"\tclass MyClass implements MyTrait {\n" +
"\t ^^^^^^^^\n" +
"\t ^^^^^^^\n" +
"The type MyTrait cannot be a superinterface of MyClass; a superinterface must be an interface\n" +
"----------\n");
}
Expand Down Expand Up @@ -961,7 +961,7 @@ public void testTraits42() {
"----------\n" +
"2. ERROR in Sample.groovy (at line 6)\n" +
"\tclass MyClass implements MyTrait {\n" +
"\t ^^^^^^^^\n" +
"\t ^^^^^^^\n" +
"The type MyTrait cannot be a superinterface of MyClass; a superinterface must be an interface\n" +
"----------\n");
}
Expand Down Expand Up @@ -996,7 +996,7 @@ public void testTraits43() {
"----------\n" +
"2. ERROR in Sample.groovy (at line 8)\n" +
"\tclass MyClass implements MyTrait {\n" +
"\t ^^^^^^^^\n" +
"\t ^^^^^^^\n" +
"The type MyTrait cannot be a superinterface of MyClass; a superinterface must be an interface\n" +
"----------\n");
}
Expand Down
Loading

0 comments on commit b5aac4b

Please sign in to comment.