Set assert fix (with tests) (#925)

* Set assert fix (with tests)

* toNumber() -> function
This commit is contained in:
Jaco Greeff
2019-05-16 15:34:59 +02:00
committed by GitHub
parent 6729da721a
commit b701bdf763
2 changed files with 23 additions and 7 deletions
+15 -6
View File
@@ -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<string>', () => {
@@ -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', () => {
+8 -1
View File
@@ -56,7 +56,7 @@ export default class CodecSet extends Set<string> 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<string> 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
*/