|
1 | 1 | package dotty.tools.io
|
2 | 2 |
|
3 |
| -import org.junit.Assert._ |
4 | 3 | import org.junit.Test
|
5 | 4 |
|
6 | 5 | class PathTest {
|
7 | 6 | // Ref https://github.com/lampepfl/dotty/issues/11644#issuecomment-792457275
|
8 | 7 | @Test def parent(): Unit = {
|
9 |
| - assertEquals(Directory(".."), Path("").parent) |
10 |
| - assertEquals(Directory(".."), Path(".").parent) |
11 |
| - assertEquals(Directory("..") / "..", Path("..").parent) |
12 |
| - assertEquals(Directory("."), Path("foo.txt").parent) |
13 |
| - assertEquals(Directory("."), (Path(".") / "foo.txt").parent) |
14 |
| - assertEquals(Directory("."), (Path("bar") / ".").parent) |
15 |
| - assertEquals(Directory("."), (Path("foo") / ".." / "." / "bar").parent) |
16 |
| - assertEquals(Directory("baz") / "bar", (Path(".") / "baz" / "bar" / "foo.txt").parent) |
| 8 | + testParent(Path(""), Directory("..")) |
| 9 | + testParent(Path("."), Directory("..")) |
| 10 | + testParent(Path(".") / ".", Directory("..")) |
| 11 | + testParent(Path(".."), Directory("..") / "..") |
| 12 | + testParent(Path("..") / ".", Directory("..") / "..") |
| 13 | + testParent(Path("..") / "..", Directory("..") / ".." / "..") |
| 14 | + testParent(Path(".") / "..", |
| 15 | + Directory("..") / "..", |
| 16 | + Directory(".") / ".." / "..") |
| 17 | + |
| 18 | + testParent(Path("foo") / ".", Directory(".")) |
| 19 | + testParent(Path("foo") / ".." / "bar", Directory("foo") / "..") |
| 20 | + testParent(Path("foo") / ".." / "." / "bar", Directory("foo") / ".." / ".") |
| 21 | + |
| 22 | + testParent(Path("foo.txt"), Directory(".")) |
| 23 | + testParent(Path(".") / "foo.txt", Directory(".")) |
| 24 | + testParent(Path(".") / "baz" / "bar" / "foo.txt", |
| 25 | + Directory(".") / "baz" / "bar", |
| 26 | + Directory("baz") / "bar") |
17 | 27 |
|
18 | 28 | for (root <- Path.roots) {
|
19 |
| - val rootDir = Directory(root.path) |
20 |
| - assertEquals(rootDir, root.parent) |
21 |
| - assertEquals(rootDir, (root / "foo.txt").parent) |
22 |
| - assertEquals(rootDir / "baz" / "bar", (root / "baz" / "bar" / "foo.txt").parent) |
| 29 | + testParent(root, root) |
| 30 | + testParent(root / ".", root) |
| 31 | + testParent(root / "..", root / ".." / "..") |
| 32 | + testParent(root / "foo" / ".", root) |
| 33 | + testParent(root / "foo.txt", root) |
| 34 | + testParent(root / "baz" / "bar" / "foo.txt", root / "baz" / "bar") |
| 35 | + testParent(root / "foo" / "bar" / "..", root / "foo" / "bar" / ".." / "..") |
23 | 36 | }
|
24 | 37 | }
|
| 38 | + |
| 39 | + /** The parent of a path may have multiple valid non-canonical representations. |
| 40 | + * Here we test that the parent of the specified path is among a curated list |
| 41 | + * of representations we consider to be valid. |
| 42 | + */ |
| 43 | + private def testParent(path: Path, expected: Path*): Unit = { |
| 44 | + val actual = path.parent |
| 45 | + val some = if (expected.length > 1) " one of" else "" |
| 46 | + assert(expected.contains(actual), |
| 47 | + s"""expected$some: ${expected.mkString("<",">, <",">")} but was: <$actual>""") |
| 48 | + } |
25 | 49 | }
|
0 commit comments