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:
@@ -24,6 +24,15 @@ describe('BTreeMap', (): void => {
|
||||
).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 => {
|
||||
const s = new Struct(registry, {
|
||||
placeholder: U32,
|
||||
|
||||
@@ -61,6 +61,14 @@ describe('BTreeSet', (): void => {
|
||||
).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 => {
|
||||
const s = new Struct(registry, {
|
||||
placeholder: U32,
|
||||
|
||||
@@ -46,6 +46,35 @@ describe('Enum', (): void => {
|
||||
).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 => {
|
||||
expect(
|
||||
new Enum(
|
||||
|
||||
@@ -51,7 +51,7 @@ function createFromValue (registry: Registry, def: TypesDef, index = 0, value?:
|
||||
|
||||
return {
|
||||
index,
|
||||
value: new Clazz(registry, value)
|
||||
value: value instanceof Clazz ? value : new Clazz(registry, value)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -63,6 +63,14 @@ describe('Option', (): void => {
|
||||
).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 (without)', undefined, '');
|
||||
testDecode('Uint8Array (with)', Uint8Array.from([1, 12, 102, 111, 111]), 'foo');
|
||||
|
||||
@@ -44,6 +44,19 @@ describe('Result', (): void => {
|
||||
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 => {
|
||||
expect(registry.createType('DispatchResult').toRawType()).toEqual('Result<(),{"_enum":{"Other":"Null","CannotLookup":"Null","BadOrigin":"Null","Module":"{\\"index\\":\\"u8\\",\\"error\\":\\"u8\\"}"}}>');
|
||||
});
|
||||
|
||||
@@ -61,6 +61,18 @@ describe('Struct', (): void => {
|
||||
).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 => {
|
||||
const s = new Struct(registry, {
|
||||
foo: Vec.with(Struct.with({
|
||||
|
||||
@@ -42,6 +42,14 @@ describe('Tuple', (): void => {
|
||||
testDecode('array', ['bazzing', 69]);
|
||||
testDecode('hex', '0x1c62617a7a696e6745000000');
|
||||
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 => {
|
||||
|
||||
@@ -50,7 +50,8 @@ export default class Tuple extends AbstractArray<Codec> {
|
||||
|
||||
return Types.map((Type, index): Codec => {
|
||||
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) {
|
||||
throw new Error(`Tuple: failed on ${index}:: ${error.message}`);
|
||||
}
|
||||
|
||||
@@ -61,6 +61,14 @@ describe('Vec', (): void => {
|
||||
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 => {
|
||||
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
|
||||
|
||||
@@ -17,6 +17,14 @@ describe('VecFixed', (): void => {
|
||||
it('constructs via Uint8Array', (): void => {
|
||||
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 => {
|
||||
|
||||
@@ -15,9 +15,9 @@ export default function compareSet (a: Set<any>, b?: any): boolean {
|
||||
if (Array.isArray(b)) {
|
||||
return compareSetArray(a, b);
|
||||
} else if (b instanceof Set) {
|
||||
return compareSetArray(a, [...b.entries()]);
|
||||
return compareSetArray(a, [...b.values()]);
|
||||
} else if (isObject(b)) {
|
||||
return compareSetArray(a, Object.entries(b));
|
||||
return compareSetArray(a, Object.values(b));
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user