summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornea <romangraef@gmail.com>2021-08-13 16:49:33 +0200
committernea <romangraef@gmail.com>2021-08-13 16:49:33 +0200
commit97278b1e4ecd4db5224db83e167f6c7a68d4abb7 (patch)
tree114a83fb3f698a1978bf6b0c0c318eca15fdf4f0
parente4753338d60396c00f80b494366b2869f8541944 (diff)
downloadwebos-97278b1e4ecd4db5224db83e167f6c7a68d4abb7.tar.gz
webos-97278b1e4ecd4db5224db83e167f6c7a68d4abb7.tar.bz2
webos-97278b1e4ecd4db5224db83e167f6c7a68d4abb7.zip
more checks
-rw-r--r--src/jsMain/kotlin/io/Path.kt18
-rw-r--r--src/jsTest/kotlin/io/PathTest.kt90
2 files changed, 89 insertions, 19 deletions
diff --git a/src/jsMain/kotlin/io/Path.kt b/src/jsMain/kotlin/io/Path.kt
index f486581..20e4a90 100644
--- a/src/jsMain/kotlin/io/Path.kt
+++ b/src/jsMain/kotlin/io/Path.kt
@@ -1,12 +1,14 @@
package io
-sealed interface Path {
- val parts: List<String>
+sealed class Path {
+ abstract val parts: List<String>
fun toAbsolutePath(relativeTo: Absolute): Absolute {
return relativeTo.resolve(this)
}
- fun resolve(path: Path): Path
+ abstract fun resolve(path: Path): Path
+
+ abstract val stringPath: String
companion object {
val root = Absolute(listOf())
@@ -38,19 +40,23 @@ sealed interface Path {
}
}
- data class Relative internal constructor(override val parts: List<String>) : Path {
+ data class Relative internal constructor(override val parts: List<String>) : Path() {
override fun resolve(path: Path): Path {
if (path is Absolute) return path
return Relative(this.parts + path.parts)
}
+
+ override val stringPath: String get() = parts.joinToString("/")
}
- data class Absolute internal constructor(override val parts: List<String>) : Path {
+ data class Absolute internal constructor(override val parts: List<String>) : Path() {
override fun resolve(path: Path): Absolute {
if (path is Absolute) return path
return Absolute(this.parts + path.parts)
}
+ override val stringPath: String get() = "/" + parts.joinToString("/")
+
fun relativize(path: Path): Relative = when (path) {
is Relative -> path
is Absolute -> {
@@ -72,4 +78,6 @@ sealed interface Path {
}
}
}
+
+ override fun toString(): String = "Path($stringPath)"
}
diff --git a/src/jsTest/kotlin/io/PathTest.kt b/src/jsTest/kotlin/io/PathTest.kt
index 8ea577c..174c2b2 100644
--- a/src/jsTest/kotlin/io/PathTest.kt
+++ b/src/jsTest/kotlin/io/PathTest.kt
@@ -2,30 +2,92 @@ package io
import io.kotest.assertions.assertSoftly
import io.kotest.core.spec.style.FunSpec
+import io.kotest.data.forAll
+import io.kotest.data.row
+import io.kotest.matchers.be
+import io.kotest.matchers.should
+import io.kotest.matchers.shouldBe
+import io.kotest.matchers.types.beOfType
import io.kotest.matchers.types.shouldBeTypeOf
class PathTest : FunSpec({
val homeDir = Path.of("/home") as Path.Absolute
test("recognize relative paths as such") {
- listOf(
- Path.of("a/b"),
- Path.of("."),
- Path.of("a", "b"),
- Path.ofShell("a/b", userHome = homeDir),
- Path.ofShell(".", userHome = homeDir),
- Path.ofShell("a", "b", userHome = homeDir),
- Path.ofShell(listOf("a", "b"), userHome = homeDir),
- ).forEach {
+ forAll(
+ row(Path.of("a/b")),
+ row(Path.of(".")),
+ row(Path.of("a", "b")),
+ row(Path.ofShell("a/b", userHome = homeDir)),
+ row(Path.ofShell(".", userHome = homeDir)),
+ row(Path.ofShell("a", "b", userHome = homeDir)),
+ row(Path.ofShell(listOf("a", "b"), userHome = homeDir)),
+ ) {
assertSoftly(it) { shouldBeTypeOf<Path.Relative>() }
}
}
test("recognize absolute paths as such") {
- listOf(
- Path.of("/a/b"),
- Path.of("/"),
- Path.ofShell("/b/c", userHome = homeDir),
- ).forEach {
+ forAll(
+ row(Path.of("/a/b")),
+ row(Path.of("/")),
+ row(Path.ofShell("/b/c", userHome = homeDir)),
+ ) {
assertSoftly(it) { shouldBeTypeOf<Path.Absolute>() }
}
}
+ test("Path.of(x).stringPath == x") {
+ forAll(
+ row("/"),
+ row("/a/b"),
+ row("a/b"),
+ row("."),
+ ) { name ->
+ assertSoftly(Path.of(name).stringPath) {
+ shouldBe(name)
+ }
+ assertSoftly(Path.ofShell(name, homeDir).stringPath) {
+ shouldBe(name)
+ }
+ }
+ }
+ test("Shell resolution of home directory") {
+ forAll(
+ row("~/a", "/home/a"),
+ row("~", "/home"),
+ row("~/.", "/home/."),
+ row("/a", "/a"),
+ row("a", "a"),
+ ) { a, b ->
+ assertSoftly(Path.ofShell(a, homeDir).stringPath) {
+ shouldBe(b)
+ }
+ }
+ }
+ test("Relative path resolution works") {
+ forAll(
+ row("/a/b", "c/d", "/a/b/c/d"),
+ row("/a/b", "/c/d", "/c/d"),
+ row("/a/", "c", "/a/c"),
+ ) { a, b, c ->
+ val x = Path.of(a)
+ val y = Path.of(b)
+ val z = x.resolve(y)
+ assertSoftly {
+ x should beOfType<Path.Absolute>()
+ z should beOfType<Path.Absolute>()
+ z.stringPath should be(c)
+ }
+ }
+ }
+ test("Equality checks should work") {
+ forAll(
+ row("a"),
+ row("a/b"),
+ row("/a/b"),
+ row("c//d")
+ ) {
+ assertSoftly {
+ Path.of(it) should be(Path.of(it))
+ }
+ }
+ }
})