Skip to content

Commit

Permalink
#66 check for lines
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Nov 29, 2024
1 parent 2021455 commit bd86662
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 13 deletions.
6 changes: 1 addition & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,7 @@ SOFTWARE.
<dependency>
<groupId>org.eolang</groupId>
<artifactId>eo-parser</artifactId>
<version>0.43.2</version>
<!--
EO-parser should be used in compile scope in order to be able provide
XSLs for imports for lint XSLs.
-->
<version>0.44.0</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
53 changes: 53 additions & 0 deletions src/main/resources/org/eolang/lints/refs/line-is-absent.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" id="line-is-absent" version="2.0">
<!--
Here we go through all objects and find what their @base
are referring to. If we find the object they refer to,
everything is OK. If we don't, we report an error.
-->
<xsl:output encoding="UTF-8" method="xml"/>
<xsl:template match="/">
<defects>
<xsl:for-each select="//o[@base and not(starts-with(@base, '.')) and @base!='$' and @base!='^']">
<xsl:variable name="self" select="."/>
<xsl:variable name="target" select="ancestor::*[o[@name=$self/@base and not(@line)]][1]/o[@name=$self/@base and not(@line)]"/>
<xsl:if test="$target">
<xsl:element name="defect">
<xsl:attribute name="line">
<xsl:value-of select="if (@line) then @line else '0'"/>
</xsl:attribute>
<xsl:attribute name="severity">
<xsl:text>error</xsl:text>
</xsl:attribute>
<xsl:text>Attribute @line is absent at "</xsl:text>
<xsl:value-of select="$target/@name"/>
<xsl:text>"</xsl:text>
</xsl:element>
</xsl:if>
</xsl:for-each>
</defects>
</xsl:template>
</xsl:stylesheet>
13 changes: 5 additions & 8 deletions src/test/java/org/eolang/lints/LintByXslTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
*/
package org.eolang.lints;

import com.jcabi.log.Logger;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.yegor256.xsline.Shift;
import com.yegor256.xsline.StClasspath;
import com.yegor256.xsline.TrDefault;
import com.yegor256.xsline.Train;
import com.yegor256.xsline.Xsline;
import java.io.IOException;
Expand All @@ -38,7 +38,6 @@
import org.eolang.jucs.ClasspathSource;
import org.eolang.parser.CheckPack;
import org.eolang.parser.EoSyntax;
import org.eolang.parser.TrParsing;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.core.IsEqual;
Expand Down Expand Up @@ -88,24 +87,22 @@ void testsAllLintsByXmir(final String pack) {
final Yaml yaml = new Yaml();
final Map<String, Object> map = yaml.load(pack);
final Iterable<String> xsls = (Iterable<String>) map.get("xsls");
Train<Shift> train = new TrParsing();
Train<Shift> train = new TrDefault<>();
if (xsls != null) {
for (final String xsl : xsls) {
train = train.with(new StClasspath(xsl));
}
}
final XML out = new Xsline(train).pass(
new XMLDocument(map.get("xmir").toString())
);
Logger.debug(this, "Output XML:\n%s", out);
final XML xmir = new XMLDocument(map.get("xmir").toString());
final XML out = new Xsline(train).pass(xmir);
final Collection<String> failures = new LinkedList<>();
for (final String xpath : (Iterable<String>) map.get("tests")) {
if (out.nodes(xpath).isEmpty()) {
failures.add(xpath);
}
}
MatcherAssert.assertThat(
String.format("Broken XML:\n%s", out),
String.format("Input XML:%n%s%nBroken XML:%n%s", xmir, out),
failures,
Matchers.emptyIterable()
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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.
---
xsls:
- /org/eolang/lints/refs/line-is-absent.xsl
tests:
- /defects[count(defect[@severity='error'])=1]
xmir: |
<program>
<objects>
<o abstract="">
<o name="foo"/>
<o name="bar" base="foo"/>
</o>
</objects>
</program>

0 comments on commit bd86662

Please sign in to comment.