aboutsummaryrefslogtreecommitdiff
path: root/node_modules/structured-clone/test/test.js
blob: 1a201e572edca71c4d91f9ebceb8394e2cbc8f0c (plain)
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
var test = require('tape');

test('deep cloning test', function (t) {
  t.plan(12)

  var clone = require('../')

  var a = { }
  a.b = a
  a.c = { $ref: '$' }
  a.d = new Buffer([0xde, 0xad])
  a.e = [ a, a.b ]
  a.f = new Date()
  a.g = /ab+a/i

  var a = clone(a);

  t.ok(a.b == a);
  t.ok(a.c.$ref == '$')
  t.ok(Buffer.isBuffer(a.d))
  t.ok(a.d[0] == 0xde)
  t.ok(a.d[1] == 0xad)
  t.ok(a.d.length == 2);
  t.ok(Array.isArray(a.e));
  t.ok(a.e.length == 2);
  t.ok(a.e[0] == a);
  t.ok(a.e[1] == a.b);
  t.ok(a.f instanceof Date);
  t.ok(a.g instanceof RegExp);
})

test('serializing test', function (t) {
  t.plan(14)

  var clone = require('../')

  var a = { }
  a.b = a
  a.c = { $ref: '$' }
  a.d = new Buffer([0xde, 0xad])
  a.e = [ a, a.b ]
  a.f = new Date()
  a.g = /ab+a/i

  var buf = clone.serialize(a);
  t.ok(buf.length, 'Buffer has length')
  t.ok(Buffer.isBuffer(buf), 'Buffer has length')

  var a = clone.deserialize(buf);

  t.ok(a.b == a);
  t.ok(a.c.$ref == '$')
  t.ok(Buffer.isBuffer(a.d))
  t.ok(a.d[0] == 0xde)
  t.ok(a.d[1] == 0xad)
  t.ok(a.d.length == 2);
  t.ok(Array.isArray(a.e));
  t.ok(a.e.length == 2);
  t.ok(a.e[0] == a);
  t.ok(a.e[1] == a.b);
  t.ok(a.f instanceof Date);
  t.ok(a.g instanceof RegExp);
})

test('deep copy root object', function (t) {
  t.plan(3);

  var clone = require('../');

  var buf = clone(new Buffer([0xca, 0xfe, 0xba, 0xbe]));
  t.ok(Buffer.isBuffer(buf));
  t.ok(buf.length == 4);
  t.ok(buf.readUInt32BE(0) == 0xcafebabe);
});

test('serializing root object', function (t) {
  t.plan(3);

  var clone = require('../');

  var buf = clone.deserialize(clone.serialize(new Buffer([0xca, 0xfe, 0xba, 0xbe])));
  t.ok(Buffer.isBuffer(buf));
  t.ok(buf.length == 4);
  t.ok(buf.readUInt32BE(0) == 0xcafebabe);
});


test('errors', function (t) {
  t.plan(4);

  var clone = require('../');

  var err = clone(new Error('boo!'));
  t.ok(err instanceof Error);
  t.ok(err.message == 'boo!');

  var err = clone.deserialize(clone.serialize(new Error('boo!')));
  t.ok(err instanceof Error);
  t.ok(err.message == 'boo!');
});