Skip to content

Commit dc5dbab

Browse files
committed
improve test coverage
1 parent fa3a535 commit dc5dbab

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

+stdlib/+fileio/absolute_path.m

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,29 @@
1212
% * abspath: absolute path, if determined
1313

1414
arguments
15-
p (1,:) string
15+
p string {mustBeScalarOrEmpty}
1616
end
1717

1818
% have to expand ~ first
19-
p = stdlib.fileio.expanduser(p);
19+
abspath = stdlib.fileio.expanduser(p);
2020

21-
if ~stdlib.fileio.is_absolute_path(p)
22-
% otherwise the default is Documents/Matlab, which is probably not wanted.
23-
p = fullfile(pwd, p);
21+
if isempty(abspath)
22+
return
2423
end
2524

26-
abspath = p;
25+
if ~stdlib.fileio.is_absolute_path(abspath)
26+
% otherwise the default is Documents/Matlab, which is probably not wanted.
27+
abspath = fullfile(pwd, abspath);
28+
end
2729

28-
for i = 1:length(abspath)
29-
if ispc && startsWith(abspath(i), "\\")
30-
% UNC path is not canonicalized
31-
continue
32-
end
33-
try
34-
abspath(i) = string(java.io.File(abspath(i)).getCanonicalPath());
35-
catch excp
36-
error("stdlib:fileio:absolute_path", "%s is not a canonicalizable path. %s", abspath(i), excp.message)
37-
end
30+
if ispc && startsWith(abspath, "\\")
31+
% UNC path is not canonicalized
32+
return
33+
end
34+
try
35+
abspath = string(java.io.File(abspath).getCanonicalPath());
36+
catch excp
37+
error("stdlib:fileio:absolute_path", "%s is not a canonicalizable path. %s", abspath, excp.message)
3838
end
3939

4040
end % function

+stdlib/+fileio/is_exe.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
% * ok: boolean logical
99

1010
arguments
11-
file (1,1) string {mustBeNonzeroLengthText}
11+
file string {mustBeScalarOrEmpty}
12+
end
13+
14+
if isempty(file)
15+
ok = logical.empty;
16+
return
1217
end
1318

1419
if ~isfile(file)

+stdlib/+test/TestFileImpure.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ function test_makedir(tc)
2323

2424
function test_which_name(tc)
2525

26+
tc.verifyEmpty(stdlib.fileio.which(tempname))
27+
2628
if ismac
2729
n = "ls";
2830
else
@@ -38,6 +40,10 @@ function test_which_name(tc)
3840
function test_is_exe_which_fullpath(tc)
3941
import matlab.unittest.constraints.IsFile
4042

43+
tc.verifyEmpty(stdlib.fileio.is_exe(string.empty))
44+
tc.verifyFalse(stdlib.fileio.is_exe(""))
45+
tc.verifyFalse(stdlib.fileio.is_exe(tempname))
46+
4147
n = "matlab";
4248
%% is_exe test
4349
p = fullfile(matlabroot, "bin", n);

+stdlib/+test/TestFilePure.m

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ function test_is_absolute_path(tc)
3232
tc.verifyTrue(is_absolute_path('~/foo'))
3333
if ispc
3434
tc.verifyTrue(is_absolute_path('x:/foo'))
35-
tc.verifyEqual(is_absolute_path(["x:/abc", "x:/123", "", "c"]), [true, true, false, false])
36-
tc.verifyTrue(all(is_absolute_path(["x:/abc"; "x:/123"])))
3735
tc.verifyFalse(is_absolute_path('/foo'))
3836
else
3937
tc.verifyTrue(is_absolute_path('/foo'))
@@ -61,10 +59,10 @@ function test_absolute_path(tc)
6159
pt1 = absolute_path("bar/../2foo");
6260
tc.verifyFalse(contains(pt1, ".."))
6361

64-
va = absolute_path(["2foo", "4foo"]);
65-
tc.verifyFalse(any(startsWith(va, "2")))
66-
vs = extractBefore(va, 2);
67-
tc.verifyEqual(vs(1), vs(2))
62+
va = absolute_path("2foo");
63+
vb = absolute_path("4foo");
64+
tc.verifyFalse(startsWith(va, "2"))
65+
tc.verifyTrue(strncmp(va, vb, 2))
6866

6967
tc.verifyEmpty(absolute_path(string.empty))
7068
tc.verifyEqual(absolute_path(""), string(pwd))

0 commit comments

Comments
 (0)