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 NumberCoder tests #949

Merged
Show file tree
Hide file tree
Changes from all 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/wild-kiwis-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
137 changes: 137 additions & 0 deletions packages/abi-coder/src/coders/number.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { U8_MAX, U16_MAX, U32_MAX } from '../../test/utils/constants';

import NumberCoder from './number';

describe('NumberCoder', () => {
it('should encode min u8 number as a u8 coder', () => {
const coder = new NumberCoder('u8');
const expected = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0]);
const actual = coder.encode(0);

expect(actual).toStrictEqual(expected);
});

it('should decode a min u8 number as a u8 coder', () => {
const coder = new NumberCoder('u8');
const expectedValue = 0;
const expectedLength = 8;
const [actualValue, actualLength] = coder.decode(new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0]), 0);

expect(actualValue).toBe(expectedValue);
expect(actualLength).toBe(expectedLength);
});

it('should encode max u8 number as a u8 coder', () => {
const coder = new NumberCoder('u8');
const expected = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 255]);
const actual = coder.encode(U8_MAX);

expect(actual).toStrictEqual(expected);
});

it('should decode a max u8 number as a u8 coder', () => {
const coder = new NumberCoder('u8');
const expectedValue = U8_MAX;
const expectedLength = 8;
const [actualValue, actualLength] = coder.decode(new Uint8Array([0, 0, 0, 0, 0, 0, 0, 255]), 0);

expect(actualValue).toBe(expectedValue);
expect(actualLength).toBe(expectedLength);
});

it('should encode min u16 number as a u16 coder', () => {
const coder = new NumberCoder('u16');
const expected = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0]);
const actual = coder.encode(0);

expect(actual).toStrictEqual(expected);
});

it('should decode a min u16 number as a u16 coder', () => {
const coder = new NumberCoder('u16');
const expectedValue = 0;
const expectedLength = 8;
const [actualValue, actualLength] = coder.decode(new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0]), 0);

expect(actualValue).toBe(expectedValue);
expect(actualLength).toBe(expectedLength);
});

it('should encode max u16 number as a u16 coder', () => {
const coder = new NumberCoder('u16');
const expected = new Uint8Array([0, 0, 0, 0, 0, 0, 255, 255]);
const actual = coder.encode(U16_MAX);

expect(actual).toStrictEqual(expected);
});

it('should decode a max u16 number as a u16 coder', () => {
const coder = new NumberCoder('u16');
const expectedValue = U16_MAX;
const expectedLength = 8;
const [actualValue, actualLength] = coder.decode(
new Uint8Array([0, 0, 0, 0, 0, 0, 255, 255]),
0
);

expect(actualValue).toBe(expectedValue);
expect(actualLength).toBe(expectedLength);
});

it('should encode min u32 number as a u32 coder', () => {
const coder = new NumberCoder('u32');
const expected = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0]);
const actual = coder.encode(0);

expect(actual).toStrictEqual(expected);
});

it('should decode a min u32 number as a u32 coder', () => {
const coder = new NumberCoder('u32');
const expectedValue = 0;
const expectedLength = 8;
const [actualValue, actualLength] = coder.decode(new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0]), 0);

expect(actualValue).toBe(expectedValue);
expect(actualLength).toBe(expectedLength);
});

it('should encode max u32 number as a u32 coder', () => {
const coder = new NumberCoder('u32');
const expected = new Uint8Array([0, 0, 0, 0, 255, 255, 255, 255]);
const actual = coder.encode(U32_MAX);

expect(actual).toStrictEqual(expected);
});

it('should decode a max u32 number as a u32 coder', () => {
const coder = new NumberCoder('u32');
const expectedValue = U32_MAX;
const expectedLength = 8;
const [actualValue, actualLength] = coder.decode(
new Uint8Array([0, 0, 0, 0, 255, 255, 255, 255]),
0
);

expect(actualValue).toBe(expectedValue);
expect(actualLength).toBe(expectedLength);
});

it('should throw if a negative number is encoded', () => {
const coder = new NumberCoder('u8');
const invalidInput = -1;

expect(() => {
coder.encode(invalidInput);
}).toThrow('Invalid u8');
});

it('should throw if coder is too small for number size', () => {
const coder = new NumberCoder('u8');
const invalidInput = U32_MAX;

expect(() => {
coder.encode(invalidInput);
}).toThrow('Invalid u8. Too many bytes.');
});
});