This repository was archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmap_tests.js
127 lines (112 loc) · 3.78 KB
/
map_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
'use strict';
var BSON = require('../..');
var M = BSON.Map;
var createBSON = require('../utils');
const expect = require('chai').expect;
describe('Map', function() {
/**
* @ignore
*/
it('should correctly exercise the map', function(done) {
var m = new M([['a', 1], ['b', 2]]);
expect(m.has('a')).to.be.ok;
expect(m.has('b')).to.be.ok;
expect(1).to.equal(m.get('a'));
expect(2).to.equal(m.get('b'));
expect(m.set('a', 3) === m).to.be.ok;
expect(m.has('a')).to.be.ok;
expect(3).to.equal(m.get('a'));
// Get the values
var iterator = m.values();
expect(3).to.equal(iterator.next().value);
expect(2).to.equal(iterator.next().value);
expect(true).to.equal(iterator.next().done);
// Get the entries
iterator = m.entries();
expect(['a', 3]).to.deep.equal(iterator.next().value);
expect(['b', 2]).to.deep.equal(iterator.next().value);
expect(true).to.deep.equal(iterator.next().done);
// Get the keys
iterator = m.keys();
expect('a').to.deep.equal(iterator.next().value);
expect('b').to.deep.equal(iterator.next().value);
expect(true).to.deep.equal(iterator.next().done);
// Collect values
var values = [];
// Get entries forEach
m.forEach(function(value, key, map) {
expect(value != null).to.be.ok;
expect(key != null).to.be.ok;
expect(map != null).to.be.ok;
expect(m === this).to.be.ok;
values.push([key, value]);
}, m);
expect([['a', 3], ['b', 2]]).to.deep.equal(values);
// Modify the state
expect(true).to.equal(m.delete('a'));
m.set('c', 5);
m.set('a', 7);
// Validate order is preserved
// Get the keys
iterator = m.keys();
expect('b').to.deep.equal(iterator.next().value);
expect('c').to.deep.equal(iterator.next().value);
expect('a').to.deep.equal(iterator.next().value);
expect(true).to.deep.equal(iterator.next().done);
// Get the entries
iterator = m.entries();
expect(['b', 2]).to.deep.equal(iterator.next().value);
expect(['c', 5]).to.deep.equal(iterator.next().value);
expect(['a', 7]).to.deep.equal(iterator.next().value);
expect(true).to.deep.equal(iterator.next().done);
// Get the values
iterator = m.values();
expect(2).to.equal(iterator.next().value);
expect(5).to.equal(iterator.next().value);
expect(7).to.equal(iterator.next().value);
expect(true).to.equal(iterator.next().done);
done();
});
/**
* @ignore
*/
it('should serialize a map', function(done) {
// Serialize top level map only
var m = new M([['a', 1], ['b', 2]]);
var bson = createBSON();
// Serialize the map
var data = bson.serialize(m, false, true);
// Deserialize the data
var object = bson.deserialize(data);
expect({ a: 1, b: 2 }).to.deep.equal(object);
// Serialize nested maps
var m1 = new M([['a', 1], ['b', 2]]);
m = new M([['c', m1]]);
// Serialize the map
data = bson.serialize(m, false, true);
// Deserialize the data
object = bson.deserialize(data);
expect({ c: { a: 1, b: 2 } }).to.deep.equal(object);
done();
// Serialize top level map only
m = new M([['1', 1], ['0', 2]]);
bson = createBSON();
// Serialize the map, validating that the order in the resulting BSON is preserved
data = bson.serialize(m, false, true);
expect('13000000103100010000001030000200000000').to.equal(data.toString('hex'));
});
/**
* @ignore
*/
it('should not crash due to object that looks like map', function(done) {
// Serialize top level map only
var m = { entries: 'test' };
var bson = createBSON();
// Serialize the map
var data = bson.serialize(m, false, true);
// Deserialize the data
var object = bson.deserialize(data);
expect(m).to.deep.equal(object);
done();
});
});