Cleanup type tests (#339)

* Cleanup type tests

* Add Data (as now found as a Rust type)

* Data naming
This commit is contained in:
Jaco Greeff
2018-10-24 11:27:55 +02:00
committed by Amaury Martiny
parent 0ae6b7e76c
commit 6d3de598de
26 changed files with 98 additions and 80 deletions
+2 -2
View File
@@ -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<u8>. 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) {
+9
View File
@@ -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 {
}
@@ -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');
});
});
@@ -16,7 +16,7 @@ describe('ExtrinsicStatus', () => {
it('has the correct hash', () => {
expect(
status.value.toHex()
status.value.toString()
).toEqual('0xc465b92a72b1d20918d64cd4effa70c2bb58b53a3f8c24c3ac8fd8f465f059b4');
});
});
@@ -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');
});
});
+1 -1
View File
@@ -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
+4 -2
View File
@@ -64,8 +64,10 @@ export class MisbehaviorKind extends EnumType<BftDoublePrepare | BftDoubleCommit
}
type MisbehaviorReportValue = {
unstakeThreshold?: AnyNumber,
validatorPayment?: AnyNumber
misbehavior?: MisbehaviorKind | number,
parentHash?: Hash | Uint8Array | string,
parentNumber?: AnyNumber,
target?: AuthorityId | string
};
// A Misbehaviour report against a specific AuthorityId
-31
View File
@@ -1,31 +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 BN from 'bn.js';
import Moment from './Moment';
import U64 from './U64';
const testDecode = (type, input, expected, toJSON = false) =>
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]));
});
+35
View File
@@ -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]));
});
});
+1 -1
View File
@@ -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<Date> {
constructor (value: Uint8Array | Moment | Date | BN | number = 0) {
constructor (value: Moment | Date | AnyNumber = 0) {
super(
Moment.decodeMoment(value)
);
@@ -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
-29
View File
@@ -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]));
});
+32
View File
@@ -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]));
});
});
+1 -1
View File
@@ -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<string> {
constructor (value: Text | string | AnyU8a = '') {
constructor (value: Text | string | AnyU8a | { toString: () => string } = '') {
super(
Text.decodeText(value)
);
+1
View File
@@ -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';