Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Accept number equal to 1 or 0 while checking data type of bool. #629

Merged
merged 1 commit into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/eosjs-serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -800,8 +800,8 @@ export function createInitialTypes(): Map<string, Type> {
bool: createType({
name: 'bool',
serialize(buffer: SerialBuffer, data: boolean) {
if (typeof data !== 'boolean') {
throw new Error('Expected true or false');
if ( !(typeof data === 'boolean' || typeof data === 'number' && ( data === 1 || data === 0))) {
throw new Error('Expected boolean or number equal to 1 or 0');
}
buffer.push(data ? 1 : 0);
},
Expand Down
82 changes: 82 additions & 0 deletions src/tests/eosjs-serialize.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { ec } from 'elliptic';

import { createInitialTypes, Type, SerialBuffer } from '../eosjs-serialize';

describe('Serialize', () => {
let types: Map<string, Type>;

beforeAll(() => {
types = createInitialTypes();
});

it('should be able to createInitialTypes', () => {
expect(types).toBeTruthy();
});

describe('bool', () => {
let boolType: Type;
let mockedBuffer: SerialBuffer;

const shouldThrowErrorForValue = (value: any) => {
try {
boolType.serialize(mockedBuffer, value);
} catch (e) {
expect(e.message).toBe('Expected boolean or number equal to 1 or 0');
}
};

const shouldNotThrowErrorForValue = (value: any) => {
expect(() => {
boolType.serialize(mockedBuffer, value);
}).not.toThrow();
};

beforeAll(() => {
boolType = types.get('bool');
mockedBuffer = Object.create(SerialBuffer);
mockedBuffer.push = jest.fn().mockImplementation((value) => {
return;
});
});

it('should be able to create bool type', () => {
expect(boolType).toBeTruthy();
});

it('should throw error when calling serialize when type is not boolean or number', () => {
const dataValue = 'string';

shouldThrowErrorForValue(dataValue);
});

it('should throw error when calling serialize when number that is not 1 or 0', () => {
const dataValue = 10;

shouldThrowErrorForValue(dataValue);
});

it('should not throw error when calling serialize with false', () => {
const dataValue = false;

shouldNotThrowErrorForValue(dataValue);
});

it('should not throw error when calling serialize with true', () => {
const dataValue = true;

shouldNotThrowErrorForValue(dataValue);
});

it('should not throw error when calling serialize with 0', () => {
const dataValue = 0;

shouldNotThrowErrorForValue(dataValue);
});

it('should not throw error when calling serialize with 1', () => {
const dataValue = 1;

shouldNotThrowErrorForValue(dataValue);
});
});
});