From 10bdaf518aed7e8dd4a2da419870c0cf4c8aa16c Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 24 Jan 2019 14:38:20 +0100 Subject: [PATCH] Check actual valueOf in bool for yes/no (#633) * Check actual valueOf in bool for yes/no * Add test for false encoding --- packages/types/src/Bool.spec.ts | 13 +++++++------ packages/types/src/Bool.ts | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/types/src/Bool.spec.ts b/packages/types/src/Bool.spec.ts index b1e5608a25..2859a41c68 100644 --- a/packages/types/src/Bool.spec.ts +++ b/packages/types/src/Bool.spec.ts @@ -20,15 +20,16 @@ describe('Bool', () => { }); describe('encode', () => { - const testEncode = (to: CodecTo, expected: string | Uint8Array | boolean) => + const testEncode = (to: CodecTo, expected: string | Uint8Array | boolean, value: boolean) => it(`can encode ${to}`, () => { - expect(new Bool(true)[to]()).toEqual(expected); + expect(new Bool(value)[to]()).toEqual(expected); }); - testEncode('toJSON', true); - testEncode('toHex', '0x01'); - testEncode('toString', 'true'); - testEncode('toU8a', Uint8Array.from([1])); + testEncode('toJSON', true, true); + testEncode('toHex', '0x01', true); + testEncode('toString', 'true', true); + testEncode('toU8a', Uint8Array.from([1]), true); + testEncode('toU8a', Uint8Array.from([0]), false); }); it('correctly encodes length', () => { diff --git a/packages/types/src/Bool.ts b/packages/types/src/Bool.ts index b745fe3353..4ba7ae071a 100644 --- a/packages/types/src/Bool.ts +++ b/packages/types/src/Bool.ts @@ -73,6 +73,6 @@ export default class Bool extends Boolean implements Codec { * @param isBare true when the value has none of the type-specific prefixes (internal) */ toU8a (isBare?: boolean): Uint8Array { - return new Uint8Array([this ? 1 : 0]); + return new Uint8Array([this.valueOf() ? 1 : 0]); } }