Skip to content

Commit

Permalink
Added test code
Browse files Browse the repository at this point in the history
  • Loading branch information
james-pre committed Sep 13, 2024
1 parent fcf9cb3 commit 97d7d39
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
52 changes: 52 additions & 0 deletions test/struct-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { writeFileSync } from 'fs';
import { deserialize, member, serialize, struct, types as t } from '../src/struct.js';
import { join } from 'path';

@struct()
class Header {
@t.char(4) public readonly magic_start = 'test';

@t.uint16 public segments: number = 0;

@t.char(4) public readonly magic_end = 'end';
}

@struct()
class AnotherHeader extends Header {
@t.uint64 public _plus = 0x12345678;
}

@struct()
class Segment {
@t.uint64 public id = 0x021;
@t.uint32(64) public data: number[] = [];
}

@struct()
class BinObject {
@member(AnotherHeader)
header: AnotherHeader = new AnotherHeader();

@t.char(32)
comment: string = '';

@member(Segment, 16)
public segments: Segment[] = [new Segment()];
}

const obj = new BinObject();
obj.comment = '!!! Omg, hi! this is cool' + '.'.repeat(32);
obj.header.segments = 1;

const segment = new Segment();
segment.data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
serialize(segment);
obj.segments = [segment];

const bin = serialize(obj);
writeFileSync(join(import.meta.dirname, '../tmp/bin'), bin);

const omg = new BinObject();
deserialize(omg, bin);

console.log(omg);
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"entryPoints": ["src/index.ts"],
"out": "docs"
},
"include": ["./src/**/*.ts"],
"include": ["./src/**/*.ts", "./test/**/*.ts"],
"exclude": ["node_modules"]
}

0 comments on commit 97d7d39

Please sign in to comment.