Skip to content

Commit

Permalink
TestRelative for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Dec 9, 2024
1 parent 6f4b188 commit 6c09b79
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 127 deletions.
177 changes: 50 additions & 127 deletions test/TestFilePure.m
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
classdef TestFilePure < matlab.unittest.TestCase

properties (ClassSetupParameter)
classToTest = {"TestFilePure"};
classToTest = {"TestFilePure"};
end

properties (TestParameter)
p_relative_to
p_proximate_to

p_is_absolute
in_filename = {"", "/a/b/c", "/a/b/c/", "a/b/c.txt", "a/b/c.txt.gz"}
ref_filename = {"", "c", "", "c.txt", "c.txt.gz"}

p_is_subdir

p_filename = {
{"", ""}, ...
{"/a/b/c", "c"}, ...
{"/a/b/c/", ""}, ...
{"a/b/c.txt", "c.txt"}, ...
{"a/b/c.txt.gz", "c.txt.gz"}
};

p_parent

p_stem ={{"/a/b/c", "c"}, {"/a/b/c/", ""}, {"a/b/c/", ""}, {"a/b/c.txt", "c"}, {"a/b/c.txt.gz", "c.txt"}}

p_join = {{"", "", ""}, ...
{"a", "", "a"}, ...
{"", "a", "a"}, ...
Expand All @@ -26,15 +33,31 @@
{"a/b", "..", "a/b/.."}, ...
{"a/b", "c/d", "a/b/c/d"}, ...
{"ab/cd", "/ef", "/ef"} ...
};
}

in_suffix = {"", "/a/b/c", "/a/b/c/", "a/b/c.txt", "a/b/c.txt.gz", ".stat", ".stat.txt"}
ref_suffix = {"", "", "", ".txt", ".gz", ".stat", ".txt"}
p_suffix = {{"", ""}, {"/a/b/c", ""}, {"/a/b/c/", ""}, {"a/b/c.txt", ".txt"}, {"a/b/c.txt.gz", ".gz"}, {".stat", ".stat"}, {".stat.txt", ".txt"}}

in_norm = {"", "a/..", "//a/b/", "/a/b/", "a/b/", "a/../c", "a/b/../c", "a/b/../../c", "a/b/../../c/..", ...
"a/b/../../c/../..", "a////b", ".a", "..a", "a.", "a..", "./a/.", "../a"}
ref_norm = {".", ".", "/a/b", "/a/b", "a/b", "c", "a/c", "c", ".", ...
"..", "a/b", ".a", "..a", "a.", "a..", "a", "../a"}
p_with_suffix = {{"foo.h5", ".nc", "foo.nc"}, {"c", "", "c"}, {"c.nc", "", "c"}, {"", ".nc", ".nc"}, {"a//b///c///", ".h5", "a/b/c/.h5"}}

p_norm = {
{"", "."}, ...
{"a/..", "."}, ...
{"//a/b/", "/a/b"}, ...
{"/a/b/", "/a/b"}, ...
{"a/b/", "a/b"}, ...
{"a/../c", "c"}, ...
{"a/b/../c", "a/c"}, ...
{"a/b/../../c", "c"}, ...
{"a/b/../../c/..", "."}, ...
{"a/b/../../c/../..", ".."}, ...
{"a////b", "a/b"}, ...
{".a", ".a"}, ...
{"..a", "..a"}, ...
{"a.", "a."}, ...
{"a..", "a.."}, ...
{"./a/.", "a"}, ...
{"../a", "../a"}
};

p_root
p_root_name
Expand All @@ -44,8 +67,10 @@
tobj
end


methods (TestParameterDefinition, Static)
function [p_relative_to, p_proximate_to, p_root, p_root_name, p_parent] = init_relative_to(classToTest) %#ok<INUSD>

function [p_root, p_root_name, p_parent] = init_relative_to(classToTest) %#ok<INUSD>

p_root = {{"", ""}, ...
{"a/b", ""}, ...
Expand Down Expand Up @@ -77,50 +102,7 @@
{"c:\a/b", "c:\a"}
};

p_relative_to = {{"", "", "."}, ...
{"Hello", "Hello", "."}, ...
{"Hello", "Hello/", "."}, ...
{"a/./b", "a/b", "."}, ...
{"a/b", "a/./b", "."}, ...
{"./this/one", "./this/two", "../two"}, ...
{"/path/same", "/path/same/hi/..", "hi/.."}, ...
{"", "/", ""}, ...
{"/", "", ""}, ...
{"/", "/", "."}, ...
{"/dev/null", "/dev/null", "."}, ...
{"/a/b", "c", ""}, ...
{"c", "/a/b", ""}, ...
{"/a/b", "/a/b", "."}, ...
{"/a/b", "/a", ".."}, ...
{"/a/b/c/d", "/a/b", "../.."}, ...
{"this/one", "this/two", "../two"}};
% NOTE: ".." in relative_to(base) is ambiguous including for python.pathlib, C++ <filesystem>, etc.

