diff --git a/src/main/java/org/eolang/lints/errors/LtObjectIsNotUnique.java b/src/main/java/org/eolang/lints/errors/LtObjectIsNotUnique.java new file mode 100644 index 00000000..e569be03 --- /dev/null +++ b/src/main/java/org/eolang/lints/errors/LtObjectIsNotUnique.java @@ -0,0 +1,88 @@ +/* + * 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.errors; + +import com.jcabi.xml.XML; +import java.util.Collection; +import java.util.LinkedList; +import java.util.Map; +import java.util.Objects; +import org.eolang.lints.Defect; +import org.eolang.lints.Lint; +import org.eolang.lints.Severity; + +/** + * Object is not unique. + * + * @since 0.0.30 + */ +public final class LtObjectIsNotUnique implements Lint> { + + @Override + public String name() { + return "object-is-not-unique"; + } + + @Override + public Collection defects(final Map pkg) { + final Collection defects = new LinkedList<>(); + pkg.values().forEach( + xmir -> { + final String src = xmir.xpath("/program/@name").get(0); + final String name = xmir.xpath("/program/objects/o[1]/@name").get(0); + pkg.values().forEach( + oth -> { + if (!Objects.equals(oth, xmir)) { + final String other = oth.xpath("/program/objects/o[1]/@name").get(0); + if (name.equals(other)) { + defects.add( + new Defect.Default( + this.name(), + Severity.ERROR, + oth.xpath("/program/@name").stream() + .findFirst() + .orElse("unknown"), + Integer.parseInt( + oth.xpath("/program/objects/o[1]/@line").get(0) + ), + String.format( + "The object name '%s' is not unique, original object was found in '%s'", + name, src + ) + ) + ); + } + } + } + ); + } + ); + return defects; + } + + @Override + public String motive() throws Exception { + throw new UnsupportedOperationException("#motive()"); + } +} diff --git a/src/test/java/org/eolang/lints/errors/LtObjectIsNotUniqueTest.java b/src/test/java/org/eolang/lints/errors/LtObjectIsNotUniqueTest.java new file mode 100644 index 00000000..30be12fc --- /dev/null +++ b/src/test/java/org/eolang/lints/errors/LtObjectIsNotUniqueTest.java @@ -0,0 +1,87 @@ +/* + * 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.errors; + +import com.jcabi.xml.XML; +import com.jcabi.xml.XMLDocument; +import org.cactoos.io.ResourceOf; +import org.cactoos.map.MapEntry; +import org.cactoos.map.MapOf; +import org.eolang.parser.EoSyntax; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.Test; + +/** + * Tests for {@link LtObjectIsNotUnique}. + * + * @since 0.0.30 + */ +final class LtObjectIsNotUniqueTest { + + @Test + void catchesDuplicates() throws Exception { + ; + MatcherAssert.assertThat( + "Defects are empty, but they should not", + new LtObjectIsNotUnique().defects( + new MapOf( + new MapEntry<>( + "foo", LtObjectIsNotUniqueTest.xmir("foo") + ), + new MapEntry<>( + "bar-with-foo", LtObjectIsNotUniqueTest.xmir("bar-with-foo") + ) + ) + ), + Matchers.hasSize(Matchers.greaterThan(0)) + ); + } + + @Test + void allowsAllUnique() { + MatcherAssert.assertThat( + "Defects aren't empty, but they should", + new LtObjectIsNotUnique().defects( + new MapOf( + new MapEntry<>("foo", new XMLDocument("")), + new MapEntry<>("bar", new XMLDocument("")) + ) + ), + 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(); + } +} diff --git a/src/test/resources/org/eolang/lints/errors/object-is-not-unique/bar-with-foo.eo b/src/test/resources/org/eolang/lints/errors/object-is-not-unique/bar-with-foo.eo new file mode 100644 index 00000000..c1dd8f66 --- /dev/null +++ b/src/test/resources/org/eolang/lints/errors/object-is-not-unique/bar-with-foo.eo @@ -0,0 +1,2 @@ +# Bar. +[] > foo diff --git a/src/test/resources/org/eolang/lints/errors/object-is-not-unique/bar.eo b/src/test/resources/org/eolang/lints/errors/object-is-not-unique/bar.eo new file mode 100644 index 00000000..e69de29b diff --git a/src/test/resources/org/eolang/lints/errors/object-is-not-unique/foo.eo b/src/test/resources/org/eolang/lints/errors/object-is-not-unique/foo.eo new file mode 100644 index 00000000..24ccef07 --- /dev/null +++ b/src/test/resources/org/eolang/lints/errors/object-is-not-unique/foo.eo @@ -0,0 +1,2 @@ +# Foo. +[] > foo