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 pathbson_corpus_tests.js
91 lines (78 loc) · 2.6 KB
/
bson_corpus_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
'use strict';
var BSON = require('../..'),
Decimal128 = BSON.Decimal128,
fs = require('fs'),
expect = require('chai').expect,
path = require('path'),
createBSON = require('../utils'),
bson = createBSON();
var deserializeOptions = {
bsonRegExp: true,
promoteLongs: true,
promoteValues: false
};
var serializeOptions = {
ignoreUndefined: false
};
// tests from the corpus that we need to skip, and explanations why
var skip = {
'NaN with payload':
'passing this would require building a custom type to store the NaN payload data.'
};
function findScenarios() {
return fs
.readdirSync(path.join(__dirname, 'specs/bson-corpus'))
.filter(x => x.indexOf('json') !== -1)
.map(x => JSON.parse(fs.readFileSync(path.join(__dirname, 'specs/bson-corpus', x), 'utf8')));
}
describe('BSON Corpus', function() {
findScenarios().forEach(scenario => {
describe(scenario.description, function() {
if (scenario.valid) {
describe('valid', function() {
scenario.valid.forEach(v => {
if (skip.hasOwnProperty(v.description)) {
it.skip(v.description, () => {});
return;
}
it(v.description, function() {
var cB = new Buffer(v.canonical_bson, 'hex');
if (v.degenerate_bson) var dB = new Buffer(v.degenerate_bson, 'hex');
if (v.converted_bson) var convB = new Buffer(v.converted_bson, 'hex');
var roundTripped = bson.serialize(
bson.deserialize(cB, deserializeOptions),
serializeOptions
);
if (scenario.deprecated) expect(convB).to.deep.equal(roundTripped);
else expect(cB).to.deep.equal(roundTripped);
if (dB) {
expect(cB).to.deep.equal(
bson.serialize(bson.deserialize(dB, deserializeOptions), serializeOptions)
);
}
});
});
});
}
if (scenario.decodeErrors) {
describe('decodeErrors', function() {
scenario.decodeErrors.forEach(d => {
it(d.description, function() {
var B = new Buffer(d.bson, 'hex');
expect(() => bson.deserialize(B, deserializeOptions)).to.throw();
});
});
});
}
if (scenario.parseErrors) {
describe('parseErrors', function() {
scenario.parseErrors.forEach(p => {
it(p.description, function() {
expect(() => Decimal128.fromString(scenario.string)).to.throw();
});
});
});
}
});
});
});