From 4eb287a46b38319b4df30fef6f7fb2215806de85 Mon Sep 17 00:00:00 2001 From: h1alexbel Date: Mon, 13 Jan 2025 19:43:47 +0300 Subject: [PATCH 1/3] bug(#227): allowsLongerAlias --- .../lints/critical/LtIncorrectAlias.java | 7 ++++- .../lints/critical/LtIncorrectAliasTest.java | 23 +++++++++++++++ .../critical/incorrect-alias/longer-alias.eo | 28 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/org/eolang/lints/critical/incorrect-alias/longer-alias.eo diff --git a/src/main/java/org/eolang/lints/critical/LtIncorrectAlias.java b/src/main/java/org/eolang/lints/critical/LtIncorrectAlias.java index 18f2c8a20..d0297c8a4 100644 --- a/src/main/java/org/eolang/lints/critical/LtIncorrectAlias.java +++ b/src/main/java/org/eolang/lints/critical/LtIncorrectAlias.java @@ -53,7 +53,12 @@ public Collection defects(final Map pkg) { if (xmir.nodes("/program/metas/meta[head='package']").size() != 1) { continue; } - final String pointer = alias.xpath("text()").get(0); + final String pointer; + if (Boolean.parseBoolean(alias.xpath("contains(text(), ' ')").get(0))) { + pointer = alias.xpath("substring-before(text(), ' ')").get(0); + } else { + pointer = alias.xpath("text()").get(0); + } final String lookup = String.format( "%s/%s", xmir.xpath("/program/metas/meta[head='package']/tail/text()").get(0), diff --git a/src/test/java/org/eolang/lints/critical/LtIncorrectAliasTest.java b/src/test/java/org/eolang/lints/critical/LtIncorrectAliasTest.java index 92f97fcdc..4a0f46705 100644 --- a/src/test/java/org/eolang/lints/critical/LtIncorrectAliasTest.java +++ b/src/test/java/org/eolang/lints/critical/LtIncorrectAliasTest.java @@ -119,6 +119,29 @@ void ignoresProgram(final String name) throws IOException { ); } + @Test + void allowsLongerAlias() throws IOException { + MatcherAssert.assertThat( + "Defects aren't empty, but they should", + new LtIncorrectAlias().defects( + new MapOf( + new MapEntry<>( + "longer-alias", + new EoSyntax( + new ResourceOf( + "org/eolang/lints/critical/incorrect-alias/longer-alias.eo" + ) + ).parsed() + ), + new MapEntry<>( + "foo/stdout", new XMLDocument("") + ) + ) + ), + Matchers.emptyIterable() + ); + } + @Test @ExtendWith(MktmpResolver.class) void acceptsValidDirectory(@Mktmp final Path dir) throws IOException { diff --git a/src/test/resources/org/eolang/lints/critical/incorrect-alias/longer-alias.eo b/src/test/resources/org/eolang/lints/critical/incorrect-alias/longer-alias.eo new file mode 100644 index 000000000..60083a381 --- /dev/null +++ b/src/test/resources/org/eolang/lints/critical/incorrect-alias/longer-alias.eo @@ -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!" From 05c548eaa7d5809620820284364d5b66d850943c Mon Sep 17 00:00:00 2001 From: h1alexbel Date: Mon, 13 Jan 2025 20:20:43 +0300 Subject: [PATCH 2/3] bug(#227): scans second --- .../lints/critical/LtIncorrectAlias.java | 26 ++++++++++++------- .../lints/critical/LtIncorrectAliasTest.java | 4 +-- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/eolang/lints/critical/LtIncorrectAlias.java b/src/main/java/org/eolang/lints/critical/LtIncorrectAlias.java index d0297c8a4..7d3993f94 100644 --- a/src/main/java/org/eolang/lints/critical/LtIncorrectAlias.java +++ b/src/main/java/org/eolang/lints/critical/LtIncorrectAlias.java @@ -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; @@ -39,6 +40,11 @@ */ public final class LtIncorrectAlias implements Lint> { + /** + * Pattern contains dot. + */ + private static final Pattern DOT_PATTERN = Pattern.compile("\\."); + @Override public String name() { return "incorrect-alias"; @@ -53,17 +59,19 @@ public Collection defects(final Map pkg) { if (xmir.nodes("/program/metas/meta[head='package']").size() != 1) { continue; } - final String pointer; + final String pointer = alias.xpath("text()").get(0); + final String lookup; if (Boolean.parseBoolean(alias.xpath("contains(text(), ' ')").get(0))) { - pointer = alias.xpath("substring-before(text(), ' ')").get(0); + lookup = LtIncorrectAlias.DOT_PATTERN.matcher( + alias.xpath("substring-after(text(), ' ')").get(0) + ).replaceAll("/"); } else { - pointer = alias.xpath("text()").get(0); + lookup = String.format( + "%s/%s", + xmir.xpath("/program/metas/meta[head='package']/tail/text()").get(0), + pointer + ); } - final String 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( @@ -74,7 +82,7 @@ public Collection defects(final Map 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 ) diff --git a/src/test/java/org/eolang/lints/critical/LtIncorrectAliasTest.java b/src/test/java/org/eolang/lints/critical/LtIncorrectAliasTest.java index 4a0f46705..b24317d4d 100644 --- a/src/test/java/org/eolang/lints/critical/LtIncorrectAliasTest.java +++ b/src/test/java/org/eolang/lints/critical/LtIncorrectAliasTest.java @@ -120,7 +120,7 @@ void ignoresProgram(final String name) throws IOException { } @Test - void allowsLongerAlias() throws IOException { + void scansSecondPartInLongerAlias() throws IOException { MatcherAssert.assertThat( "Defects aren't empty, but they should", new LtIncorrectAlias().defects( @@ -134,7 +134,7 @@ void allowsLongerAlias() throws IOException { ).parsed() ), new MapEntry<>( - "foo/stdout", new XMLDocument("") + "org/eolang/io/stdout", new XMLDocument("") ) ) ), From c4427042b2e2841f519313fa294843f2ccb92e86 Mon Sep 17 00:00:00 2001 From: h1alexbel Date: Mon, 13 Jan 2025 20:37:43 +0300 Subject: [PATCH 3/3] bug(#227): acceptsValidDirectoryWithLongerAlias --- .../lints/critical/LtIncorrectAliasTest.java | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/eolang/lints/critical/LtIncorrectAliasTest.java b/src/test/java/org/eolang/lints/critical/LtIncorrectAliasTest.java index b24317d4d..7818ca1f7 100644 --- a/src/test/java/org/eolang/lints/critical/LtIncorrectAliasTest.java +++ b/src/test/java/org/eolang/lints/critical/LtIncorrectAliasTest.java @@ -134,7 +134,7 @@ void scansSecondPartInLongerAlias() throws IOException { ).parsed() ), new MapEntry<>( - "org/eolang/io/stdout", new XMLDocument("") + "org/eolang/io/stdout", new XMLDocument("") ) ) ), @@ -163,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"), "".getBytes()); + Files.createDirectory( + Files.createDirectory( + Files.createDirectory( + dir.resolve("org") + ).resolve("eolang") + ).resolve("io") + ); + Files.write( + dir.resolve("org/eolang/io/stdout.xmir"), "".getBytes() + ); + Files.write( + dir.resolve("org/eolang/io/stdout-test.xmir"), + "".getBytes() + ); + MatcherAssert.assertThat( + "Defects are not empty, but should be", + new Programs(dir).defects(), + Matchers.emptyIterable() + ); + } }