Skip to content

Commit d65205d

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

File tree

2 files changed

+140
-91
lines changed

2 files changed

+140
-91
lines changed

test/TestFilePure.m

Lines changed: 5 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
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
1110
in_filename = {"", "/a/b/c", "/a/b/c/", "a/b/c.txt", "a/b/c.txt.gz"}
1211
ref_filename = {"", "c", "", "c.txt", "c.txt.gz"}
1312

14-
p_is_subdir
1513
p_parent
1614

1715
p_join = {{"", "", ""}, ...
@@ -44,8 +42,10 @@
4442
tobj
4543
end
4644

45+
4746
methods (TestParameterDefinition, Static)
48-
function [p_relative_to, p_proximate_to, p_root, p_root_name, p_parent] = init_relative_to(classToTest) %#ok<INUSD>
47+
48+
function [p_root, p_root_name, p_parent] = init_relative_to(classToTest) %#ok<INUSD>
4949

5050
p_root = {{"", ""}, ...
5151
{"a/b", ""}, ...
@@ -77,50 +77,6 @@
7777
{"c:\a/b", "c:\a"}
7878
};
7979

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-
105-
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-
12480
p_parent{12}{2} = "c:/";
12581
p_parent{13}{2} = "c:/";
12682
p_parent{14}{2} = "c:/a";
@@ -133,8 +89,6 @@
13389

13490
p_root_name{4}{2} = "c:";
13591

136-
end % if ispc
137-
13892
end
13993

14094

@@ -149,30 +103,8 @@
149103

150104
end
151105

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-
173106
end
174107

175-
end
176108

177109

178110
methods (TestClassSetup)
@@ -271,23 +203,5 @@ function test_with_suffix(tc)
271203
end
272204

273205

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} + ")")
289-
end
290-
291-
292206
end
293207
end

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)