Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: refactor EnumCoder tests #938

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
---
---
26 changes: 26 additions & 0 deletions packages/abi-coder/src/abi-coder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
danielbate marked this conversation as resolved.
Show resolved Hide resolved
});
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]);
danielbate marked this conversation as resolved.
Show resolved Hide resolved
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();
});
});