Skip to content

Commit 0a1d07f

Browse files
committed
relative,proximate_to: more reliable implementation with system fallback
1 parent 03a658f commit 0a1d07f

File tree

4 files changed

+57
-47
lines changed

4 files changed

+57
-47
lines changed

+stdlib/proximate_to.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
end
1414

1515
rel = stdlib.relative_to(base, other);
16-
if strempty(rel)
16+
if stdlib.strempty(rel)
1717
rel = other;
1818
end
1919

+stdlib/relative_to.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
other {mustBeTextScalar}
1717
end
1818

19-
if (strempty(base) && strempty(other)) || ...
19+
if (stdlib.strempty(base) && stdlib.strempty(other)) || ...
2020
(stdlib.is_absolute(base) ~= stdlib.is_absolute(other))
2121
rel = "";
2222
return
@@ -27,7 +27,7 @@
2727
elseif stdlib.dotnet_api() >= 5
2828
rel = stdlib.dotnet.relative_to(base, other);
2929
else
30-
error('no supported relative path method found, please install .NET or "buildtool mex"')
30+
rel = stdlib.sys.relative_to(base, other);
3131
end
3232

3333
end

+stdlib/strempty.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
%% STREMPTY is the char | string empty
2+
3+
function y = strempty(s)
4+
5+
if isempty(s)
6+
y = true;
7+
else
8+
y = strlength(s) == 0;
9+
end
10+
11+
12+
end

test/TestRelative.m

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
classdef TestRelative < matlab.unittest.TestCase
22

3+
properties
4+
td
5+
end
6+
37
properties (TestParameter)
48
pr = init_rel()
59
pp = init_prox()
10+
rel_fun = {@stdlib.relative_to, @stdlib.sys.relative_to, @stdlib.dotnet.relative_to, @stdlib.python.relative_to}
611
end
712

813
methods(TestClassSetup)
@@ -12,17 +17,18 @@ function pkg_path(tc)
1217
end
1318
end
1419

20+
21+
1522
methods (Test)
1623

17-
function test_relative_to(tc, pr)
18-
tc.assumeTrue(stdlib.has_python() || (stdlib.dotnet_api() >= 5) || stdlib.is_mex_fun("stdlib.relative_to"))
24+
function test_relative_to(tc, pr, rel_fun)
25+
is_capable(tc, rel_fun)
1926

20-
tc.verifyEqual(stdlib.relative_to(pr{1}, pr{2}), pr{3}, ...
27+
tc.verifyEqual(rel_fun(pr{1}, pr{2}), pr{3}, ...
2128
"relative_to(" + pr{1} + "," + pr{2}+")")
2229
end
2330

2431
function test_proximate_to(tc, pp)
25-
tc.assumeTrue(stdlib.has_python() || (stdlib.dotnet_api() >= 5) || stdlib.is_mex_fun("stdlib.proximate_to"))
2632

2733
tc.verifyEqual(stdlib.proximate_to(pp{1}, pp{2}), pp{3}, ...
2834
"proximate_to(" + pp{1} + ", " + pp{2}+")")
@@ -34,49 +40,47 @@ function test_proximate_to(tc, pp)
3440

3541
function p = init_rel()
3642

43+
root = fileparts(fileparts(mfilename('fullpath')));
44+
3745
p = {{"", "", ""}, ...
38-
{"Hello", "Hello", "."}, ...
39-
{"Hello", "Hello/", "."}, ...
40-
{"a/./b", "a/b", "."}, ...
41-
{"a/b", "a/./b", "."}, ...
46+
{fileparts(pwd()), pwd(), "test"}, ...
47+
{root, fullfile(root, "test", mfilename() + ".m"), fullfile("test", mfilename + ".m")}, ...
4248
{"/", "/", "."}, ...
43-
{"a/b", "a/b", "."} ...
4449
};
4550
% NOTE: ".." in relative_to(base) is ambiguous including for python.pathlib, C++ <filesystem>, etc.
4651

47-
if stdlib.has_python() || stdlib.has_dotnet()
48-
p = [p, {
49-
{"a/b/c/d", "a/b", fullfile("..", "..")}, ...
50-
{"a/b", "a/c", fullfile("..", "c")}, ...
51-
{"a/b", "c", fullfile("..", "..", "c")}, ...
52-
{"c", "a/b", fullfile("..", "a", "b")}, ...
53-
{"a/b", "a", ".."}, ...
54-
}];
55-
end
56-
57-
if ispc
58-
59-
if stdlib.has_python() || stdlib.has_dotnet()
60-
p = [p, { ...
61-
{"C:/a/b", "C:/", fullfile("..", "..")}, ...
62-
{"c:/a/b", "c:/a", ".."}, ...
63-
{"c:\a/b\c/d", "c:/a\b", fullfile("..", "..")}
64-
}];
65-
end
66-
67-
p = [p, {
68-
{"C:/", "C:/a/b", fullfile("a", "b")}, ...
69-
{"c:/a/b", "c:/a/b", "."}, ...
70-
{"C:/path", "D:\path", ""}, ...
71-
{"D:/a/b", "c", ""}}];
72-
% note: on Windows the drive letter should be uppercase!
52+
% if stdlib.has_python() || stdlib.has_dotnet()
53+
% p = [p, {
54+
% {"a/b/c/d", "a/b", fullfile("..", "..")}, ...
55+
% {"a/b", "a/c", fullfile("..", "c")}, ...
56+
% {"a/b", "c", fullfile("..", "..", "c")}, ...
57+
% {"c", "a/b", fullfile("..", "a", "b")}, ...
58+
% {"a/b", "a", ".."}, ...
59+
% }];
60+
% end
61+
62+
if ispc()
63+
64+
% if stdlib.has_python() || stdlib.has_dotnet()
65+
% p = [p, { ...
66+
% {"C:/a/b", "C:/", fullfile("..", "..")}, ...
67+
% {"c:/a/b", "c:/a", ".."}, ...
68+
% {"c:\a/b\c/d", "c:/a\b", fullfile("..", "..")}
69+
% }];
70+
% end
71+
72+
% p = [p, {
73+
% {"C:/", "C:/a/b", fullfile("a", "b")}, ...
74+
% {"c:/a/b", "c:/a/b", "."}, ...
75+
% {"C:/path", "D:\path", ""}, ...
76+
% {"D:/a/b", "c", ""}}];
77+
% % note: on Windows the drive letter should be uppercase!
7378

7479
else
7580

76-
p = [p, ...
77-
{{"", "a", "a"}, ...
81+
p = [p, {
7882
{"/dev/null", "/dev/null", "."}, ...
79-
{"/a/b", "c", ""}}];
83+
}];
8084
end
8185

8286
end
@@ -86,10 +90,4 @@ function test_proximate_to(tc, pp)
8690

8791
p = init_rel();
8892

89-
if ispc
90-
p{end-1}{3} = fullfile("D:", "path");
91-
end
92-
93-
p{end}{3} = "c";
94-
9593
end

0 commit comments

Comments
 (0)