Skip to content

Commit 60268dd

Browse files
committed
more java methods
1 parent 3e27d59 commit 60268dd

File tree

6 files changed

+72
-54
lines changed

6 files changed

+72
-54
lines changed

+stdlib/+fileio/filename.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
path string
55
end
66

7-
[~, n, e] = fileparts(path);
7+
% NOT https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getName()
8+
% because by our definition, a trailing directory component is not part of the filename
9+
% this is like C++17 filesystem::path::filename
810

11+
[~, n, e] = fileparts(path);
912
p = n + e;
1013

1114
end

+stdlib/+fileio/is_exe.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@
1313

1414
ok = java.io.File(file).canExecute();
1515

16+
% more complicated
17+
% ok = java.nio.file.Files.isExecutable(java.io.File(stdlib.fileio.absolute_path(file)).toPath());
18+
1619
end

+stdlib/+fileio/parent.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
function p = parent(path)
22
% PARENT parent directory of path
3+
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getParent()
34
arguments
45
path string
56
end
67

8+
p = stdlib.fileio.posix(java.io.File(path).getParent());
9+
710
% must drop trailing slash - need to as_posix for windows
8-
p = fileparts(strip(stdlib.fileio.posix(path), 'right', '/'));
11+
%p = fileparts(strip(stdlib.fileio.posix(path), 'right', '/'));
912

1013
end

+stdlib/+fileio/samepath.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
return
1111
end
1212

13-
% unlike c++ filesystem, isSameFile does not canonicalize first
13+
% java.nio.file.Files needs CANONICAL -- not just absolute path
1414
p1 = java.io.File(stdlib.fileio.canonical(path1)).toPath();
1515
p2 = java.io.File(stdlib.fileio.canonical(path2)).toPath();
1616

test/TestFileImpure.m

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,36 @@ function setup_path(tc)
1414

1515
methods (Test)
1616

17+
function test_exists(tc)
18+
19+
tc.verifyTrue(stdlib.exists(pwd))
20+
tc.verifyTrue(stdlib.exists(string(mfilename("fullpath"))+".m"))
21+
tc.verifyFalse(stdlib.exists("not-exists"))
22+
23+
end
24+
25+
26+
function test_is_readable(tc)
27+
28+
tc.verifyTrue(stdlib.is_readable(pwd))
29+
tc.verifyTrue(stdlib.is_readable(string(mfilename("fullpath"))+".m"))
30+
tc.verifyFalse(stdlib.is_readable("not-exists"))
31+
32+
end
33+
34+
35+
function test_is_writable(tc)
36+
37+
tc.verifyTrue(stdlib.is_writable(pwd))
38+
tc.verifyFalse(stdlib.is_writable(matlabroot))
39+
tc.verifyFalse(stdlib.is_writable("not-exists"))
40+
41+
end
42+
43+
44+
1745
function test_expanduser(tc)
1846

19-
tc.verifyEmpty(stdlib.expanduser(string.empty))
2047
tc.verifyEqual(stdlib.expanduser(""), "")
2148

2249
tc.verifyEqual(stdlib.expanduser("~abc"), "~abc")
@@ -30,6 +57,33 @@ function test_expanduser(tc)
3057
end
3158

3259

60+
function test_absolute_path(tc)
61+
import matlab.unittest.constraints.StartsWithSubstring
62+
63+
tc.verifyEqual(stdlib.absolute_path(""), "")
64+
65+
pabs = stdlib.absolute_path('2foo');
66+
pabs2 = stdlib.absolute_path('4foo');
67+
tc.verifyThat(pabs, ~StartsWithSubstring("2"))
68+
tc.verifyTrue(strncmp(pabs, pabs2, 2))
69+
70+
par1 = stdlib.absolute_path("../2foo");
71+
tc.verifyNotEmpty(par1)
72+
73+
par2 = stdlib.absolute_path("../4foo");
74+
tc.verifyTrue(strncmp(par2, pabs2, 2))
75+
76+
pt1 = stdlib.absolute_path("bar/../2foo");
77+
tc.verifyNotEmpty(pt1)
78+
79+
va = stdlib.absolute_path("2foo");
80+
vb = stdlib.absolute_path("4foo");
81+
tc.verifyThat(va, ~StartsWithSubstring("2"))
82+
tc.verifyTrue(strncmp(va, vb, 2))
83+
84+
end
85+
86+
3387
function test_canonical(tc)
3488
import matlab.unittest.fixtures.TemporaryFolderFixture
3589
import matlab.unittest.fixtures.CurrentFolderFixture
@@ -39,7 +93,7 @@ function test_canonical(tc)
3993
tc.applyFixture(CurrentFolderFixture(td))
4094

4195
% all non-existing files
42-
tc.verifyEmpty(stdlib.canonical(string.empty))
96+
4397
tc.verifyEqual(stdlib.canonical(""), "")
4498

4599
pabs = stdlib.canonical('2foo');
@@ -78,7 +132,7 @@ function test_resolve(tc)
78132
tc.applyFixture(CurrentFolderFixture(td))
79133

80134
% all non-existing files
81-
tc.verifyEmpty(stdlib.resolve(string.empty))
135+
82136
tc.verifyEqual(stdlib.resolve(""), stdlib.fileio.posix(pwd))
83137

