Skip to content

Commit

Permalink
Add checks to Unixpath
Browse files Browse the repository at this point in the history
  • Loading branch information
monaullah committed Dec 4, 2024
1 parent ebbd9ad commit 07cecad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/src/main/java/nva/commons/core/paths/UnixPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ public boolean isEmptyPath() {
}

public UnixPath replacePathElementByIndexFromEnd(int index, String replacement) {
if (isRoot() || isEmptyPath() || path.size() <= index) {
return this;
}
var newPath = new ArrayList<>(path);
newPath.set(lastPathElementIndex() - index, replacement);
return new UnixPath(newPath);
Expand Down
19 changes: 19 additions & 0 deletions core/src/test/java/nva/commons/core/paths/UnixPathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,25 @@ void shouldReplacePathElementByIndexFromEnd(int indexFromEnd) {
assertThat(actualPath, is(equalTo(expectedPath)));
}

@Test
void shouldReturnEmptyPathWhenReplacingElementByIndexFromEndInEmptyPath() {
var actualPath = UnixPath.EMPTY_PATH.replacePathElementByIndexFromEnd(0, "replacement");
assertThat(actualPath, is(equalTo(UnixPath.EMPTY_PATH)));
}

@Test
void shouldDoNothingWhenReplacingElementByIndexFromEndInRootPath() {
var actualPath = UnixPath.ROOT_PATH.replacePathElementByIndexFromEnd(0, "replacement");
assertThat(actualPath, is(equalTo(UnixPath.ROOT_PATH)));
}

@Test
void shouldDoNothingWhenReplacingIndexThatDoesNotExist() {
var originalPath = UnixPath.of("justASingleElement");
var actualPath = originalPath.replacePathElementByIndexFromEnd(1, "replacement");
assertThat(actualPath, is(equalTo(originalPath)));
}

private static class ClassWithUnixPath {

private UnixPath field;
Expand Down

0 comments on commit 07cecad

Please sign in to comment.