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

Commit

Permalink
Merge pull request #629 from EOSIO/accept-1-and-0
Browse files Browse the repository at this point in the history
Accept number equal to 1 or 0 while checking data type of bool.
  • Loading branch information
jlamarr22 authored Dec 11, 2019
2 parents 67ee21f + 84916d2 commit e2c667e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
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);
});
});
});

0 comments on commit e2c667e

Please sign in to comment.