-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathvar-array_test.js
More file actions
109 lines (89 loc) · 3.31 KB
/
var-array_test.js
File metadata and controls
109 lines (89 loc) · 3.31 KB
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
import { XdrReader } from '../../src/serialization/xdr-reader';
import { XdrWriter } from '../../src/serialization/xdr-writer';
const subject = new XDR.VarArray(XDR.Int, 2);
describe('VarArray#read', function () {
it('decodes correctly', function () {
expect(read([0x00, 0x00, 0x00, 0x00])).to.eql([]);
expect(read([0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00])).to.eql([0]);
expect(read([0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01])).to.eql([1]);
expect(
read([
0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
])
).to.eql([0, 1]);
expect(
read([
0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01
])
).to.eql([1, 1]);
});
it('throws read error when the encoded array is too large', function () {
expect(() => read([0x00, 0x00, 0x00, 0x03])).to.throw(/read error/i);
});
it('fast-fails when declared length cannot fit remaining bytes for fixed-size child', function () {
let calls = 0;
class FixedSizeChild {
static read() {
calls += 1;
return 0;
}
static write() {}
static isValid() {
return true;
}
}
const arr = new XDR.VarArray(FixedSizeChild, 10);
const io = new XdrReader([0x00, 0x00, 0x00, 0x02]);
expect(() => arr.read(io)).to.throw(/insufficient bytes/i);
expect(calls).to.eql(0);
});
function read(bytes) {
let io = new XdrReader(bytes);
return subject.read(io);
}
});
describe('VarArray#write', function () {
it('encodes correctly', function () {
expect(write([])).to.eql([0x00, 0x00, 0x00, 0x00]);
expect(write([0])).to.eql([0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]);
expect(write([0, 1])).to.eql([
0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
]);
});
it('throws a write error if the value is too large', function () {
expect(() => write([1, 2, 3])).to.throw(/write error/i);
});
it('throws a write error if a child element is of the wrong type', function () {
expect(() => write([1, null])).to.throw(/write error/i);
expect(() => write([1, undefined])).to.throw(/write error/i);
expect(() => write([1, 'hi'])).to.throw(/write error/i);
});
function write(value) {
let io = new XdrWriter(256);
subject.write(value, io);
return io.toArray();
}
});
describe('VarArray#isValid', function () {
it('returns true for an array of the correct sizes with the correct types', function () {
expect(subject.isValid([])).to.be.true;
expect(subject.isValid([1])).to.be.true;
expect(subject.isValid([1, 2])).to.be.true;
});
it('returns false for arrays of the wrong size', function () {
expect(subject.isValid([1, 2, 3])).to.be.false;
});
it('returns false if a child element is invalid for the child type', function () {
expect(subject.isValid([1, null])).to.be.false;
expect(subject.isValid([1, undefined])).to.be.false;
expect(subject.isValid([1, 'hello'])).to.be.false;
expect(subject.isValid([1, []])).to.be.false;
expect(subject.isValid([1, {}])).to.be.false;
});
});
describe('VarArray#constructor', function () {
let subject = new XDR.VarArray(XDR.Int);
it('defaults to max length of a uint max value', function () {
expect(subject._maxLength).to.eql(Math.pow(2, 32) - 1);
});
});