Skip to content

Commit 03ce6d0

Browse files
committed
Merge branch '__rultor'
2 parents 2744cc5 + c442704 commit 03ce6d0

File tree

3 files changed

+103
-6
lines changed

3 files changed

+103
-6
lines changed

src/main/java/org/eolang/lints/critical/LtIncorrectAlias.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Collection;
2929
import java.util.LinkedList;
3030
import java.util.Map;
31+
import java.util.regex.Pattern;
3132
import org.cactoos.io.ResourceOf;
3233
import org.cactoos.text.TextOf;
3334
import org.cactoos.text.UncheckedText;
@@ -41,6 +42,11 @@
4142
*/
4243
public final class LtIncorrectAlias implements Lint<Map<String, XML>> {
4344

45+
/**
46+
* Pattern contains dot.
47+
*/
48+
private static final Pattern DOT_PATTERN = Pattern.compile("\\.");
49+
4450
@Override
4551
public String name() {
4652
return "incorrect-alias";
@@ -56,11 +62,18 @@ public Collection<Defect> defects(final Map<String, XML> pkg) {
5662
continue;
5763
}
5864
final String pointer = alias.xpath("text()").get(0);
59-
final String lookup = String.format(
60-
"%s/%s",
61-
xmir.xpath("/program/metas/meta[head='package']/tail/text()").get(0),
62-
pointer
63-
);
65+
final String lookup;
66+
if (Boolean.parseBoolean(alias.xpath("contains(text(), ' ')").get(0))) {
67+
lookup = LtIncorrectAlias.DOT_PATTERN.matcher(
68+
alias.xpath("substring-after(text(), ' ')").get(0)
69+
).replaceAll("/");
70+
} else {
71+
lookup = String.format(
72+
"%s/%s",
73+
xmir.xpath("/program/metas/meta[head='package']/tail/text()").get(0),
74+
pointer
75+
);
76+
}
6477
if (!pkg.containsKey(lookup)) {
6578
defects.add(
6679
new Defect.Default(
@@ -71,7 +84,7 @@ public Collection<Defect> defects(final Map<String, XML> pkg) {
7184
xmir.xpath("/program/metas/meta[head='alias'][1]/@line").get(0)
7285
),
7386
String.format(
74-
"Incorrect pointing alias '%s', there is no %s",
87+
"Incorrect pointing alias '%s', since there is no %s",
7588
pointer,
7689
lookup
7790
)

src/test/java/org/eolang/lints/critical/LtIncorrectAliasTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,29 @@ void ignoresProgram(final String name) throws IOException {
119119
);
120120
}
121121

122+
@Test
123+
void scansSecondPartInLongerAlias() throws IOException {
124+
MatcherAssert.assertThat(
125+
"Defects aren't empty, but they should",
126+
new LtIncorrectAlias().defects(
127+
new MapOf<String, XML>(
128+
new MapEntry<>(
129+
"longer-alias",
130+
new EoSyntax(
131+
new ResourceOf(
132+
"org/eolang/lints/critical/incorrect-alias/longer-alias.eo"
133+
)
134+
).parsed()
135+
),
136+
new MapEntry<>(
137+
"org/eolang/io/stdout", new XMLDocument("<program><objects/></program>")
138+
)
139+
)
140+
),
141+
Matchers.emptyIterable()
142+
);
143+
}
144+
122145
@Test
123146
@ExtendWith(MktmpResolver.class)
124147
void acceptsValidDirectory(@Mktmp final Path dir) throws IOException {
@@ -140,4 +163,37 @@ void acceptsValidDirectory(@Mktmp final Path dir) throws IOException {
140163
Matchers.emptyIterable()
141164
);
142165
}
166+
167+
@Test
168+
@ExtendWith(MktmpResolver.class)
169+
void acceptsValidDirectoryWithLongerAlias(@Mktmp final Path dir) throws IOException {
170+
Files.write(
171+
dir.resolve("main.xmir"),
172+
new EoSyntax(
173+
new ResourceOf(
174+
"org/eolang/lints/critical/incorrect-alias/longer-alias.eo"
175+
)
176+
).parsed().toString().getBytes()
177+
);
178+
Files.write(dir.resolve("main-test.xmir"), "<program><objects/></program>".getBytes());
179+
Files.createDirectory(
180+
Files.createDirectory(
181+
Files.createDirectory(
182+
dir.resolve("org")
183+
).resolve("eolang")
184+
).resolve("io")
185+
);
186+
Files.write(
187+
dir.resolve("org/eolang/io/stdout.xmir"), "<program><objects/></program>".getBytes()
188+
);
189+
Files.write(
190+
dir.resolve("org/eolang/io/stdout-test.xmir"),
191+
"<program><objects/></program>".getBytes()
192+
);
193+
MatcherAssert.assertThat(
194+
"Defects are not empty, but should be",
195+
new Programs(dir).defects(),
196+
Matchers.emptyIterable()
197+
);
198+
}
143199
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2016-2024 Objectionary.com
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included
13+
# in all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
+alias stdout org.eolang.io.stdout
24+
+package foo
25+
26+
[] > main
27+
stdout > @
28+
"hi!"

0 commit comments

Comments
 (0)