forked from objectionary/lints
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
src/main/java/org/eolang/lints/errors/LtObjectIsNotUnique.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Map<String, XML>> { | ||
|
||
@Override | ||
public String name() { | ||
return "object-is-not-unique"; | ||
} | ||
|
||
@Override | ||
public Collection<Defect> defects(final Map<String, XML> pkg) { | ||
final Collection<Defect> 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()"); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
src/test/java/org/eolang/lints/errors/LtObjectIsNotUniqueTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, XML>( | ||
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<String, XML>( | ||
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(); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/test/resources/org/eolang/lints/errors/object-is-not-unique/bar-with-foo.eo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Bar. | ||
[] > foo |
Empty file.
2 changes: 2 additions & 0 deletions
2
src/test/resources/org/eolang/lints/errors/object-is-not-unique/foo.eo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Foo. | ||
[] > foo |