Skip to content

Commit 60445f2

Browse files
committed
samepath: choose_method
1 parent a7e84f9 commit 60445f2

File tree

6 files changed

+25
-35
lines changed

6 files changed

+25
-35
lines changed

+stdlib/cpu_arch.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,3 @@
1212

1313
end
1414

15-
%!assert(!isempty(cpu_arch()))

+stdlib/get_shell.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,3 @@
1919
end
2020

2121
end
22-
23-
%!assert(ischar(get_shell()))

+stdlib/private/choose_method.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
has = true;
6262

63-
if endsWith(name, "is_char_device")
63+
if endsWith(name, ["is_char_device", "samepath"])
6464
if ispc(), continue, end
6565
end
6666

+stdlib/samepath.m

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,17 @@
1212
%%% Inputs
1313
% * path1, path2: paths to compare
1414

15-
function y = samepath(path1, path2)
15+
function y = samepath(path1, path2, method)
1616
arguments
1717
path1 {mustBeTextScalar}
1818
path2 {mustBeTextScalar}
19+
method (1,:) string = ["python", "java", "sys", "native"]
1920
end
2021

22+
% For this function, Python is over 10x faster than Java
2123

22-
if stdlib.has_python()
23-
% For this function, Python is over 10x faster than Java
24-
y = stdlib.python.samepath(path1, path2);
25-
elseif stdlib.has_java()
26-
y = stdlib.java.samepath(path1, path2);
27-
elseif isunix()
28-
y = stdlib.sys.samepath(path1, path2);
29-
else
30-
% string comparison is not preferred
31-
y = stdlib.native.samepath(path1, path2);
32-
end
24+
fun = choose_method(method, "samepath");
25+
26+
y = fun(path1, path2);
3327

34-
%!assert(samepath(".", "."))
35-
%!assert(samepath(".", "./"))
36-
%!assert(!samepath("not-exist", "not-exist/.."))
28+
end

test/TestSame.m

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
{"..", pwd() + "/.."}, ...
88
{pwd(), pwd() + "/."}}
99

10-
same_fun = {@stdlib.samepath, @stdlib.sys.samepath, @stdlib.java.samepath, @stdlib.python.samepath, @stdlib.native.samepath}
10+
fun = {'sys', 'java', 'python', 'native'}
1111
end
1212

1313
methods(TestClassSetup)
@@ -19,18 +19,25 @@ function pkg_path(tc)
1919

2020
methods(Test)
2121

22-
function test_samepath(tc, p_same, same_fun)
23-
is_capable(tc, same_fun)
24-
25-
tc.verifyTrue(same_fun(p_same{1}, p_same{2}))
22+
function test_samepath(tc, p_same, fun)
23+
tc.assertNotEmpty(which("stdlib." + fun + ".samepath"))
24+
try
25+
y = stdlib.samepath(p_same{:}, fun);
26+
tc.verifyTrue(y)
27+
catch e
28+
tc.verifyEqual(e.identifier, 'stdlib:choose_method:NameError', e.message)
29+
end
2630
end
2731

28-
function test_samepath_notexist(tc, same_fun)
29-
is_capable(tc, same_fun)
3032

31-
tc.verifyFalse(same_fun("", ""))
33+
function test_samepath_notexist(tc, fun)
3234
t = tempname();
33-
tc.verifyFalse(same_fun(t, t))
35+
try
36+
tc.verifyFalse(stdlib.samepath("", "", fun))
37+
tc.verifyFalse(stdlib.samepath(t, t, fun))
38+
catch e
39+
tc.verifyEqual(e.identifier, 'stdlib:choose_method:NameError', e.message)
40+
end
3441
end
3542

3643
end

test/is_capable.m

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,7 @@ function is_capable(tc, f)
1818

1919
elseif contains(n, "python")
2020

21-
tc.assumeTrue(stdlib.has_python(), "Python not available")
22-
23-
elseif contains(n, ".sys.")
24-
25-
if endsWith(n, "samepath")
26-
tc.assumeTrue(isunix(), "unix only function")
27-
end
21+
tc.assumeTrue(stdlib.has_python(), "Python not available")
2822

2923
end
3024

0 commit comments

Comments
 (0)