Skip to content

Commit

Permalink
chore(struct): Test unpacking binary data into a struct with a sub-st…
Browse files Browse the repository at this point in the history
…ruct property
  • Loading branch information
mkapal committed Apr 23, 2024
1 parent 27054e7 commit 1dbb7b7
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/packets/base/Struct.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { byte, string } from '../../decorators';
import { byte, string, struct } from '../../decorators';
import { stringToBytes } from '../../tests';
import { PacketType } from '../enums';
import { Struct } from './Struct';
Expand Down Expand Up @@ -91,20 +91,30 @@ describe('Struct', () => {
});

it('should unpack binary data', () => {
class SubStruct extends Struct {
@byte() Byte1 = 0;
@byte() Byte2 = 0;
}

class CustomStruct extends Struct {
@string(6) StringProperty = 'string';
@byte() NumberProperty = 0;
@struct(SubStruct) SubStructProperty = new SubStruct();
}

const buffer = new Uint8Array([
...stringToBytes('test'), // StringProperty[6]
0,
0,
25, // NumberProperty
2, // Byte1
4, // Byte2
]);
const packet = new CustomStruct().unpack(buffer);

expect(packet.StringProperty).toEqual('test');
expect(packet.NumberProperty).toEqual(25);
expect(packet.SubStructProperty.Byte1).toEqual(2);
expect(packet.SubStructProperty.Byte2).toEqual(4);
});
});

0 comments on commit 1dbb7b7

Please sign in to comment.