84138
pabs = stdlib.resolve('2foo');
@@ -127,11 +181,10 @@ function test_makedir(tc)
127181

128182
function test_samepath(tc)
129183

130-
tc.verifyEmpty(stdlib.samepath(string.empty, string.empty))
131-
tc.verifyTrue(stdlib.samepath("", ""))
132-
tc.verifyFalse(stdlib.samepath(tempname, tempname))
184+
tc.verifyTrue(stdlib.samepath("", ""), "empty not same")
185+
tc.verifyFalse(stdlib.samepath(tempname, tempname), "tempname not same")
133186
tc.verifyTrue(stdlib.samepath("~/b/..", "~/c/.."), "tilde path ..")
134-
tc.verifyTrue(stdlib.samepath(".", fullfile(pwd, "a/..")))
187+
tc.verifyTrue(stdlib.samepath(".", fullfile(pwd, "a/..")), "dot path ..")
135188
end
136189

137190

@@ -155,7 +208,6 @@ function test_is_exe_which_fullpath(tc)
155208
import matlab.unittest.constraints.IsFile
156209
import matlab.unittest.constraints.EndsWithSubstring
157210

158-
tc.verifyEmpty(stdlib.is_exe(string.empty))
159211
tc.verifyFalse(stdlib.is_exe(""))
160212
tc.verifyFalse(stdlib.is_exe(tempname))
161213

test/TestFilePure.m

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ function test_posix(tc)
2727

2828
function test_filename(tc)
2929

30-
tc.verifyEmpty(stdlib.filename(string.empty))
3130
tc.verifyEqual(stdlib.filename(""), "")
3231

3332
tc.verifyEqual(stdlib.filename("/foo/bar/baz"), "baz")
@@ -41,7 +40,6 @@ function test_filename(tc)
4140

4241
function test_parent(tc)
4342

44-
tc.verifyEmpty(stdlib.parent(string.empty))
4543
tc.verifyEqual(stdlib.parent(""), "")
4644

4745
tc.verifyEqual(stdlib.parent("/foo/bar/baz"), "/foo/bar")
@@ -81,21 +79,8 @@ function test_stem(tc)
8179
end
8280

8381

84-
function test_path_tail(tc)
85-
86-
tc.verifyEmpty(stdlib.path_tail(string.empty))
87-
tc.verifyEqual(stdlib.path_tail(""), "")
88-
tc.verifyEqual(stdlib.path_tail("/foo/bar/baz"), "baz")
89-
tc.verifyEqual(stdlib.path_tail("/foo/bar/baz/"), "baz")
90-
tc.verifyEqual(stdlib.path_tail("/foo/bar/baz/."), "baz")
91-
tc.verifyEqual(stdlib.path_tail("/foo/bar/baz/.."), "bar")
92-
tc.verifyEqual(stdlib.path_tail("/foo/bar/baz.txt"), "baz.txt")
93-
94-
end
95-
9682
function test_is_absolute_path(tc)
9783

98-
tc.verifyEmpty(stdlib.is_absolute_path(string.empty))
9984
tc.verifyFalse(stdlib.is_absolute_path(""))
10085

10186
tc.verifyTrue(stdlib.is_absolute_path('~/foo'))
@@ -109,36 +94,9 @@ function test_is_absolute_path(tc)
10994
tc.verifyFalse(stdlib.is_absolute_path("c"))
11095
end
11196

112-
function test_absolute_path(tc)
113-
import matlab.unittest.constraints.StartsWithSubstring
114-
115-
tc.verifyEmpty(stdlib.absolute_path(string.empty))
116-
tc.verifyEqual(stdlib.absolute_path(""), "")
117-
118-
pabs = stdlib.absolute_path('2foo');
119-
pabs2 = stdlib.absolute_path('4foo');
120-
tc.verifyThat(pabs, ~StartsWithSubstring("2"))
121-
tc.verifyTrue(strncmp(pabs, pabs2, 2))
122-
123-
par1 = stdlib.absolute_path("../2foo");
124-
tc.verifyNotEmpty(par1)
125-
126-
par2 = stdlib.absolute_path("../4foo");
127-
tc.verifyTrue(strncmp(par2, pabs2, 2))
128-
129-
pt1 = stdlib.absolute_path("bar/../2foo");
130-
tc.verifyNotEmpty(pt1)
131-
132-
va = stdlib.absolute_path("2foo");
133-
vb = stdlib.absolute_path("4foo");
134-
tc.verifyThat(va, ~StartsWithSubstring("2"))
135-
tc.verifyTrue(strncmp(va, vb, 2))
136-
137-
end
13897

13998
function test_normalize(tc)
14099

141-
tc.verifyEmpty(stdlib.normalize(string.empty))
142100
tc.verifyEqual(stdlib.normalize(""), "")
143101

144102
pabs = stdlib.normalize('2foo//');
@@ -148,7 +106,6 @@ function test_normalize(tc)
148106

149107
function test_with_suffix(tc)
150108

151-
tc.verifyEmpty(stdlib.with_suffix(string.empty, ".nc"))
152109
tc.verifyEqual(stdlib.with_suffix("", ""), "")
153110

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

0 commit comments

Comments
 (0)