From d8566bc08367a70a7860b56807bc2721caeaa4ee Mon Sep 17 00:00:00 2001 From: Marcus Pousette Date: Sat, 20 Nov 2021 10:56:02 +0100 Subject: [PATCH] Update failing tests --- borsh-ts/test/schema.test.ts | 6 +- lib/binary.js | 6 +- lib/schema.d.ts | 9 ++- lib/schema.js | 8 +-- lib/test/schema.test.js | 108 +++++++++++++++-------------------- 5 files changed, 62 insertions(+), 75 deletions(-) diff --git a/borsh-ts/test/schema.test.ts b/borsh-ts/test/schema.test.ts index 1c6f02b0..e0530dcc 100644 --- a/borsh-ts/test/schema.test.ts +++ b/borsh-ts/test/schema.test.ts @@ -16,8 +16,8 @@ describe("struct", () => { const expectedResult: StructKind = { kind: "struct", fields: [ - ["a", "typeA"], - ["b", "typeB"], + ["a", "u8"], + ["b", "u16"], ], }; expect(generatedSchemas).toEqual(expectedResult); @@ -41,7 +41,7 @@ describe("struct", () => { }); expect(generatedSchemas.get(InnerStruct)).toEqual({ kind: "struct", - fields: [["b", "typeB"]], + fields: [["b", "u8"]], }); }); }); diff --git a/lib/binary.js b/lib/binary.js index c679f9cd..d81636a0 100644 --- a/lib/binary.js +++ b/lib/binary.js @@ -34,10 +34,10 @@ const bn_js_1 = __importDefault(require("bn.js")); const error_1 = require("./error"); // TODO: Make sure this polyfill not included when not required const encoding = __importStar(require("text-encoding-utf-8")); -const TextDecoder = typeof global.TextDecoder !== "function" +const ResolvedTextDecoder = typeof TextDecoder !== "function" ? encoding.TextDecoder - : global.TextDecoder; -const textDecoder = new TextDecoder("utf-8", { fatal: true }); + : TextDecoder; +const textDecoder = new ResolvedTextDecoder("utf-8", { fatal: true }); /// Binary encoder. class BinaryWriter { constructor() { diff --git a/lib/schema.d.ts b/lib/schema.d.ts index 98af0134..48fbf6ed 100644 --- a/lib/schema.d.ts +++ b/lib/schema.d.ts @@ -1,5 +1,7 @@ import "reflect-metadata"; export declare type Schema = Map; +declare type Constructor = new (...args: any[]) => T; +export declare type FieldType = 'u8' | 'u16' | 'u32' | 'u64' | 'u128' | 'u256' | 'u512' | 'f32' | 'f64' | 'String' | Constructor; export interface StructKind { kind: 'struct'; fields: any[][]; @@ -17,13 +19,13 @@ export interface FieldMetaData { * @param kind 'struct' or 'variant. 'variant' equivalnt to Rust Enum * @returns Schema decorator function for classes */ -export declare const Variant: (index: number) => (ctor: Function) => void; +export declare const variant: (index: number) => (ctor: Function) => void; /** * @param properties, the properties of the field mapping to schema * @returns */ -export declare function Field(properties: { - type: any; +export declare function field(properties: { + type: FieldType; option?: boolean; index?: number; }): (target: {} | any, name?: PropertyKey) => any; @@ -33,3 +35,4 @@ export declare function Field(properties: { * @returns Schema map */ export declare const generateSchemas: (clazzes: any[], validate?: boolean) => Map; +export {}; diff --git a/lib/schema.js b/lib/schema.js index 2317e4df..cb577445 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -1,6 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.generateSchemas = exports.Field = exports.Variant = void 0; +exports.generateSchemas = exports.field = exports.variant = void 0; require("reflect-metadata"); const _1 = require("."); const error_1 = require("./error"); @@ -13,7 +13,7 @@ const structMetaDataKey = (constructorName) => { * @param kind 'struct' or 'variant. 'variant' equivalnt to Rust Enum * @returns Schema decorator function for classes */ -exports.Variant = (index) => { +exports.variant = (index) => { return (ctor) => { // Create a custom serialization, for enum by prepend instruction index ctor.prototype.borshSerialize = function (schema, writer) { @@ -30,7 +30,7 @@ exports.Variant = (index) => { * @param properties, the properties of the field mapping to schema * @returns */ -function Field(properties) { +function field(properties) { return (target, name) => { const metaDataKey = structMetaDataKey(target.constructor.name); let schema = Reflect.getMetadata(metaDataKey, target.constructor); // Assume StructKind already exist @@ -71,7 +71,7 @@ function Field(properties) { Reflect.defineMetadata(metaDataKey, schema, target.constructor); }; } -exports.Field = Field; +exports.field = field; /** * @param clazzes * @param validate, run validation? diff --git a/lib/test/schema.test.js b/lib/test/schema.test.js index 8ba27c7f..3b537713 100644 --- a/lib/test/schema.test.js +++ b/lib/test/schema.test.js @@ -9,81 +9,77 @@ Object.defineProperty(exports, "__esModule", { value: true }); const error_1 = require("../error"); const index_1 = require("../index"); const schema_1 = require("../schema"); -describe('struct', () => { - test('any field by string', () => { +describe("struct", () => { + test("any field by string", () => { class TestStruct { } __decorate([ - schema_1.Field({ type: 'typeA' }) + schema_1.field({ type: "u8" }) ], TestStruct.prototype, "a", void 0); __decorate([ - schema_1.Field({ type: 'typeB' }) + schema_1.field({ type: "u16" }) ], TestStruct.prototype, "b", void 0); const generatedSchemas = schema_1.generateSchemas([TestStruct]).get(TestStruct); const expectedResult = { - kind: 'struct', + kind: "struct", fields: [ - ['a', 'typeA'], - ['b', 'typeB'] - ] + ["a", "u8"], + ["b", "u16"], + ], }; expect(generatedSchemas).toEqual(expectedResult); }); - test('struct fields', () => { + test("struct fields", () => { class InnerStruct { } __decorate([ - schema_1.Field({ type: 'typeB' }) + schema_1.field({ type: "u8" }) ], InnerStruct.prototype, "b", void 0); class TestStruct { } __decorate([ - schema_1.Field({ type: InnerStruct }) + schema_1.field({ type: InnerStruct }) ], TestStruct.prototype, "a", void 0); const generatedSchemas = schema_1.generateSchemas([TestStruct]); expect(generatedSchemas.get(TestStruct)).toEqual({ - kind: 'struct', - fields: [ - ['a', InnerStruct], - ] + kind: "struct", + fields: [["a", InnerStruct]], }); expect(generatedSchemas.get(InnerStruct)).toEqual({ - kind: 'struct', - fields: [ - ['b', 'typeB'], - ] + kind: "struct", + fields: [["b", "u8"]], }); }); }); -describe('enum', () => { - test('enum base', () => { +describe("enum", () => { + test("enum base", () => { let TestEnum = class TestEnum { constructor(a) { this.a = a; } }; __decorate([ - schema_1.Field({ type: 'u8' }) + schema_1.field({ type: "u8" }) ], TestEnum.prototype, "a", void 0); TestEnum = __decorate([ - schema_1.Variant(1) + schema_1.variant(1) ], TestEnum); const instance = new TestEnum(3); const generatedSchemas = schema_1.generateSchemas([TestEnum]); const buf = index_1.serialize(generatedSchemas, instance); expect(buf).toEqual(Buffer.from([1, 3])); }); - test('enum field', () => { + test("enum field", () => { let TestEnum = class TestEnum { constructor(a) { this.a = a; } }; __decorate([ - schema_1.Field({ type: 'u8' }) + schema_1.field({ type: "u8" }) ], TestEnum.prototype, "a", void 0); TestEnum = __decorate([ - schema_1.Variant(1) + schema_1.variant(1) ], TestEnum); class TestStruct { constructor(value) { @@ -91,7 +87,7 @@ describe('enum', () => { } } __decorate([ - schema_1.Field({ type: TestEnum }) + schema_1.field({ type: TestEnum }) ], TestStruct.prototype, "enum", void 0); const instance = new TestStruct(new TestEnum(4)); const generatedSchemas = schema_1.generateSchemas([TestStruct]); @@ -99,12 +95,12 @@ describe('enum', () => { expect(buf).toEqual(Buffer.from([1, 4])); }); }); -describe('option', () => { - test('field option', () => { +describe("option", () => { + test("field option", () => { class TestStruct { } __decorate([ - schema_1.Field({ type: 'u8', option: true }) + schema_1.field({ type: "u8", option: true }) ], TestStruct.prototype, "a", void 0); const schema = schema_1.generateSchemas([TestStruct]).get(TestStruct); expect(schema).toEqual({ @@ -112,17 +108,17 @@ describe('option', () => { [ "a", { - kind: 'option', - type: 'u8' + kind: "option", + type: "u8", }, - ] + ], ], kind: "struct", }); }); }); -describe('order', () => { - test('explicit', () => { +describe("order", () => { + test("explicit", () => { class TestStruct { constructor(a, b) { this.a = a; @@ -130,47 +126,41 @@ describe('order', () => { } } __decorate([ - schema_1.Field({ type: 'u8', index: 1 }) + schema_1.field({ type: "u8", index: 1 }) ], TestStruct.prototype, "a", void 0); __decorate([ - schema_1.Field({ type: 'u8', index: 0 }) + schema_1.field({ type: "u8", index: 0 }) ], TestStruct.prototype, "b", void 0); const schema = schema_1.generateSchemas([TestStruct]).get(TestStruct); expect(schema).toEqual({ fields: [ - [ - "b", - "u8", - ], - [ - "a", - "u8", - ], + ["b", "u8"], + ["a", "u8"], ], kind: "struct", }); }); - test('explicit non zero offset', () => { + test("explicit non zero offset", () => { class TestStruct { } __decorate([ - schema_1.Field({ type: 'u8', index: 1 }) + schema_1.field({ type: "u8", index: 1 }) ], TestStruct.prototype, "a", void 0); const thrower = () => { schema_1.generateSchemas([TestStruct], true); }; // Error is thrown since 1 field with index 1 is undefined behaviour - // Expect first index to be 0 + // Expect first index to be 0 expect(thrower).toThrow(error_1.BorshError); }); - test('explicit gaps', () => { + test("explicit gaps", () => { class TestStruct { } __decorate([ - schema_1.Field({ type: 'u8', index: 0 }) + schema_1.field({ type: "u8", index: 0 }) ], TestStruct.prototype, "a", void 0); __decorate([ - schema_1.Field({ type: 'u8', index: 2 }) + schema_1.field({ type: "u8", index: 2 }) ], TestStruct.prototype, "b", void 0); const thrower = () => { schema_1.generateSchemas([TestStruct], true); @@ -179,26 +169,20 @@ describe('order', () => { // Expected no gaps expect(thrower).toThrow(error_1.BorshError); }); - test('implicit', () => { + test("implicit", () => { class TestStruct { } __decorate([ - schema_1.Field({ type: 'u8' }) + schema_1.field({ type: "u8" }) ], TestStruct.prototype, "a", void 0); __decorate([ - schema_1.Field({ type: 'u8' }) + schema_1.field({ type: "u8" }) ], TestStruct.prototype, "b", void 0); const schema = schema_1.generateSchemas([TestStruct]).get(TestStruct); expect(schema).toEqual({ fields: [ - [ - "a", - "u8", - ], - [ - "b", - "u8", - ], + ["a", "u8"], + ["b", "u8"], ], kind: "struct", });