Make Enum/Tuple constructor use value directly if instance (#1954)

* Make Enum/Tuple constructor use value directly if instance

* Lint

* Lint
This commit is contained in:
Axel Chalon
2020-02-28 13:39:11 +01:00
committed by GitHub
parent 6f581d6122
commit 94608b0c3f
12 changed files with 108 additions and 4 deletions
@@ -24,6 +24,15 @@ describe('BTreeMap', (): void => {
).toEqual('{}'); ).toEqual('{}');
}); });
it('decodes reusing instanciated inputs', (): void => {
const key = new Text(registry, 'foo');
const val = new Text(registry, 'bar');
expect(
(new (BTreeMap.with(Text, Text))(registry, new Map([[key, val]]))).eq(new Map([[key, val]]))
).toBe(true);
});
it('decodes within more complicated types', (): void => { it('decodes within more complicated types', (): void => {
const s = new Struct(registry, { const s = new Struct(registry, {
placeholder: U32, placeholder: U32,
@@ -61,6 +61,14 @@ describe('BTreeSet', (): void => {
).toEqual('[]'); ).toEqual('[]');
}); });
it('decodes reusing instanciated inputs', (): void => {
const foo = new Text(registry, 'bar');
expect(
(new BTreeSet(registry, Text, new Set([foo]))).eq(new Set([foo]))
).toBe(true);
});
it('decodes within more complicated types', (): void => { it('decodes within more complicated types', (): void => {
const s = new Struct(registry, { const s = new Struct(registry, {
placeholder: U32, placeholder: U32,
+29
View File
@@ -46,6 +46,35 @@ describe('Enum', (): void => {
).toEqual('some text value'); ).toEqual('some text value');
}); });
it('decodes reusing instanciated inputs', (): void => {
const foo = new Text(registry, 'bar');
expect(
new Enum(
registry,
{ foo: Text },
{ foo }
).value
).toBe(foo);
expect(
new Enum(
registry,
{ foo: Text },
foo,
0
).value
).toBe(foo);
expect(
new Enum(
registry,
{ foo: Text },
new Enum(registry, { foo: Text }, { foo })
).value
).toBe(foo);
});
it('decodes from hex', (): void => { it('decodes from hex', (): void => {
expect( expect(
new Enum( new Enum(
+1 -1
View File
@@ -51,7 +51,7 @@ function createFromValue (registry: Registry, def: TypesDef, index = 0, value?:
return { return {
index, index,
value: new Clazz(registry, value) value: value instanceof Clazz ? value : new Clazz(registry, value)
}; };
} }
+8
View File
@@ -63,6 +63,14 @@ describe('Option', (): void => {
).toEqual(0x12345678); ).toEqual(0x12345678);
}); });
it('decodes reusing instanciated inputs', (): void => {
const foo = new Text(registry, 'bar');
expect(
(new Option(registry, Text, foo)).value
).toBe(foo);
});
testDecode('string (with)', 'foo', 'foo'); testDecode('string (with)', 'foo', 'foo');
testDecode('string (without)', undefined, ''); testDecode('string (without)', undefined, '');
testDecode('Uint8Array (with)', Uint8Array.from([1, 12, 102, 111, 111]), 'foo'); testDecode('Uint8Array (with)', Uint8Array.from([1, 12, 102, 111, 111]), 'foo');
+13
View File
@@ -44,6 +44,19 @@ describe('Result', (): void => {
expect(result.toHex()).toEqual('0x01146572726f72'); expect(result.toHex()).toEqual('0x01146572726f72');
}); });
it('decodes reusing instanciated inputs', (): void => {
const foo = new Text(registry, 'bar');
expect(
new Result(
registry,
Text,
Text,
{ Ok: foo }
).asOk
).toBe(foo);
});
it('returns a proper raw typedef rom a built-in', (): void => { it('returns a proper raw typedef rom a built-in', (): void => {
expect(registry.createType('DispatchResult').toRawType()).toEqual('Result<(),{"_enum":{"Other":"Null","CannotLookup":"Null","BadOrigin":"Null","Module":"{\\"index\\":\\"u8\\",\\"error\\":\\"u8\\"}"}}>'); expect(registry.createType('DispatchResult').toRawType()).toEqual('Result<(),{"_enum":{"Other":"Null","CannotLookup":"Null","BadOrigin":"Null","Module":"{\\"index\\":\\"u8\\",\\"error\\":\\"u8\\"}"}}>');
}); });
+12
View File
@@ -61,6 +61,18 @@ describe('Struct', (): void => {
).toEqual('{}'); ).toEqual('{}');
}); });
it('decodes reusing instanciated inputs', (): void => {
const foo = new Text(registry, 'bar');
expect(
(new Struct(
registry,
{ foo: Text },
{ foo }
)).get('foo')
).toBe(foo);
});
it('decodes a more complicated type', (): void => { it('decodes a more complicated type', (): void => {
const s = new Struct(registry, { const s = new Struct(registry, {
foo: Vec.with(Struct.with({ foo: Vec.with(Struct.with({
+8
View File
@@ -42,6 +42,14 @@ describe('Tuple', (): void => {
testDecode('array', ['bazzing', 69]); testDecode('array', ['bazzing', 69]);
testDecode('hex', '0x1c62617a7a696e6745000000'); testDecode('hex', '0x1c62617a7a696e6745000000');
testDecode('Uint8Array', Uint8Array.from([28, 98, 97, 122, 122, 105, 110, 103, 69, 0, 0, 0])); testDecode('Uint8Array', Uint8Array.from([28, 98, 97, 122, 122, 105, 110, 103, 69, 0, 0, 0]));
it('decodes reusing instanciated inputs', (): void => {
const foo = new Text(registry, 'bar');
expect(
(new Tuple(registry, [Text], [foo]))[0]
).toBe(foo);
});
}); });
describe('encoding', (): void => { describe('encoding', (): void => {
+2 -1
View File
@@ -50,7 +50,8 @@ export default class Tuple extends AbstractArray<Codec> {
return Types.map((Type, index): Codec => { return Types.map((Type, index): Codec => {
try { try {
return new Type(registry, value && value[index]); if (value?.[index] instanceof Type) return value[index] as any;
return new Type(registry, value?.[index]);
} catch (error) { } catch (error) {
throw new Error(`Tuple: failed on ${index}:: ${error.message}`); throw new Error(`Tuple: failed on ${index}:: ${error.message}`);
} }
+8
View File
@@ -61,6 +61,14 @@ describe('Vec', (): void => {
expect(vector.Type).toEqual('Text'); expect(vector.Type).toEqual('Text');
}); });
it('decodes reusing instanciated inputs', (): void => {
const foo = new Text(registry, 'bar');
expect(
(new Vec(registry, Text, [foo]))[0]
).toBe(foo);
});
it('decodes a complex type via construction', (): void => { it('decodes a complex type via construction', (): void => {
const test = createTypeUnsafe(registry, 'Vec<(PropIndex, AccountId)>', [new Uint8Array([ const test = createTypeUnsafe(registry, 'Vec<(PropIndex, AccountId)>', [new Uint8Array([
4, 10, 0, 0, 0, 209, 114, 167, 76, 218, 76, 134, 89, 18, 195, 43, 160, 168, 10, 87, 174, 105, 171, 174, 65, 14, 92, 203, 89, 222, 232, 78, 47, 68, 50, 219, 79 4, 10, 0, 0, 0, 209, 114, 167, 76, 218, 76, 134, 89, 18, 195, 43, 160, 168, 10, 87, 174, 105, 171, 174, 65, 14, 92, 203, 89, 222, 232, 78, 47, 68, 50, 219, 79
@@ -17,6 +17,14 @@ describe('VecFixed', (): void => {
it('constructs via Uint8Array', (): void => { it('constructs via Uint8Array', (): void => {
expect(new VecFixed(registry, Text, 2, new Uint8Array([0x00, 0x04, 0x31])).toHex()).toEqual('0x000431'); expect(new VecFixed(registry, Text, 2, new Uint8Array([0x00, 0x04, 0x31])).toHex()).toEqual('0x000431');
}); });
it('decodes reusing instanciated inputs', (): void => {
const foo = new Text(registry, 'bar');
expect(
(new VecFixed(registry, Text, 1, [foo]))[0]
).toBe(foo);
});
}); });
describe('utils', (): void => { describe('utils', (): void => {
+2 -2
View File
@@ -15,9 +15,9 @@ export default function compareSet (a: Set<any>, b?: any): boolean {
if (Array.isArray(b)) { if (Array.isArray(b)) {
return compareSetArray(a, b); return compareSetArray(a, b);
} else if (b instanceof Set) { } else if (b instanceof Set) {
return compareSetArray(a, [...b.entries()]); return compareSetArray(a, [...b.values()]);
} else if (isObject(b)) { } else if (isObject(b)) {
return compareSetArray(a, Object.entries(b)); return compareSetArray(a, Object.values(b));
} }
return false; return false;