Skip to content

Commit

Permalink
absolute: don't normalize, add test cases like Ffilesystem'
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Nov 25, 2024
1 parent 8a4dd4f commit 129c90c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
16 changes: 14 additions & 2 deletions +stdlib/absolute.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
use_java (1,1) logical = false
end

cwd = stdlib.posix(pwd());

if (isempty(p) || strlength(p) == 0) && (isempty(base) || strlength(base) == 0)
c = cwd;
return
end

if expand_tilde
c = stdlib.expanduser(p, use_java);
else
Expand All @@ -36,9 +43,14 @@
% .getAbsolutePath(), .toAbsolutePath()
% default is Documents/Matlab, which is probably not wanted.
if isempty(base) || strlength(base) == 0
c = stdlib.join(pwd, c);
c = cwd + "/" + c;
else
c = stdlib.join(stdlib.absolute(base, string.empty, expand_tilde, use_java), c);
d = stdlib.absolute(base, string.empty, expand_tilde, use_java);
if isempty(c) || strlength(c) == 0
c = d;
else
c = d + "/" + c;
end
end
end

Expand Down
31 changes: 26 additions & 5 deletions test/TestResolve.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
classdef TestResolve < matlab.unittest.TestCase

methods(TestClassSetup)

function setup_path(tc)
import matlab.unittest.fixtures.PathFixture
cwd = fileparts(mfilename("fullpath"));
top = fullfile(cwd, "..");
tc.applyFixture(PathFixture(top))
end

end


methods(Test)


Expand All @@ -12,11 +24,20 @@ function test_absolute(tc)
td = tc.applyFixture(TemporaryFolderFixture).Folder;
tc.applyFixture(CurrentFolderFixture(td))

tc.verifyEqual(stdlib.absolute(""), stdlib.posix(td))
tc.verifyEqual(stdlib.absolute("",""), stdlib.posix(td))
tc.verifyEqual(stdlib.absolute("hi"), stdlib.join(td, "hi"))
tc.verifyEqual(stdlib.absolute("", "hi"), stdlib.join(td, "hi"))
tc.verifyEqual(stdlib.absolute("there", "hi"), stdlib.join(td, "hi/there"))
td = stdlib.posix(td);

tc.verifyEqual(stdlib.absolute(""), td)
tc.verifyEqual(stdlib.absolute("",""), td)

r = td + "/hi";
tc.verifyEqual(stdlib.absolute("hi"), r)
tc.verifyEqual(stdlib.absolute("", "hi"), r)
tc.verifyEqual(stdlib.absolute("hi", ""), r)

tc.verifyEqual(stdlib.absolute("./hi"), td + "/./hi")
tc.verifyEqual(stdlib.absolute("../hi"), td + "/../hi")

tc.verifyEqual(stdlib.absolute("there", "hi"), td + "/hi/there")

end

Expand Down

0 comments on commit 129c90c

Please sign in to comment.