Skip to content

Commit

Permalink
feat(#114): check package
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Jan 2, 2025
1 parent 9347faa commit e21ed5e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/main/java/org/eolang/lints/errors/LtObjectIsNotUnique.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ public Collection<Defect> defects(final Map<String, XML> pkg) {
xmir -> {
final String src = xmir.xpath("/program/@name").get(0);
final String name = xmir.xpath("/program/objects/o[1]/@name").get(0);
final String pack = LtObjectIsNotUnique.packageName(xmir);
pkg.values().forEach(
oth -> {
if (!Objects.equals(oth, xmir)) {
final String other = oth.xpath("/program/objects/o[1]/@name").get(0);
if (name.equals(other)) {
if (name.equals(other) && LtObjectIsNotUnique.packageName(oth).equals(pack)) {
defects.add(
new Defect.Default(
this.name(),
Expand All @@ -81,6 +82,16 @@ public Collection<Defect> defects(final Map<String, XML> pkg) {
return defects;
}

private static String packageName(final XML xmir) {
final String name;
if (xmir.nodes("/program/metas/meta[head='package']").size() == 1) {
name = xmir.xpath("/program/metas/meta[head='package']/tail/text()").get(0);
} else {
name = "";
}
return name;
}

@Override
public String motive() throws Exception {
throw new UnsupportedOperationException("#motive()");
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/org/eolang/lints/errors/LtObjectIsNotUniqueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ void allowsAllUnique() throws Exception {
);
}

@Test
void allowsNonUniqueInDifferentPackages() throws Exception {
MatcherAssert.assertThat(
"Defects aren't empty, but they should",
new LtObjectIsNotUnique().defects(
new MapOf<String, XML>(
new MapEntry<>("foo", LtObjectIsNotUniqueTest.xmir("foo")),
new MapEntry<>("bar", LtObjectIsNotUniqueTest.xmir("foo-packaged"))
)
),
Matchers.emptyIterable()
);
}

private static XML xmir(final String name) throws Exception {
return new EoSyntax(
name,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
+package utils

# Foo.
[] > foo

0 comments on commit e21ed5e

Please sign in to comment.