|
| 1 | +'use strict'; |
| 2 | +/* |
| 3 | +======== A Handy Little Nodeunit Reference ======== |
| 4 | +https://github.com/caolan/nodeunit |
| 5 | +
|
| 6 | +Test methods: |
| 7 | + test.expect(numAssertions) |
| 8 | + test.done() |
| 9 | +Test assertions: |
| 10 | + test.ok(value, [message]) |
| 11 | + test.equal(actual, expected, [message]) |
| 12 | + test.notEqual(actual, expected, [message]) |
| 13 | + test.deepEqual(actual, expected, [message]) |
| 14 | + test.notDeepEqual(actual, expected, [message]) |
| 15 | + test.strictEqual(actual, expected, [message]) |
| 16 | + test.notStrictEqual(actual, expected, [message]) |
| 17 | + test.throws(block, [error], [message]) |
| 18 | + test.doesNotThrow(block, [error], [message]) |
| 19 | + test.ifError(value) |
| 20 | +*/ |
| 21 | +var setPath = require('./index.js'); |
| 22 | +var now = new Date(); |
| 23 | +var getDefaultObject = function () { |
| 24 | + return { |
| 25 | + nested: { |
| 26 | + thing: { |
| 27 | + foo: 'bar' |
| 28 | + }, |
| 29 | + is: { |
| 30 | + cool: true |
| 31 | + } |
| 32 | + }, |
| 33 | + dataUndefined: undefined, |
| 34 | + dataDate: now, |
| 35 | + dataNumber: 42, |
| 36 | + dataString: 'foo', |
| 37 | + dataNull: null, |
| 38 | + dataBoolean: true |
| 39 | + }; |
| 40 | +}; |
| 41 | + |
| 42 | +exports['object-path-set'] = { |
| 43 | + setUp: function (done) { |
| 44 | + // setup here |
| 45 | + done(); |
| 46 | + }, |
| 47 | + 'types': function (test) { |
| 48 | + var obj = getDefaultObject(); |
| 49 | + test.expect(5); |
| 50 | + obj = setPath(obj, 'dataUndefined', 'newValue'); |
| 51 | + test.equal(typeof obj.dataUndefined, 'string', 'typeof dataUndefined is wrong'); |
| 52 | + obj = setPath(obj, 'dataDate', 'newValue'); |
| 53 | + test.equal(typeof obj.dataDate, 'string', 'typeof dataDate is wrong'); |
| 54 | + obj = setPath(obj, 'nested', 'newValue'); |
| 55 | + test.equal(typeof obj.nested, 'string', 'typeof nested is wrong'); |
| 56 | + obj = setPath(obj, 'nested.foo', 'newValue'); |
| 57 | + test.equal(typeof obj.nested, 'object', 'typeof nested is wrong'); |
| 58 | + test.equal(typeof obj.nested.foo, 'string', 'typeof nested.foo is wrong'); |
| 59 | + test.done(); |
| 60 | + }, |
| 61 | + 'convert to object': function (test) { |
| 62 | + test.expect(5); |
| 63 | + test.deepEqual(setPath(1234, 'a', 42), {a: 42}, '1234 should have been converted to an object'); |
| 64 | + test.deepEqual(setPath(null, 'a', 42), {a: 42}, 'null should have been converted to an object'); |
| 65 | + test.deepEqual(setPath(true, 'a', 42), {a: 42}, 'true should have been converted to an object'); |
| 66 | + test.deepEqual(setPath({a: 123}, 'a.b', 42), {a: {b: 42}}, '{a: 123} should have been converted to an object'); |
| 67 | + test.deepEqual(setPath(null, 'a.b.c.d', null), {a:{b:{c:{d:null}}}}, 'null should have been converted to an object with a.b.c.d'); |
| 68 | + test.done(); |
| 69 | + }, |
| 70 | + 'delimiter': function (test) { |
| 71 | + test.expect(3); |
| 72 | + test.deepEqual(setPath({}, 'a|b|c|d', 42), {"a|b|c|d": 42}, 'should not have used my custom delimiter'); |
| 73 | + test.deepEqual(setPath({}, 'a|b|c|d', 42, '|'), {a:{b:{c:{d:42}}}}, 'should have used my custom delimiter'); |
| 74 | + test.deepEqual(setPath({}, 'a.b.c.d', 42, '|'), {"a.b.c.d": 42}, 'should have used my custom delimiter'); |
| 75 | + test.done(); |
| 76 | + }, |
| 77 | + 'values': function (test) { |
| 78 | + test.expect(4); |
| 79 | + test.deepEqual(setPath({}, 'a.b', 42), {a:{b: 42}}, 'expecting a number'); |
| 80 | + test.deepEqual(setPath({}, 'a.b', undefined), {a:{b: undefined}}, 'expecting undefined'); |
| 81 | + test.deepEqual(setPath({}, 'a.b', true), {a:{b: true}}, 'expecting a boolean'); |
| 82 | + test.deepEqual(setPath({}, 'a.b', 'wow'), {a:{b: 'wow'}}, 'expecting a string'); |
| 83 | + test.done(); |
| 84 | + }, |
| 85 | + 'multiple sets': function (test) { |
| 86 | + var obj; |
| 87 | + test.expect(1); |
| 88 | + obj = setPath(obj, 'a', 42); |
| 89 | + obj = setPath(obj, 'b', true); |
| 90 | + obj = setPath(obj, 'c.d', {}); |
| 91 | + obj = setPath(obj, 'c.d.e', {}); |
| 92 | + obj = setPath(obj, 'c.d.f', 'foo'); |
| 93 | + test.deepEqual(obj, {a: 42, b: true, c:{d:{e:{}, f:'foo'}}}, 'should have created a deep nested object'); |
| 94 | + test.done(); |
| 95 | + } |
| 96 | +}; |
0 commit comments