forked from primus/ejson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
92 lines (76 loc) · 2.54 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
describe('EJSON', function () {
'use strict';
var assume = require('assume')
, EJSON = require('./');
it('has the `_canonicalStringify` method', function () {
assume(EJSON._canonicalStringify).to.be.a('function');
});
it('is keyOrderSensitive', function () {
assume(EJSON.equals({
a: {b: 1, c: 2},
d: {e: 3, f: 4}
}, {
d: {f: 4, e: 3},
a: {c: 2, b: 1}
})).to.equal(true);
assume(EJSON.equals({
a: {b: 1, c: 2},
d: {e: 3, f: 4}
}, {
a: {c: 2, b: 1},
d: {f: 4, e: 3}
}, { keyOrderSensitive: true })).to.equal(false);
assume(EJSON.equals({
a: {}
}, {
a: {b: 2}
}, { keyOrderSensitive: true })).to.equal(false);
assume(EJSON.equals({
a: {b: 2}
}, {
a: {}
}, { keyOrderSensitive: true })).to.equal(false);
});
it('supports nesting and literal', function () {
var d = new Date()
, obj = {$date: d};
var eObj = EJSON.toJSONValue(obj)
, roundTrip = EJSON.fromJSONValue(eObj);
assume(obj).to.deep.equal(roundTrip);
});
it('equals correctly', function () {
assume(EJSON.equals({a: 1, b: 2, c: 3}, {a: 1, c: 3, b: 2})).to.equal(true);
assume(EJSON.equals({a: 1, b: 2}, {a: 1, c: 3, b: 2})).to.equal(false);
assume(EJSON.equals({a: 1, b: 2, c: 3}, {a: 1, b: 2})).to.equal(false);
assume(EJSON.equals({a: 1, b: 2, c: 3}, {a: 1, c: 3, b: 4})).to.equal(false);
assume(EJSON.equals({a: {}}, {a: {b:2}})).to.equal(false);
assume(EJSON.equals({a: {b:2}}, {a: {}})).to.equal(false);
});
it('equality and falsiness', function () {
assume(EJSON.equals(null, null)).to.equal(true);
assume(EJSON.equals(undefined, undefined)).to.equal(true);
assume(EJSON.equals({foo: 'foo'}, null)).to.equal(false);
assume(EJSON.equals(null, {foo: 'foo'})).to.equal(false);
assume(EJSON.equals(undefined, {foo: 'foo'})).to.equal(false);
assume(EJSON.equals({foo: 'foo'}, undefined)).to.equal(false);
});
it('clones', function () {
function cloneTest(x, identical) {
var y = EJSON.clone(x);
assume(y).to.deep.equal(x);
assume(x === y).to.equal(!!identical);
}
cloneTest(null, true);
cloneTest(undefined, true);
cloneTest(42, true);
cloneTest('asdf', true);
cloneTest([1, 2, 3]);
cloneTest([1, 'fasdf', {foo: 42}]);
cloneTest({x: 42, y: 'asdf'});
function testCloneArgs(/*arguments*/) {
var clonedArgs = EJSON.clone(arguments);
assume(clonedArgs).to.deep.equal([1, 2, 'foo', [4]]);
}
testCloneArgs(1, 2, 'foo', [4]);
});
});