From b701bdf7639cb97490d4030297701a934e8fa021 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 16 May 2019 15:34:59 +0200 Subject: [PATCH] Set assert fix (with tests) (#925) * Set assert fix (with tests) * toNumber() -> function --- packages/types/src/codec/Set.spec.ts | 21 +++++++++++++++------ packages/types/src/codec/Set.ts | 9 ++++++++- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/packages/types/src/codec/Set.spec.ts b/packages/types/src/codec/Set.spec.ts index a484126ff6..fbd53e98ed 100644 --- a/packages/types/src/codec/Set.spec.ts +++ b/packages/types/src/codec/Set.spec.ts @@ -17,6 +17,12 @@ const SET_ROLES = { light: 0b00000010, authority: 0b00000100 }; +const SET_WITHDRAW = { + TransactionPayment: 0b00000001, + Transfer: 0b00000010, + Reserve: 0b00000100, + Fee: 0b00001000 +}; describe('Set', () => { it('constructs via an Array', () => { @@ -40,13 +46,16 @@ describe('Set', () => { ).toThrow(/Invalid key 'invalid'/); }); - it('constructs via Uint8Array', () => { - const set = new Set(SET_FIELDS, new Uint8Array([0b00000001 | 0b00000010 | 0b00010000 | 0b10000000])); + it('allows construction via number', () => { + expect( + (new Set(SET_WITHDRAW, 15)).eq(['TransactionPayment', 'Transfer', 'Reserve', 'Fee']) + ).toBe(true); + }); - expect(set.encodedLength).toEqual(1); - expect(set.toJSON()).toEqual([ - 'header', 'body', 'justification' - ]); + it('does not allow invalid number', () => { + expect( + () => new Set(SET_WITHDRAW, 31) + ).toThrow(/Mismatch decoding '31', computed as '15'/); }); it('hash a valid encoding', () => { diff --git a/packages/types/src/codec/Set.ts b/packages/types/src/codec/Set.ts index 6cd6c35dbd..8337a3d28d 100644 --- a/packages/types/src/codec/Set.ts +++ b/packages/types/src/codec/Set.ts @@ -56,7 +56,7 @@ export default class CodecSet extends Set implements Codec { const computed = CodecSet.encodeSet(setValues, result); - assert(value !== computed, `Set: Mismatch decoding '${value}', computed as '${computed}' with ${result}`); + assert(value === computed, `Set: Mismatch decoding '${value}', computed as '${computed}' with ${result}`); return result; } @@ -138,6 +138,13 @@ export default class CodecSet extends Set implements Codec { return this.strings; } + /** + * @description The encoded value for the set members + */ + toNumber (): number { + return this.valueEncoded; + } + /** * @description Returns the base runtime type name for this instance */