Skip to content
Open
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
63 changes: 51 additions & 12 deletions src/test/java/org/eolang/lints/LtByXslTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ void lintsInMultipleThreads() {
@ParameterizedTest
@ClasspathSource(value = "org/eolang/lints/packs/single/", glob = "**.yaml")
void testsAllLintsByEo(final String yaml, final String pack) {
org.junit.jupiter.api.Assumptions.assumeTrue(
!((Map<?, ?>) new org.yaml.snakeyaml.Yaml().load(yaml)).containsKey("lint"),
String.format("Pack '%s' is a Java lint pack", pack)
);
MatcherAssert.assertThat(
String.format(
"Pack '%s' doesn't tell the story as expected",
Expand All @@ -125,6 +129,32 @@ void testsAllLintsByEo(final String yaml, final String pack) {
);
}

@SuppressWarnings("JTCOP.RuleNotContainsTestWord")
@Execution(ExecutionMode.CONCURRENT)
@ParameterizedTest
@ClasspathSource(value = "org/eolang/lints/packs/single/", glob = "**.yaml")
void testsAllLintsByLintName(final String yaml, final String pack) {
org.junit.jupiter.api.Assumptions.assumeTrue(
((Map<?, ?>) new org.yaml.snakeyaml.Yaml().load(yaml)).containsKey("lint"),
String.format("Pack '%s' has no lint key", pack)
);
MatcherAssert.assertThat(
String.format(
"Pack '%s' doesn't tell the story as expected",
pack
),
new fixtures.XtDefects(
new XtSticky(
new XtLint(
yaml,
eo -> new EoProgram(pack, new InputOf(eo)).parse()
)
)
),
new XtoryMatcher(new DefectsMatcher())
);
}

@Test
void returnsMotive() throws Exception {
MatcherAssert.assertThat(
Expand Down Expand Up @@ -480,22 +510,31 @@ private static Predicate<Path> yamls() {
*/
@SuppressWarnings("StreamResourceLeak")
private static boolean hasMatchingXsl(final Path yaml) {
final boolean result;
try {
return Files.walk(Paths.get("src/main/resources/org/eolang/lints"))
.filter(Files::isRegularFile)
.filter(path -> path.toString().endsWith(".xsl"))
.map(path -> path.getParent().getFileName().toString()).anyMatch(
group -> Files.exists(
Paths.get("src/main/resources/org/eolang/lints/").resolve(group).resolve(
String.format(
"%s.xsl",
yaml.getParent().getFileName().toString()
)
if (((Map<?, ?>) new org.yaml.snakeyaml.Yaml().load(
new String(Files.readAllBytes(yaml), java.nio.charset.StandardCharsets.UTF_8)
)).containsKey("lint")) {
result = true;
} else {
result = Files.walk(Paths.get("src/main/resources/org/eolang/lints"))
.filter(Files::isRegularFile)
.filter(path -> path.toString().endsWith(".xsl"))
.map(path -> path.getParent().getFileName().toString()).anyMatch(
group -> Files.exists(
Paths.get("src/main/resources/org/eolang/lints/")
.resolve(group).resolve(
String.format(
"%s.xsl",
yaml.getParent().getFileName().toString()
)
)
)
)
);
);
}
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
return result;
}
}
123 changes: 123 additions & 0 deletions src/test/java/org/eolang/lints/XtLint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com
* SPDX-License-Identifier: MIT
*/
package org.eolang.lints;

import com.jcabi.xml.XML;
import com.yegor256.xsline.Shift;
import com.yegor256.xsline.Xsline;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import org.eolang.xax.Xtory;
import org.xembly.Directives;
import org.xembly.Xembler;

/**
* A story that runs a Java-implemented lint by its name.
* Reads the {@code lint} key from the pack YAML and applies
* the resolved {@link Lint} to the input, producing a
* {@code <defects>} document.
* @since 1.0
*/
public final class XtLint implements Xtory {

/**
* Original story.
*/
private final Xtory origin;

/**
* Ctor.
* @param yaml YAML pack
* @param parser Parser
*/
public XtLint(final String yaml, final Xtory.Parser parser) {
this(new org.eolang.xax.XtYaml(yaml, parser));
}

/**
* Ctor.
* @param origin Original story
*/
private XtLint(final Xtory origin) {
this.origin = origin;
}

@Override
public Map<String, Object> map() {
return this.origin.map();
}

@Override
public XML before() {
return this.origin.before();
}

@Override
public XML after() {
return this.xsline().pass(this.before());
}

@Override
public Xsline xsline() {
return new Xsline(
new Shift() {
@Override
public String uid() {
return XtLint.this.name();
}

@Override
public XML apply(final int position, final XML xml) {
return XtLint.this.defects(xml);
}
}
);
}

@Override
public Collection<String> asserts() {
return this.origin.asserts();
}

/**
* Lint name from the YAML map.
* @return Lint name
*/
private String name() {
return String.valueOf(this.origin.map().get("lint"));
}

/**
* Run the lint and serialize defects into XML.
* @param xml Input XMIR
* @return Defects document
*/
private XML defects(final XML xml) {
final Directives dirs = new Directives().add("defects");
try {
for (final Lint lint : new PkMono()) {
if (lint.name().equals(this.name())) {
for (final Defect defect : lint.defects(xml)) {
dirs.add("defect")
.attr("line", defect.line())
.attr("severity", defect.severity().mnemo())
.set(defect.text())
.up();
}
break;
}
}
} catch (final IOException ex) {
throw new IllegalStateException(
String.format("Failed to run lint %s", this.name()),
ex
);
}
return new com.jcabi.xml.XMLDocument(
new Xembler(dirs).xmlQuietly()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com
# SPDX-License-Identifier: MIT
---
lint: ascii-only
asserts:
- /defects[count(defect[@severity='warning'])=1]
document: |
<object author="tests">
<comments>
<comment line="1">привет</comment>
</comments>
</object>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com
# SPDX-License-Identifier: MIT
---
lint: incorrect-unlint
asserts:
- /defects[count(defect[@severity='error'])=1]
document: |
<object author="tests">
<metas>
<meta line="1">
<head>unlint</head>
<tail>non-existent-lint-name:5</tail>
</meta>
</metas>
<o line="3" name="foo"/>
</object>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com
# SPDX-License-Identifier: MIT
---
lint: reserved-name
asserts:
- /defects[count(defect)=0]
document: |
<object author="tests">
<o line="1" name="main">
<o line="2" name="my-object" base="Φ.foo"/>
</o>
</object>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com
# SPDX-License-Identifier: MIT
---
lint: syntax-version
asserts:
- /defects[count(defect)=0]
document: |
<object author="tests" version="0.0.1">
<metas>
<meta line="1">
<head>syntax</head>
<tail>0.0.1</tail>
</meta>
</metas>
<o line="3" name="foo"/>
</object>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# SPDX-FileCopyrightText: Copyright (c) 2016-2026 Objectionary.com
# SPDX-License-Identifier: MIT
---
lint: syntax-version
asserts:
- /defects[count(defect[@severity='error'])=1]
document: |
<object author="tests" version="0.0.1">
<metas>
<meta line="1">
<head>syntax</head>
<tail>999.0.0</tail>
</meta>
</metas>
<o line="3" name="foo"/>
</object>
Loading