Skip to content

Commit

Permalink
Fix for #1011: rework import/package recovery and reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Dec 15, 2020
1 parent 29f00a5 commit 06da448
Show file tree
Hide file tree
Showing 10 changed files with 29,033 additions and 29,155 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2019 the original author or authors.
* Copyright 2009-2020 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 @@ -17,15 +17,10 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.List;
import java.util.Map;

import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.ImportNode;
import org.codehaus.groovy.ast.ModuleNode;
import org.codehaus.jdt.groovy.internal.compiler.ast.AliasImportReference;
import org.eclipse.jdt.internal.compiler.ast.ImportReference;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
Expand Down Expand Up @@ -1371,348 +1366,4 @@ public void testAmbiguous_GRE945_ji() {

runConformTest(sources, "abc");
}

@Test
public void testParsingBlankPackage() {
//@formatter:off
String[] sources = {
"Foo.groovy",
"package \n" +
"class Name { }\n",
};
//@formatter:on

runNegativeTest(sources, "----------\n" +
"1. ERROR in Foo.groovy (at line 1)\n" +
"\tpackage \n" +
"\t^\n" +
"Groovy:Invalid package specification\n" +
"----------\n");
}

@Test
public void testParsingBlankPackage2() {
//@formatter:off
String[] sources = {
"Foo.groovy",
"package ;\n" +
"class Name { }\n",
};
//@formatter:on

runNegativeTest(sources, "----------\n" +
"1. ERROR in Foo.groovy (at line 1)\n" +
"\tpackage ;\n" +
"\t^\n" +
"Groovy:Invalid package specification\n" +
"----------\n");
}

@Test // does the second error now get reported after the package problem
public void testParsingBlankPackage3() {
//@formatter:off
String[] sources = {
"Foo.groovy",
"package ;\n" +
"class Name { \n" +
" asdf\n" +
"}\n",
};
//@formatter:on

runNegativeTest(sources, "----------\n" +
"1. ERROR in Foo.groovy (at line 1)\n" +
"\tpackage ;\n" +
"\t^\n" +
"Groovy:Invalid package specification\n" +
"----------\n" +
"2. ERROR in Foo.groovy (at line 3)\n" +
"\tasdf\n" +
"\t^\n" +
"Groovy:unexpected token: asdf\n" +
"----------\n");
}

@Test
public void testParsingBlankImport_538() {
//@formatter:off
String[] sources = {
"A.groovy",
"import ",
};
//@formatter:on

runNegativeTest(sources, "----------\n" +
"1. ERROR in A.groovy (at line 1)\n" +
"\timport \n" +
"\t^^^^^^^\n" +
"Groovy:unable to resolve class ?\n" +
"----------\n");

ModuleNode mn = getModuleNode("A.groovy");
assertNotNull(mn);
assertFalse(mn.encounteredUnrecoverableError());

List<ImportNode> imports = mn.getImports();
ImportNode recoveredImport = imports.get(0);
assertEquals(0, recoveredImport.getStart());
assertEquals(7, recoveredImport.getEnd());
assertEquals("?", recoveredImport.getType().getName());

ClassNode cn = mn.getClasses().get(0);
assertNotNull(cn);
assertTrue(cn.getName().equals("A"));
}

@Test
public void testParsingBlankImport2_538() {
//@formatter:off
String[] sources = {
"A.groovy",
"import ;",
};
//@formatter:on

runNegativeTest(sources, "----------\n" +
"1. ERROR in A.groovy (at line 1)\n" +
"\timport ;\n" +
"\t^^^^^^^\n" +
"Groovy:unable to resolve class ?\n" +
"----------\n");

ModuleNode mn = getModuleNode("A.groovy");
assertNotNull(mn);
assertFalse(mn.encounteredUnrecoverableError());

List<ImportNode> imports = mn.getImports();
ImportNode recoveredImport = imports.get(0);
assertEquals(0, recoveredImport.getStart());
assertEquals(7, recoveredImport.getEnd());
assertEquals("?", recoveredImport.getType().getName());

ClassNode cn = mn.getClasses().get(0);
assertNotNull(cn);
assertTrue(cn.getName().equals("A"));
}

@Test
public void testParsingDotTerminatedImport_538() {
//@formatter:off
String[] sources = {
"A.groovy",
"import foo.",
};
//@formatter:on

runNegativeTest(sources, "----------\n" +
"1. ERROR in A.groovy (at line 1)\n" +
"\timport foo.\n" +
"\t ^\n" +
"Groovy:Invalid import\n" +
"----------\n");

ModuleNode mn = getModuleNode("A.groovy");
assertNotNull(mn);
assertFalse(mn.encounteredUnrecoverableError());

List<ImportNode> imports = mn.getStarImports();
ImportNode recoveredImport = imports.get(0);
assertEquals("foo.", recoveredImport.getPackageName());

ClassNode cn = mn.getClasses().get(0);
assertNotNull(cn);
assertTrue(cn.getName().equals("A"));
}

