Skip to content

Commit

Permalink
fix: Enable creation of packages with repeated simple names in qualif…
Browse files Browse the repository at this point in the history
…ied name (#4770)
  • Loading branch information
slarse authored Jul 9, 2022
1 parent 603e0a5 commit c0857b1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,7 @@ protected CtRole getRole() {

@Override
public CtPackage put(String simpleName, CtPackage pack) {
if (pack == null) {
return null;
}
// they are the same
if (CtPackageImpl.this.getQualifiedName().equals(pack.getQualifiedName())) {
addAllTypes(pack, CtPackageImpl.this);
addAllPackages(pack, CtPackageImpl.this);
if (pack == null || pack == CtPackageImpl.this) {
return null;
}

Expand Down
31 changes: 31 additions & 0 deletions src/test/java/spoon/reflect/factory/PackageFactoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package spoon.reflect.factory;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.MatcherAssert.assertThat;

import spoon.Launcher;
import spoon.reflect.declaration.CtPackage;
import spoon.test.GitHubIssue;

class PackageFactoryTest {

@GitHubIssue(issueNumber = 4764, fixed = true)
void getOrCreate_returnsNestedPackageStructure_whenQualifiedNameRepeatsSimpleName() {
// contract: A qualified name that is simply repetitions of a single simple name results in the expected
// nested package structure

PackageFactory factory = new Launcher().getFactory().Package();
String topLevelPackageName = "spoon";
String nestedPackageName = topLevelPackageName + "." + topLevelPackageName;

CtPackage packageWithDuplicatedSimpleNames = factory.getOrCreate(nestedPackageName);
CtPackage topLevelPackage = factory.get(topLevelPackageName);

assertThat(topLevelPackage.getQualifiedName(), equalTo(topLevelPackageName));
assertThat(packageWithDuplicatedSimpleNames.getQualifiedName(), equalTo(nestedPackageName));

assertThat(topLevelPackage.getPackage(topLevelPackageName), sameInstance(packageWithDuplicatedSimpleNames));
assertThat(packageWithDuplicatedSimpleNames.getParent(), sameInstance(topLevelPackage));
}
}

0 comments on commit c0857b1

Please sign in to comment.