Skip to content

Commit

Permalink
Update failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-pousette committed Nov 20, 2021
1 parent 7c15681 commit d8566bc
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 75 deletions.
6 changes: 3 additions & 3 deletions borsh-ts/test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ describe("struct", () => {
const expectedResult: StructKind = {
kind: "struct",
fields: [
["a", "typeA"],
["b", "typeB"],
["a", "u8"],
["b", "u16"],
],
};
expect(generatedSchemas).toEqual(expectedResult);
Expand All @@ -41,7 +41,7 @@ describe("struct", () => {
});
expect(generatedSchemas.get(InnerStruct)).toEqual({
kind: "struct",
fields: [["b", "typeB"]],
fields: [["b", "u8"]],
});
});
});
Expand Down
6 changes: 3 additions & 3 deletions lib/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
9 changes: 6 additions & 3 deletions lib/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import "reflect-metadata";
export declare type Schema = Map<Function, any>;
declare type Constructor<T> = new (...args: any[]) => T;
export declare type FieldType = 'u8' | 'u16' | 'u32' | 'u64' | 'u128' | 'u256' | 'u512' | 'f32' | 'f64' | 'String' | Constructor<any>;
export interface StructKind {
kind: 'struct';
fields: any[][];
Expand All @@ -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;
Expand All @@ -33,3 +35,4 @@ export declare function Field(properties: {
* @returns Schema map
*/
export declare const generateSchemas: (clazzes: any[], validate?: boolean) => Map<any, StructKind>;
export {};
8 changes: 4 additions & 4 deletions lib/schema.js
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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?
Expand Down
108 changes: 46 additions & 62 deletions lib/test/schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,168 +9,158 @@ 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) {
this.enum = value;
}
}
__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]);
const buf = index_1.serialize(generatedSchemas, instance);
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({
fields: [
[
"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;
this.b = b;
}
}
__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);
Expand All @@ -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",
});
Expand Down

0 comments on commit d8566bc

Please sign in to comment.