Skip to content

Commit 72bf0ef

Browse files
test: add some test
1 parent 61183d5 commit 72bf0ef

File tree

3 files changed

+178
-16
lines changed

3 files changed

+178
-16
lines changed

tests/path-compatibility.test.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
import nodepath from 'path';
22
import path from '../build/cjs/index.cjs.js';
33

4-
test('member variables are equal to the original', () => {
5-
expect(path.sep).toBe(nodepath.sep);
6-
expect(path.delimiter).toBe(nodepath.delimiter);
4+
test('props are equal to the original', () => {
5+
[
6+
[path, nodepath],
7+
[path.posix, nodepath.posix],
8+
[path.win32, nodepath.win32],
9+
].forEach(([path, nodepath]) => {
10+
expect(path.sep).toBe(nodepath.sep);
11+
expect(path.delimiter).toBe(nodepath.delimiter);
712

8-
expect(path.posix.sep).toBe(nodepath.posix.sep);
9-
expect(path.posix.delimiter).toBe(nodepath.posix.delimiter);
10-
11-
expect(path.win32.sep).toBe(nodepath.win32.sep);
12-
expect(path.win32.delimiter).toBe(nodepath.win32.delimiter);
13+
// functions of nodepath do not depend on `this`
14+
expect(path.basename).toBe(nodepath.basename);
15+
expect(path.dirname).toBe(nodepath.dirname);
16+
expect(path.extname).toBe(nodepath.extname);
17+
expect(path.format).toBe(nodepath.format);
18+
expect(path.isAbsolute).toBe(nodepath.isAbsolute);
19+
expect(path.join).toBe(nodepath.join);
20+
expect(path.normalize).toBe(nodepath.normalize);
21+
expect(path.parse).toBe(nodepath.parse);
22+
expect(path.relative).toBe(nodepath.relative);
23+
expect(path.resolve).toBe(nodepath.resolve);
24+
expect(path.toNamespacedPath).toBe(nodepath.toNamespacedPath);
25+
});
1326
});

tests/path-fn.test.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
1-
import nodepath from 'path';
1+
import { fs as memfs } from 'memfs';
22
import path from '../build/cjs/index.cjs.js';
33

4+
test('path(): return type', () => {
5+
[path, path.posix, path.win32].forEach((path) => {
6+
expect(() => (path as any)()).toThrowError();
47

5-
test('path() should return the correct type', () => {
6-
expect(() => (path as any)()).toThrowError();
8+
const p = path('foo.txt');
9+
expect(p).toBeInstanceOf(path.PathNice);
10+
expect(path(p)).toBeInstanceOf(path.PathNice);
711

8-
expect(path('foo.txt')).toBeInstanceOf(path.PathNice);
9-
expect(path(path('foo.txt'))).toBeInstanceOf(path.PathNice);
10-
expect(path('foo.txt', path('bar.md'))).toBeInstanceOf(path.PathNiceArr);
11-
expect(path(['foo.txt', 'bar.md'])).toBeInstanceOf(path.PathNiceArr);
12-
expect(path(path(['foo.txt', 'bar.md']))).toBeInstanceOf(path.PathNiceArr);
12+
const pp = ['foo.txt', path('bar.md')] as const;
13+
expect(path(...pp)).toBeInstanceOf(path.PathNiceArr);
14+
expect(path(pp)).toBeInstanceOf(path.PathNiceArr);
15+
expect(path(path(pp))).toBeInstanceOf(path.PathNiceArr);
16+
});
17+
});
18+
19+
test('path.bindFS()', () => {
20+
[path, path.posix, path.win32].forEach((path) => {
21+
const _path = path.bindFS(memfs as any);
22+
23+
_path('/foo.txt').writeFileSync('test');
24+
expect(_path('/foo.txt').readFileToStringSync()).toBe('test');
25+
26+
const { dirs, files } = _path('/').lsSync();
27+
expect(dirs.length).toBe(0);
28+
expect(files.length).toBe(1);
29+
expect(files[0].separator('/').raw).toBe('/foo.txt');
30+
});
1331
});

tests/path-nice-path-related.test.ts

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import path from '../build/cjs/index.cjs.js';
2+
3+
const pathPosix = path.posix;
4+
const pathWin32 = path.win32;
5+
6+
test('PathNice.raw', () => {
7+
const p = path('!@#$%^&()1234567890');
8+
expect(p.raw).toBe('!@#$%^&()1234567890');
9+
});
10+
11+
test('PathNice.join()', () => {
12+
expect(pathPosix('../data').join('settings.json').raw).toBe('../data/settings.json');
13+
14+
expect(pathWin32('../data').join('settings.json').raw).toBe(
15+
'..\\data\\settings.json',
16+
);
17+
18+
expect(pathPosix('/home').join('fuu', pathPosix('data.json')).raw).toBe(
19+
'/home/fuu/data.json',
20+
);
21+
22+
expect(pathWin32('C:\\Users').join('fuu', pathWin32('data.json')).raw).toBe(
23+
'C:\\Users\\fuu\\data.json',
24+
);
25+
});
26+
27+
test('PathNice.dirname()', () => {
28+
expect(pathPosix('/usr/local/bin').dirname().raw).toBe('/usr/local');
29+
30+
expect(pathWin32('C:\\Users\\fuu').dirname().raw).toBe('C:\\Users');
31+
32+
expect(pathPosix('./src/index.ts').dirname('./dist').raw).toBe('dist/index.ts');
33+
expect(pathWin32('./src/index.ts').dirname('./dist').raw).toBe('dist\\index.ts');
34+
35+
expect(pathPosix('./src/index.ts').dirname(pathPosix('./dist')).raw).toBe(
36+
'dist/index.ts',
37+
);
38+
expect(pathWin32('./src/index.ts').dirname(pathWin32('./dist')).raw).toBe(
39+
'dist\\index.ts',
40+
);
41+
42+
expect(
43+
pathPosix('path-nice/dist/types.ts').dirname((old) =>
44+
old.replace(/dist/g, 'build'),
45+
).raw,
46+
).toBe('path-nice/build/types.ts');
47+
expect(
48+
pathWin32('path-nice/dist/types.ts').dirname((old) =>
49+
old.replace(/dist/g, 'build'),
50+
).raw,
51+
).toBe('path-nice\\build\\types.ts');
52+
});
53+
54+
test('PathNice.parent', () => {
55+
expect(pathPosix('/usr/local/bin').parent.raw).toBe('/usr/local');
56+
expect(pathWin32('C:\\Users\\fuu').parent.raw).toBe('C:\\Users');
57+
});
58+
59+
test('PathNice.filename()', () => {
60+
expect(path('./src/index.js').filename()).toBe('index.js');
61+
62+
expect(pathPosix('/home/fuu///').filename()).toBe('fuu');
63+
64+
expect(pathPosix('/home/fuu/bar.txt').filename('foo.md').raw).toBe(
65+
'/home/fuu/foo.md',
66+
);
67+
68+
expect(pathWin32('C:\\Users\\fuu\\\\\\').filename()).toBe('fuu');
69+
70+
expect(pathWin32('C:\\Users\\fuu\\bar.txt').filename('foo.md').raw).toBe(
71+
'C:\\Users\\fuu\\foo.md',
72+
);
73+
74+
expect(pathPosix('./data/storage.json').filename((n) => 'old.' + n).raw).toBe(
75+
'data/old.storage.json',
76+
);
77+
expect(pathWin32('./data/storage.json').filename((n) => 'old.' + n).raw).toBe(
78+
'data\\old.storage.json',
79+
);
80+
});
81+
82+
test('PathNice.ext()', () => {
83+
expect(path('./src/index.js').ext()).toBe('.js');
84+
('.js');
85+
86+
expect(path('./LICENSE').ext()).toBe('');
87+
('');
88+
89+
expect(path('.bashrc').ext()).toBe('');
90+
('');
91+
92+
expect(pathPosix('./src/index.js').ext('.ts').raw).toBe('./src/index.ts');
93+
expect(pathWin32('./src/index.js').ext('.ts').raw).toBe('./src\\index.ts');
94+
95+
expect(
96+
pathPosix('./public/help.htm').ext((ext) => (ext === '.htm' ? '.html' : ext)).raw,
97+
).toBe('./public/help.html');
98+
expect(
99+
pathWin32('./public/help.htm').ext((ext) => (ext === '.htm' ? '.html' : ext)).raw,
100+
).toBe('./public\\help.html');
101+
102+
expect(pathPosix('./README.md').ext(null).raw).toBe('./README');
103+
expect(pathWin32('./README.md').ext(null).raw).toBe('.\\README');
104+
});
105+
106+
test('PathNice.separator()', () => {
107+
expect(path('/home/fuu/data.json').separator()).toBe('/');
108+
109+
expect(path('C:\\Windows\\System32').separator()).toBe('\\');
110+
111+
expect(path('index.js').separator()).toBe('none');
112+
113+
expect(path('C:\\Windows/System32').separator()).toBe('hybrid');
114+
115+
expect(path('/home/fuu/data.json').separator('\\').raw).toBe(
116+
'\\home\\fuu\\data.json',
117+
);
118+
119+
expect(path('C:\\Windows\\System32').separator('/').raw).toBe('C:/Windows/System32');
120+
});
121+
122+
test('PathNice.prefixFilename()', () => {
123+
expect(pathPosix('data/January').prefixFilename('2021-').raw).toBe(
124+
'data/2021-January',
125+
);
126+
expect(pathWin32('data/January').prefixFilename('2021-').raw).toBe(
127+
'data\\2021-January',
128+
);
129+
});
130+
131+
// test('PathNice.', () => {});

0 commit comments

Comments
 (0)