test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. var Mode = require('../');
  2. var assert = require('assert');
  3. describe('stat-mode', function () {
  4. it('should export the `Mode` constructor', function () {
  5. assert.equal('function', typeof Mode);
  6. assert.equal('Mode', Mode.name);
  7. });
  8. describe('Mode', function () {
  9. it('should return a `Mode` instance with `new`', function () {
  10. var m = new Mode({});
  11. assert(m instanceof Mode);
  12. });
  13. it('should return a `Mode` instance without `new`', function () {
  14. var m = Mode({});
  15. assert(m instanceof Mode);
  16. });
  17. it('should throw an Error if no `stat` object is passed in', function () {
  18. try {
  19. new Mode();
  20. assert(false, 'unreachable');
  21. } catch (e) {
  22. assert.equal('must pass in a "stat" object', e.message);
  23. }
  24. });
  25. [
  26. {
  27. mode: 33188 /* 0100644 */,
  28. octal: '0644',
  29. string: '-rw-r--r--',
  30. type: 'file'
  31. },
  32. {
  33. mode: 16877 /* 040755 */,
  34. octal: '0755',
  35. string: 'drwxr-xr-x',
  36. type: 'directory'
  37. },
  38. {
  39. mode: 16832 /* 040700 */,
  40. octal: '0700',
  41. string: 'drwx------',
  42. type: 'directory'
  43. },
  44. {
  45. mode: 41325 /* 0120555 */,
  46. octal: '0555',
  47. string: 'lr-xr-xr-x',
  48. type: 'symbolicLink'
  49. },
  50. {
  51. mode: 8592 /* 020620 */,
  52. octal: '0620',
  53. string: 'crw--w----',
  54. type: 'characterDevice'
  55. },
  56. {
  57. mode: 24960 /* 060600 */,
  58. octal: '0600',
  59. string: 'brw-------',
  60. type: 'blockDevice'
  61. },
  62. {
  63. mode: 4516 /* 010644 */,
  64. octal: '0644',
  65. string: 'prw-r--r--',
  66. type: 'FIFO'
  67. }
  68. ].forEach(function (test) {
  69. var m = new Mode(test);
  70. var isFn = 'is' + test.type[0].toUpperCase() + test.type.substring(1);
  71. var strMode = m.toString();
  72. var opposite = test.type == 'file' ? 'isDirectory' : 'isFile';
  73. var first = test.type == 'file' ? 'd' : '-';
  74. describe('input: 0' + test.mode.toString(8), function () {
  75. describe('#toString()', function () {
  76. it('should equal "' + test.string + '"', function () {
  77. assert.equal(m.toString(), test.string);
  78. });
  79. });
  80. describe('#toOctal()', function () {
  81. it('should equal "' + test.octal + '"', function () {
  82. assert.equal(m.toOctal(), test.octal);
  83. });
  84. });
  85. describe('#' + isFn + '()', function () {
  86. it('should return `true` for #' + isFn + '()', function () {
  87. assert.ok(m[isFn]());
  88. });
  89. it('should remain "' + strMode + '" after #' + isFn + '(true) (gh-2)', function () {
  90. assert.equal(true, m[isFn](true));
  91. assert.equal(strMode, m.toString());
  92. });
  93. });
  94. describe('#' + opposite + '(true)', function () {
  95. it('should return `false` for `#' + opposite + '(true)`', function () {
  96. assert.equal(false, m[opposite](true));
  97. });
  98. it('should be "' + first + m.toString().substring(1) + '" after #' + opposite + '(true) (gh-2)', function () {
  99. assert.equal(first + m.toString().substring(1), m.toString());
  100. });
  101. });
  102. });
  103. });
  104. });
  105. });