From 22c53c032fd6be4a9b81d65fa48f9d413e80c1e4 Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Tue, 19 Nov 2024 11:15:52 +0300 Subject: [PATCH] #10 more tests --- src/main/java/org/eolang/lints/Defect.java | 7 ++ src/main/java/org/eolang/lints/Program.java | 28 +------- src/main/java/org/eolang/lints/XslLints.java | 68 +++++++++++++++++++ .../eolang/lints/errors/noname-attributes.xsl | 4 -- .../eolang/lints/metas/duplicate-aliases.xsl | 7 +- .../unused-alias.xsl} | 11 +-- .../java/org/eolang/lints/ProgramTest.java | 44 ++++++++++++ .../java/org/eolang/lints/XslLintsTest.java | 52 ++++++++++++++ .../lints/errors/catches-noname-attrs.yaml | 8 +-- .../catches-unused-aliases.yaml | 2 +- 10 files changed, 187 insertions(+), 44 deletions(-) create mode 100644 src/main/java/org/eolang/lints/XslLints.java rename src/main/resources/org/eolang/lints/{errors/unused-aliases.xsl => metas/unused-alias.xsl} (85%) create mode 100644 src/test/java/org/eolang/lints/XslLintsTest.java rename src/test/resources/org/eolang/lints/{errors => metas}/catches-unused-aliases.yaml (97%) diff --git a/src/main/java/org/eolang/lints/Defect.java b/src/main/java/org/eolang/lints/Defect.java index c3d6e64..110744a 100644 --- a/src/main/java/org/eolang/lints/Defect.java +++ b/src/main/java/org/eolang/lints/Defect.java @@ -96,6 +96,13 @@ final class Default implements Defect { this.txt = text; } + @Override + public String toString() { + return String.format( + "[%s %s]:%d %s", this.rle, this.sev, this.lineno, this.txt + ); + } + @Override public String rule() { return this.rle; diff --git a/src/main/java/org/eolang/lints/Program.java b/src/main/java/org/eolang/lints/Program.java index 7c12e99..d4cf3ce 100644 --- a/src/main/java/org/eolang/lints/Program.java +++ b/src/main/java/org/eolang/lints/Program.java @@ -25,15 +25,12 @@ import com.jcabi.xml.XML; import com.jcabi.xml.XMLDocument; -import io.github.secretx33.resourceresolver.PathMatchingResourcePatternResolver; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Path; -import java.util.Arrays; import java.util.Collection; import java.util.LinkedList; -import org.cactoos.io.InputOf; -import org.cactoos.iterable.Mapped; +import org.cactoos.iterable.Sticky; /** * A single XMIR program to analyze. @@ -46,7 +43,7 @@ public final class Program { /** * Lints to use. */ - private static final Iterable LINTS = Program.all(); + private static final Iterable LINTS = new Sticky<>(new XslLints()); /** * The XMIR program to analyze. @@ -82,25 +79,4 @@ public Collection defects() throws IOException { return messages; } - /** - * All lints. - * @return List of all lints - */ - private static Iterable all() { - try { - return new Mapped<>( - res -> new LintByXsl( - new InputOf(res.getInputStream()) - ), - Arrays.asList( - new PathMatchingResourcePatternResolver().getResources( - "classpath*:org/eolang/lints/**/*.xsl" - ) - ) - ); - } catch (final IOException ex) { - throw new IllegalArgumentException(ex); - } - } - } diff --git a/src/main/java/org/eolang/lints/XslLints.java b/src/main/java/org/eolang/lints/XslLints.java new file mode 100644 index 0000000..28e2d04 --- /dev/null +++ b/src/main/java/org/eolang/lints/XslLints.java @@ -0,0 +1,68 @@ +/* + * 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. + */ +package org.eolang.lints; + +import io.github.secretx33.resourceresolver.PathMatchingResourcePatternResolver; +import java.io.IOException; +import java.util.Arrays; +import org.cactoos.io.InputOf; +import org.cactoos.iterable.IterableEnvelope; +import org.cactoos.iterable.Mapped; + +/** + * All lints defined by XSLs. + * + * @since 0.1.0 + */ +public final class XslLints extends IterableEnvelope { + + /** + * Ctor. + */ + public XslLints() { + super(XslLints.all()); + } + + /** + * All lints. + * @return List of all lints + */ + private static Iterable all() { + try { + return new Mapped<>( + res -> new LintByXsl( + new InputOf(res.getInputStream()) + ), + Arrays.asList( + new PathMatchingResourcePatternResolver().getResources( + "classpath*:org/eolang/lints/**/*.xsl" + ) + ) + ); + } catch (final IOException ex) { + throw new IllegalArgumentException(ex); + } + } + +} diff --git a/src/main/resources/org/eolang/lints/errors/noname-attributes.xsl b/src/main/resources/org/eolang/lints/errors/noname-attributes.xsl index c0fc5fb..33d6929 100644 --- a/src/main/resources/org/eolang/lints/errors/noname-attributes.xsl +++ b/src/main/resources/org/eolang/lints/errors/noname-attributes.xsl @@ -51,10 +51,6 @@ SOFTWARE. " has attribute without a name - , line= - - , pos= - diff --git a/src/main/resources/org/eolang/lints/metas/duplicate-aliases.xsl b/src/main/resources/org/eolang/lints/metas/duplicate-aliases.xsl index 0800425..a56cd2c 100644 --- a/src/main/resources/org/eolang/lints/metas/duplicate-aliases.xsl +++ b/src/main/resources/org/eolang/lints/metas/duplicate-aliases.xsl @@ -22,14 +22,13 @@ 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. --> - - + - - + + duplicate-aliases diff --git a/src/main/resources/org/eolang/lints/errors/unused-aliases.xsl b/src/main/resources/org/eolang/lints/metas/unused-alias.xsl similarity index 85% rename from src/main/resources/org/eolang/lints/errors/unused-aliases.xsl rename to src/main/resources/org/eolang/lints/metas/unused-alias.xsl index 2c186fd..58f9ef1 100644 --- a/src/main/resources/org/eolang/lints/errors/unused-aliases.xsl +++ b/src/main/resources/org/eolang/lints/metas/unused-alias.xsl @@ -22,13 +22,12 @@ 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. --> - - + - - + + @@ -42,7 +41,9 @@ SOFTWARE. The alias " - " is not used + " is not used, but defined as "+alias + + " diff --git a/src/test/java/org/eolang/lints/ProgramTest.java b/src/test/java/org/eolang/lints/ProgramTest.java index 6e42375..e47b826 100644 --- a/src/test/java/org/eolang/lints/ProgramTest.java +++ b/src/test/java/org/eolang/lints/ProgramTest.java @@ -60,4 +60,48 @@ void simpleTest(@Mktmp final Path dir) throws IOException { ); } + @Test + void largerBrokenProgramTest() throws IOException { + MatcherAssert.assertThat( + "checking passes", + new Program( + new EoSyntax( + new InputOf( + String.join( + "\n", + "# This is the license", + "", + "+version 8.8.8-beta", + "+alias org.eolang.txt.sprintf", + "+alias org . eolang . txt . broken", + "+version 1.1-another maybe be wrong", + "+package Z.Y.Z", + "+home some-wrong-URL", + "+architect broken-email-here", + "", + "# комментарий здесь", + "[] > foo-bar", + " (bar 42) > zzz", + " 44 > zzz", + "", + "[] > foo-bar", + "", + "[] > another", + "", + "42 > forty-two" + ) + ) + ).parsed() + ).defects(), + Matchers.allOf( + Matchers.iterableWithSize(Matchers.greaterThan(0)), + Matchers.hasItem( + Matchers.hasToString( + "[broken-aliases ERROR]:5 The alias is invalid: \"org . eolang . txt . broken\"" + ) + ) + ) + ); + } + } diff --git a/src/test/java/org/eolang/lints/XslLintsTest.java b/src/test/java/org/eolang/lints/XslLintsTest.java new file mode 100644 index 0000000..0ad3237 --- /dev/null +++ b/src/test/java/org/eolang/lints/XslLintsTest.java @@ -0,0 +1,52 @@ +/* + * 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. + */ +package org.eolang.lints; + +import com.jcabi.xml.XML; +import com.jcabi.xml.XMLDocument; +import java.io.IOException; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; + +/** + * Test for {@link XslLints}. + * + * @since 0.0.1 + */ +final class XslLintsTest { + + @Test + void passesOnSimpleXmir() throws IOException { + final XML xmir = new XMLDocument(""); + for (final Lint lint : new XslLints()) { + MatcherAssert.assertThat( + "passes with no exceptions", + lint.defects(xmir), + Matchers.notNullValue() + ); + } + } + +} diff --git a/src/test/resources/org/eolang/lints/errors/catches-noname-attrs.yaml b/src/test/resources/org/eolang/lints/errors/catches-noname-attrs.yaml index 41de625..644afd1 100644 --- a/src/test/resources/org/eolang/lints/errors/catches-noname-attrs.yaml +++ b/src/test/resources/org/eolang/lints/errors/catches-noname-attrs.yaml @@ -25,10 +25,10 @@ xsls: - /org/eolang/lints/errors/noname-attributes.xsl tests: - /defects[count(defect[@severity='error'])=4] - - /defects/defect[@line='3' and contains(text(),'pos=2')] - - /defects/defect[@line='4' and contains(text(),'pos=2')] - - /defects/defect[@line='11' and contains(text(),'pos=19')] - - /defects/defect[@line='17' and contains(text(),'pos=2')] + - /defects/defect[@line='3'] + - /defects/defect[@line='4'] + - /defects/defect[@line='11'] + - /defects/defect[@line='17'] eo: | # This is the default 64+ symbols comment in front of abstract object. [] > abs diff --git a/src/test/resources/org/eolang/lints/errors/catches-unused-aliases.yaml b/src/test/resources/org/eolang/lints/metas/catches-unused-aliases.yaml similarity index 97% rename from src/test/resources/org/eolang/lints/errors/catches-unused-aliases.yaml rename to src/test/resources/org/eolang/lints/metas/catches-unused-aliases.yaml index 7b52f04..bd84a0c 100644 --- a/src/test/resources/org/eolang/lints/errors/catches-unused-aliases.yaml +++ b/src/test/resources/org/eolang/lints/metas/catches-unused-aliases.yaml @@ -23,7 +23,7 @@ xsls: - /org/eolang/parser/expand-aliases.xsl - /org/eolang/parser/resolve-aliases.xsl - - /org/eolang/lints/errors/unused-aliases.xsl + - /org/eolang/lints/metas/unused-alias.xsl tests: - /defects[count(defect[@severity='error'])=2] - /defects/defect[@line='1']