forked from junghans-schneider/pbf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.test.js
More file actions
291 lines (223 loc) · 7.05 KB
/
compile.test.js
File metadata and controls
291 lines (223 loc) · 7.05 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
'use strict';
var fs = require('fs');
var path = require('path');
var test = require('tap').test;
var resolve = require('resolve-protobuf-schema').sync;
var Pbf = require('../');
var compile = require('../compile');
test('compiles vector tile proto', function(t) {
var proto = resolve(path.join(__dirname, '../bench/vector_tile.proto'));
var tileBuf = fs.readFileSync(path.join(__dirname, 'fixtures/12665.vector.pbf'));
var Tile = compile(proto).Tile;
var tile = Tile.read(new Pbf(tileBuf));
t.equal(tile.layers.length, 11);
var pbf = new Pbf();
Tile.write(tile, pbf);
var buf = pbf.finish();
t.equal(buf.length, 124946);
t.end();
});
test('compiles proto with embedded type reference', function(t) {
var proto = resolve(path.join(__dirname, './fixtures/embedded_type.proto'));
compile(proto);
t.end();
});
test('compiles packed proto', function(t) {
var proto = resolve(path.join(__dirname, './fixtures/packed.proto'));
var NotPacked = compile(proto).NotPacked;
var FalsePacked = compile(proto).FalsePacked;
var original = {
types: [0, 1, 0, 1],
value: [300, 400, 500]
};
var pbf = new Pbf();
NotPacked.write(original, pbf);
var buf = pbf.finish();
var decompressed = FalsePacked.read(new Pbf(buf));
t.equals(buf.length, 17);
t.deepEqual(original, decompressed);
t.end();
});
test('reads packed with unpacked field', function(t) {
var proto = resolve(path.join(__dirname, './fixtures/packed.proto'));
var Packed = compile(proto).Packed;
var FalsePacked = compile(proto).FalsePacked;
var original = {
types: [0, 1, 0, 1],
value: [300, 400, 500]
};
var pbf = new Pbf();
Packed.write(original, pbf);
var buf = pbf.finish();
var decompressed = FalsePacked.read(new Pbf(buf));
t.equals(buf.length, 14);
t.deepEqual(original, decompressed);
t.end();
});
test('compiles packed proto3', function(t) {
var proto = resolve(path.join(__dirname, './fixtures/packed_proto3.proto'));
var NotPacked = compile(proto).NotPacked;
var FalsePacked = compile(proto).FalsePacked;
var original = {
types: [0, 1, 0, 1],
value: [300, 400, 500]
};
var pbf = new Pbf();
FalsePacked.write(original, pbf);
var falsePackedBuf = pbf.finish();
pbf = new Pbf();
NotPacked.write(original, pbf);
var notPackedBuf = pbf.finish();
var decompressed = NotPacked.read(new Pbf(falsePackedBuf));
t.deepEqual(original, decompressed);
t.equals(notPackedBuf.length, 14);
t.ok(falsePackedBuf.length > notPackedBuf.length, 'Did not respect [packed=false]');
t.end();
});
test('compiles packed with multi-byte tags', function(t) {
var proto = resolve(path.join(__dirname, './fixtures/packed_proto3.proto'));
var Packed = compile(proto).Packed;
var original = {
value: [300, 400, 500]
};
var pbf = new Pbf();
Packed.write(original, pbf);
var buf = pbf.finish();
var decompressed = Packed.read(new Pbf(buf));
t.equals(buf.length, 9);
t.deepEqual(original, decompressed);
t.end();
});
test('compiles defaults', function(t) {
var proto = resolve(path.join(__dirname, './fixtures/defaults.proto'));
var Envelope = compile(proto).Envelope;
var pbf = new Pbf();
Envelope.write({}, pbf);
var buf = pbf.finish();
var data = Envelope.read(new Pbf(buf));
t.equals(buf.length, 0);
t.deepEqual(data, {
type: {
options: {},
value: 1
},
name: 'test',
flag: true,
weight: 1.5,
id: 1
});
t.end();
});
test('compiles proto3 ignoring defaults', function(t) {
var proto = resolve(path.join(__dirname, './fixtures/defaults_proto3.proto'));
var Envelope = compile(proto).Envelope;
var pbf = new Pbf();
Envelope.write({}, pbf);
var buf = pbf.finish();
var data = Envelope.read(new Pbf(buf));
t.equals(buf.length, 0);
t.equals(data.type, 0);
t.equals(data.name, '');
t.equals(data.flag, false);
t.equals(data.weight, 0);
t.equals(data.id, 0);
t.end();
});
test('compiles maps', function(t) {
var proto = resolve(path.join(__dirname, './fixtures/map.proto'));
var Envelope = compile(proto).Envelope;
var original = {
kv : {
a: 'value a',
b: 'value b'
},
kn : {
a : 1,
b : 2
}
};
var pbf = new Pbf();
Envelope.write(original, pbf);
var buf = pbf.finish();
var decompressed = Envelope.read(new Pbf(buf));
t.deepEqual(original, decompressed);
t.end();
});
test('does not write undefined or null values', function(t) {
var proto = resolve(path.join(__dirname, './fixtures/embedded_type.proto'));
var EmbeddedType = compile(proto).EmbeddedType;
var pbf = new Pbf();
EmbeddedType.write({}, pbf);
EmbeddedType.write({
'sub_field': null
}, pbf);
EmbeddedType.write({
value: null
});
t.end();
});
test('handles all implicit default values', function(t) {
var proto = resolve(path.join(__dirname, './fixtures/defaults_implicit.proto'));
var Envelope = compile(proto).Envelope;
var pbf = new Pbf();
Envelope.write({}, pbf);
var buf = pbf.finish();
var data = Envelope.read(new Pbf(buf));
t.equals(buf.length, 0);
t.equals(data.type, 0);
t.equals(data.name, '');
t.equals(data.flag, false);
t.equals(data.weight, 0);
t.equals(data.id, 0);
t.deepEqual(data.tags, []);
t.deepEqual(data.numbers, []);
t.equals(data.bytes, undefined);
t.equals(data.custom, undefined);
t.deepEqual(data.types, []);
t.end();
});
test('sets oneof field name', function(t) {
var proto = resolve(path.join(__dirname, './fixtures/oneof.proto'));
var Envelope = compile(proto).Envelope;
var pbf = new Pbf();
Envelope.write({}, pbf);
var data = Envelope.read(new Pbf(pbf.finish()));
t.equals(data.value, undefined);
t.equals(data.id, 0);
pbf = new Pbf();
Envelope.write({
float: 1.5
}, pbf);
data = Envelope.read(new Pbf(pbf.finish()));
t.equals(data.value, 'float');
t.equals(data[data.value], 1.5);
t.end();
});
test('handles negative varint', function(t) {
var proto = resolve(path.join(__dirname, './fixtures/varint.proto'));
var Envelope = compile(proto).Envelope;
var pbf = new Pbf();
Envelope.write({
int: -5,
long: -10
}, pbf);
var buf = pbf.finish();
var data = Envelope.read(new Pbf(buf));
t.equals(data.int, -5);
t.equals(data.long, -10);
t.end();
});
test('handles unsigned varint', function(t) {
var proto = resolve(path.join(__dirname, './fixtures/varint.proto'));
var Envelope = compile(proto).Envelope;
var pbf = new Pbf();
Envelope.write({
uint: Math.pow(2, 31),
ulong: Math.pow(2, 63)
}, pbf);
var buf = pbf.finish();
var data = Envelope.read(new Pbf(buf));
t.equals(data.uint, Math.pow(2, 31));
t.equals(data.ulong, Math.pow(2, 63));
t.end();
});