Skip to content

Commit

Permalink
Extract API for nested/parental root checking to utils module
Browse files Browse the repository at this point in the history
  • Loading branch information
tuancoltech committed Nov 11, 2024
1 parent a445b7b commit cf05c1f
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.orbitmvi.orbit.viewmodel.container
import dev.arkbuilders.arklib.data.folders.FoldersRepo
import dev.arkbuilders.arklib.utils.DeviceStorageUtils
import dev.arkbuilders.arklib.utils.listChildren
import dev.arkbuilders.components.utils.hasNestedOrParentalRoot
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.nio.file.Path
Expand Down Expand Up @@ -192,10 +193,7 @@ internal class ArkFilePickerViewModel(
val root = roots.find { root -> file.startsWith(root) }
val favorites = rootsWithFavorites[root]?.flatten()

val hasNestedRoot = roots.contains(file)
|| (roots.indexOfFirst { path ->
val index = path.toString().indexOf(file.toString())
(index >= 0) && (path.toString()[index] == '/') } >= 0)
val hasNestedRoot = file.hasNestedOrParentalRoot(roots)

if (hasNestedRoot) {
postSideEffect(FilePickerSideEffect.NestedRootProhibited)
Expand Down
10 changes: 10 additions & 0 deletions utils/src/main/java/dev/arkbuilders/components/utils/PathExt.kt
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 utils/src/test/java/dev/arkbuilders/components/utils/PathExtTest.kt
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)
}
}

0 comments on commit cf05c1f

Please sign in to comment.