test-path-extname.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict';
  2. var tape = require('tape');
  3. var path = require('../');
  4. var slashRE = /\//g;
  5. var pairs = [
  6. [__filename, '.js'],
  7. ['', ''],
  8. ['/path/to/file', ''],
  9. ['/path/to/file.ext', '.ext'],
  10. ['/path.to/file.ext', '.ext'],
  11. ['/path.to/file', ''],
  12. ['/path.to/.file', ''],
  13. ['/path.to/.file.ext', '.ext'],
  14. ['/path/to/f.ext', '.ext'],
  15. ['/path/to/..ext', '.ext'],
  16. ['/path/to/..', ''],
  17. ['file', ''],
  18. ['file.ext', '.ext'],
  19. ['.file', ''],
  20. ['.file.ext', '.ext'],
  21. ['/file', ''],
  22. ['/file.ext', '.ext'],
  23. ['/.file', ''],
  24. ['/.file.ext', '.ext'],
  25. ['.path/file.ext', '.ext'],
  26. ['file.ext.ext', '.ext'],
  27. ['file.', '.'],
  28. ['.', ''],
  29. ['./', ''],
  30. ['.file.ext', '.ext'],
  31. ['.file', ''],
  32. ['.file.', '.'],
  33. ['.file..', '.'],
  34. ['..', ''],
  35. ['../', ''],
  36. ['..file.ext', '.ext'],
  37. ['..file', '.file'],
  38. ['..file.', '.'],
  39. ['..file..', '.'],
  40. ['...', '.'],
  41. ['...ext', '.ext'],
  42. ['....', '.'],
  43. ['file.ext/', '.ext'],
  44. ['file.ext//', '.ext'],
  45. ['file/', ''],
  46. ['file//', ''],
  47. ['file./', '.'],
  48. ['file.//', '.'] ];
  49. tape('path.posix.extname', function (t) {
  50. pairs.forEach(function (p) {
  51. var input = p[0];
  52. var expected = p[1];
  53. t.strictEqual(expected, path.posix.extname(input));
  54. });
  55. t.end();
  56. });
  57. tape('path.win32.extname', { skip: true }, function (t) {
  58. pairs.forEach(function (p) {
  59. var input = p[0].replace(slashRE, '\\');
  60. var expected = p[1];
  61. t.strictEqual(expected, path.win32.extname(input));
  62. t.strictEqual(expected, path.win32.extname("C:" + input));
  63. });
  64. t.end();
  65. });
  66. tape('path.win32.extname backslash', { skip: true }, function (t) {
  67. // On Windows, backslash is a path separator.
  68. t.strictEqual(path.win32.extname('.\\'), '');
  69. t.strictEqual(path.win32.extname('..\\'), '');
  70. t.strictEqual(path.win32.extname('file.ext\\'), '.ext');
  71. t.strictEqual(path.win32.extname('file.ext\\\\'), '.ext');
  72. t.strictEqual(path.win32.extname('file\\'), '');
  73. t.strictEqual(path.win32.extname('file\\\\'), '');
  74. t.strictEqual(path.win32.extname('file.\\'), '.');
  75. t.strictEqual(path.win32.extname('file.\\\\'), '.');
  76. t.end();
  77. });
  78. tape('path.posix.extname backslash', function (t) {
  79. // On *nix, backslash is a valid name component like any other character.
  80. t.strictEqual(path.posix.extname('.\\'), '');
  81. t.strictEqual(path.posix.extname('..\\'), '.\\');
  82. t.strictEqual(path.posix.extname('file.ext\\'), '.ext\\');
  83. t.strictEqual(path.posix.extname('file.ext\\\\'), '.ext\\\\');
  84. t.strictEqual(path.posix.extname('file\\'), '');
  85. t.strictEqual(path.posix.extname('file\\\\'), '');
  86. t.strictEqual(path.posix.extname('file.\\'), '.\\');
  87. t.strictEqual(path.posix.extname('file.\\\\'), '.\\\\');
  88. t.end();
  89. });