Skip to content

Commit

Permalink
Add tests, Add bufferSize for encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
superman2211 committed May 9, 2022
1 parent 8de46ee commit 1b63d52
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/wild-elephants-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@xobj/buffer": minor
"@xobj/core": minor
---

Add tests, Add bufferSize to encoder
2 changes: 1 addition & 1 deletion packages/buffer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"scripts": {
"build": "yarn tsc && yarn rollup -c",
"lint": "yarn eslint --fix ./src/*.ts ./src/**/*.ts",
"test": "jest",
"test": "jest --verbose",
"coverage": "jest --coverage",
"clean": "rimraf dist && rimraf coverage"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/buffer/src/reader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IBuffer } from './types';
import { IBufferReader } from './types';

export class BufferReader implements IBuffer {
export class BufferReader implements IBufferReader {
private _data: DataView;
private _position: number;
private _littleEndian: boolean;
Expand Down
37 changes: 37 additions & 0 deletions packages/buffer/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,40 @@ export interface IBuffer {
get buffer(): ArrayBuffer;
get littleEndian(): boolean;
}

export interface IBufferReader extends IBuffer {
readUint8(): number;
readUint16(): number;
readUint32(): number;
readUintVar(): number;
readIntVar(): number;
readInt8(): number;
readInt16(): number;
readInt32(): number;
readFloat32(): number;
readFloat64(): number;
readString(): string;
readBigInt(): bigint;
readBuffer(): ArrayBuffer;
readFlags(count: number): boolean[];
readBitset(count: number): boolean[];
}

export interface IBufferWriter extends IBuffer {
get bufferSize(): number;
writeUint8(value: number): void;
writeUint16(value: number): void;
writeUint32(value: number): void;
writeUintVar(value: number): void;
writeIntVar(value: number): void;
writeInt8(value: number): void;
writeInt16(value: number): void;
writeInt32(value: number): void;
writeFloat32(value: number): void;
writeFloat64(value: number): void;
writeString(value: string): void;
writeBigInt(value: bigint): void;
writeBuffer(value: ArrayBuffer): void;
writeFlags(value: boolean[]): void;
writeBitset(value: boolean[]): void;
}
4 changes: 2 additions & 2 deletions packages/buffer/src/writer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IBuffer } from './types';
import { IBufferWriter } from './types';

export class BufferWriter implements IBuffer {
export class BufferWriter implements IBufferWriter {
private _data: DataView;
private _position: number;
private _length: number;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"scripts": {
"build": "yarn tsc && yarn rollup -c",
"lint": "yarn eslint --fix ./src/*.ts ./src/**/*.ts",
"test": "jest",
"test": "jest --verbose",
"coverage": "jest --coverage",
"clean": "rimraf dist && rimraf coverage"
},
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { initBigIntEncoders } from './encoders/bigint';
export interface EncodeOptions {
encoders?: Map<ValueType, EncoderMethod>;
detectors?: DetectorMethod[];
bufferSize?: number;
}

export interface EncodeState {
Expand Down Expand Up @@ -60,10 +61,12 @@ function detect(state: EncodeState, value: any): ValueType {
}

export function encode(value: any, options?: EncodeOptions): ArrayBuffer {
const writer = new BufferWriter();
const bufferSize = options?.bufferSize ?? 1024;
const encoders = options?.encoders ?? DEFAULT_ENCODERS;
const detectors = options?.detectors ?? DEFAULT_DETECTORS;

const writer = new BufferWriter(bufferSize);

const state: EncodeState = {
writer,
detectors,
Expand Down
23 changes: 23 additions & 0 deletions packages/core/test/custom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,27 @@ describe('custom', () => {
expect(target.unit).toBeInstanceOf(Unit);
expect(target.target).toBeInstanceOf(Point);
});

it('should throw when set incorrect decoders', () => {
expect(() => {
const buffer = encode({ test: 123 });

const decoders = new Map();
decode(buffer, { decoders });
}).toThrow();
});

it('should throw when set incorrect detectors', () => {
expect(() => {
const detectors = [];
encode({ test: 123 }, { detectors });
}).toThrow();
});

it('should throw when set incorrect encoders', () => {
expect(() => {
const encoders = new Map();
encode({ test: 123 }, { encoders });
}).toThrow();
});
});
8 changes: 7 additions & 1 deletion packages/core/test/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-undef */

import { isNumberType, ValueType } from '../src';
import { isFloatType, isNumberType, ValueType } from '../src';

describe('types', () => {
it('should check number type', () => {
Expand All @@ -20,4 +20,10 @@ describe('types', () => {
expect(isNumberType(ValueType.STRING)).toBeFalsy();
expect(isNumberType(ValueType.TRUE)).toBeFalsy();
});

it('should check float type', () => {
expect(isFloatType(ValueType.INT8)).toBeFalsy();
expect(isFloatType(ValueType.FLOAT32)).toBeTruthy();
expect(isFloatType(ValueType.FLOAT64)).toBeTruthy();
});
});

0 comments on commit 1b63d52

Please sign in to comment.