Skip to content

Commit be47a68

Browse files
committed
Refactor object name stripping logic for better readability
1 parent e92b0a7 commit be47a68

5 files changed

Lines changed: 33 additions & 20 deletions

File tree

eo-maven-plugin/src/main/java/org/eolang/maven/ObjectsIndex.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,20 @@ final class ObjectsIndex {
7171
* @throws Exception If something unexpected happened.
7272
*/
7373
public boolean contains(final String name) throws Exception {
74-
return this.objects.value().contains(name);
74+
final String prefix = "org.eolang.";
75+
final String stripped;
76+
if (name.startsWith(prefix)) {
77+
stripped = name.substring(prefix.length());
78+
} else {
79+
stripped = name;
80+
}
81+
return this.objects.value().contains(stripped);
7582
}
7683

7784
/**
7885
* Converts object name to the format that is used in the objectionary.
79-
* - "objects/org/eolang/array.eo" -> "org.eolang.array"
80-
* - "tests/org/eolang/seq-tests.eo" -> "org.eolang.seq-tests"
86+
* - "objects/array.eo" -> "array"
87+
* - "objects/io/stdout.eo" -> "io.stdout"
8188
* @param name Object name in raw format.
8289
* @return Object name in objectionary format.
8390
*/

eo-maven-plugin/src/main/java/org/eolang/maven/OyRemote.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,18 @@ static final class UrlOy {
172172
* @throws MalformedURLException in case of incorrect URL
173173
*/
174174
public URL value(final String name) throws MalformedURLException {
175+
final String prefix = "org.eolang.";
176+
final String stripped;
177+
if (name.startsWith(prefix)) {
178+
stripped = name.substring(prefix.length());
179+
} else {
180+
stripped = name;
181+
}
175182
return new URL(
176183
String.format(
177184
this.template,
178185
this.hash.value(),
179-
name.replace(".", "/")
186+
stripped.replace(".", "/")
180187
)
181188
);
182189
}

eo-maven-plugin/src/test/java/org/eolang/maven/ObjectsIndexTest.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,39 @@
2121
final class ObjectsIndexTest {
2222

2323
@Test
24-
@SuppressWarnings("PMD.UnnecessaryLocalRule")
2524
void runsContainsWithOnlyOneCallToDecoratedObject() throws Exception {
2625
final AtomicInteger calls = new AtomicInteger(0);
27-
final String object = "org.eolang.io.stderr";
2826
final ObjectsIndex index = new ObjectsIndex(
2927
new ScalarOf<>(
3028
() -> {
3129
calls.incrementAndGet();
32-
return Collections.singleton(object);
30+
return Collections.singleton("io.stderr");
3331
}
3432
)
3533
);
36-
index.contains(object);
37-
index.contains(object);
34+
index.contains("org.eolang.io.stderr");
35+
index.contains("org.eolang.io.stderr");
3836
MatcherAssert.assertThat(
39-
"The number of calls should be 1",
37+
String.format(
38+
"Scalar was called %d times instead of exactly once",
39+
calls.get()
40+
),
4041
calls.get(),
4142
Matchers.is(1)
4243
);
4344
}
4445

4546
@Test
46-
@SuppressWarnings("PMD.UnnecessaryLocalRule")
4747
void runsContainsSuccessfully() throws Exception {
48-
final String object = "org.eolang.io.stderr";
4948
MatcherAssert.assertThat(
5049
"The object must contain the value",
5150
new ObjectsIndex(
5251
new ScalarOf<>(
5352
() -> {
54-
return Collections.singleton(object);
53+
return Collections.singleton("io.stderr");
5554
}
5655
)
57-
).contains(object),
56+
).contains("org.eolang.io.stderr"),
5857
Matchers.is(true)
5958
);
6059
}
@@ -66,7 +65,7 @@ void doesNotContainUnknownValue() throws Exception {
6665
new ObjectsIndex(
6766
new ScalarOf<>(
6867
() -> {
69-
return Collections.singleton("org.eolang.io.stderr");
68+
return Collections.singleton("io.stderr");
7069
}
7170
)
7271
).contains("unknown"),

eo-maven-plugin/src/test/java/org/eolang/maven/OyIndexedTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void containsInFakeIndex() throws IOException {
4747
"OyIndexed with fake index must contain stdout object, but it doesn't",
4848
new OyIndexed(
4949
new Objectionary.Fake(),
50-
new ObjectsIndex(() -> Collections.singleton(this.stdout()))
50+
new ObjectsIndex(() -> Collections.singleton("io.stdout"))
5151
).contains(this.stdout()),
5252
Matchers.is(true)
5353
);

eo-maven-plugin/src/test/java/org/eolang/maven/OyRemoteTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ void buildsCorrectUrlForProgram() throws Exception {
2828
new OyRemote.UrlOy(
2929
"https://raw/objectionary/home/%s/objects/%s.eo",
3030
"abcde"
31-
).value("org.eolang.app"),
31+
).value("org.eolang.io.stdout"),
3232
Matchers.is(
33-
new URL("https://raw/objectionary/home/abcde/objects/org/eolang/app.eo")
33+
new URL("https://raw/objectionary/home/abcde/objects/io/stdout.eo")
3434
)
3535
);
3636
}
@@ -42,9 +42,9 @@ void buildsCorrectUrlForDirectory() throws Exception {
4242
new OyRemote.UrlOy(
4343
"https://github.com/objectionary/home/tree/%s/objects/%s",
4444
"abcde"
45-
).value("org.eolang.test"),
45+
).value("org.eolang.ss"),
4646
Matchers.is(
47-
new URL("https://github.com/objectionary/home/tree/abcde/objects/org/eolang/test")
47+
new URL("https://github.com/objectionary/home/tree/abcde/objects/ss")
4848
)
4949
);
5050
}

0 commit comments

Comments
 (0)