Skip to content

Commit 6c09b79

Browse files
committed
TestRelative for clarity
1 parent 6f4b188 commit 6c09b79

File tree

2 files changed

+185
-127
lines changed

2 files changed

+185
-127
lines changed

test/TestFilePure.m

Lines changed: 50 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
classdef TestFilePure < matlab.unittest.TestCase
22

33
properties (ClassSetupParameter)
4-
classToTest = {"TestFilePure"};
4+
classToTest = {"TestFilePure"};
55
end
66

77
properties (TestParameter)
8-
p_relative_to
9-
p_proximate_to
8+
109
p_is_absolute
11-
in_filename = {"", "/a/b/c", "/a/b/c/", "a/b/c.txt", "a/b/c.txt.gz"}
12-
ref_filename = {"", "c", "", "c.txt", "c.txt.gz"}
1310

14-
p_is_subdir
11+
12+
p_filename = {
13+
{"", ""}, ...
14+
{"/a/b/c", "c"}, ...
15+
{"/a/b/c/", ""}, ...
16+
{"a/b/c.txt", "c.txt"}, ...
17+
{"a/b/c.txt.gz", "c.txt.gz"}
18+
};
19+
1520
p_parent
1621

22+
p_stem ={{"/a/b/c", "c"}, {"/a/b/c/", ""}, {"a/b/c/", ""}, {"a/b/c.txt", "c"}, {"a/b/c.txt.gz", "c.txt"}}
23+
1724
p_join = {{"", "", ""}, ...
1825
{"a", "", "a"}, ...
1926
{"", "a", "a"}, ...
@@ -26,15 +33,31 @@
2633
{"a/b", "..", "a/b/.."}, ...
2734
{"a/b", "c/d", "a/b/c/d"}, ...
2835
{"ab/cd", "/ef", "/ef"} ...
29-
};
36+
}
3037

31-
in_suffix = {"", "/a/b/c", "/a/b/c/", "a/b/c.txt", "a/b/c.txt.gz", ".stat", ".stat.txt"}
32-
ref_suffix = {"", "", "", ".txt", ".gz", ".stat", ".txt"}
38+
p_suffix = {{"", ""}, {"/a/b/c", ""}, {"/a/b/c/", ""}, {"a/b/c.txt", ".txt"}, {"a/b/c.txt.gz", ".gz"}, {".stat", ".stat"}, {".stat.txt", ".txt"}}
3339

34-
in_norm = {"", "a/..", "//a/b/", "/a/b/", "a/b/", "a/../c", "a/b/../c", "a/b/../../c", "a/b/../../c/..", ...
35-
"a/b/../../c/../..", "a////b", ".a", "..a", "a.", "a..", "./a/.", "../a"}
36-
ref_norm = {".", ".", "/a/b", "/a/b", "a/b", "c", "a/c", "c", ".", ...
37-
"..", "a/b", ".a", "..a", "a.", "a..", "a", "../a"}
40+
p_with_suffix = {{"foo.h5", ".nc", "foo.nc"}, {"c", "", "c"}, {"c.nc", "", "c"}, {"", ".nc", ".nc"}, {"a//b///c///", ".h5", "a/b/c/.h5"}}
41+
42+
p_norm = {
43+
{"", "."}, ...
44+
{"a/..", "."}, ...
45+
{"//a/b/", "/a/b"}, ...
46+
{"/a/b/", "/a/b"}, ...
47+
{"a/b/", "a/b"}, ...
48+
{"a/../c", "c"}, ...
49+
{"a/b/../c", "a/c"}, ...
50+
{"a/b/../../c", "c"}, ...
51+
{"a/b/../../c/..", "."}, ...
52+
{"a/b/../../c/../..", ".."}, ...
53+
{"a////b", "a/b"}, ...
54+
{".a", ".a"}, ...
55+
{"..a", "..a"}, ...
56+
{"a.", "a."}, ...
57+
{"a..", "a.."}, ...
58+
{"./a/.", "a"}, ...
59+
{"../a", "../a"}
60+
};
3861

3962
p_root
4063
p_root_name
@@ -44,8 +67,10 @@
4467
tobj
4568
end
4669

70+
4771
methods (TestParameterDefinition, Static)
48-
function [p_relative_to, p_proximate_to, p_root, p_root_name, p_parent] = init_relative_to(classToTest) %#ok<INUSD>
72+
73+
function [p_root, p_root_name, p_parent] = init_relative_to(classToTest) %#ok<INUSD>
4974

