|
| 1 | +classdef TestResolve < matlab.unittest.TestCase |
| 2 | + |
| 3 | +methods(Test) |
| 4 | + |
| 5 | + |
| 6 | +function test_absolute(tc) |
| 7 | +import matlab.unittest.fixtures.TemporaryFolderFixture |
| 8 | +import matlab.unittest.fixtures.CurrentFolderFixture |
| 9 | +import matlab.unittest.constraints.StartsWithSubstring |
| 10 | +import matlab.unittest.constraints.EndsWithSubstring |
| 11 | + |
| 12 | +tc.assumeTrue(stdlib.has_java) |
| 13 | + |
| 14 | +td = tc.applyFixture(TemporaryFolderFixture).Folder; |
| 15 | +tc.applyFixture(CurrentFolderFixture(td)) |
| 16 | + |
| 17 | +tc.verifyEqual(stdlib.absolute(""), stdlib.posix(td)) |
| 18 | +tc.verifyEqual(stdlib.absolute("",""), stdlib.posix(td)) |
| 19 | +tc.verifyEqual(stdlib.absolute("hi"), stdlib.join(td, "hi")) |
| 20 | +tc.verifyEqual(stdlib.absolute("", "hi"), stdlib.join(td, "hi")) |
| 21 | +tc.verifyEqual(stdlib.absolute("there", "hi"), stdlib.join(td, "hi/there")) |
| 22 | + |
| 23 | +end |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +function test_canonical(tc) |
| 28 | +import matlab.unittest.fixtures.TemporaryFolderFixture |
| 29 | +import matlab.unittest.fixtures.CurrentFolderFixture |
| 30 | +import matlab.unittest.constraints.StartsWithSubstring |
| 31 | +import matlab.unittest.constraints.EndsWithSubstring |
| 32 | + |
| 33 | +tc.assumeTrue(stdlib.has_java) |
| 34 | + |
| 35 | +td = tc.applyFixture(TemporaryFolderFixture).Folder; |
| 36 | +tc.applyFixture(CurrentFolderFixture(td)) |
| 37 | + |
| 38 | +% all non-existing files |
| 39 | + |
| 40 | +tc.verifyEqual(stdlib.canonical(""), ".") |
| 41 | + |
| 42 | +pabs = stdlib.canonical('2foo'); |
| 43 | +tc.verifyThat(pabs, StartsWithSubstring("2foo")) |
| 44 | + |
| 45 | +par1 = stdlib.canonical("../2foo"); |
| 46 | +tc.verifyThat(par1, StartsWithSubstring("..")) |
| 47 | + |
| 48 | +pt1 = stdlib.canonical("bar/../2foo"); |
| 49 | +tc.verifyEqual(pt1, "2foo") |
| 50 | + |
| 51 | +% test existing file |
| 52 | +r = stdlib.parent(mfilename('fullpath')); |
| 53 | +tc.verifyEqual(stdlib.canonical(fullfile(r, "..")), stdlib.parent(r)) |
| 54 | + |
| 55 | +% on windows, ~ is expanded even without expanduser |
| 56 | +if ~ispc |
| 57 | +tc.verifyThat(stdlib.canonical("~", false), EndsWithSubstring("~")) |
| 58 | +end |
| 59 | + |
| 60 | +h = stdlib.homedir; |
| 61 | +tc.verifyEqual(stdlib.canonical("~"), h) |
| 62 | +tc.verifyEqual(stdlib.canonical("~/"), h) |
| 63 | +tc.verifyEqual(stdlib.canonical("~/.."), stdlib.parent(h)) |
| 64 | + |
| 65 | +tc.verifyEqual(stdlib.canonical("nobody.txt"), "nobody.txt") |
| 66 | +tc.verifyEqual(stdlib.canonical("../nobody.txt"), "../nobody.txt") |
| 67 | + |
| 68 | +end |
| 69 | + |
| 70 | + |
| 71 | +function test_resolve(tc) |
| 72 | +import matlab.unittest.fixtures.TemporaryFolderFixture |
| 73 | +import matlab.unittest.fixtures.CurrentFolderFixture |
| 74 | +import matlab.unittest.constraints.StartsWithSubstring |
| 75 | +import matlab.unittest.constraints.EndsWithSubstring |
| 76 | +import matlab.unittest.constraints.ContainsSubstring |
| 77 | + |
| 78 | +tc.assumeTrue(stdlib.has_java) |
| 79 | + |
| 80 | +td = tc.applyFixture(TemporaryFolderFixture).Folder; |
| 81 | +tc.applyFixture(CurrentFolderFixture(td)) |
| 82 | + |
| 83 | +% all non-existing files |
| 84 | + |
| 85 | +tc.verifyEqual(stdlib.resolve(""), stdlib.posix(td)) |
| 86 | + |
| 87 | +pabs = stdlib.resolve('2foo'); |
| 88 | +pabs2 = stdlib.resolve('4foo'); |
| 89 | +tc.verifyThat(pabs, ~StartsWithSubstring("2")) |
| 90 | +tc.verifyTrue(strncmp(pabs, pabs2, 2)) |
| 91 | + |
| 92 | +par1 = stdlib.resolve("../2foo"); |
| 93 | +tc.verifyNotEmpty(par1) |
| 94 | +tc.verifyThat(par1, ~ContainsSubstring("..")) |
| 95 | + |
| 96 | +par2 = stdlib.resolve("../4foo"); |
| 97 | +tc.verifyTrue(strncmp(par2, pabs2, 2)) |
| 98 | + |
| 99 | +pt1 = stdlib.resolve("bar/../2foo"); |
| 100 | +tc.verifyNotEmpty(pt1) |
| 101 | +tc.verifyThat(pt1, ~ContainsSubstring("..")) |
| 102 | + |
| 103 | +va = stdlib.resolve("2foo"); |
| 104 | +vb = stdlib.resolve("4foo"); |
| 105 | +tc.verifyThat(va, ~StartsWithSubstring("2")) |
| 106 | +tc.verifyTrue(strncmp(va, vb, 2)) |
| 107 | + |
| 108 | +% test existing file |
| 109 | +r = stdlib.parent(mfilename('fullpath')); |
| 110 | +rp = stdlib.parent(r); |
| 111 | +tc.verifyEqual(stdlib.resolve(rp), stdlib.parent(r)) |
| 112 | + |
| 113 | +h = stdlib.homedir; |
| 114 | +tc.verifyEqual(stdlib.resolve("~"), h) |
| 115 | +tc.verifyEqual(stdlib.resolve("~/"), h) |
| 116 | +tc.verifyEqual(stdlib.resolve("~/.."), stdlib.parent(h)) |
| 117 | + |
| 118 | +tc.verifyEqual(stdlib.resolve("nobody.txt"), stdlib.join(td, "nobody.txt")) |
| 119 | +tc.verifyEqual(stdlib.resolve("../nobody.txt"), stdlib.join(stdlib.parent(td), "nobody.txt")) |
| 120 | + |
| 121 | +end |
| 122 | + |
| 123 | + |
| 124 | +end |
| 125 | + |
| 126 | +end |
0 commit comments