Skip to content

Commit

Permalink
Revert to from again
Browse files Browse the repository at this point in the history
  • Loading branch information
amaury1093 committed Oct 15, 2018
1 parent a48e192 commit a173804
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 55 deletions.
2 changes: 1 addition & 1 deletion packages/type-extrinsics/src/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import Metadata from '@polkadot/types/Metadata';
import fromMetadata from './fromMetadata';

export default fromMetadata(
new Metadata(metadataRpc)
new Metadata().fromJSON(metadataRpc)
);
2 changes: 1 addition & 1 deletion packages/type-storage/src/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import Metadata from '@polkadot/types/Metadata';
import fromMetadata from './fromMetadata';

export default fromMetadata(
new Metadata(metadataRpc)
new Metadata().fromJSON(metadataRpc)
);
2 changes: 1 addition & 1 deletion packages/types/src/ExtrinsicStatus.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import rpc from './ExtrinsicStatus.rpc.json';
import ExtrinsicStatus from './ExtrinsicStatus';

describe('ExtrinsicStatus', () => {
const status = new ExtrinsicStatus(rpc.params.result);
const status = new ExtrinsicStatus().fromJSON(rpc.params.result);

it('has the correct type', () => {
expect(
Expand Down
4 changes: 3 additions & 1 deletion packages/types/src/Header.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import json from './Header.only.json';

describe('Header', () => {
it('decodes an actual JSON response', () => {
const header = new Header(json);
const header = new Header();

header.fromJSON(json);

expect(
header.blockNumber.toNumber()
Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/Metadata.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import rpcdata from './Metadata.rpc';

describe('Metadata', () => {
it('decodes properly', () => {
const decoded = new Metadata(rpcdata);
const decoded = new Metadata().fromJSON(rpcdata);
const str = JSON.stringify(decoded.toJSON());

console.error(str);
Expand All @@ -20,7 +20,7 @@ describe('Metadata', () => {

it('decodes when length not present (HACK)', () => {
const u8a = hexToU8a(rpcdata);
const decoded = new Metadata(u8a.subarray(2));
const decoded = new Metadata().fromU8a(u8a.subarray(2));

expect(decoded.events.length).not.toBe(0);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/RuntimeVersion.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import rpc from './RuntimeVersion.rpc.json';
import RuntimeVersion from './RuntimeVersion';

describe('RuntimeVersion', () => {
const version = new RuntimeVersion(rpc.result);
const version = new RuntimeVersion().fromJSON(rpc.result);

it('has the correct authoring', () => {
expect(version.authoringVersion.toNumber()).toEqual(1);
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/SignedBlock.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import rpc from './SignedBlock.rpc.json';
import SignedBlock from './SignedBlock';

describe('SignedBlock', () => {
const block = new SignedBlock(rpc.result);
const block = new SignedBlock().fromJSON(rpc.result);

it('has the correct stateRoot', () => {
expect(
Expand Down
6 changes: 3 additions & 3 deletions packages/types/src/Type.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import Type from './Type';
describe('Type', () => {
it('fails to cleanup invalid boxes', () => {
expect(
() => new Type('Box<Proposal')
() => new Type().fromJSON('Box<Proposal')
).toThrow(/find closing matching/);
});

it('handles nested types', () => {
expect(
new Type('Box<Vec<AccountId>>').toString()
new Type().fromJSON('Box<Vec<AccountId>>').toString()
).toEqual('Vec<AccountId>');
});

it('handles aliasses, multiples per line', () => {
expect(
new Type('(Vec<u8>, AccountId, Vec<u8>)').toString()
new Type().fromJSON('(Vec<u8>, AccountId, Vec<u8>)').toString()
).toEqual('(Bytes, AccountId, Bytes)');
});

Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/codec/Compact.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe('Compact', () => {

it('constructs properly via fromU8a', () => {
expect(
new Compact(new Uint8Array([254, 255, 3, 0])).raw
new Compact().fromU8a(new Uint8Array([254, 255, 3, 0])).raw
).toEqual(new BN(0xffff));
});

Expand Down
59 changes: 18 additions & 41 deletions packages/types/src/codec/Enum.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,32 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.

import Enum from './Enum';
import EnumType from './EnumType';
import Text from '../Text';
import U32 from '../U32';

const testDecode = (type, input, expected) =>
it(`can decode from ${type}`, () => {
const e = new Enum(['foo', 'bar'], input);
expect(e.toString()).toBe(expected);
});

const testEncode = (to, expected) =>
it(`can encode ${to}`, () => {
const e = new Enum(['foo', 'bar'], 1);
expect(e[to]()).toEqual(expected);
});

describe('Enum', () => {

testDecode('Enum', undefined, 'foo');
testDecode('Enum', new Enum([], 1), 'bar');
testDecode('number', 0, 'foo');
testDecode('number', 1, 'bar');
testDecode('Uint8Array', Uint8Array.from([0]), 'foo');
testDecode('Uint8Array', Uint8Array.from([1]), 'bar');

testEncode('toJSON', 1);
testEncode('toNumber', 1);
testEncode('toString', 'bar');
testEncode('toU8a', Uint8Array.from([1]));

it('provides a clean toString()', () => {
describe('Struct', () => {
it('provides a clean toString() (raw)', () => {
expect(
new Enum(['foo', 'bar']).toString()
).toEqual('foo');
new EnumType(
[Text, U32]
).fromU8a(new Uint8Array([0, 2 << 2, 49, 50])).raw.toString()
).toEqual('12');
});

it('provides a clean toString() (enum)', () => {
expect(
new Enum(['foo', 'bar'], new Enum([], 1)).toNumber()
).toEqual(1);
});

it('converts to and from U8a', () => {
expect(
new Enum(['foo', 'bar'], new Uint8Array([1])).toU8a()
).toEqual(new Uint8Array([1]));
new EnumType(
[Text, U32]
).fromU8a(new Uint8Array([1, 2 << 2, 49, 50])).toString()
).toEqual('U32');
});

it('converts from JSON', () => {
it('allows checking against defined indexes', () => {
expect(
new Enum(['foo', 'bar'], 5).toString()
).toEqual('5');
new EnumType(
{ 1: Text, 5: U32 }
).fromU8a(new Uint8Array([1, 2 << 2, 49, 50])).toString()
).toEqual('Text');
});
});
2 changes: 1 addition & 1 deletion packages/types/src/codec/Option.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Option', () => {

it('has value toString() (provided)', () => {
expect(
new Option(Text, new Uint8Array([1, 4 << 2, 49, 50, 51, 52])).toString()
new Option(Text).fromU8a(new Uint8Array([1, 4 << 2, 49, 50, 51, 52])).toString()
).toEqual('1234');
});

Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/codec/Vector.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('Vector', () => {

it('allows contruction via JSON', () => {
expect(
new Vector(Text, ['6', '7']).toString()
new Vector(Text).fromJSON(['6', '7']).toString()
).toEqual('[6, 7]');
});

Expand Down

0 comments on commit a173804

Please sign in to comment.