Adjust size-assert for U8Fixed (#4787)

This commit is contained in:
Jaco
2022-04-30 22:35:23 +03:00
committed by GitHub
parent 1870ad26d5
commit d29660dc4e
3 changed files with 34 additions and 3 deletions
@@ -35,13 +35,43 @@ describe('U8aFixed', (): void => {
new Uint8Array([0x02, 0x03, 0x00, 0x00])
);
});
it('constructs when passed Uint8Array is >= length', (): void => {
expect(
new (U8aFixed.with(32))(registry, new Uint8Array([0x00, 0x01, 0x02, 0x03])).toU8a()
).toEqual(
new Uint8Array([0x00, 0x01, 0x02, 0x03])
);
expect(
new (U8aFixed.with(32))(registry, new Uint8Array([0x00, 0x01, 0x02, 0x03, 0x04, 0x05])).toU8a()
).toEqual(
new Uint8Array([0x00, 0x01, 0x02, 0x03])
);
});
it('constructs when passed string is === length', (): void => {
expect(
new (U8aFixed.with(32))(registry, '1234').toU8a()
).toEqual(
new Uint8Array([49, 50, 51, 52])
);
});
it('fails construction when passed string is > length', (): void => {
expect(
() => new (U8aFixed.with(32))(registry, '0x000102030405').toU8a()
).toThrow(/Expected input with 4 bytes/);
expect(
() => new (U8aFixed.with(256))(registry, '1363HWTPzDrzAQ6ChFiMU6mP4b6jmQid2ae55JQcKtZnpLGv')
).toThrow(/Expected input with 32 bytes/);
});
});
describe('utils', (): void => {
let u8a: U8aFixed;
beforeEach((): void => {
u8a = new U8aFixed(registry, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 32);
u8a = new U8aFixed(registry, [1, 2, 3, 4], 32);
});
it('limits the length', (): void => {
@@ -3,7 +3,7 @@
import type { AnyU8a, CodecClass, Registry, U8aBitLength } from '../types';
import { assert, u8aToU8a } from '@polkadot/util';
import { assert, isU8a, u8aToU8a } from '@polkadot/util';
import { Raw } from '../native/Raw';
@@ -16,7 +16,7 @@ function decodeU8aFixed (value: AnyU8a, bitLength: U8aBitLength): [AnyU8a, numbe
return [new Uint8Array(byteLength), 0];
}
assert(u8a.length >= byteLength, () => `Expected at least ${byteLength} bytes (${bitLength} bits), found ${u8a.length} bytes`);
assert(isU8a(value) ? u8a.length >= byteLength : u8a.length === byteLength, () => `Expected input with ${byteLength} bytes (${bitLength} bits), found ${u8a.length} bytes`);
return [u8a.subarray(0, byteLength), byteLength];
}