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 3 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 @@
---
---
97 changes: 97 additions & 0 deletions packages/abi-coder/src/coders/enum.test.ts
Original file line number Diff line number Diff line change
@@ -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]);
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('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();
});
});