-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
62 lines (55 loc) · 1.82 KB
/
index.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
function every (buffer, predicate) {
if (Uint8Array && Uint8Array.prototype.every) {
return buffer.every(predicate);
} else {
for (let i = 0; i < buffer.length; i++) {
if (!predicate(buffer[i], i, buffer)) {
return false;
}
}
return true;
}
}
/**
* A simple plugin for Chai that compares byte arrays on per-element basis.
*
* @example
* expect(new Uint8Array([1, 2])).to.equalBytes([1, 2]);
* expect(new Uint8Array[65, 66, 67])).to.equalBytes('414243');
*/
function chaiBytes (chai) {
const Assertion = chai.Assertion;
Assertion.addMethod('equalBytes', function (expected) {
if (typeof expected === 'string') {
if (expected.length % 2 !== 0 || !/^[0-9a-f]*$/i.test(expected)) {
throw new TypeError('Invalid hex string: ' + expected);
}
} else if (expected.length === undefined) {
throw new TypeError('equalBytes consumes string, array, or array-like object; got none of those');
}
const actual = this._obj;
new Assertion(actual).to.be.a('uint8array');
let assert;
if (typeof expected === 'string') {
// expected value is a hex string
assert = expected.length === actual.length * 2 &&
every(actual, (x, i) => {
return x === parseInt(expected.substring(2 * i, 2 * i + 2), 16);
});
} else /* Got an array */ {
// expected value is an array
assert = expected.length === actual.length &&
every(actual, (x, i) => { return expected[i] === x; });
}
this.assert(
assert,
'expected #{this} to equal #{exp}',
'expected #{this} to not equal #{exp}',
expected
);
});
chai.assert.equalBytes = (value, expected, message) => {
return new Assertion(value, message, chai.assert.equalBytes, true).to.equalBytes(expected);
};
}
module.exports = chaiBytes;