3 var umask = require('..');
5 var Code = require('code');
6 var Lab = require('lab');
7 var lab = Lab.script();
10 var describe = lab.describe;
12 var expect = Code.expect;
14 describe('validates umask', function () {
15 // signature of validator: validate(obj, key, val)
16 // store valid value in obj[key]
17 // return false if invalid
19 it('accepts numbers', function (done) {
23 result = umask.validate(o, 'umask', 0);
24 expect(result).to.equal(true);
25 expect(o.umask).to.equal(0);
27 result = umask.validate(o, 'umask', 511);
28 expect(result).to.equal(true);
29 expect(o.umask).to.equal(511);
33 it('accepts strings', function (done) {
37 result = umask.validate(o, 'umask', "0");
38 expect(result).to.equal(true);
39 expect(o.umask).to.equal(0);
41 result = umask.validate(o, 'umask', "0777");
42 expect(result).to.equal(true);
43 expect(o.umask).to.equal(511);
48 it('rejects other types', function (done) {
49 expect(umask.validate(undefined, undefined, false)).to.equal(false);
50 expect(umask.validate(undefined, undefined, {})).to.equal(false);
55 it('rejects non-octalish strings', function (done) {
56 expect(umask.validate(undefined, undefined, "1")).to.equal(false);
61 it('rejects NaN strings', function (done) {
62 expect(umask.validate(undefined, undefined, NaN)).to.equal(false);
68 describe('umask to string', function () {
69 it("converts umask to string", function (done) {
70 expect(umask.toString(0)).to.equal("0000");
71 expect(umask.toString(1)).to.equal("0001");
72 expect(umask.toString(7)).to.equal("0007");
73 expect(umask.toString(8)).to.equal("0010");
74 expect(umask.toString(511)).to.equal("0777");
75 expect(umask.toString(18)).to.equal("0022");
76 expect(umask.toString(16)).to.equal("0020");
81 describe('umask from string', function () {
82 it('converts valid values', function (done) {
83 expect(umask.fromString("0000")).to.equal(0);
84 expect(umask.fromString("0")).to.equal(0);
85 expect(umask.fromString("0777")).to.equal(511);
86 expect(umask.fromString("0024")).to.equal(20);
88 expect(umask.fromString(0)).to.equal(0);
89 expect(umask.fromString(20)).to.equal(20);
90 expect(umask.fromString(21)).to.equal(21);
91 expect(umask.fromString(511)).to.equal(511);
96 it('converts valid values', function (done) {
97 expect(umask.fromString("0000")).to.equal(0);
98 expect(umask.fromString("0")).to.equal(0);
99 expect(umask.fromString("010")).to.equal(8);
100 expect(umask.fromString("0777")).to.equal(511);
101 expect(umask.fromString("0024")).to.equal(20);
103 expect(umask.fromString("8")).to.equal(8);
104 expect(umask.fromString("9")).to.equal(9);
105 expect(umask.fromString("18")).to.equal(18);
106 expect(umask.fromString("16")).to.equal(16);
108 expect(umask.fromString(0)).to.equal(0);
109 expect(umask.fromString(20)).to.equal(20);
110 expect(umask.fromString(21)).to.equal(21);
111 expect(umask.fromString(511)).to.equal(511);
113 expect(umask.fromString(0.1)).to.equal(0);
114 expect(umask.fromString(511.1)).to.equal(511);
119 it('errors on empty string', function (done) {
120 umask.fromString("", function (err, val) {
121 expect(err.message).to.equal('Expected octal string, got "", defaulting to "0022"');
122 expect(val).to.equal(18);
127 it('errors on invalid octal string', function (done) {
128 umask.fromString("099", function (err, val) {
129 expect(err.message).to.equal('Expected octal string, got "099", defaulting to "0022"');
130 expect(val).to.equal(18);
135 it('errors when non-string, non-number (boolean)', function (done) {
136 umask.fromString(false, function (err, val) {
137 expect(err.message).to.equal('Expected number or octal string, got false, defaulting to "0022"');
138 expect(val).to.equal(18);
143 it('errors when non-string, non-number (object)', function (done) {
144 umask.fromString({}, function (err, val) {
145 expect(err.message).to.equal('Expected number or octal string, got {}, defaulting to "0022"');
146 expect(val).to.equal(18);
151 it('errors when out of range (<0)', function (done) {
152 umask.fromString(-1, function (err, val) {
153 expect(err.message).to.equal('Must be in range 0..511 (0000..0777), got -1');
154 expect(val).to.equal(18);
159 it('errors when out of range (>511)', function (done) {
160 umask.fromString(512, function (err, val) {
161 expect(err.message).to.equal('Must be in range 0..511 (0000..0777), got 512');
162 expect(val).to.equal(18);