Skip to content

Commit

Permalink
Add test and improve build
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-pousette committed Nov 29, 2021
1 parent 458e1e7 commit 661e94e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solvei/borsh",
"version": "0.0.17",
"version": "0.0.18",
"readme": "README.md",
"homepage": "https://github.com/the-solvei/borsh-ts#README",
"description": "Binary Object Representation Serializer for Hashing",
Expand Down
32 changes: 26 additions & 6 deletions src/__tests__/schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import BN from "bn.js";
import { BinaryReader } from "../binary";
import { BorshError } from "../error";
import { deserialize, serialize } from "../index";
Expand All @@ -9,19 +10,38 @@ describe("struct", () => {
@field({ type: "u8" })
public a: number;

@field({ type: "u16" })
public b: number;
}
@field({ type: "u64" })
public b: BN;

const generatedSchemas = generateSchemas([TestStruct]).get(TestStruct);
constructor(properties?: { a: number; b: BN }) {
if (properties) {
this.a = properties.a;
this.b = properties.b;
}
}
}
const generatedSchemas = generateSchemas([TestStruct]);
const expectedResult: StructKind = {
kind: "struct",
fields: [
["a", "u8"],
["b", "u16"],
["b", "u64"],
],
};
expect(generatedSchemas).toEqual(expectedResult);
expect(generatedSchemas.get(TestStruct)).toEqual(expectedResult);
const bn123 = new BN(123);
const instance = new TestStruct({ a: 1, b: bn123 });
const buf = serialize(generatedSchemas, instance);
expect(buf).toEqual(Buffer.from([1, 123, 0, 0, 0, 0, 0, 0, 0]));
const deserialized = deserialize(
generatedSchemas,
TestStruct,
Buffer.from(buf)
);
expect(deserialized.a).toEqual(1);
expect(deserialized.b.toNumber()).toEqual(123);
const bufAgain = serialize(generatedSchemas, deserialized);
expect(bufAgain).toEqual(Buffer.from([1, 123, 0, 0, 0, 0, 0, 0, 0]));
});

test("struct fields", () => {
Expand Down
8 changes: 2 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
{
"compilerOptions": {
"esModuleInterop": true,
"lib": [
"es2015",
"esnext",
"dom"
],
"lib": ["DOM", "ES6", "DOM.Iterable", "ScriptHost", "ES2016.Array.Include"],
"module": "commonjs",
"target": "esnext",
"target": "es6",
"moduleResolution": "node",
"outDir": "./dist",
"declaration": true,
Expand Down

0 comments on commit 661e94e

Please sign in to comment.