-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathStruct.js
146 lines (119 loc) · 3.45 KB
/
Struct.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import assert from 'assert';
import {Struct, String as StringT, Pointer, uint8, DecodeStream, EncodeStream} from 'restructure';
describe('Struct', function() {
describe('decode', function() {
it('should decode into an object', function() {
const struct = new Struct({
name: new StringT(uint8),
age: uint8
});
assert.deepEqual(struct.fromBuffer(Buffer.from('\x05devon\x15')), {
name: 'devon',
age: 21
});
});
it('should support process hook', function() {
const struct = new Struct({
name: new StringT(uint8),
age: uint8
});
struct.process = function() {
return this.canDrink = this.age >= 21;
};
assert.deepEqual(struct.fromBuffer(Buffer.from('\x05devon\x20')), {
name: 'devon',
age: 32,
canDrink: true
});
});
it('should support function keys', function() {
const struct = new Struct({
name: new StringT(uint8),
age: uint8,
canDrink() { return this.age >= 21; }
});
assert.deepEqual(struct.fromBuffer(Buffer.from('\x05devon\x20')), {
name: 'devon',
age: 32,
canDrink: true
});
});
});
describe('size', function() {
it('should compute the correct size', function() {
const struct = new Struct({
name: new StringT(uint8),
age: uint8
});
assert.equal(struct.size({name: 'devon', age: 21}), 7);
});
it('should compute the correct size with pointers', function() {
const struct = new Struct({
name: new StringT(uint8),
age: uint8,
ptr: new Pointer(uint8, new StringT(uint8))
});
const size = struct.size({
name: 'devon',
age: 21,
ptr: 'hello'
});
assert.equal(size, 14);
});
it('should get the correct size when no value is given', function() {
const struct = new Struct({
name: new StringT(4),
age: uint8
});
assert.equal(struct.size(), 5);
});
it('should throw when getting non-fixed length size and no value is given', function() {
const struct = new Struct({
name: new StringT(uint8),
age: uint8
});
assert.throws(() => struct.size(), /not a fixed size/i);
});
});
describe('encode', function() {
it('should encode objects to buffers', function() {
const struct = new Struct({
name: new StringT(uint8),
age: uint8
});
const buf = struct.toBuffer({
name: 'devon',
age: 21
});
assert.deepEqual(buf, Buffer.from('\x05devon\x15'));
});
it('should support preEncode hook', function() {
const struct = new Struct({
nameLength: uint8,
name: new StringT('nameLength'),
age: uint8
});
struct.preEncode = function() {
return this.nameLength = this.name.length;
};
const buf = struct.toBuffer({
name: 'devon',
age: 21
});
assert.deepEqual(buf, Buffer.from('\x05devon\x15'));
});
it('should encode pointer data after structure', function() {
const struct = new Struct({
name: new StringT(uint8),
age: uint8,
ptr: new Pointer(uint8, new StringT(uint8))
});
const buf = struct.toBuffer({
name: 'devon',
age: 21,
ptr: 'hello'
});
assert.deepEqual(buf, Buffer.from('\x05devon\x15\x08\x05hello'));
});
});
});