Skip to content

Commit

Permalink
test: refactor EnumCoder tests (#938)
Browse files Browse the repository at this point in the history
* test: refactor and increase verbosity of enum coder tests

* chore: changeset

* test: introduce test cases for native enums

* test: add feature test in abi coder for native enum

* test: remove abi coder test case for native enum as redundant

---------

Co-authored-by: danielbate <--global>
  • Loading branch information
danielbate authored May 16, 2023
1 parent 03e4988 commit 2527865
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .changeset/gold-swans-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
1 change: 1 addition & 0 deletions packages/abi-coder/src/abi-coder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
109 changes: 109 additions & 0 deletions packages/abi-coder/src/coders/enum.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/* 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', () => {
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.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(() => {
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
{ nope: 42 }
)
).toThrow();
});
});

0 comments on commit 2527865

Please sign in to comment.