From 129c90ca393c0d9beaa1c619810ef6954df08a41 Mon Sep 17 00:00:00 2001 From: scivision Date: Mon, 25 Nov 2024 12:26:43 -0500 Subject: [PATCH] absolute: don't normalize, add test cases like Ffilesystem' --- +stdlib/absolute.m | 16 ++++++++++++++-- test/TestResolve.m | 31 ++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/+stdlib/absolute.m b/+stdlib/absolute.m index 5034af2..56bcafb 100644 --- a/+stdlib/absolute.m +++ b/+stdlib/absolute.m @@ -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 @@ -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 diff --git a/test/TestResolve.m b/test/TestResolve.m index ae99d7a..1f729a5 100644 --- a/test/TestResolve.m +++ b/test/TestResolve.m @@ -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) @@ -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