@Test
public void testParsingBlankImportStatic_538() {
//@formatter:off
String[] sources = {
"A.groovy",
"import static \n",
};
//@formatter:on

runNegativeTest(sources, "----------\n" +
"1. ERROR in A.groovy (at line 1)\n" +
"\timport static \n" +
"\t^^^^^^^^^^^^^^\n" +
"Groovy:unable to resolve class ?\n" +
"----------\n");

ModuleNode mn = getModuleNode("A.groovy");
assertNotNull(mn);
assertFalse(mn.encounteredUnrecoverableError());

List<ImportNode> imports = mn.getImports();
ImportNode recoveredImport = imports.get(0);
assertEquals(0, recoveredImport.getStart());
assertEquals(14, recoveredImport.getEnd());
assertEquals("?", recoveredImport.getType().getName());
assertTrue(mn.getStaticImports().isEmpty());

ClassNode cn = mn.getClasses().get(0);
assertNotNull(cn);
assertTrue(cn.getName().equals("A"));
}

@Test
public void testParsingBlankImportStatic2_538() {
//@formatter:off
String[] sources = {
"A.groovy",
"import static ;\n",
};
//@formatter:on

runNegativeTest(sources, "----------\n" +
"1. ERROR in A.groovy (at line 1)\n" +
"\timport static ;\n" +
"\t^^^^^^^^^^^^^^\n" +
"Groovy:unable to resolve class ?\n" +
"----------\n");

ModuleNode mn = getModuleNode("A.groovy");
assertNotNull(mn);
assertFalse(mn.encounteredUnrecoverableError());

List<ImportNode> imports = mn.getImports();
ImportNode recoveredImport = imports.get(0);
assertEquals(0, recoveredImport.getStart());
assertEquals(14, recoveredImport.getEnd());
assertEquals("?", recoveredImport.getType().getName());
assertTrue(mn.getStaticImports().isEmpty());

ClassNode cn = mn.getClasses().get(0);
assertNotNull(cn);
assertTrue(cn.getName().equals("A"));
}

@Test
public void testParsingDotTerminatedImportStatic_538() {
//@formatter:off
String[] sources = {
"A.groovy",
"import static foo.Bar.",
};
//@formatter:on

runNegativeTest(sources,
"----------\n" +
"1. ERROR in A.groovy (at line 1)\n" +
"\timport static foo.Bar.\n" +
"\t ^\n" +
"Groovy:Invalid import\n" +
"----------\n" +
"2. ERROR in A.groovy (at line 1)\n" +
"\timport static foo.Bar.\n" +
"\t ^^^^^^^\n" +
"Groovy:unable to resolve class foo.Bar\n" +
"----------\n");

ModuleNode mn = getModuleNode("A.groovy");
assertNotNull(mn);
assertFalse(mn.encounteredUnrecoverableError());

Map<String, ImportNode> imports = mn.getStaticStarImports();
ImportNode recoveredImport = imports.get("foo.Bar");
assertEquals("foo.Bar", recoveredImport.getType().getName());

ClassNode cn = mn.getClasses().get(0);
assertNotNull(cn);
assertEquals("A", cn.getName());
}

@Test
public void testParsingDotTerminatedImportFollowedByClassDeclaration_538() {
//@formatter:off
String[] sources = {
"A.groovy",
"import foo.\n" +
"\n" +
"class Wibble {}\n",
};
//@formatter:on

runNegativeTest(sources, "----------\n" +
"1. ERROR in A.groovy (at line 1)\n" +
"\timport foo.\n" +
"\t ^\n" +
"Groovy:Invalid import\n" +
"----------\n");

ModuleNode mn = getModuleNode("A.groovy");
assertNotNull(mn);
assertFalse(mn.encounteredUnrecoverableError());

List<ImportNode> imports = mn.getStarImports();
ImportNode recoveredImport = imports.get(0);
assertEquals("foo.", recoveredImport.getPackageName());

ClassNode cn = mn.getClasses().get(0);
assertNotNull(cn);
assertEquals("Wibble", cn.getName());
}

@Test
public void testParsingDotTerminatedImportFollowedByModifierAndClassDeclaration_538() {
//@formatter:off
String[] sources = {
"A.groovy",
"import foo.\n" +
"\n" +
"public class Wibble {}\n",
};
//@formatter:on

runNegativeTest(sources, "----------\n" +
"1. ERROR in A.groovy (at line 1)\n" +
"\timport foo.\n" +
"\t ^\n" +
"Groovy:Invalid import\n" +
"----------\n");

ModuleNode mn = getModuleNode("A.groovy");
assertNotNull(mn);
assertFalse(mn.encounteredUnrecoverableError());

List<ImportNode> imports = mn.getStarImports();
ImportNode recoveredImport = imports.get(0);
assertEquals("foo.", recoveredImport.getPackageName());

ClassNode cn = mn.getClasses().get(0);
assertNotNull(cn);
assertEquals("Wibble", cn.getName());
}

@Test
public void testParsingBlankImportFollowedByClassDeclaration_538() {
//@formatter:off
String[] sources = {
"A.groovy",
"import\n" +
"\n" +
"public class Wibble {}\n",
};
//@formatter:on

runNegativeTest(sources, "----------\n" +
"1. ERROR in A.groovy (at line 1)\n" +
"\timport\n" +
"\t^^^^^^\n" +
"Groovy:unable to resolve class ?\n" +
"----------\n");

ModuleNode mn = getModuleNode("A.groovy");
assertNotNull(mn);
assertFalse(mn.encounteredUnrecoverableError());

List<ImportNode> imports = mn.getImports();
ImportNode recoveredImport = imports.get(0);
assertEquals("?", recoveredImport.getType().getName());

ClassNode cn = mn.getClasses().get(0);
assertNotNull(cn);
assertEquals("Wibble", cn.getName());
}
}
Loading

0 comments on commit 06da448

Please sign in to comment.