Skip to content

Commit

Permalink
feat(objectionary#117): ParsedEo for XMIRs
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Jan 9, 2025
1 parent 6542631 commit d4b5c25
Show file tree
Hide file tree
Showing 3 changed files with 221 additions and 24 deletions.
66 changes: 66 additions & 0 deletions src/test/java/org/eolang/lints/ParsedEo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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 org.cactoos.Scalar;
import org.cactoos.io.ResourceOf;
import org.eolang.parser.EoSyntax;

/**
* Parsed EO syntax to XMIR.
* @since 0.0.31
*/
public final class ParsedEo implements Scalar<XML> {

/**
* Filename prefix.
*/
private final String prefix;

/**
* EO filename.
*/
private final String name;

/**
* Ctor.
* @param prfx Filename prefix
* @param nme EO filename
*/
public ParsedEo(final String prfx, final String nme) {
this.prefix = prfx;
this.name = nme;
}

@Override
public XML value() throws Exception {
return new EoSyntax(
this.name,
new ResourceOf(
String.format("%s%s.eo", this.prefix, this.name)
)
).parsed();
}
}
117 changes: 117 additions & 0 deletions src/test/java/org/eolang/lints/critical/LtAtomIsNotUniqueTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* 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.critical;

import com.jcabi.xml.XML;
import org.cactoos.map.MapEntry;
import org.cactoos.map.MapOf;
import org.eolang.lints.ParsedEo;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

/**
* Tests for {@link LtAtomIsNotUnique}.
*
* @since 0.0.31
*/
final class LtAtomIsNotUniqueTest {

/**
* Prefix to EO snippet objects.
*/
private static final String EO_PREFIX = "org/eolang/lints/critical/atom-is-not-unique/";

@Test
void catchesAtomDuplicates() throws Exception {
MatcherAssert.assertThat(
"Defects are empty, but they should not",
new LtAtomIsNotUnique().defects(
new MapOf<String, XML>(
new MapEntry<>(
"foo",
new ParsedEo(LtAtomIsNotUniqueTest.EO_PREFIX, "foo").value()
),
new MapEntry<>(
"bar",
new ParsedEo(LtAtomIsNotUniqueTest.EO_PREFIX, "bar").value()
)
)
),
Matchers.hasSize(Matchers.greaterThan(0))
);
}

@Test
void catchesDuplicatesInSingleFile() throws Exception {
MatcherAssert.assertThat(
"Defects are empty, but they should not",
new LtAtomIsNotUnique().defects(
new MapOf<>(
new MapEntry<>(
"dup",
new ParsedEo(LtAtomIsNotUniqueTest.EO_PREFIX, "dup").value()
)
)
),
Matchers.hasSize(Matchers.greaterThan(0))
);
}

@Test
void allowsUniqueAtoms() throws Exception {
MatcherAssert.assertThat(
"Defects aren't empty, but they should",
new LtAtomIsNotUnique().defects(
new MapOf<String, XML>(
new MapEntry<>(
"a",
new ParsedEo(LtAtomIsNotUniqueTest.EO_PREFIX, "a").value()
),
new MapEntry<>(
"b",
new ParsedEo(LtAtomIsNotUniqueTest.EO_PREFIX, "b").value()
)
)
),
Matchers.emptyIterable()
);
}

@Test
void allowsUniqueAtomsInSingleFile() throws Exception {
MatcherAssert.assertThat(
"Defects aren't empty, but they should",
new LtAtomIsNotUnique().defects(
new MapOf<>(
new MapEntry<>(
"spb",
new ParsedEo(LtAtomIsNotUniqueTest.EO_PREFIX, "spb").value()
)
)
),
Matchers.emptyIterable()
);
}
}
62 changes: 38 additions & 24 deletions src/test/java/org/eolang/lints/errors/LtObjectIsNotUniqueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
package org.eolang.lints.errors;

import com.jcabi.xml.XML;
import org.cactoos.io.ResourceOf;
import org.cactoos.map.MapEntry;
import org.cactoos.map.MapOf;
import org.eolang.parser.EoSyntax;
import org.eolang.lints.ParsedEo;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
Expand All @@ -39,17 +38,24 @@
*/
final class LtObjectIsNotUniqueTest {

/**
* Prefix to EO snippet objects.
*/
private static final String EO_PREFIX = "org/eolang/lints/errors/object-is-not-unique/";

@Test
void catchesDuplicates() throws Exception {
MatcherAssert.assertThat(
"Defects are empty, but they should not",
new LtObjectIsNotUnique().defects(
new MapOf<String, XML>(
new MapEntry<>(
"foo", LtObjectIsNotUniqueTest.xmir("foo")
"foo",
new ParsedEo(LtObjectIsNotUniqueTest.EO_PREFIX, "foo").value()
),
new MapEntry<>(
"bar-with-foo", LtObjectIsNotUniqueTest.xmir("bar-with-foo")
"bar-with-foo",
new ParsedEo(LtObjectIsNotUniqueTest.EO_PREFIX, "bar-with-foo").value()
)
)
),
Expand All @@ -64,10 +70,12 @@ void catchesDuplicatesAcrossMultipleObjects() throws Exception {
new LtObjectIsNotUnique().defects(
new MapOf<String, XML>(
new MapEntry<>(
"test-1", LtObjectIsNotUniqueTest.xmir("test-1")
"test-1",
new ParsedEo(LtObjectIsNotUniqueTest.EO_PREFIX, "test-1").value()
),
new MapEntry<>(
"test-2", LtObjectIsNotUniqueTest.xmir("test-2")
"test-2",
new ParsedEo(LtObjectIsNotUniqueTest.EO_PREFIX, "test-2").value()
)
)
),
Expand All @@ -81,8 +89,14 @@ void allowsAllUnique() throws Exception {
"Defects aren't empty, but they should",
new LtObjectIsNotUnique().defects(
new MapOf<String, XML>(
new MapEntry<>("c", LtObjectIsNotUniqueTest.xmir("c")),
new MapEntry<>("e", LtObjectIsNotUniqueTest.xmir("e"))
new MapEntry<>(
"c",
new ParsedEo(LtObjectIsNotUniqueTest.EO_PREFIX, "c").value()
),
new MapEntry<>(
"e",
new ParsedEo(LtObjectIsNotUniqueTest.EO_PREFIX, "e").value()
)
)
),
Matchers.emptyIterable()
Expand All @@ -95,8 +109,14 @@ void allowsNonUniqueInDifferentPackages() throws Exception {
"Defects aren't empty, but they should",
new LtObjectIsNotUnique().defects(
new MapOf<String, XML>(
new MapEntry<>("baz", LtObjectIsNotUniqueTest.xmir("baz")),
new MapEntry<>("baz-packaged", LtObjectIsNotUniqueTest.xmir("baz-packaged"))
new MapEntry<>(
"baz",
new ParsedEo(LtObjectIsNotUniqueTest.EO_PREFIX, "baz").value()
),
new MapEntry<>(
"baz-packaged",
new ParsedEo(LtObjectIsNotUniqueTest.EO_PREFIX, "baz-packaged").value()
)
)
),
Matchers.emptyIterable()
Expand All @@ -109,23 +129,17 @@ void allowsNonUniqueMultipleObjectsInDifferentPackages() throws Exception {
"Defects aren't empty, but they should",
new LtObjectIsNotUnique().defects(
new MapOf<String, XML>(
new MapEntry<>("mul", LtObjectIsNotUniqueTest.xmir("mul")),
new MapEntry<>("mul-packaged", LtObjectIsNotUniqueTest.xmir("mul-packaged"))
new MapEntry<>(
"mul",
new ParsedEo(LtObjectIsNotUniqueTest.EO_PREFIX, "mul").value()
),
new MapEntry<>(
"mul-packaged",
new ParsedEo(LtObjectIsNotUniqueTest.EO_PREFIX, "mul-packaged").value()
)
)
),
Matchers.emptyIterable()
);
}

private static XML xmir(final String name) throws Exception {
return new EoSyntax(
name,
new ResourceOf(
String.format(
"org/eolang/lints/errors/object-is-not-unique/%s.eo",
name
)
)
).parsed();
}
}

0 comments on commit d4b5c25

Please sign in to comment.