mirror of
https://github.com/processing/processing4.git
synced 2026-01-27 10:21:26 +01:00
Added tests for ImportStatement and some minor style clean up.
This commit is contained in:
@@ -1811,7 +1811,7 @@ public class JavaEditor extends Editor {
|
||||
if (!imports.isEmpty()) {
|
||||
ArrayList<String> importHeaders = new ArrayList<>();
|
||||
for (ImportStatement importStatement : imports) {
|
||||
importHeaders.add(importStatement.getFullClassName());
|
||||
importHeaders.add(importStatement.getFullMemberName());
|
||||
}
|
||||
List<AvailableContribution> installLibsHeaders =
|
||||
getNotInstalledAvailableLibs(importHeaders);
|
||||
|
||||
@@ -739,7 +739,7 @@ public class CompletionGenerator {
|
||||
.map(list -> list.stream()
|
||||
.map(is -> {
|
||||
if (is.getMemberName().equals(finalClassName)) {
|
||||
return is.getFullClassName();
|
||||
return is.getFullMemberName();
|
||||
} else if (is.isStarredImport()) {
|
||||
return is.getPackageName() + "." + finalClassName;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
/*
|
||||
Part of the Processing project - http://processing.org
|
||||
Copyright (c) 2012-15 The Processing Foundation
|
||||
Copyright (c) 2012-19 The Processing Foundation
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License version 2
|
||||
@@ -25,9 +25,6 @@ import java.util.regex.Matcher;
|
||||
|
||||
/**
|
||||
* Wrapper for import statements
|
||||
*
|
||||
* @author Manindra Moharana <me@mkmoharana.com>
|
||||
*
|
||||
*/
|
||||
public class ImportStatement {
|
||||
|
||||
@@ -84,9 +81,9 @@ public class ImportStatement {
|
||||
* Prase an import statement from a fully qualified name.
|
||||
*
|
||||
* @param importString The fully qualified name from which an import statement should be built. This supports static
|
||||
* prepended so both "java.util.List" and "static org.processing.package.Factory.build" are supported. Note that
|
||||
* the static prepending is required if the import is static.
|
||||
* @return
|
||||
* prepended so both "java.util.List" and "static org.processing.package.Factory.build" are supported but
|
||||
* "import java.util.List;" is not. Note that the static prepending is required if the import is static.
|
||||
* @return Newly parsed import statement information.
|
||||
*/
|
||||
public static ImportStatement parse(String importString) {
|
||||
Matcher matcher = SourceUtils.IMPORT_REGEX_NO_KEYWORD.matcher(importString);
|
||||
@@ -145,7 +142,7 @@ public class ImportStatement {
|
||||
* "org.processing.package.Factory.build" would be returned for
|
||||
* "import static org.processing.package.Factory.build".
|
||||
*/
|
||||
public String getFullClassName(){
|
||||
public String getFullMemberName(){
|
||||
return packageName + "." + memberName;
|
||||
}
|
||||
|
||||
|
||||
@@ -403,7 +403,7 @@ public class PreprocessingService {
|
||||
new StringWriter(),
|
||||
result.scrubbedPdeCode,
|
||||
codeFolderImports.stream()
|
||||
.map(ImportStatement::getFullClassName)
|
||||
.map(ImportStatement::getFullMemberName)
|
||||
.collect(Collectors.toList())
|
||||
);
|
||||
} catch (SketchException e) {
|
||||
|
||||
147
java/test/processing/mode/java/pdex/ImportStatementTest.java
Normal file
147
java/test/processing/mode/java/pdex/ImportStatementTest.java
Normal file
@@ -0,0 +1,147 @@
|
||||
package processing.mode.java.pdex;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class ImportStatementTest {
|
||||
|
||||
private ImportStatement wholePackage;
|
||||
private ImportStatement singleClass;
|
||||
private ImportStatement staticNoWildcard;
|
||||
private ImportStatement staticWildcard;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
wholePackage = ImportStatement.parse("java.util.*");
|
||||
singleClass = ImportStatement.parse("java.util.List");
|
||||
staticNoWildcard = ImportStatement.parse("static org.processing.test.Test.init");
|
||||
staticWildcard = ImportStatement.parse("static org.processing.test.Test.*");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWholePackageShortcut() {
|
||||
Assert.assertTrue(wholePackage.isSameAs(ImportStatement.wholePackage("java.util")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleClassShortcut() {
|
||||
Assert.assertTrue(singleClass.isSameAs(ImportStatement.singleClass("java.util.List")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFullSourceLine() {
|
||||
Assert.assertEquals(
|
||||
wholePackage.getFullSourceLine(),
|
||||
"import java.util.*;"
|
||||
);
|
||||
|
||||
Assert.assertEquals(
|
||||
singleClass.getFullSourceLine(),
|
||||
"import java.util.List;"
|
||||
);
|
||||
|
||||
Assert.assertEquals(
|
||||
staticNoWildcard.getFullSourceLine(),
|
||||
"import static org.processing.test.Test.init;"
|
||||
);
|
||||
|
||||
Assert.assertEquals(
|
||||
staticWildcard.getFullSourceLine(),
|
||||
"import static org.processing.test.Test.*;"
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFullMemberName() {
|
||||
Assert.assertEquals(
|
||||
wholePackage.getFullMemberName(),
|
||||
"java.util.*"
|
||||
);
|
||||
|
||||
Assert.assertEquals(
|
||||
singleClass.getFullMemberName(),
|
||||
"java.util.List"
|
||||
);
|
||||
|
||||
Assert. assertEquals(
|
||||
staticNoWildcard.getFullMemberName(),
|
||||
"org.processing.test.Test.init"
|
||||
);
|
||||
|
||||
Assert.assertEquals(
|
||||
staticWildcard.getFullMemberName(),
|
||||
"org.processing.test.Test.*"
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetMemberName() {
|
||||
Assert.assertEquals(
|
||||
wholePackage.getMemberName(),
|
||||
"*"
|
||||
);
|
||||
|
||||
Assert.assertEquals(
|
||||
singleClass.getMemberName(),
|
||||
"List"
|
||||
);
|
||||
|
||||
Assert. assertEquals(
|
||||
staticNoWildcard.getMemberName(),
|
||||
"Test.init"
|
||||
);
|
||||
|
||||
Assert.assertEquals(
|
||||
staticWildcard.getMemberName(),
|
||||
"Test.*"
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPackageName() {
|
||||
Assert.assertEquals(
|
||||
wholePackage.getPackageName(),
|
||||
"java.util"
|
||||
);
|
||||
|
||||
Assert.assertEquals(
|
||||
singleClass.getPackageName(),
|
||||
"java.util"
|
||||
);
|
||||
|
||||
Assert. assertEquals(
|
||||
staticNoWildcard.getPackageName(),
|
||||
"org.processing.test"
|
||||
);
|
||||
|
||||
Assert.assertEquals(
|
||||
staticWildcard.getPackageName(),
|
||||
"org.processing.test"
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsStarredImport() {
|
||||
Assert.assertTrue(wholePackage.isStarredImport());
|
||||
Assert.assertFalse(singleClass.isStarredImport());
|
||||
Assert.assertFalse(staticNoWildcard.isStarredImport());
|
||||
Assert.assertTrue(staticWildcard.isStarredImport());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsStaticImport() {
|
||||
Assert.assertFalse(wholePackage.isStaticImport());
|
||||
Assert.assertFalse(singleClass.isStaticImport());
|
||||
Assert.assertTrue(staticNoWildcard.isStaticImport());
|
||||
Assert.assertTrue(staticWildcard.isStaticImport());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsSameAs() {
|
||||
Assert.assertTrue(wholePackage.isSameAs(ImportStatement.parse("java.util.*")));
|
||||
Assert.assertFalse(wholePackage.isSameAs(ImportStatement.parse("java.other.*")));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user