// Copyright 2017-2021 @axiasolar/types authors & contributors // SPDX-License-Identifier: Apache-2.0 import { stringToU8a, u8aConcat } from '@axiasolar/util'; import { TypeRegistry } from '../create'; import { Text, Type } from '.'; describe('Type', (): void => { const registry = new TypeRegistry(); it('fails to cleanup invalid boxes', (): void => { expect( (): Type => new Type(registry, 'Box { expect( new Type(registry, '(AccountId)').toString() ).toEqual('AccountId'); }); it('does not touch tuples with multiple values', (): void => { expect( new Type(registry, '(AccountId, Balance)').toString() ).toEqual('(AccountId,Balance)'); }); it('handles nested types', (): void => { expect( new Type(registry, 'Box>').toString() ).toEqual('Vec'); }); it('handles nested types (embedded)', (): void => { expect( new Type(registry, '(u32, Box>)').toString() ).toEqual('(u32,Vec)'); }); it('handles aliasses, multiples per line', (): void => { expect( new Type(registry, '(Vec, AccountId, Vec)').toString() ).toEqual('(Bytes,AccountId,Bytes)'); }); it('removes whitespaces', (): void => { expect( new Type(registry, 'T :: AccountId').toString() ).toEqual('AccountId'); }); it('changes PairOf -> (T, T)', (): void => { expect( new Type(registry, 'PairOf').toString() ).toEqual('(Balance,Balance)'); }); it('changes PairOf (embedded) -> (T, T)', (): void => { expect( new Type(registry, '(Vec, PairOf, Vec)').toString() ).toEqual('(Bytes,(Balance,Balance),Vec)'); }); it('changes () -> ()', (): void => { expect( new Type(registry, '()').toString() ).toEqual('()'); }); it('has the sanitized', (): void => { expect( new Type( registry, new Text(registry, ' Box ') ).toString() ).toEqual('Proposal'); // eslint-disable-line }); it('unwraps compact', (): void => { expect( new Type(registry, '::Type').toString() ).toEqual('Compact'); }); it('handles InherentOfflineReport', (): void => { expect( new Type(registry, '::Inherent').toString() ).toEqual('InherentOfflineReport'); }); it('encodes correctly via toU8a()', (): void => { const type = 'Compact'; expect(new Text(registry, type).toU8a()).toEqual( u8aConcat( new Uint8Array([type.length << 2]), stringToU8a(type) ) ); }); it('creates a decodable U8a for sanitized types', (): void => { const original = '::Inherent'; const expected = 'InherentOfflineReport'; const u8a = new Type(registry, original).toU8a(); const decoded = new Type(registry, u8a); expect(decoded.encodedLength).toEqual(original.length + 1); // extra byte for length expect(decoded.toString()).toEqual(expected); }); it('has the correct raw', (): void => { expect(new Type(registry).toRawType()).toEqual('Type'); }); });