-
Notifications
You must be signed in to change notification settings - Fork 1
/
strDirname.test.ts
51 lines (45 loc) · 2.8 KB
/
strDirname.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { describe, it, expect } from 'vitest';
import strDirname from '../../src/strDirname.js';
import onWindowsOs from '../../src/utils/onWindowsOs.js';
let _ = onWindowsOs() ? '\\' : '/';
describe('str/strDirname', () => {
it('works', () => {
expect(strDirname('')).toEqual('');
expect(strDirname('/etc/passwd')).toEqual(`${_}etc`);
expect(strDirname('/etc/')).toEqual(_);
expect(strDirname('C:\\')).toEqual(`C:${_}`);
expect(strDirname('.')).toEqual('.');
expect(strDirname('..')).toEqual('..');
expect(strDirname('../../../../')).toEqual(`..${_}..${_}..`);
expect(strDirname('../../../../', 2)).toEqual(`..${_}..`);
expect(strDirname('/usr/local/lib', 2)).toEqual(`${_}usr`);
const path = '/home/hello\\dir\\otherDir/lastDir\\filename.txt';
expect(strDirname(path)).toEqual(`${_}home${_}hello${_}dir${_}otherDir${_}lastDir`);
expect(strDirname(path, -999)).toEqual(`${_}home${_}hello${_}dir${_}otherDir${_}lastDir`);
expect(strDirname(path, -1)).toEqual(`${_}home${_}hello${_}dir${_}otherDir${_}lastDir`);
expect(strDirname(path, 0)).toEqual(`${_}home${_}hello${_}dir${_}otherDir${_}lastDir`);
expect(strDirname(path, 1)).toEqual(`${_}home${_}hello${_}dir${_}otherDir${_}lastDir`);
expect(strDirname(path, 2)).toEqual(`${_}home${_}hello${_}dir${_}otherDir`);
expect(strDirname(path, 3)).toEqual(`${_}home${_}hello${_}dir`);
expect(strDirname(path, 4)).toEqual(`${_}home${_}hello`);
expect(strDirname(path, 5)).toEqual(`${_}home`);
expect(strDirname(path, 6)).toEqual(`${_}`);
expect(strDirname(path, 7)).toEqual(`${_}`);
expect(strDirname(path, 8)).toEqual(`${_}`);
expect(strDirname(path, 999)).toEqual(`${_}`);
_ = '*';
expect(strDirname(path, undefined, _)).toEqual(`${_}home${_}hello${_}dir${_}otherDir${_}lastDir`);
expect(strDirname(path, -999, _)).toEqual(`${_}home${_}hello${_}dir${_}otherDir${_}lastDir`);
expect(strDirname(path, -1, _)).toEqual(`${_}home${_}hello${_}dir${_}otherDir${_}lastDir`);
expect(strDirname(path, 0, _)).toEqual(`${_}home${_}hello${_}dir${_}otherDir${_}lastDir`);
expect(strDirname(path, 1, _)).toEqual(`${_}home${_}hello${_}dir${_}otherDir${_}lastDir`);
expect(strDirname(path, 2, _)).toEqual(`${_}home${_}hello${_}dir${_}otherDir`);
expect(strDirname(path, 3, _)).toEqual(`${_}home${_}hello${_}dir`);
expect(strDirname(path, 4, _)).toEqual(`${_}home${_}hello`);
expect(strDirname(path, 5, _)).toEqual(`${_}home`);
expect(strDirname(path, 6, _)).toEqual(`${_}`);
expect(strDirname(path, 7, _)).toEqual(`${_}`);
expect(strDirname(path, 8, _)).toEqual(`${_}`);
expect(strDirname(path, 999, _)).toEqual(`${_}`);
});
});