Skip to content

Commit

Permalink
add add pattern.quote, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemGet committed Dec 30, 2024
1 parent 6305004 commit c00b9b6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
8 changes: 6 additions & 2 deletions eo-maven-plugin/src/main/java/org/eolang/maven/Unplace.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.io.File;
import java.nio.file.Path;
import java.util.regex.Pattern;

/**
* Make program name from a path.
Expand Down Expand Up @@ -56,7 +57,7 @@ final class Unplace {
* @param dir The name of the parent dir
*/
Unplace(final Path dir) {
this(dir, ".eo$");
this(dir, ".eo");
}

/**
Expand Down Expand Up @@ -86,7 +87,10 @@ final class Unplace {
public String make(final Path file) {
return file.toString().substring(
this.parent.toString().length() + 1
).replaceAll(this.extension, "").replace(File.separator, ".");
).replaceAll(
Pattern.quote(this.extension).concat("$"),
""
).replace(File.separator, ".");
}

}
29 changes: 29 additions & 0 deletions eo-maven-plugin/src/test/java/org/eolang/maven/UnplaceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.nio.file.Paths;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

Expand Down Expand Up @@ -55,4 +57,31 @@ void makesName(
);
}

@ParameterizedTest
@CsvSource({
"/tmp/foo/bar, /tmp/foo/bar/a/b/c.phi, a.b.c",
"/tmp/foo/bar, /tmp/foo/bar/a/b/.cd.ef.phi, a.b..cd.ef"
})
void makesNameWithCustomExtension(
final String base,
final String source,
final String name
) {
MatcherAssert.assertThat(
CatalogsTest.TO_ADD_MESSAGE,
new Unplace(Paths.get(base), ".phi").make(
Paths.get(source)
),
Matchers.equalTo(name)
);
}

@Test
void interpretsExtensionAsLiteralString() {
Assertions.assertDoesNotThrow(
() -> new Unplace(Paths.get("/tmp/foo/bar"), "(")
.make(Paths.get("/tmp/foo/bar/a/b/c(")),
"Extension interpreted not as literal string"
);
}
}

0 comments on commit c00b9b6

Please sign in to comment.