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 d65205d
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 91 deletions.
96 changes: 5 additions & 91 deletions test/TestFilePure.m
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
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_parent

p_join = {{"", "", ""}, ...
Expand Down Expand Up @@ -44,8 +42,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 +77,6 @@
{"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,8 +89,6 @@

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

end % if ispc

end


Expand All @@ -149,30 +103,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 @@ -271,23 +203,5 @@ function test_with_suffix(tc)
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} + ")")
end


end
end
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 d65205d

Please sign in to comment.