From 3e1becfa688f2cba280c475caf39aec3ce14a095 Mon Sep 17 00:00:00 2001 From: nea Date: Mon, 16 Aug 2021 15:05:45 +0200 Subject: more test --- build.gradle.kts | 6 +++++- src/jsMain/kotlin/io/Path.kt | 23 +++++++++-------------- src/jsTest/kotlin/io/PathTest.kt | 11 +++++++++++ 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index b8e248f..1302fab 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,7 +14,11 @@ kotlin { targets { js(IR) { nodejs { } - browser { testTask { useMocha() } } + browser { + webpackTask { + output.libraryTarget = "umd" + } + testTask { useMocha() } } } } sourceSets { diff --git a/src/jsMain/kotlin/io/Path.kt b/src/jsMain/kotlin/io/Path.kt index 20e4a90..241c559 100644 --- a/src/jsMain/kotlin/io/Path.kt +++ b/src/jsMain/kotlin/io/Path.kt @@ -60,21 +60,16 @@ sealed class Path { fun relativize(path: Path): Relative = when (path) { is Relative -> path is Absolute -> { - var commonPrefix = true - val partList = mutableListOf() - var returns = 0 - for ((idx, part) in path.parts.withIndex()) { - if (idx < this.parts.size) { - if (this.parts[idx] == part && commonPrefix) { - continue - } else { - commonPrefix = false - returns++ - } - } - partList.add(part) + var idx = 0 + while (idx < path.parts.size && idx < parts.size && path.parts[idx] == parts[idx]) { + idx++ } - Relative(List(returns) { ".." } + partList) + val returns = if (idx < parts.size) { + parts.size - idx + } else { + 0 + } + Relative(List(returns) { ".." } + path.parts.subList(idx, path.parts.size)) } } } diff --git a/src/jsTest/kotlin/io/PathTest.kt b/src/jsTest/kotlin/io/PathTest.kt index 174c2b2..9ba8a9e 100644 --- a/src/jsTest/kotlin/io/PathTest.kt +++ b/src/jsTest/kotlin/io/PathTest.kt @@ -90,4 +90,15 @@ class PathTest : FunSpec({ } } } + test("relaitivization works") { + forAll( + row("/a/b", "/a", ".."), + row("/a", "/a/b", "b"), + row("/a/b", "/a/c", "../c"), + ) { a, b, c -> + assertSoftly { + Path.of(a).shouldBeTypeOf().relativize(Path.of(b)) shouldBe Path.of(c) + } + } + } }) -- cgit