test.js 3.6 KB

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