From 7cb2ed4d9c5b801e1a39329daeeaf18d550cc294 Mon Sep 17 00:00:00 2001 From: danielbate <--global> Date: Wed, 26 Apr 2023 15:02:49 +0100 Subject: [PATCH 1/2] test: refactor and increase verbosity of tuple coder tests --- packages/abi-coder/src/coders/tuple.test.ts | 82 +++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/packages/abi-coder/src/coders/tuple.test.ts b/packages/abi-coder/src/coders/tuple.test.ts index e69de29bb2d..38b694ad985 100644 --- a/packages/abi-coder/src/coders/tuple.test.ts +++ b/packages/abi-coder/src/coders/tuple.test.ts @@ -0,0 +1,82 @@ +/* eslint-disable @typescript-eslint/ban-ts-comment */ +import { bn } from '@fuel-ts/math'; + +import { U64_MAX } from '../../test/utils/constants'; + +import BooleanCoder from './boolean'; +import TupleCoder from './tuple'; +import U64Coder from './u64'; + +describe('Tuple Coder', () => { + const coder = new TupleCoder<[BooleanCoder, U64Coder]>([new BooleanCoder(), new U64Coder()]); + + it('should encode a tuple containing a boolean and u64', () => { + const expected = new Uint8Array([ + 0, 0, 0, 0, 0, 0, 0, 1, 255, 255, 255, 255, 255, 255, 255, 255, + ]); + const actual = coder.encode([true, U64_MAX]); + + expect(actual).toStrictEqual(expected); + }); + + it('should decode a tuple containing a boolean and u64', () => { + const expectedValue = [true, bn(U64_MAX)]; + const expectedLength = 16; + const [actualValue, actualLength] = coder.decode( + new Uint8Array([0, 0, 0, 0, 0, 0, 0, 1, 255, 255, 255, 255, 255, 255, 255, 255]), + 0 + ); + + expect(JSON.stringify(actualValue)).toStrictEqual(JSON.stringify(expectedValue)); + expect(actualLength).toBe(expectedLength); + }); + + it('should not throw given correctly typed inputs', () => { + expect(() => coder.encode([true, bn(1234)])).not.toThrow(); + }); + + it('should throw when provided with extra inputs', () => { + expect(() => + coder.encode( + // @ts-expect-error + [true, bn(1337), false] + ) + ).toThrow('Types/values length mismatch'); + }); + + it('should throw type error with both missing inputs', () => { + expect(() => + coder.encode( + // @ts-expect-error + [] + ) + ).toThrow('Types/values length mismatch'); + }); + + it('should throw type error with a missing input', () => { + expect(() => + coder.encode( + // @ts-expect-error + [true] + ) + ).toThrow('Types/values length mismatch'); + }); + + it('should throw type error with invalid inputs', () => { + expect(() => + coder.encode( + // @ts-expect-error + [bn(1234), true] + ) + ).toThrow(); + }); + + it('should throw when input is an object', () => { + expect(() => + coder.encode( + // @ts-expect-error + { nope: 42 } + ) + ).toThrow(); + }); +}); From 267d6611cf68f9eef28f1c1a0cf1b31d4d1c49a4 Mon Sep 17 00:00:00 2001 From: danielbate <--global> Date: Fri, 28 Apr 2023 12:42:14 +0100 Subject: [PATCH 2/2] chore: changeset --- .changeset/calm-actors-attack.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .changeset/calm-actors-attack.md diff --git a/.changeset/calm-actors-attack.md b/.changeset/calm-actors-attack.md new file mode 100644 index 00000000000..a49ba48448f --- /dev/null +++ b/.changeset/calm-actors-attack.md @@ -0,0 +1,2 @@ +--- +--- \ No newline at end of file