Skip to content

Commit 14366eb

Browse files
committed
relative, proximate: no coerce
1 parent adc27b1 commit 14366eb

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

+stdlib/proximate_to.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
% * rel: relative path from base to other
88

99
function rel = proximate_to(base, other)
10-
arguments
11-
base (1,1) string
12-
other (1,1) string
13-
end
1410

1511
rel = stdlib.relative_to(base, other);
1612
if stdlib.strempty(rel)
1713
rel = other;
14+
if isstring(base)
15+
rel = string(rel);
16+
end
1817
end
1918

2019
end

+stdlib/relative_to.m

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@
1111
% https://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html#relativize-java.nio.file.Path-
1212

1313
function rel = relative_to(base, target)
14-
arguments
15-
base (1,1) string
16-
target (1,1) string
17-
end
14+
% arguments
15+
% base (1,1) string
16+
% target (1,1) string
17+
% end
1818

1919
% matlab.io.internal.filesystem.relativepath only works on Windows (!) and only
2020
% then with / filesep.
2121

2222
if stdlib.strempty(base) || stdlib.strempty(target)
23-
rel = "";
23+
if isstring(base) || isstring(target)
24+
rel = "";
25+
else
26+
rel = '';
27+
end
2428
return
2529
end
2630

@@ -35,10 +39,17 @@
3539
n = n + 1;
3640
end
3741

38-
rel = join(tp(n+1:end), filesep);
42+
if n+1 <= length(tp)
43+
rel = join(tp(n+1:end), filesep);
44+
if iscell(rel)
45+
rel = rel{1};
46+
end
47+
else
48+
rel = '.';
49+
end
3950

40-
if stdlib.strempty(rel)
41-
rel = ".";
51+
if isstring(base) || isstring(target)
52+
rel = string(rel);
4253
end
4354

4455
end

test/TestRelative.m

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
classdef (SharedTestFixtures={ matlab.unittest.fixtures.PathFixture(fileparts(fileparts(mfilename('fullpath'))))}, ...
2-
TestTags = {'R2019b', 'pure'}) ...
2+
TestTags = {'R2017b', 'pure'}) ...
33
TestRelative < matlab.unittest.TestCase
44

55

@@ -12,14 +12,18 @@
1212

1313
function test_relative_to(tc, pr)
1414
r = stdlib.relative_to(pr{1}, pr{2});
15-
1615
tc.verifyEqual(r, pr{3}, "relative_to(" + pr{1} + "," + pr{2}+")")
16+
17+
r = stdlib.relative_to(string(pr{1}), pr{2});
18+
tc.verifyEqual(r, string(pr{3}))
1719
end
1820

1921
function test_proximate_to(tc, pr)
2022
r = stdlib.proximate_to(pr{1}, pr{2});
21-
2223
tc.verifyEqual(r, pr{3}, "proximate_to(" + pr{1} + ", " + pr{2}+")")
24+
25+
r = stdlib.proximate_to(string(pr{1}), pr{2});
26+
tc.verifyEqual(r, string(pr{3}))
2327
end
2428

2529
end
@@ -28,13 +32,18 @@ function test_proximate_to(tc, pr)
2832

2933
function pr = init_rel()
3034

35+
pr = {{'', '', ''}, ...
36+
{pwd(), pwd(), '.'}, ...
37+
{fileparts(pwd()), pwd(), 'test'}
38+
};
39+
40+
3141
root = fileparts(fileparts(mfilename('fullpath')));
42+
if ~isempty(root)
43+
pr{end+1} = {root, [root, '/test/', mfilename(), '.m'], ['test/', mfilename, '.m']};
44+
end
45+
3246

33-
pr = {{"", "", ""}, ...
34-
{pwd(), pwd(), "."}, ...
35-
{fileparts(pwd()), pwd(), "test"}, ...
36-
{root, fullfile(root, "test", mfilename() + ".m"), fullfile("test", mfilename + ".m")}
37-
};
3847
% NOTE: ".." in relative_to(base) is ambiguous including for python.pathlib, C++ <filesystem>, etc.
3948

4049
% if stdlib.has_python() || stdlib.has_dotnet()
@@ -67,8 +76,8 @@ function test_proximate_to(tc, pr)
6776
else
6877

6978
pr = [pr, {
70-
{"/", "/", "."}, ...
71-
{"/dev/null", "/dev/null", "."}, ...
79+
{'/', '/', '.'}, ...
80+
{'/dev/null', '/dev/null', '.'}, ...
7281
}];
7382
end
7483
end

0 commit comments

Comments
 (0)