5075
p_root = {{"", ""}, ...
5176
{"a/b", ""}, ...
@@ -77,50 +102,7 @@
77102
{"c:\a/b", "c:\a"}
78103
};
79104

80-
p_relative_to = {{"", "", "."}, ...
81-
{"Hello", "Hello", "."}, ...
82-
{"Hello", "Hello/", "."}, ...
83-
{"a/./b", "a/b", "."}, ...
84-
{"a/b", "a/./b", "."}, ...
85-
{"./this/one", "./this/two", "../two"}, ...
86-
{"/path/same", "/path/same/hi/..", "hi/.."}, ...
87-
{"", "/", ""}, ...
88-
{"/", "", ""}, ...
89-
{"/", "/", "."}, ...
90-
{"/dev/null", "/dev/null", "."}, ...
91-
{"/a/b", "c", ""}, ...
92-
{"c", "/a/b", ""}, ...
93-
{"/a/b", "/a/b", "."}, ...
94-
{"/a/b", "/a", ".."}, ...
95-
{"/a/b/c/d", "/a/b", "../.."}, ...
96-
{"this/one", "this/two", "../two"}};
97-
% NOTE: ".." in relative_to(base) is ambiguous including for python.pathlib, C++ <filesystem>, etc.
98-
99-
p_proximate_to = p_relative_to;
100-
101-
p_proximate_to{6}{3} = "/";
102-
p_proximate_to{10}{3} = "c";
103-
p_proximate_to{11}{3} = "/a/b";
104-
105105
if ispc
106-
107-
p_relative_to = [p_relative_to, ...
108-
{{"c:\a\b", "c:/", "../.."}, ...
109-
{"c:\", "c:/a/b", "a/b"}, ...
110-
{"c:/a/b", "c:/a/b", "."}, ...
111-
{"c:/a/b", "c:/a", ".."}, ...
112-
{"c:\a/b\c/d", "c:/a\b", "../.."}, ...
113-
{"c:/path", "d:/path", ""}}];
114-
115-
p_proximate_to = p_relative_to;
116-
% NOTE: ".." in proximate_to(base) is ambiguous including for python.pathlib, C++ <filesystem>, etc
117-
118-
p_proximate_to{8}{3} = "/";
119-
p_proximate_to{12}{3} = "c";
120-
p_proximate_to{13}{3} = "/a/b";
121-
122-
p_proximate_to{end}{3} = "d:/path";
123-
124106
p_parent{12}{2} = "c:/";
125107
p_parent{13}{2} = "c:/";
126108
p_parent{14}{2} = "c:/a";
@@ -133,7 +115,7 @@
133115

134116
p_root_name{4}{2} = "c:";
135117

136-
end % if ispc
118+
end
137119

138120
end
139121

@@ -149,30 +131,8 @@
149131

150132
end
151133

152-
153-
function [p_is_subdir] = init_is_subdir(classToTest) %#ok<INUSD>
154-
155-
p_is_subdir = {
156-
{"a/b", "a/b", false}, ...
157-
{"a//b/c", "a/b", false}, ...
158-
{"a/b", "a//b", false}, ...
159-
{"a/./b/c", "a/b", false}, ...
160-
{"a/b/c", "a/./b", true}, ...
161-
{"a/b", "a/b/", false}, ...
162-
{"a/b", "a", true}, ...
163-
{"a/.c", "a", true}
164-
};
165-
% NOTE: ".." in is_subdir (either argument) is ambiguous
166-
167-
if ispc
168-
p_is_subdir{end+1} = {"c:\", "c:/", false};
169-
else
170-
p_is_subdir{end+1} = {"/", "/", false};
171-
end
172-
173134
end
174135

175-
end
176136

177137

178138
methods (TestClassSetup)
@@ -211,32 +171,22 @@ function test_join(tc, p_join)
211171
end
212172

213173

214-
function test_filename(tc, in_filename, ref_filename)
215-
tc.verifyEqual(stdlib.filename(in_filename), string(ref_filename))
174+
function test_filename(tc, p_filename)
175+
tc.verifyEqual(stdlib.filename(p_filename{1}), string(p_filename{2}))
216176
end
217177

218178

219179
function test_parent(tc, p_parent)
220180
tc.verifyEqual(stdlib.parent(p_parent{1}), p_parent{2}, p_parent{1})
221181
end
222182

223-
function test_suffix(tc, in_suffix, ref_suffix)
224-
tc.verifyEqual(stdlib.suffix(in_suffix), string(ref_suffix))
183+
function test_suffix(tc, p_suffix)
184+
tc.verifyEqual(stdlib.suffix(p_suffix{1}), string(p_suffix{2}))
225185
end
226186

227187

228-
function test_stem(tc)
229-
230-
tc.verifyEqual(stdlib.stem(""), "")
231-
232-
tc.verifyEqual(stdlib.stem("/a/b/c"), "c")
233-
tc.verifyEqual(stdlib.stem("/a/b/c/"), "")
234-
235-
tc.verifyEqual(stdlib.stem("a/b/c/"), "")
236-
237-
tc.verifyEqual(stdlib.stem("a/b/c.txt"), "c")
238-
tc.verifyEqual(stdlib.stem("a/b/c.txt.gz"), "c.txt")
239-
188+
function test_stem(tc, p_stem)
189+
tc.verifyEqual(stdlib.stem(p_stem{1}), p_stem{2})
240190
end
241191

242192

@@ -245,9 +195,9 @@ function test_is_absolute(tc, p_is_absolute)
245195
end
246196

247197

248-
function test_normalize(tc, in_norm, ref_norm)
198+
function test_normalize(tc, p_norm)
249199

250-
tc.verifyEqual(stdlib.normalize(in_norm), ref_norm)
200+
tc.verifyEqual(stdlib.normalize(p_norm{1}), p_norm{2})
251201

252202
end
253203

@@ -257,35 +207,8 @@ function test_root(tc, p_root)
257207
end
258208

259209

260-
function test_with_suffix(tc)
261-
262-
tc.verifyEqual(stdlib.with_suffix("", ""), "")
263-
264-
tc.verifyEqual(stdlib.with_suffix("foo.h5", ".nc"), "foo.nc")
265-
266-
tc.verifyEqual(stdlib.with_suffix("c", ""), "c")
267-
tc.verifyEqual(stdlib.with_suffix("c.nc", ""), "c")
268-
tc.verifyEqual(stdlib.with_suffix("", ".nc"), ".nc")
269-
270-
tc.verifyEqual(stdlib.with_suffix("a//b///c///", ".h5"), "a/b/c/.h5")
271-
end
272-
273-
274-
function test_relative_to(tc, p_relative_to)
275-
tc.assumeTrue(stdlib.has_java)
276-
tc.verifyEqual(stdlib.relative_to(p_relative_to{1}, p_relative_to{2}), p_relative_to{3}, "relative_to(" + p_relative_to{1} + "," + p_relative_to{2}+")")
277-
end
278-
279-
280-
function test_proximate_to(tc, p_proximate_to)
281-
tc.assumeTrue(stdlib.has_java)
282-
tc.verifyEqual(stdlib.proximate_to(p_proximate_to{1}, p_proximate_to{2}), p_proximate_to{3}, "proximate_to(" + p_proximate_to{1} + "," + p_proximate_to{2}+")")
283-
end
284-
285-
286-
function test_is_subdir(tc, p_is_subdir)
287-
tc.assumeTrue(stdlib.has_java)
288-
tc.verifyEqual(stdlib.is_subdir(p_is_subdir{1}, p_is_subdir{2}), p_is_subdir{3}, "subdir(" + p_is_subdir{1} + "," + p_is_subdir{2} + ")")
210+
function test_with_suffix(tc, p_with_suffix)
211+
tc.verifyEqual(stdlib.with_suffix(p_with_suffix{1}, p_with_suffix{2}), p_with_suffix{3})
289212
end
290213

291214

test/TestRelative.m

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
classdef TestRelative < matlab.unittest.TestCase
2+
3+
properties (ClassSetupParameter)
4+
classToTest = {"TestRelative"};
5+
end
6+
7+
properties (TestParameter)
8+
p_relative_to
9+
p_proximate_to
10+
p_is_subdir
11+
12+
end
13+
14+
properties
15+
tobj
16+
end
17+
18+
19+
methods (TestParameterDefinition, Static)
20+
21+
function [p_relative_to, p_proximate_to] = init_relative_to(classToTest) %#ok<INUSD>
22+
23+
p_relative_to = {{"", "", "."}, ...
24+
{"Hello", "Hello", "."}, ...
25+
{"Hello", "Hello/", "."}, ...
26+
{"a/./b", "a/b", "."}, ...
27+
{"a/b", "a/./b", "."}, ...
28+
{"./this/one", "./this/two", "../two"}, ...
29+
{"/path/same", "/path/same/hi/..", "hi/.."}, ...
30+
{"", "/", ""}, ...
31+
{"/", "", ""}, ...
32+
{"/", "/", "."}, ...
33+
{"/dev/null", "/dev/null", "."}, ...
34+
{"/a/b", "c", ""}, ...
35+
{"c", "/a/b", ""}, ...
36+
{"/a/b", "/a/b", "."}, ...
37+
{"/a/b", "/a", ".."}, ...
38+
{"/a/b/c/d", "/a/b", "../.."}, ...
39+
{"this/one", "this/two", "../two"}};
40+
% NOTE: ".." in relative_to(base) is ambiguous including for python.pathlib, C++ <filesystem>, etc.
41+
42+
p_proximate_to = p_relative_to;
43+
44+
p_proximate_to{6}{3} = "/";
45+
p_proximate_to{10}{3} = "c";
46+
p_proximate_to{11}{3} = "/a/b";
47+
48+
if ispc
49+
50+
p_relative_to = [p_relative_to, ...
51+
{{"c:\a\b", "c:/", "../.."}, ...
52+
{"c:\", "c:/a/b", "a/b"}, ...
53+
{"c:/a/b", "c:/a/b", "."}, ...
54+
{"c:/a/b", "c:/a", ".."}, ...
55+
{"c:\a/b\c/d", "c:/a\b", "../.."}, ...
56+
{"c:/path", "d:/path", ""}}];
57+
58+
p_proximate_to = p_relative_to;
59+
% NOTE: ".." in proximate_to(base) is ambiguous including for python.pathlib, C++ <filesystem>, etc
60+
61+
p_proximate_to{8}{3} = "/";
62+
p_proximate_to{12}{3} = "c";
63+
p_proximate_to{13}{3} = "/a/b";
64+
65+
p_proximate_to{end}{3} = "d:/path";
66+
end
67+
68+
end
69+
70+
71+
function [p_is_subdir] = init_is_subdir(classToTest) %#ok<INUSD>
72+
73+
p_is_subdir = {
74+
{"a/b", "a/b", false}, ...
75+
{"a//b/c", "a/b", true}, ...
76+
{"a/b", "a//b", false}, ...
77+
{"a/./b/c", "a/b", false}, ...
78+
{"a/b/c", "a/./b", false}, ...
79+
{"a/b", "a/b/", false}, ...
80+
{"a/b", "a", true}, ...
81+
{"a/.c", "a", true}
82+
};
83+
% NOTE: ".." in is_subdir (either argument) is ambiguous
84+
85+
if ispc
86+
p_is_subdir{end+1} = {"c:\", "c:/", false};
87+
else
88+
p_is_subdir{end+1} = {"/", "/", false};
89+
end
90+
91+
end
92+
93+
end
94+
95+
96+
methods (TestClassSetup)
97+
98+
function classSetup(tc, classToTest)
99+
constructor = str2func(classToTest);
100+
tc.tobj = constructor();
101+
end
102+
103+
function setup_path(tc)
104+
import matlab.unittest.fixtures.PathFixture
105+
cwd = fileparts(mfilename("fullpath"));
106+
top = fullfile(cwd, "..");
107+
tc.applyFixture(PathFixture(top))
108+
end
109+
110+
end
111+
112+
113+
methods (Test, ParameterCombination = "sequential")
114+
115+
116+
function test_relative_to(tc, p_relative_to)
117+
tc.assumeTrue(stdlib.has_java)
118+
tc.verifyEqual(stdlib.relative_to(p_relative_to{1}, p_relative_to{2}), p_relative_to{3}, "relative_to(" + p_relative_to{1} + "," + p_relative_to{2}+")")
119+
end
120+
121+
122+
function test_proximate_to(tc, p_proximate_to)
123+
tc.assumeTrue(stdlib.has_java)
124+
tc.verifyEqual(stdlib.proximate_to(p_proximate_to{1}, p_proximate_to{2}), p_proximate_to{3}, "proximate_to(" + p_proximate_to{1} + "," + p_proximate_to{2}+")")
125+
end
126+
127+
128+
function test_is_subdir(tc, p_is_subdir)
129+
tc.assumeTrue(stdlib.has_java)
130+
tc.verifyEqual(stdlib.is_subdir(p_is_subdir{1}, p_is_subdir{2}), p_is_subdir{3}, "subdir(" + p_is_subdir{1} + "," + p_is_subdir{2} + ")")
131+
end
132+
133+
end
134+
135+
end

0 commit comments

Comments
 (0)