UNPKG

3.72 kBJavaScriptView Raw
1var assert = require('assert');
2var pathParse = require('./index');
3
4var winParseTests = [
5 [{ root: 'C:\\', dir: 'C:\\path\\dir', base: 'index.html', ext: '.html', name: 'index' }, 'C:\\path\\dir\\index.html'],
6 [{ root: 'C:\\', dir: 'C:\\another_path\\DIR\\1\\2\\33', base: 'index', ext: '', name: 'index' }, 'C:\\another_path\\DIR\\1\\2\\33\\index'],
7 [{ root: '', dir: 'another_path\\DIR with spaces\\1\\2\\33', base: 'index', ext: '', name: 'index' }, 'another_path\\DIR with spaces\\1\\2\\33\\index'],
8 [{ root: '\\', dir: '\\foo', base: 'C:', ext: '', name: 'C:' }, '\\foo\\C:'],
9 [{ root: '', dir: '', base: 'file', ext: '', name: 'file' }, 'file'],
10 [{ root: '', dir: '.', base: 'file', ext: '', name: 'file' }, '.\\file'],
11
12 // unc
13 [{ root: '\\\\server\\share\\', dir: '\\\\server\\share\\', base: 'file_path', ext: '', name: 'file_path' }, '\\\\server\\share\\file_path'],
14 [{ root: '\\\\server two\\shared folder\\', dir: '\\\\server two\\shared folder\\', base: 'file path.zip', ext: '.zip', name: 'file path' }, '\\\\server two\\shared folder\\file path.zip'],
15 [{ root: '\\\\teela\\admin$\\', dir: '\\\\teela\\admin$\\', base: 'system32', ext: '', name: 'system32' }, '\\\\teela\\admin$\\system32'],
16 [{ root: '\\\\?\\UNC\\', dir: '\\\\?\\UNC\\server', base: 'share', ext: '', name: 'share' }, '\\\\?\\UNC\\server\\share']
17];
18
19var winSpecialCaseFormatTests = [
20 [{dir: 'some\\dir'}, 'some\\dir\\'],
21 [{base: 'index.html'}, 'index.html'],
22 [{}, '']
23];
24
25var unixParseTests = [
26 [{ root: '/', dir: '/home/user/dir', base: 'file.txt', ext: '.txt', name: 'file' }, '/home/user/dir/file.txt'],
27 [{ root: '/', dir: '/home/user/a dir', base: 'another File.zip', ext: '.zip', name: 'another File' }, '/home/user/a dir/another File.zip'],
28 [{ root: '/', dir: '/home/user/a dir/', base: 'another&File.', ext: '.', name: 'another&File' }, '/home/user/a dir//another&File.'],
29 [{ root: '/', dir: '/home/user/a$$$dir/', base: 'another File.zip', ext: '.zip', name: 'another File' }, '/home/user/a$$$dir//another File.zip'],
30 [{ root: '', dir: 'user/dir', base: 'another File.zip', ext: '.zip', name: 'another File' }, 'user/dir/another File.zip'],
31 [{ root: '', dir: '', base: 'file', ext: '', name: 'file' }, 'file'],
32 [{ root: '', dir: '', base: '.\\file', ext: '', name: '.\\file' }, '.\\file'],
33 [{ root: '', dir: '.', base: 'file', ext: '', name: 'file' }, './file'],
34 [{ root: '', dir: '', base: 'C:\\foo', ext: '', name: 'C:\\foo' }, 'C:\\foo']
35];
36
37var unixSpecialCaseFormatTests = [
38 [{dir: 'some/dir'}, 'some/dir/'],
39 [{base: 'index.html'}, 'index.html'],
40 [{}, '']
41];
42
43var errors = [
44 {input: null, message: /Parameter 'pathString' must be a string, not/},
45 {input: {}, message: /Parameter 'pathString' must be a string, not object/},
46 {input: true, message: /Parameter 'pathString' must be a string, not boolean/},
47 {input: 1, message: /Parameter 'pathString' must be a string, not number/},
48 {input: undefined, message: /Parameter 'pathString' must be a string, not undefined/},
49];
50
51checkParseFormat(pathParse.win32, winParseTests);
52checkParseFormat(pathParse.posix, unixParseTests);
53checkErrors(pathParse.win32);
54checkErrors(pathParse.posix);
55
56function checkErrors(parse) {
57 errors.forEach(function(errorCase) {
58 try {
59 parse(errorCase.input);
60 } catch(err) {
61 assert.ok(err instanceof TypeError);
62 assert.ok(
63 errorCase.message.test(err.message),
64 'expected ' + errorCase.message + ' to match ' + err.message
65 );
66 return;
67 }
68
69 assert.fail('should have thrown');
70 });
71}
72
73function checkParseFormat(parse, testCases) {
74 testCases.forEach(function(testCase) {
75 assert.deepEqual(parse(testCase[1]), testCase[0]);
76 });
77}