summaryrefslogtreecommitdiff
path: root/src/jsTest/kotlin/io/PathTest.kt
blob: 9ba8a9e0a36b78ecf4fe357371be2ed2d15368ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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") {
		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") {
		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))
			}
		}
	}
	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<Path.Absolute>().relativize(Path.of(b)) shouldBe Path.of(c)
			}
		}
	}
})