-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualPathTests.cs
More file actions
123 lines (116 loc) · 4.79 KB
/
VirtualPathTests.cs
File metadata and controls
123 lines (116 loc) · 4.79 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
namespace Ramstack.FileSystem;
[TestFixture]
public class VirtualPathTests
{
[TestCase("", "")]
[TestCase(".", "")]
[TestCase("/", "")]
[TestCase("/.", "")]
[TestCase("file.txt", ".txt")]
[TestCase("/path/to/file.txt", ".txt")]
[TestCase("/path/to/.hidden", ".hidden")]
[TestCase("/path/to/file", "")]
[TestCase("/path.with.dots/to/file.txt", ".txt")]
[TestCase("/path/with.dots/file.", "")]
[TestCase("/path.with.dots/to/.hidden.ext", ".ext")]
[TestCase("file.with.multiple.dots.ext", ".ext")]
[TestCase("/path/to/file.with.multiple.dots.ext", ".ext")]
[TestCase("/.hidden", ".hidden")]
public void GetExtension(string path, string expected)
{
foreach (var p in GetPathVariations(path))
{
Assert.That(VirtualPath.GetExtension(p), Is.EqualTo(expected));
Assert.That(VirtualPath.GetExtension(p.AsSpan()).ToString(), Is.EqualTo(expected));
}
}
[TestCase("", "")]
[TestCase(".", ".")]
[TestCase(".hidden", ".hidden")]
[TestCase("file.txt", "file.txt")]
[TestCase("/path/to/file.txt", "file.txt")]
[TestCase("/path/to/.hidden", ".hidden")]
[TestCase("/path/to/file", "file")]
[TestCase("/path/with.dots/file.txt", "file.txt")]
[TestCase("/path/with.dots/file.", "file.")]
[TestCase("/path/to/file.with.multiple.dots.ext", "file.with.multiple.dots.ext")]
[TestCase("/path/to/.hidden.ext", ".hidden.ext")]
[TestCase("/.hidden", ".hidden")]
[TestCase("/path/to/", "")]
[TestCase("/path/to/directory/", "")]
public void GetFileName(string path, string expected)
{
foreach (var p in GetPathVariations(path))
{
Assert.That(VirtualPath.GetFileName(p), Is.EqualTo(expected));
Assert.That(VirtualPath.GetFileName(p.AsSpan()).ToString(), Is.EqualTo(expected));
}
}
[TestCase("", "")]
[TestCase("/", "")]
[TestCase("/dir", "/")]
[TestCase("/dir/file", "/dir")]
[TestCase("/dir/dir/", "/dir/dir")]
[TestCase("dir/dir", "dir")]
[TestCase("dir/dir/", "dir/dir")]
[TestCase("//", "")]
[TestCase("///", "")]
[TestCase("//dir", "/")]
[TestCase("///dir", "/")]
[TestCase("////dir", "/")]
[TestCase("/dir///dir", "/dir")]
[TestCase("/dir///dir///", "/dir///dir")]
[TestCase("//dir///dir///", "//dir///dir")]
[TestCase("dir///dir", "dir")]
public void GetDirectoryName(string path, string expected)
{
foreach (var p in GetPathVariations(path))
{
if (p.Contains('\\') && expected != "/")
expected = expected.Replace("/", "\\");
Assert.That(VirtualPath.GetDirectoryName(p), Is.EqualTo(expected));
Assert.That(VirtualPath.GetDirectoryName(p.AsSpan()).ToString(), Is.EqualTo(expected));
}
}
[TestCase("/", ExpectedResult = true)]
[TestCase("/a/b/c", ExpectedResult = true)]
[TestCase("/a/ /c", ExpectedResult = true)]
[TestCase("/a/ ", ExpectedResult = true)]
[TestCase("/a/ /c", ExpectedResult = true)]
[TestCase("/a/./b/c", ExpectedResult = false)]
[TestCase("/a/../b/c", ExpectedResult = false)]
[TestCase("/a/./../b/c", ExpectedResult = false)]
[TestCase("/a/", ExpectedResult = false)]
[TestCase("/a//b", ExpectedResult = false)]
[TestCase("a/b", ExpectedResult = false)]
[TestCase("a/b/", ExpectedResult = false)]
[TestCase("/a\\b", ExpectedResult = false)]
[TestCase("", ExpectedResult = false)]
[TestCase(" ", ExpectedResult = false)]
[TestCase(" /", ExpectedResult = false)]
public bool IsNormalized(string path) =>
VirtualPath.IsNormalized(path);
[TestCase("", "/")]
[TestCase(".", "/")]
[TestCase(".", "/")]
[TestCase("/home/", "/home")]
[TestCase("/home/..folder1/.folder2/file", "/home/..folder1/.folder2/file")]
[TestCase("/home/././", "/home")]
[TestCase("/././././/home/user/documents", "/home/user/documents")]
[TestCase("/home/./user/./././/documents", "/home/user/documents")]
[TestCase("/home/../home/user//documents", "/home/user/documents")]
[TestCase("/home/../home/user/../../home/config/documents", "/home/config/documents")]
[TestCase("/home/../home/user/./.././.././home/config/documents", "/home/config/documents")]
[TestCase("..", "/")]
[TestCase("../..", "/")]
[TestCase("../../../", "/")]
[TestCase("/home/../..", "/")]
[TestCase("/home/../../..", "/")]
public void Normalize(string path, string expected)
{
foreach (var p in GetPathVariations(path))
Assert.That(VirtualPath.Normalize(p),Is.EqualTo(expected));
}
private static string[] GetPathVariations(string path) =>
[path, path.Replace('/', '\\')];
}