From 6d3de598de2f53d7f9a32cd17f0d4bcdec4f3cc5 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Wed, 24 Oct 2018 11:27:55 +0200 Subject: [PATCH] Cleanup type tests (#339) * Cleanup type tests * Add Data (as now found as a Rust type) * Data naming --- .../types/src/{Bft.spec.js => Bft.spec.ts} | 0 packages/types/src/Bytes.ts | 4 +-- packages/types/src/Data.ts | 9 +++++ .../{Extrinsic.spec.js => Extrinsic.spec.ts} | 2 +- ...Status.spec.js => ExtrinsicStatus.spec.ts} | 2 +- .../src/{Header.spec.js => Header.spec.ts} | 13 ++++--- .../{KeyValue.spec.js => KeyValue.spec.ts} | 0 packages/types/src/KeyValue.ts | 2 +- .../{Metadata.spec.js => Metadata.spec.ts} | 0 ...port.spec.js => MisbehaviorReport.spec.ts} | 0 packages/types/src/MisbehaviorReport.ts | 6 ++-- packages/types/src/Moment.spec.js | 31 ---------------- packages/types/src/Moment.spec.ts | 35 +++++++++++++++++++ packages/types/src/Moment.ts | 2 +- ...come.spec.js => NewAccountOutcome.spec.ts} | 0 .../src/{Origin.spec.js => Origin.spec.ts} | 0 ...Version.spec.js => RuntimeVersion.spec.ts} | 0 ...geSet.spec.js => StorageChangeSet.spec.ts} | 0 ...{StorageKey.spec.js => StorageKey.spec.ts} | 8 ++--- packages/types/src/Text.spec.js | 29 --------------- packages/types/src/Text.spec.ts | 32 +++++++++++++++++ packages/types/src/Text.ts | 2 +- .../types/src/{Type.spec.js => Type.spec.ts} | 0 ...orPrefs.spec.js => ValidatorPrefs.spec.ts} | 0 ...hreshold.spec.js => VoteThreshold.spec.ts} | 0 packages/types/src/index.ts | 1 + 26 files changed, 98 insertions(+), 80 deletions(-) rename packages/types/src/{Bft.spec.js => Bft.spec.ts} (100%) create mode 100644 packages/types/src/Data.ts rename packages/types/src/{Extrinsic.spec.js => Extrinsic.spec.ts} (92%) rename packages/types/src/{ExtrinsicStatus.spec.js => ExtrinsicStatus.spec.ts} (95%) rename packages/types/src/{Header.spec.js => Header.spec.ts} (68%) rename packages/types/src/{KeyValue.spec.js => KeyValue.spec.ts} (100%) rename packages/types/src/{Metadata.spec.js => Metadata.spec.ts} (100%) rename packages/types/src/{MisbehaviorReport.spec.js => MisbehaviorReport.spec.ts} (100%) delete mode 100644 packages/types/src/Moment.spec.js create mode 100644 packages/types/src/Moment.spec.ts rename packages/types/src/{NewAccountOutcome.spec.js => NewAccountOutcome.spec.ts} (100%) rename packages/types/src/{Origin.spec.js => Origin.spec.ts} (100%) rename packages/types/src/{RuntimeVersion.spec.js => RuntimeVersion.spec.ts} (100%) rename packages/types/src/{StorageChangeSet.spec.js => StorageChangeSet.spec.ts} (100%) rename packages/types/src/{StorageKey.spec.js => StorageKey.spec.ts} (77%) delete mode 100644 packages/types/src/Text.spec.js create mode 100644 packages/types/src/Text.spec.ts rename packages/types/src/{Type.spec.js => Type.spec.ts} (100%) rename packages/types/src/{ValidatorPrefs.spec.js => ValidatorPrefs.spec.ts} (100%) rename packages/types/src/{VoteThreshold.spec.js => VoteThreshold.spec.ts} (100%) diff --git a/packages/types/src/Bft.spec.js b/packages/types/src/Bft.spec.ts similarity index 100% rename from packages/types/src/Bft.spec.js rename to packages/types/src/Bft.spec.ts diff --git a/packages/types/src/Bytes.ts b/packages/types/src/Bytes.ts index 0412786a5e..c3c9c9be9e 100644 --- a/packages/types/src/Bytes.ts +++ b/packages/types/src/Bytes.ts @@ -8,8 +8,8 @@ import { AnyU8a } from './types'; import Compact, { DEFAULT_LENGTH_BITS } from './codec/Compact'; import U8a from './codec/U8a'; -// A Bytes. The significant difference between this and a normal Uint8Array is that -// this version allows for length-encoding. (i.e. it is a variable-item codec, the same +// A Bytes wrapper for Vec. The significant difference between this and a normal Uint8Array +// is that this version allows for length-encoding. (i.e. it is a variable-item codec, the same // as what is found in Text and Vector) export default class Bytes extends U8a { constructor (value: AnyU8a) { diff --git a/packages/types/src/Data.ts b/packages/types/src/Data.ts new file mode 100644 index 0000000000..8a5b86a3fe --- /dev/null +++ b/packages/types/src/Data.ts @@ -0,0 +1,9 @@ +// Copyright 2017-2018 @polkadot/types authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import U8a from './codec/U8a'; + +// A raw data structure. It is just an encoding of a U8a, without any length encoding +export default class Data extends U8a { +} diff --git a/packages/types/src/Extrinsic.spec.js b/packages/types/src/Extrinsic.spec.ts similarity index 92% rename from packages/types/src/Extrinsic.spec.js rename to packages/types/src/Extrinsic.spec.ts index 835c739412..69c780d798 100644 --- a/packages/types/src/Extrinsic.spec.js +++ b/packages/types/src/Extrinsic.spec.ts @@ -29,6 +29,6 @@ describe('Extrinsic', () => { expect(extrinsic.signature.nonce.toNumber()).toEqual(1); expect(extrinsic.signature.era.toU8a()).toEqual(new Uint8Array([0x27, 0x0e])); expect(extrinsic.callIndex).toEqual(new Uint8Array([1, 0])); - expect(extrinsic.method.args[0].toHex()).toEqual('0xff0e6422725af0e9aede3bef6eba77bc87afadf60b9735057ae93801f4c472ad7b'); + expect(extrinsic.args[0].toString()).toEqual('5CPaGq4KcmntaboAxg5nyqGXdyzaBV2hj6PvhNA3syigiRg8'); }); }); diff --git a/packages/types/src/ExtrinsicStatus.spec.js b/packages/types/src/ExtrinsicStatus.spec.ts similarity index 95% rename from packages/types/src/ExtrinsicStatus.spec.js rename to packages/types/src/ExtrinsicStatus.spec.ts index d00902a0c2..d2f6527098 100644 --- a/packages/types/src/ExtrinsicStatus.spec.js +++ b/packages/types/src/ExtrinsicStatus.spec.ts @@ -16,7 +16,7 @@ describe('ExtrinsicStatus', () => { it('has the correct hash', () => { expect( - status.value.toHex() + status.value.toString() ).toEqual('0xc465b92a72b1d20918d64cd4effa70c2bb58b53a3f8c24c3ac8fd8f465f059b4'); }); }); diff --git a/packages/types/src/Header.spec.js b/packages/types/src/Header.spec.ts similarity index 68% rename from packages/types/src/Header.spec.js rename to packages/types/src/Header.spec.ts index 2ce3db86c3..1ac31d5bac 100644 --- a/packages/types/src/Header.spec.js +++ b/packages/types/src/Header.spec.ts @@ -26,18 +26,17 @@ describe('Header', () => { ).toEqual('[]'); }); - // FIXME check again when we have a valid chain/UI - it.skip('creates actual valid hash', () => { + it('creates actual valid hash', () => { expect( new Header({ digest: { logs: [] }, - extrinsicsRoot: '0x5ce1a2c55efaf371f06fd94672779daf130591a12f7513ae786bea4fa7bd499e', - number: 1556355, - parentHash: '0x09223919f63b5ded0c7bf7d953ba22bf30ea8a19e6be4de35f25163b2b268490', - stateRoot: '0xd45159e04f36955ecb9c73d6ca514ac2d0e3ce00f58588b7c142d36cf7d237ec' + extrinsicsRoot: '0xfd068a7f7315e1c5529945ddd80b650abfc1643be9a79f0468673617a7c0af41', + number: 338571, + parentHash: '0x20946c40c54af3bf76a93e313d51bdcb14ee9e783cde9565d0f8c0d72727e512', + stateRoot: '0x6980a37d9297765aed7a67bb0147afa34ec6db387e79c096feb1708294399abd' }).hash.toHex() - ).toEqual('0x3a91467e3d298684c2d3ee68362615e9b21fa9e37739c02c25f8075918e1ab11'); + ).toEqual('0xfefcdd5287d017f240b816bd6c43cb9f254164adb90350f2d3f3303f604b5a61'); }); }); diff --git a/packages/types/src/KeyValue.spec.js b/packages/types/src/KeyValue.spec.ts similarity index 100% rename from packages/types/src/KeyValue.spec.js rename to packages/types/src/KeyValue.spec.ts diff --git a/packages/types/src/KeyValue.ts b/packages/types/src/KeyValue.ts index e7f777af40..56c0f84c04 100644 --- a/packages/types/src/KeyValue.ts +++ b/packages/types/src/KeyValue.ts @@ -20,7 +20,7 @@ type KeyValueValue = { // for the keys and values. (Not to be confused with the KeyValue in Metadata, that // is actually for Maps, whereas this is a representation of actaul storage values) export default class KeyValue extends Struct { - constructor (value?: KeyValueValue) { + constructor (value?: KeyValueValue | Uint8Array) { super({ key: StorageKey, value: StorageData diff --git a/packages/types/src/Metadata.spec.js b/packages/types/src/Metadata.spec.ts similarity index 100% rename from packages/types/src/Metadata.spec.js rename to packages/types/src/Metadata.spec.ts diff --git a/packages/types/src/MisbehaviorReport.spec.js b/packages/types/src/MisbehaviorReport.spec.ts similarity index 100% rename from packages/types/src/MisbehaviorReport.spec.js rename to packages/types/src/MisbehaviorReport.spec.ts diff --git a/packages/types/src/MisbehaviorReport.ts b/packages/types/src/MisbehaviorReport.ts index aedcf10302..f68b10aaf0 100644 --- a/packages/types/src/MisbehaviorReport.ts +++ b/packages/types/src/MisbehaviorReport.ts @@ -64,8 +64,10 @@ export class MisbehaviorKind extends EnumType - it(`can decode from ${type}`, () => { - expect(new Moment(input)[toJSON ? 'toJSON' : 'toString']()).toBe(expected); - }); - -const testEncode = (to, expected) => - it(`can encode ${to}`, () => { - expect(new Moment(421)[to]()).toEqual(expected); - }); - -describe('Moment', () => { - testDecode('Date', new Date(1537968546280), '2018-09-26T13:29:07.000Z'); - testDecode('Moment', new Moment(1234), 1234, true); - testDecode('number', 1234, 1234, true); - testDecode('U64', new U64(69), 69, true); - - testEncode('toBn', new BN(421)); - testEncode('toJSON', 421); - testEncode('toString', '1970-01-01T00:07:01.000Z'); - testEncode('toNumber', 421); - testEncode('toU8a', Uint8Array.from([165, 1, 0, 0, 0, 0, 0, 0])); -}); diff --git a/packages/types/src/Moment.spec.ts b/packages/types/src/Moment.spec.ts new file mode 100644 index 0000000000..4bf8814927 --- /dev/null +++ b/packages/types/src/Moment.spec.ts @@ -0,0 +1,35 @@ +// Copyright 2017-2018 @polkadot/types authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import BN from 'bn.js'; + +import Moment from './Moment'; +import U64 from './U64'; + +describe('Moment', () => { + describe('decode', () => { + const testDecode = (type: string, input: Date | Moment | U64 | number, expected: string | number, toJSON: boolean = false) => + it(`can decode from ${type}`, () => { + expect(new Moment(input)[toJSON ? 'toJSON' : 'toString']()).toBe(expected); + }); + + testDecode('Date', new Date(1537968546280), '2018-09-26T13:29:07.000Z'); + testDecode('Moment', new Moment(1234), 1234, true); + testDecode('number', 1234, 1234, true); + testDecode('U64', new U64(69), 69, true); + }); + + describe('encode', () => { + const testEncode = (to: 'toBn' | 'toJSON' | 'toString' | 'toNumber' | 'toU8a', expected: BN | number | string | Uint8Array) => + it(`can encode ${to}`, () => { + expect(new Moment(421)[to]()).toEqual(expected); + }); + + testEncode('toBn', new BN(421)); + testEncode('toJSON', 421); + testEncode('toString', '1970-01-01T00:07:01.000Z'); + testEncode('toNumber', 421); + testEncode('toU8a', Uint8Array.from([165, 1, 0, 0, 0, 0, 0, 0])); + }); +}); diff --git a/packages/types/src/Moment.ts b/packages/types/src/Moment.ts index 69ae678f5e..5b2550ee2a 100644 --- a/packages/types/src/Moment.ts +++ b/packages/types/src/Moment.ts @@ -16,7 +16,7 @@ const BITLENGTH = 64; // per-second. For any encoding/decoding the 1000 multiplier would be applied to // get it in line with JavaScript formats export default class Moment extends Base { - constructor (value: Uint8Array | Moment | Date | BN | number = 0) { + constructor (value: Moment | Date | AnyNumber = 0) { super( Moment.decodeMoment(value) ); diff --git a/packages/types/src/NewAccountOutcome.spec.js b/packages/types/src/NewAccountOutcome.spec.ts similarity index 100% rename from packages/types/src/NewAccountOutcome.spec.js rename to packages/types/src/NewAccountOutcome.spec.ts diff --git a/packages/types/src/Origin.spec.js b/packages/types/src/Origin.spec.ts similarity index 100% rename from packages/types/src/Origin.spec.js rename to packages/types/src/Origin.spec.ts diff --git a/packages/types/src/RuntimeVersion.spec.js b/packages/types/src/RuntimeVersion.spec.ts similarity index 100% rename from packages/types/src/RuntimeVersion.spec.js rename to packages/types/src/RuntimeVersion.spec.ts diff --git a/packages/types/src/StorageChangeSet.spec.js b/packages/types/src/StorageChangeSet.spec.ts similarity index 100% rename from packages/types/src/StorageChangeSet.spec.js rename to packages/types/src/StorageChangeSet.spec.ts diff --git a/packages/types/src/StorageKey.spec.js b/packages/types/src/StorageKey.spec.ts similarity index 77% rename from packages/types/src/StorageKey.spec.js rename to packages/types/src/StorageKey.spec.ts index 20ce1bafbe..64ccce61ed 100644 --- a/packages/types/src/StorageKey.spec.js +++ b/packages/types/src/StorageKey.spec.ts @@ -2,12 +2,12 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -import storage from '@polkadot/storage/static' +import storage from '@polkadot/storage/static'; -import StorageKey from './StorageKey' +import StorageKey from './StorageKey'; describe('StorageKey', () => { - it("should correctly get Alice's freeBalance storage key (hex)", () => { + it(`should correctly get Alice's freeBalance storage key (hex)`, () => { expect( new StorageKey( storage @@ -18,7 +18,7 @@ describe('StorageKey', () => { ).toBe('0xce3f3d8f09e3411403f5ca59d042a40e'); // FIXME OK this should be length-prefixed in reality }); - it("should correctly get Alice's freeBalance storage key (u8a)", () => { + it(`should correctly get Alice's freeBalance storage key (u8a)`, () => { expect( new StorageKey( storage diff --git a/packages/types/src/Text.spec.js b/packages/types/src/Text.spec.js deleted file mode 100644 index 27890a1fb8..0000000000 --- a/packages/types/src/Text.spec.js +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2017-2018 @polkadot/types authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import Text from './Text'; -import U8a from './codec/U8a'; - -const testDecode = (type, input, expected) => - it(`can decode from ${type}`, () => { - expect(new Text(input).toString()).toBe(expected); - }); - -const testEncode = (to, expected) => - it(`can encode ${to}`, () => { - expect(new Text('foo')[to]()).toEqual(expected); - }); - -describe('Text', () => { - - testDecode('string', 'foo', 'foo'); - testDecode('Text', new Text('foo'), 'foo'); - testDecode('Uint8Array', Uint8Array.from([12, 102, 111, 111]), 'foo'); - testDecode('U8a', new U8a(Uint8Array.from([12, 102, 111, 111])), 'foo'); - testDecode('object with `toString()`', { toString() { return 'foo'; } }, 'foo'); - - // testEncode('toHex', '0x0c666f6f'); // FIXME Add this - testEncode('toString', 'foo'); - testEncode('toU8a', Uint8Array.from([12, 102, 111, 111])); -}); diff --git a/packages/types/src/Text.spec.ts b/packages/types/src/Text.spec.ts new file mode 100644 index 0000000000..d261ae083d --- /dev/null +++ b/packages/types/src/Text.spec.ts @@ -0,0 +1,32 @@ +// Copyright 2017-2018 @polkadot/types authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import Text from './Text'; +import U8a from './codec/U8a'; + +describe('Text', () => { + describe('decode', () => { + const testDecode = (type: string, input: string | U8a | Uint8Array | { toString: () => string }, expected: string) => + it(`can decode from ${type}`, () => { + expect(new Text(input).toString()).toBe(expected); + }); + + testDecode('string', 'foo', 'foo'); + testDecode('Text', new Text('foo'), 'foo'); + testDecode('Uint8Array', Uint8Array.from([12, 102, 111, 111]), 'foo'); + testDecode('U8a', new U8a(Uint8Array.from([12, 102, 111, 111])), 'foo'); + testDecode('object with `toString()`', { toString (): string { return 'foo'; } }, 'foo'); + }); + + describe('encode', () => { + const testEncode = (to: 'toString' | 'toU8a', expected: string | Uint8Array) => + it(`can encode ${to}`, () => { + expect(new Text('foo')[to]()).toEqual(expected); + }); + + // testEncode('toHex', '0x0c666f6f'); // FIXME Add this + testEncode('toString', 'foo'); + testEncode('toU8a', Uint8Array.from([12, 102, 111, 111])); + }); +}); diff --git a/packages/types/src/Text.ts b/packages/types/src/Text.ts index b8335ed57c..78fd593a0b 100644 --- a/packages/types/src/Text.ts +++ b/packages/types/src/Text.ts @@ -18,7 +18,7 @@ import U8a from './codec/U8a'; // wraps the `Balance`, `T::AccountId`, etc. The reasoning - with a "TypeString" // we can nicely strip types down like "T::AcountId" -> "AccountId" export default class Text extends Base { - constructor (value: Text | string | AnyU8a = '') { + constructor (value: Text | string | AnyU8a | { toString: () => string } = '') { super( Text.decodeText(value) ); diff --git a/packages/types/src/Type.spec.js b/packages/types/src/Type.spec.ts similarity index 100% rename from packages/types/src/Type.spec.js rename to packages/types/src/Type.spec.ts diff --git a/packages/types/src/ValidatorPrefs.spec.js b/packages/types/src/ValidatorPrefs.spec.ts similarity index 100% rename from packages/types/src/ValidatorPrefs.spec.js rename to packages/types/src/ValidatorPrefs.spec.ts diff --git a/packages/types/src/VoteThreshold.spec.js b/packages/types/src/VoteThreshold.spec.ts similarity index 100% rename from packages/types/src/VoteThreshold.spec.js rename to packages/types/src/VoteThreshold.spec.ts diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index e63e56e2f0..835c323246 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -24,6 +24,7 @@ export { default as Compact32 } from './Compact32'; export { default as Compact64 } from './Compact64'; export { default as Compact128 } from './Compact128'; export { default as Compact256 } from './Compact256'; +export { default as Data } from './Data'; // NOTE Only used internally, // export { default as Digest } from './Digest'; export { default as Extrinsic } from './Extrinsic';