-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract API for nested/parental root checking to utils module
- Loading branch information
1 parent
a445b7b
commit cf05c1f
Showing
3 changed files
with
144 additions
and
4 deletions.
There are no files selected for viewing
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
10 changes: 10 additions & 0 deletions
10
utils/src/main/java/dev/arkbuilders/components/utils/PathExt.kt
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,10 @@ | ||
package dev.arkbuilders.components.utils | ||
|
||
import java.nio.file.Path | ||
|
||
fun Path.hasNestedOrParentalRoot(roots: Iterable<Path>): Boolean { | ||
val hasNestedRoot = roots.any { path -> | ||
this.startsWith(path) || path.startsWith(this) | ||
} | ||
return hasNestedRoot | ||
} |
132 changes: 132 additions & 0 deletions
132
utils/src/test/java/dev/arkbuilders/components/utils/PathExtTest.kt
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,132 @@ | ||
package dev.arkbuilders.components.utils | ||
|
||
import junit.framework.TestCase.assertEquals | ||
import org.junit.Test | ||
import java.nio.file.Path | ||
import java.nio.file.Paths | ||
|
||
class PathExtTest { | ||
|
||
// --- Tests with Set for roots parameter --- | ||
|
||
@Test | ||
fun `hasNestedOrParentalRoot should return true when there is an exact match in roots`() { | ||
val path = Paths.get("/parent/child") | ||
val roots = setOf(Paths.get("/parent/child")) | ||
assertEquals(path.hasNestedOrParentalRoot(roots), true) | ||
} | ||
|
||
@Test | ||
fun `hasNestedOrParentalRoot should return true when there is a direct parent in roots`() { | ||
val path = Paths.get("/parent/child") | ||
val roots = setOf(Paths.get("/parent")) | ||
assertEquals(path.hasNestedOrParentalRoot(roots), true) | ||
} | ||
|
||
@Test | ||
fun `hasNestedOrParentalRoot should return true when there is a direct child in roots`() { | ||
val path = Paths.get("/parent") | ||
val roots = setOf(Paths.get("/parent/child")) | ||
assertEquals(path.hasNestedOrParentalRoot(roots), true) | ||
} | ||
|
||
@Test | ||
fun `hasNestedOrParentalRoot should return true when there is a nested parent in roots`() { | ||
val path = Paths.get("/parent/child/grandchild") | ||
val roots = setOf(Paths.get("/parent")) | ||
assertEquals(path.hasNestedOrParentalRoot(roots), true) | ||
} | ||
|
||
@Test | ||
fun `hasNestedOrParentalRoot should return true when there is a nested child in roots`() { | ||
val path = Paths.get("/parent") | ||
val roots = setOf(Paths.get("/parent/child/grandchild")) | ||
assertEquals(path.hasNestedOrParentalRoot(roots), true) | ||
} | ||
|
||
@Test | ||
fun `hasNestedOrParentalRoot should return false when there are no nested or parental roots`() { | ||
val path = Paths.get("/parent/child") | ||
val roots = setOf(Paths.get("/unrelated"), Paths.get("/another")) | ||
assertEquals(path.hasNestedOrParentalRoot(roots), false) | ||
} | ||
|
||
@Test | ||
fun `hasNestedOrParentalRoot should return false when roots set is empty`() { | ||
val path = Paths.get("/parent/child") | ||
val roots = emptySet<Path>() | ||
assertEquals(path.hasNestedOrParentalRoot(roots), false) | ||
} | ||
|
||
// --- Tests with List for roots parameter --- | ||
|
||
@Test | ||
fun `hasNestedOrParentalRoot should return true when an exact match exists in roots (List)`() { | ||
val path = Paths.get("/parent/child") | ||
val roots = listOf(Paths.get("/parent/child")) | ||
assertEquals(path.hasNestedOrParentalRoot(roots), true) | ||
} | ||
|
||
@Test | ||
fun `hasNestedOrParentalRoot should return true when a direct parent exists in roots (List)`() { | ||
val path = Paths.get("/parent/child") | ||
val roots = listOf(Paths.get("/parent")) | ||
assertEquals(path.hasNestedOrParentalRoot(roots), true) | ||
} | ||
|
||
@Test | ||
fun `hasNestedOrParentalRoot should return true when a direct child exists in roots (List)`() { | ||
val path = Paths.get("/parent") | ||
val roots = listOf(Paths.get("/parent/child")) | ||
assertEquals(path.hasNestedOrParentalRoot(roots), true) | ||
} | ||
|
||
@Test | ||
fun `hasNestedOrParentalRoot should return true when a nested parent exists in roots (List)`() { | ||
val path = Paths.get("/parent/child/grandchild") | ||
val roots = listOf(Paths.get("/parent")) | ||
assertEquals(path.hasNestedOrParentalRoot(roots), true) | ||
} | ||
|
||
@Test | ||
fun `hasNestedOrParentalRoot should return false when there are no nested or parental roots (List)`() { | ||
val path = Paths.get("/parent/child") | ||
val roots = listOf(Paths.get("/unrelated"), Paths.get("/another")) | ||
assertEquals(path.hasNestedOrParentalRoot(roots), false) | ||
} | ||
|
||
@Test | ||
fun `hasNestedOrParentalRoot should return false when roots is empty`() { | ||
val path = Paths.get("/parent/child") | ||
val roots = emptyList<Path>() | ||
assertEquals(path.hasNestedOrParentalRoot(roots), false) | ||
} | ||
|
||
@Test | ||
fun `hasNestedOrParentalRoot should return true when duplicates of an exact match exist in roots (List)`() { | ||
val path = Paths.get("/parent/child") | ||
val roots = listOf(Paths.get("/parent/child"), Paths.get("/parent/child")) // Duplicate exact match | ||
assertEquals(path.hasNestedOrParentalRoot(roots), true) | ||
} | ||
|
||
@Test | ||
fun `hasNestedOrParentalRoot should return true when duplicates of a direct parent exist in roots (List)`() { | ||
val path = Paths.get("/parent/child") | ||
val roots = listOf(Paths.get("/parent"), Paths.get("/parent")) // Duplicate direct parent | ||
assertEquals(path.hasNestedOrParentalRoot(roots), true) | ||
} | ||
|
||
@Test | ||
fun `hasNestedOrParentalRoot should return true when duplicates of a nested parent exist in roots (List)`() { | ||
val path = Paths.get("/parent/child/grandchild") | ||
val roots = listOf(Paths.get("/parent"), Paths.get("/parent")) // Duplicate nested parent | ||
assertEquals(path.hasNestedOrParentalRoot(roots), true) | ||
} | ||
|
||
@Test | ||
fun `hasNestedOrParentalRoot should return false when duplicates of unrelated paths exist in roots (List)`() { | ||
val path = Paths.get("/parent/child") | ||
val roots = listOf(Paths.get("/unrelated"), Paths.get("/unrelated")) // Duplicate unrelated paths | ||
assertEquals(path.hasNestedOrParentalRoot(roots), false) | ||
} | ||
} |