-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkitchenSink.ts
78 lines (72 loc) · 1.76 KB
/
kitchenSink.ts
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
import {generateSchema, makeSchema} from '../src';
type Weapons = 'sword' | 'laser';
type KitchenSinkMessage = {
message: string;
items: (
| {
type: 'weapon';
strength: number;
weapon: Weapons;
}
| {
type: 'run';
duration: number;
direction: {
up: boolean;
down: boolean;
left: boolean;
right: boolean;
};
}
)[];
};
const KitchenSinkMessageSchema = makeSchema<KitchenSinkMessage>({
message: 'string',
items: {
flag: 'array-uint8',
elements: {
flag: 'type-lookup',
elements: {
run: {
duration: 'uint16',
direction: {
flag: 'bitmask',
up: 0,
down: 1,
left: 2,
right: 3,
},
},
weapon: {
weapon: {
flag: 'enum',
laser: 0,
sword: 1,
},
strength: 'float32',
},
},
},
},
});
test('kitchen sink test', () => {
const generator = generateSchema(KitchenSinkMessageSchema);
const buffer = generator.toBuffer({
message: 'The game is on!',
items: [
{type: 'weapon', strength: 12, weapon: 'sword'},
{type: 'weapon', strength: 34, weapon: 'laser'},
{type: 'run', duration: 45, direction: {down: true, left: false, right: true, up: false}},
],
});
expect(buffer.byteLength).toEqual(49);
const result = generator.fromBuffer(buffer);
expect(result).toEqual({
message: 'The game is on!',
items: [
{type: 'weapon', strength: 12, weapon: 'sword'},
{type: 'weapon', strength: 34, weapon: 'laser'},
{type: 'run', duration: 45, direction: {down: true, left: false, right: true, up: false}},
],
});
});