p_proximate_to = p_relative_to;

p_proximate_to{6}{3} = "/";
p_proximate_to{10}{3} = "c";
p_proximate_to{11}{3} = "/a/b";

if ispc

p_relative_to = [p_relative_to, ...
{{"c:\a\b", "c:/", "../.."}, ...
{"c:\", "c:/a/b", "a/b"}, ...
{"c:/a/b", "c:/a/b", "."}, ...
{"c:/a/b", "c:/a", ".."}, ...
{"c:\a/b\c/d", "c:/a\b", "../.."}, ...
{"c:/path", "d:/path", ""}}];

p_proximate_to = p_relative_to;
% NOTE: ".." in proximate_to(base) is ambiguous including for python.pathlib, C++ <filesystem>, etc

p_proximate_to{8}{3} = "/";
p_proximate_to{12}{3} = "c";
p_proximate_to{13}{3} = "/a/b";

p_proximate_to{end}{3} = "d:/path";

p_parent{12}{2} = "c:/";
p_parent{13}{2} = "c:/";
p_parent{14}{2} = "c:/a";
Expand All @@ -133,7 +115,7 @@

p_root_name{4}{2} = "c:";

end % if ispc
end

end

Expand All @@ -149,30 +131,8 @@

end


function [p_is_subdir] = init_is_subdir(classToTest) %#ok<INUSD>

p_is_subdir = {
{"a/b", "a/b", false}, ...
{"a//b/c", "a/b", false}, ...
{"a/b", "a//b", false}, ...
{"a/./b/c", "a/b", false}, ...
{"a/b/c", "a/./b", true}, ...
{"a/b", "a/b/", false}, ...
{"a/b", "a", true}, ...
{"a/.c", "a", true}
};
% NOTE: ".." in is_subdir (either argument) is ambiguous

if ispc
p_is_subdir{end+1} = {"c:\", "c:/", false};
else
p_is_subdir{end+1} = {"/", "/", false};
end

end

end


methods (TestClassSetup)
Expand Down Expand Up @@ -211,32 +171,22 @@ function test_join(tc, p_join)
end


function test_filename(tc, in_filename, ref_filename)
tc.verifyEqual(stdlib.filename(in_filename), string(ref_filename))
function test_filename(tc, p_filename)
tc.verifyEqual(stdlib.filename(p_filename{1}), string(p_filename{2}))
end


function test_parent(tc, p_parent)
tc.verifyEqual(stdlib.parent(p_parent{1}), p_parent{2}, p_parent{1})
end

function test_suffix(tc, in_suffix, ref_suffix)
tc.verifyEqual(stdlib.suffix(in_suffix), string(ref_suffix))
function test_suffix(tc, p_suffix)
tc.verifyEqual(stdlib.suffix(p_suffix{1}), string(p_suffix{2}))
end


function test_stem(tc)

tc.verifyEqual(stdlib.stem(""), "")

tc.verifyEqual(stdlib.stem("/a/b/c"), "c")
tc.verifyEqual(stdlib.stem("/a/b/c/"), "")

tc.verifyEqual(stdlib.stem("a/b/c/"), "")

tc.verifyEqual(stdlib.stem("a/b/c.txt"), "c")
tc.verifyEqual(stdlib.stem("a/b/c.txt.gz"), "c.txt")

function test_stem(tc, p_stem)
tc.verifyEqual(stdlib.stem(p_stem{1}), p_stem{2})
end


Expand All @@ -245,9 +195,9 @@ function test_is_absolute(tc, p_is_absolute)
end


function test_normalize(tc, in_norm, ref_norm)
function test_normalize(tc, p_norm)

tc.verifyEqual(stdlib.normalize(in_norm), ref_norm)
tc.verifyEqual(stdlib.normalize(p_norm{1}), p_norm{2})

end

Expand All @@ -257,35 +207,8 @@ function test_root(tc, p_root)
end


function test_with_suffix(tc)

tc.verifyEqual(stdlib.with_suffix("", ""), "")

tc.verifyEqual(stdlib.with_suffix("foo.h5", ".nc"), "foo.nc")

tc.verifyEqual(stdlib.with_suffix("c", ""), "c")
tc.verifyEqual(stdlib.with_suffix("c.nc", ""), "c")
tc.verifyEqual(stdlib.with_suffix("", ".nc"), ".nc")

tc.verifyEqual(stdlib.with_suffix("a//b///c///", ".h5"), "a/b/c/.h5")
end


function test_relative_to(tc, p_relative_to)
tc.assumeTrue(stdlib.has_java)
tc.verifyEqual(stdlib.relative_to(p_relative_to{1}, p_relative_to{2}), p_relative_to{3}, "relative_to(" + p_relative_to{1} + "," + p_relative_to{2}+")")
end


function test_proximate_to(tc, p_proximate_to)
tc.assumeTrue(stdlib.has_java)
tc.verifyEqual(stdlib.proximate_to(p_proximate_to{1}, p_proximate_to{2}), p_proximate_to{3}, "proximate_to(" + p_proximate_to{1} + "," + p_proximate_to{2}+")")
end


function test_is_subdir(tc, p_is_subdir)
tc.assumeTrue(stdlib.has_java)
tc.verifyEqual(stdlib.is_subdir(p_is_subdir{1}, p_is_subdir{2}), p_is_subdir{3}, "subdir(" + p_is_subdir{1} + "," + p_is_subdir{2} + ")")
function test_with_suffix(tc, p_with_suffix)
tc.verifyEqual(stdlib.with_suffix(p_with_suffix{1}, p_with_suffix{2}), p_with_suffix{3})
end


Expand Down
135 changes: 135 additions & 0 deletions test/TestRelative.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
classdef TestRelative < matlab.unittest.TestCase

properties (ClassSetupParameter)
classToTest = {"TestRelative"};
end

properties (TestParameter)
p_relative_to
p_proximate_to
p_is_subdir

end

properties
tobj
end


methods (TestParameterDefinition, Static)

function [p_relative_to, p_proximate_to] = init_relative_to(classToTest) %#ok<INUSD>

p_relative_to = {{"", "", "."}, ...
{"Hello", "Hello", "."}, ...
{"Hello", "Hello/", "."}, ...
{"a/./b", "a/b", "."}, ...
{"a/b", "a/./b", "."}, ...
{"./this/one", "./this/two", "../two"}, ...
{"/path/same", "/path/same/hi/..", "hi/.."}, ...
{"", "/", ""}, ...
{"/", "", ""}, ...
{"/", "/", "."}, ...
{"/dev/null", "/dev/null", "."}, ...
{"/a/b", "c", ""}, ...
{"c", "/a/b", ""}, ...
{"/a/b", "/a/b", "."}, ...
{"/a/b", "/a", ".."}, ...
{"/a/b/c/d", "/a/b", "../.."}, ...
{"this/one", "this/two", "../two"}};
% NOTE: ".." in relative_to(base) is ambiguous including for python.pathlib, C++ <filesystem>, etc.

p_proximate_to = p_relative_to;

p_proximate_to{6}{3} = "/";
p_proximate_to{10}{3} = "c";
p_proximate_to{11}{3} = "/a/b";

if ispc

p_relative_to = [p_relative_to, ...
{{"c:\a\b", "c:/", "../.."}, ...
{"c:\", "c:/a/b", "a/b"}, ...
{"c:/a/b", "c:/a/b", "."}, ...
{"c:/a/b", "c:/a", ".."}, ...
{"c:\a/b\c/d", "c:/a\b", "../.."}, ...
{"c:/path", "d:/path", ""}}];

p_proximate_to = p_relative_to;
% NOTE: ".." in proximate_to(base) is ambiguous including for python.pathlib, C++ <filesystem>, etc

p_proximate_to{8}{3} = "/";
p_proximate_to{12}{3} = "c";
p_proximate_to{13}{3} = "/a/b";

p_proximate_to{end}{3} = "d:/path";
end

end


function [p_is_subdir] = init_is_subdir(classToTest) %#ok<INUSD>

p_is_subdir = {
{"a/b", "a/b", false}, ...
{"a//b/c", "a/b", true}, ...
{"a/b", "a//b", false}, ...
{"a/./b/c", "a/b", false}, ...
{"a/b/c", "a/./b", false}, ...
{"a/b", "a/b/", false}, ...
{"a/b", "a", true}, ...
{"a/.c", "a", true}
};
% NOTE: ".." in is_subdir (either argument) is ambiguous

if ispc
p_is_subdir{end+1} = {"c:\", "c:/", false};
else
p_is_subdir{end+1} = {"/", "/", false};
end

end

end


methods (TestClassSetup)

function classSetup(tc, classToTest)
constructor = str2func(classToTest);
tc.tobj = constructor();
end

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

end


methods (Test, ParameterCombination = "sequential")


function test_relative_to(tc, p_relative_to)
tc.assumeTrue(stdlib.has_java)
tc.verifyEqual(stdlib.relative_to(p_relative_to{1}, p_relative_to{2}), p_relative_to{3}, "relative_to(" + p_relative_to{1} + "," + p_relative_to{2}+")")
end


function test_proximate_to(tc, p_proximate_to)
tc.assumeTrue(stdlib.has_java)
tc.verifyEqual(stdlib.proximate_to(p_proximate_to{1}, p_proximate_to{2}), p_proximate_to{3}, "proximate_to(" + p_proximate_to{1} + "," + p_proximate_to{2}+")")
end


function test_is_subdir(tc, p_is_subdir)
tc.assumeTrue(stdlib.has_java)
tc.verifyEqual(stdlib.is_subdir(p_is_subdir{1}, p_is_subdir{2}), p_is_subdir{3}, "subdir(" + p_is_subdir{1} + "," + p_is_subdir{2} + ")")
end

end

end

0 comments on commit 6c09b79

Please sign in to comment.