-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtests.js
156 lines (134 loc) · 4.14 KB
/
tests.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
var assert = require("assert");
var arson = require("../index.js");
describe("encoding and decoding", function () {
it("should work with primitive values", function () {
function check(value) {
var enc = arson.encode(value);
var dec = arson.decode(enc);
if (isNaN(value)) {
assert.ok(isNaN(dec));
} else {
assert.deepEqual(value, dec);
}
}
check(0);
check(1234);
check(NaN);
check(Infinity);
check(-Infinity);
check(true);
check(false);
check("asdf");
check("");
check(null);
check(void 0);
check({ foo: void 0 });
});
it("should work with RegExp objects", function () {
var r1 = /asdf/ig;
var r2 = arson.decode(arson.encode(r1));
assert.ok(r2 instanceof RegExp);
assert.strictEqual(r2.source, "asdf");
assert.strictEqual(r2.ignoreCase, true);
assert.strictEqual(r2.multiline, false);
assert.strictEqual(r2.global, true);
assert.ok(r2.test("xxx-asdf-yyy"));
});
it("should work with Date objects", function () {
var d1 = new Date;
var d2 = arson.decode(arson.encode(d1));
assert.ok(d2 instanceof Date);
assert.strictEqual(+d1, +d2);
var dObj = arson.decode(arson.encode({ foo: d1, bar: [d1, d1] }));
assert.strictEqual(+d1, +dObj.foo);
assert.strictEqual(+d1, +dObj.bar[0]);
assert.strictEqual(+d1, +dObj.bar[1]);
assert.strictEqual(dObj.foo, dObj.bar[0]);
assert.strictEqual(dObj.foo, dObj.bar[1]);
});
it("should work with Buffer objects", function () {
var b = new Buffer("asdf");
var bb = arson.decode(arson.encode([b, b]));
assert.strictEqual(bb[0], bb[1]);
assert.ok(bb[0] instanceof Buffer);
assert.strictEqual(bb[0].toString("utf8"), "asdf");
});
typeof Map === "function" &&
typeof Array.from === "function" &&
it("should work with Map objects", function () {
var m1 = new Map;
var value = { foo: 42 };
m1.set(1234, value);
m1.set(value, m1);
m1.set(m1, "self");
assert.strictEqual(m1.get(m1.get(1234)), m1);
var m2 = arson.decode(arson.encode(m1));
assert.strictEqual(m2.get(m2.get(1234)), m2);
assert.strictEqual(m2.get(m2), "self");
});
typeof Set === "function" &&
typeof Array.from === "function" &&
it("should work with Set objects", function () {
var s1 = new Set;
s1.add(s1);
var s2 = arson.decode(arson.encode(s1));
assert.strictEqual(Array.from(s2)[0], s2);
s2.add(s1);
var s3 = arson.decode(arson.encode(s2));
var elems = Array.from(s3);
assert.strictEqual(elems.length, 2);
assert.notStrictEqual(elems[0], elems[1]);
elems.forEach(function (s) {
assert.ok(s.has(s));
});
});
it("should work for sparse arrays", function () {
function check(array) {
assert.deepEqual(array, arson.decode(arson.encode(array)));
}
check([,]);
check([,,]);
check([,,,]);
check([1,,3]);
check([1,,3,,4]);
check([1,,3,,4,,]);
});
it("should work with circular references", function () {
var obj = {};
obj.self = obj;
var result = arson.decode(arson.encode(obj));
assert.notStrictEqual(result, obj);
assert.strictEqual(result.self, result);
});
it("should work with repeated references", function () {
var a = {};
var b = { foo: 42 };
a.x = a.y = b;
var result = arson.decode(arson.encode(a));
assert.strictEqual(result.x, result.y);
});
it("should work with the global object", function () {
var copy = arson.decode(arson.encode(global));
assert.strictEqual(copy.global, copy);
});
it("should preserve identity of immutable tuples", function () {
var tuple = require("immutable-tuple").tuple;
arson.registerType("tuple", {
deconstruct: function (t) {
if (tuple.isTuple(t)) {
return Array.from(t);
}
},
reconstruct: function (entries) {
if (entries) {
return tuple.apply(null, entries);
}
}
});
var t1 = tuple(1, 2, tuple(3, 4), 5);
var input = ["asdf", t1, true];
var output = arson.decode(arson.encode(input));
assert.notStrictEqual(input, output);
assert.strictEqual(output[1], t1);
});
});