Skip to content

Commit

Permalink
#10 more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Nov 19, 2024
1 parent edad496 commit 22c53c0
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 44 deletions.
7 changes: 7 additions & 0 deletions src/main/java/org/eolang/lints/Defect.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 2 additions & 26 deletions src/main/java/org/eolang/lints/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -46,7 +43,7 @@ public final class Program {
/**
* Lints to use.
*/
private static final Iterable<Lint> LINTS = Program.all();
private static final Iterable<Lint> LINTS = new Sticky<>(new XslLints());

/**
* The XMIR program to analyze.
Expand Down Expand Up @@ -82,25 +79,4 @@ public Collection<Defect> defects() throws IOException {
return messages;
}

/**
* All lints.
* @return List of all lints
*/
private static Iterable<Lint> 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);
}
}

}
68 changes: 68 additions & 0 deletions src/main/java/org/eolang/lints/XslLints.java
Original file line number Diff line number Diff line change
@@ -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<Lint> {

/**
* Ctor.
*/
public XslLints() {
super(XslLints.all());
}

/**
* All lints.
* @return List of all lints
*/
private static Iterable<Lint> 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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ SOFTWARE.
<xsl:text>" </xsl:text>
</xsl:if>
<xsl:text>has attribute without a name</xsl:text>
<xsl:text>, line=</xsl:text>
<xsl:value-of select="@line"/>
<xsl:text>, pos=</xsl:text>
<xsl:value-of select="@pos"/>
</xsl:element>
</xsl:for-each>
</xsl:template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:eo="https://www.eolang.org" id="duplicate-aliases" version="2.0">
<xsl:import href="/org/eolang/parser/_funcs.xsl"/>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" id="duplicate-aliases" version="2.0">
<xsl:output encoding="UTF-8" method="xml"/>
<xsl:template match="/">
<defects>
<xsl:for-each select="/program/metas/meta[head='alias']">
<xsl:variable name="name" select="eo:alias-name(.)"/>
<xsl:if test="preceding-sibling::meta[head='alias' and eo:alias-name(.)=$name]">
<xsl:variable name="name" select="tokenize(tail, ' ')[1]"/>
<xsl:if test="preceding-sibling::meta[head='alias' and tokenize(tail, ' ')[1]=$name]">
<xsl:element name="defect">
<xsl:attribute name="check">
<xsl:text>duplicate-aliases</xsl:text>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:eo="https://www.eolang.org" id="unused-aliases" version="2.0">
<xsl:import href="/org/eolang/parser/_funcs.xsl"/>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" id="unused-alias" version="2.0">
<xsl:output encoding="UTF-8" method="xml"/>
<xsl:template match="/">
<defects>
<xsl:for-each select="/program/metas/meta[head='alias']">
<xsl:variable name="name" select="eo:alias-qualified(.)"/>
<xsl:for-each select="/program/metas/meta[head='alias' and count(part)=2]">
<xsl:variable name="name" select="tokenize(tail, ' ')[2]"/>
<xsl:if test="count(//o[@base=$name]) = 0">
<xsl:element name="defect">
<xsl:attribute name="check">
Expand All @@ -42,7 +41,9 @@ SOFTWARE.
</xsl:attribute>
<xsl:text>The alias "</xsl:text>
<xsl:value-of select="$name"/>
<xsl:text>" is not used</xsl:text>
<xsl:text>" is not used, but defined as "+alias </xsl:text>
<xsl:value-of select="tail"/>
<xsl:text>"</xsl:text>
</xsl:element>
</xsl:if>
</xsl:for-each>
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/org/eolang/lints/ProgramTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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\""
)
)
)
);
}

}
52 changes: 52 additions & 0 deletions src/test/java/org/eolang/lints/XslLintsTest.java
Original file line number Diff line number Diff line change
@@ -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("<program/>");
for (final Lint lint : new XslLints()) {
MatcherAssert.assertThat(
"passes with no exceptions",
lint.defects(xmir),
Matchers.notNullValue()
);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down

0 comments on commit 22c53c0

Please sign in to comment.