From a85de0b3f1146a528de36b1513e73101f478d7e1 Mon Sep 17 00:00:00 2001 From: danielbate <--global> Date: Thu, 27 Apr 2023 12:26:24 +0100 Subject: [PATCH 1/5] test: refactor and increase verbosity of enum coder tests --- packages/abi-coder/src/coders/enum.test.ts | 97 ++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/packages/abi-coder/src/coders/enum.test.ts b/packages/abi-coder/src/coders/enum.test.ts index e69de29bb2d..50ff317edb6 100644 --- a/packages/abi-coder/src/coders/enum.test.ts +++ b/packages/abi-coder/src/coders/enum.test.ts @@ -0,0 +1,97 @@ +/* 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 EnumCoder from './enum'; +import U64Coder from './u64'; + +describe('EnumCoder', () => { + const coder = new EnumCoder('TestEnum', { a: new BooleanCoder(), b: new U64Coder() }); + + it('should encode an enum containing a boolean', () => { + const expected = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]); + const actual = coder.encode({ a: true }); + + expect(actual).toStrictEqual(expected); + }); + + it('should decode an enum containing a boolean', () => { + const expectedValue = { a: true }; + const expectedLength = 16; + const [actualValue, actualLength] = coder.decode( + new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]), + 0 + ); + + expect(actualValue).toStrictEqual(expectedValue); + expect(actualLength).toBe(expectedLength); + }); + + it('should encode an enum containing a 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({ b: bn(U64_MAX) }); + + expect(actual).toStrictEqual(expected); + }); + + it('should decode an enum containing a u64', () => { + const expectedValue = { b: 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(actualValue).toStrictEqual(expectedValue); + expect(actualLength).toBe(expectedLength); + }); + + it('should throw an error when encoding if no enum key is provided', () => { + const invalidCoder = new EnumCoder('TestEnum', {}); + + expect(() => invalidCoder.encode({} as never)).toThrow('A field for the case must be provided'); + }); + + it('should throw an error when decoded value accesses an invalid index', () => { + const input = new Uint8Array(Array.from(Array(3).keys())); + expect(() => { + coder.decode(input, 1); + }).toThrow('Invalid caseIndex'); + }); + + it('should not throw given correctly typed inputs', () => { + expect(() => coder.encode({ a: true })).not.toThrow(); + expect(() => coder.encode({ b: bn(1234) })).not.toThrow(); + }); + + it('should throw when provided with extra inputs', () => { + expect(() => + coder.encode( + // @ts-expect-error + { a: true, b: bn(1234), c: false } + ) + ).toThrow('Only one field must be provided'); + }); + + it('should throw type error with invalid input for coder', () => { + expect(() => + coder.encode( + // @ts-expect-error + { b: true } + ) + ).toThrow('Invalid u64'); + }); + + it('should throw type error with invalid input key', () => { + expect(() => + coder.encode( + // @ts-expect-error + { uwu: 42 } + ) + ).toThrow(); + }); +}); From ecb611debe7f26a929e5a0cecdbea24fd502c3dc Mon Sep 17 00:00:00 2001 From: danielbate <--global> Date: Fri, 28 Apr 2023 12:37:45 +0100 Subject: [PATCH 2/5] chore: changeset --- .changeset/gold-swans-hear.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .changeset/gold-swans-hear.md diff --git a/.changeset/gold-swans-hear.md b/.changeset/gold-swans-hear.md new file mode 100644 index 00000000000..a49ba48448f --- /dev/null +++ b/.changeset/gold-swans-hear.md @@ -0,0 +1,2 @@ +--- +--- \ No newline at end of file From 40ad570fb51f470407fbd84ffda0f3728d88b022 Mon Sep 17 00:00:00 2001 From: danielbate <--global> Date: Thu, 4 May 2023 17:07:37 +0100 Subject: [PATCH 3/5] test: introduce test cases for native enums --- packages/abi-coder/src/coders/enum.test.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/abi-coder/src/coders/enum.test.ts b/packages/abi-coder/src/coders/enum.test.ts index 50ff317edb6..50a03f876db 100644 --- a/packages/abi-coder/src/coders/enum.test.ts +++ b/packages/abi-coder/src/coders/enum.test.ts @@ -1,10 +1,14 @@ /* eslint-disable @typescript-eslint/ban-ts-comment */ import { bn } from '@fuel-ts/math'; +import { executionAsyncId } from 'async_hooks'; import { U64_MAX } from '../../test/utils/constants'; +import type Coder from './abstract-coder'; +import ArrayCoder from './array'; import BooleanCoder from './boolean'; import EnumCoder from './enum'; +import NumberCoder from './number'; import U64Coder from './u64'; describe('EnumCoder', () => { @@ -56,6 +60,14 @@ describe('EnumCoder', () => { expect(() => invalidCoder.encode({} as never)).toThrow('A field for the case must be provided'); }); + it.skip('should encode a native enum', () => { + // TODO: complete encode native enum test case + }); + + it.skip('should decode a native enum', () => { + // TODO: complete decode native enum test case + }); + it('should throw an error when decoded value accesses an invalid index', () => { const input = new Uint8Array(Array.from(Array(3).keys())); expect(() => { @@ -90,7 +102,7 @@ describe('EnumCoder', () => { expect(() => coder.encode( // @ts-expect-error - { uwu: 42 } + { nope: 42 } ) ).toThrow(); }); From 6485f38365c86e322c5790d6b8bc96a4a92c8e4f Mon Sep 17 00:00:00 2001 From: danielbate <--global> Date: Thu, 4 May 2023 17:08:21 +0100 Subject: [PATCH 4/5] test: add feature test in abi coder for native enum --- packages/abi-coder/src/abi-coder.test.ts | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/packages/abi-coder/src/abi-coder.test.ts b/packages/abi-coder/src/abi-coder.test.ts index 9aabed4aaeb..5a7464528c0 100644 --- a/packages/abi-coder/src/abi-coder.test.ts +++ b/packages/abi-coder/src/abi-coder.test.ts @@ -488,4 +488,30 @@ describe('AbiCoder', () => { expect(encoded).toStrictEqual(inputAndVecData); expect(hexlify(encoded)).toBe(expected); }); + + // TODO: remove test case + + // This test case is here for demo purposes, I don't think we should be throwing + // for this scenario as I believe we should be supporting native enums + it('should encode and decode a native enum', () => { + const types = [ + { + type: 'enum MyNativeEnum', + components: [ + { + name: 'Checked', + type: '()', + }, + { + name: 'Pending', + type: '()', + }, + ], + }, + ]; + + expect(() => { + abiCoder.encode(types, ['Checked']); + }).toThrow('Invalid type'); + }); }); From 5fc02029b8108f78b2b9b202dff9be510356daf4 Mon Sep 17 00:00:00 2001 From: danielbate <--global> Date: Tue, 16 May 2023 16:17:07 +0100 Subject: [PATCH 5/5] test: remove abi coder test case for native enum as redundant --- packages/abi-coder/src/abi-coder.test.ts | 27 +----------------------- 1 file changed, 1 insertion(+), 26 deletions(-) diff --git a/packages/abi-coder/src/abi-coder.test.ts b/packages/abi-coder/src/abi-coder.test.ts index 5a7464528c0..be6218fb70c 100644 --- a/packages/abi-coder/src/abi-coder.test.ts +++ b/packages/abi-coder/src/abi-coder.test.ts @@ -3,6 +3,7 @@ import { bn, toHex } from '@fuel-ts/math'; import AbiCoder from './abi-coder'; import type { DecodedValue } from './coders/abstract-coder'; +import type { JsonAbiFragmentType } from './json-abi'; const B256 = '0xd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b'; @@ -488,30 +489,4 @@ describe('AbiCoder', () => { expect(encoded).toStrictEqual(inputAndVecData); expect(hexlify(encoded)).toBe(expected); }); - - // TODO: remove test case - - // This test case is here for demo purposes, I don't think we should be throwing - // for this scenario as I believe we should be supporting native enums - it('should encode and decode a native enum', () => { - const types = [ - { - type: 'enum MyNativeEnum', - components: [ - { - name: 'Checked', - type: '()', - }, - { - name: 'Pending', - type: '()', - }, - ], - }, - ]; - - expect(() => { - abiCoder.encode(types, ['Checked']); - }).toThrow('Invalid type'); - }); });