Skip to content

Commit

Permalink
join: add tests, correct behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Oct 7, 2024
1 parent 0516e5a commit 372a1f6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
34 changes: 32 additions & 2 deletions +stdlib/join.m
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
function p = join(a, b)
function p = join(a, b, use_java)
%% JOIN: join two paths with posix file separator
arguments
a (1,1) string
b (1,1) string
use_java (1,1) logical = false
end

p = stdlib.posix(fullfile(a, b));
if a == "" && b == ""
p = "";
return
end

if a == ""
p = b;
return
end

if b == ""
p = a;
return
end


if use_java

p = stdlib.posix(java.io.File(stdlib.normalize(a, true)).toPath().resolve(stdlib.normalize(b, true)));

else

if stdlib.is_absolute(b)
p = b;
return
end

p = stdlib.normalize(a) + "/" + stdlib.normalize(b);

end

end
1 change: 1 addition & 0 deletions +stdlib/parent.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
% java is about 10x slower than intrinsic
p = java.io.File(p).getParent();
else
p = stdlib.posix(p);
% drop duplicated slashes in the parent path
p = regexprep(p, "//+", "/");

Expand Down
9 changes: 9 additions & 0 deletions test/TestFilePure.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
in_parent
ref_parent

in_join = {"", "a", "", "a/b/", "/", "", "a", "a//", "a/b/../", "a/b", "ab/cd"}
other_join = {"", "", "b", "c/", "", "/", "b//", "b//", "c/d/../", "c/d", "/ef"}
ref_join = {"", "a", "b", "a/b/c", "/", "/", "a/b", "a/b", "a/c", "a/b/c/d", "/ef"}

in_suffix = {"", "/foo/bar/baz", "/foo/bar/baz/", "foo/bar/baz.txt", "foo/bar/baz.txt.gz", ".stat", ".stat.txt"}
ref_suffix = {"", "", "", ".txt", ".gz", ".stat", ".txt"}

Expand Down Expand Up @@ -166,6 +170,11 @@ function test_posix(tc)
end


function test_join(tc, in_join, other_join, ref_join)
tc.verifyEqual(stdlib.join(in_join, other_join), ref_join)
end


function test_filename(tc, in_filename, ref_filename)
tc.verifyEqual(stdlib.filename(in_filename), string(ref_filename))
end
Expand Down

0 comments on commit 372a1f6

Please sign in to comment.