simple.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. 'use strict';
  2. var umask = require('..');
  3. var Code = require('code');
  4. var Lab = require('lab');
  5. var lab = Lab.script();
  6. exports.lab = lab;
  7. var describe = lab.describe;
  8. var it = lab.it;
  9. var expect = Code.expect;
  10. describe('validates umask', function () {
  11. // signature of validator: validate(obj, key, val)
  12. // store valid value in obj[key]
  13. // return false if invalid
  14. it('accepts numbers', function (done) {
  15. var o = {},
  16. result = false;
  17. result = umask.validate(o, 'umask', 0);
  18. expect(result).to.equal(true);
  19. expect(o.umask).to.equal(0);
  20. result = umask.validate(o, 'umask', 511);
  21. expect(result).to.equal(true);
  22. expect(o.umask).to.equal(511);
  23. done();
  24. });
  25. it('accepts strings', function (done) {
  26. var o = {},
  27. result;
  28. result = umask.validate(o, 'umask', "0");
  29. expect(result).to.equal(true);
  30. expect(o.umask).to.equal(0);
  31. result = umask.validate(o, 'umask', "0777");
  32. expect(result).to.equal(true);
  33. expect(o.umask).to.equal(511);
  34. done();
  35. });
  36. it('rejects other types', function (done) {
  37. expect(umask.validate(undefined, undefined, false)).to.equal(false);
  38. expect(umask.validate(undefined, undefined, {})).to.equal(false);
  39. done();
  40. });
  41. it('rejects non-octalish strings', function (done) {
  42. expect(umask.validate(undefined, undefined, "1")).to.equal(false);
  43. done();
  44. });
  45. it('rejects NaN strings', function (done) {
  46. expect(umask.validate(undefined, undefined, NaN)).to.equal(false);
  47. done();
  48. });
  49. });
  50. describe('umask to string', function () {
  51. it("converts umask to string", function (done) {
  52. expect(umask.toString(0)).to.equal("0000");
  53. expect(umask.toString(1)).to.equal("0001");
  54. expect(umask.toString(7)).to.equal("0007");
  55. expect(umask.toString(8)).to.equal("0010");
  56. expect(umask.toString(511)).to.equal("0777");
  57. expect(umask.toString(18)).to.equal("0022");
  58. expect(umask.toString(16)).to.equal("0020");
  59. done();
  60. });
  61. });
  62. describe('umask from string', function () {
  63. it('converts valid values', function (done) {
  64. expect(umask.fromString("0000")).to.equal(0);
  65. expect(umask.fromString("0")).to.equal(0);
  66. expect(umask.fromString("0777")).to.equal(511);
  67. expect(umask.fromString("0024")).to.equal(20);
  68. expect(umask.fromString(0)).to.equal(0);
  69. expect(umask.fromString(20)).to.equal(20);
  70. expect(umask.fromString(21)).to.equal(21);
  71. expect(umask.fromString(511)).to.equal(511);
  72. done();
  73. });
  74. it('converts valid values', function (done) {
  75. expect(umask.fromString("0000")).to.equal(0);
  76. expect(umask.fromString("0")).to.equal(0);
  77. expect(umask.fromString("010")).to.equal(8);
  78. expect(umask.fromString("0777")).to.equal(511);
  79. expect(umask.fromString("0024")).to.equal(20);
  80. expect(umask.fromString("8")).to.equal(8);
  81. expect(umask.fromString("9")).to.equal(9);
  82. expect(umask.fromString("18")).to.equal(18);
  83. expect(umask.fromString("16")).to.equal(16);
  84. expect(umask.fromString(0)).to.equal(0);
  85. expect(umask.fromString(20)).to.equal(20);
  86. expect(umask.fromString(21)).to.equal(21);
  87. expect(umask.fromString(511)).to.equal(511);
  88. expect(umask.fromString(0.1)).to.equal(0);
  89. expect(umask.fromString(511.1)).to.equal(511);
  90. done();
  91. });
  92. it('errors on empty string', function (done) {
  93. umask.fromString("", function (err, val) {
  94. expect(err.message).to.equal('Expected octal string, got "", defaulting to "0022"');
  95. expect(val).to.equal(18);
  96. done();
  97. });
  98. });
  99. it('errors on invalid octal string', function (done) {
  100. umask.fromString("099", function (err, val) {
  101. expect(err.message).to.equal('Expected octal string, got "099", defaulting to "0022"');
  102. expect(val).to.equal(18);
  103. done();
  104. });
  105. });
  106. it('errors when non-string, non-number (boolean)', function (done) {
  107. umask.fromString(false, function (err, val) {
  108. expect(err.message).to.equal('Expected number or octal string, got false, defaulting to "0022"');
  109. expect(val).to.equal(18);
  110. done();
  111. });
  112. });
  113. it('errors when non-string, non-number (object)', function (done) {
  114. umask.fromString({}, function (err, val) {
  115. expect(err.message).to.equal('Expected number or octal string, got {}, defaulting to "0022"');
  116. expect(val).to.equal(18);
  117. done();
  118. });
  119. });
  120. it('errors when out of range (<0)', function (done) {
  121. umask.fromString(-1, function (err, val) {
  122. expect(err.message).to.equal('Must be in range 0..511 (0000..0777), got -1');
  123. expect(val).to.equal(18);
  124. done();
  125. });
  126. });
  127. it('errors when out of range (>511)', function (done) {
  128. umask.fromString(512, function (err, val) {
  129. expect(err.message).to.equal('Must be in range 0..511 (0000..0777), got 512');
  130. expect(val).to.equal(18);
  131. done();
  132. });
  133. });
  134. });