Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/main/java/org/eolang/lints/critical/LtIncorrectAlias.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
import java.util.regex.Pattern;
import org.cactoos.io.ResourceOf;
import org.cactoos.text.TextOf;
import org.eolang.lints.Defect;
Expand All @@ -39,6 +40,11 @@
*/
public final class LtIncorrectAlias implements Lint<Map<String, XML>> {

/**
* Pattern contains dot.
*/
private static final Pattern DOT_PATTERN = Pattern.compile("\\.");

@Override
public String name() {
return "incorrect-alias";
Expand All @@ -54,11 +60,18 @@ public Collection<Defect> defects(final Map<String, XML> pkg) {
continue;
}
final String pointer = alias.xpath("text()").get(0);
final String lookup = String.format(
"%s/%s",
xmir.xpath("/program/metas/meta[head='package']/tail/text()").get(0),
pointer
);
final String lookup;
if (Boolean.parseBoolean(alias.xpath("contains(text(), ' ')").get(0))) {
lookup = LtIncorrectAlias.DOT_PATTERN.matcher(
alias.xpath("substring-after(text(), ' ')").get(0)
).replaceAll("/");
} else {
lookup = String.format(
"%s/%s",
xmir.xpath("/program/metas/meta[head='package']/tail/text()").get(0),
pointer
);
}
if (!pkg.containsKey(lookup)) {
defects.add(
new Defect.Default(
Expand All @@ -69,7 +82,7 @@ public Collection<Defect> defects(final Map<String, XML> pkg) {
xmir.xpath("/program/metas/meta[head='alias'][1]/@line").get(0)
),
String.format(
"Incorrect pointing alias '%s', there is no %s",
"Incorrect pointing alias '%s', since there is no %s",
pointer,
lookup
)
Expand Down
56 changes: 56 additions & 0 deletions src/test/java/org/eolang/lints/critical/LtIncorrectAliasTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,29 @@ void ignoresProgram(final String name) throws IOException {
);
}

@Test
void scansSecondPartInLongerAlias() throws IOException {
MatcherAssert.assertThat(
"Defects aren't empty, but they should",
new LtIncorrectAlias().defects(
new MapOf<String, XML>(
new MapEntry<>(
"longer-alias",
new EoSyntax(
new ResourceOf(
"org/eolang/lints/critical/incorrect-alias/longer-alias.eo"
)
).parsed()
),
new MapEntry<>(
"org/eolang/io/stdout", new XMLDocument("<program><objects/></program>")
)
)
),
Matchers.emptyIterable()
);
}

@Test
@ExtendWith(MktmpResolver.class)
void acceptsValidDirectory(@Mktmp final Path dir) throws IOException {
Expand All @@ -140,4 +163,37 @@ void acceptsValidDirectory(@Mktmp final Path dir) throws IOException {
Matchers.emptyIterable()
);
}

@Test
@ExtendWith(MktmpResolver.class)
void acceptsValidDirectoryWithLongerAlias(@Mktmp final Path dir) throws IOException {
Files.write(
dir.resolve("main.xmir"),
new EoSyntax(
new ResourceOf(
"org/eolang/lints/critical/incorrect-alias/longer-alias.eo"
)
).parsed().toString().getBytes()
);
Files.write(dir.resolve("main-test.xmir"), "<program><objects/></program>".getBytes());
Files.createDirectory(
Files.createDirectory(
Files.createDirectory(
dir.resolve("org")
).resolve("eolang")
).resolve("io")
);
Files.write(
dir.resolve("org/eolang/io/stdout.xmir"), "<program><objects/></program>".getBytes()
);
Files.write(
dir.resolve("org/eolang/io/stdout-test.xmir"),
"<program><objects/></program>".getBytes()
);
MatcherAssert.assertThat(
"Defects are not empty, but should be",
new Programs(dir).defects(),
Matchers.emptyIterable()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# The MIT License (MIT)
#
# Copyright (c) 2016-2024 Objectionary.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

+alias stdout org.eolang.io.stdout
+package foo

[] > main
stdout > @
"hi!"
Loading