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 B256Coder tests #946

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/shy-knives-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
85 changes: 85 additions & 0 deletions packages/abi-coder/src/coders/b256.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import B256Coder from './b256';

describe('B256Coder', () => {
const B256_DECODED = '0xd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b';
const B256_ENCODED = new Uint8Array([
213, 87, 156, 70, 223, 204, 127, 24, 32, 112, 19, 230, 91, 68, 228, 203, 78, 44, 34, 152, 244,
172, 69, 123, 168, 248, 39, 67, 243, 30, 147, 11,
]);
const B256_ZERO_DECODED = '0x0000000000000000000000000000000000000000000000000000000000000000';
const B256_ZERO_ENCODED = new Uint8Array(32);

const coder = new B256Coder();

it('should encode zero as a 256 bit hash string', () => {
const expected = B256_ZERO_ENCODED;
const actual = coder.encode(B256_ZERO_DECODED);

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

it('should encode a 256 bit hash string', () => {
const expected = B256_ENCODED;
const actual = coder.encode(B256_DECODED);

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

it('should decode zero as a 256 bit hash string', () => {
const expectedValue = B256_ZERO_DECODED;
const expectedLength = B256_ZERO_ENCODED.length;
const [actualValue, actualLength] = coder.decode(B256_ZERO_ENCODED, 0);

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

it('should decode a 256 bit hash string', () => {
const expectedValue = B256_DECODED;
const expectedLength = B256_ENCODED.length;
const [actualValue, actualLength] = coder.decode(B256_ENCODED, 0);

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

it('should throw an error when encoding a 256 bit hash string that is too short', () => {
const invalidInput = B256_DECODED.slice(0, B256_DECODED.length - 1);

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

it('should throw an error when decoding an encoded 256 bit hash string that is too short', () => {
const invalidInput = B256_ENCODED.slice(0, B256_ENCODED.length - 1);

expect(() => {
coder.decode(invalidInput, 0);
}).toThrow();
});

it('should throw an error when encoding a 256 bit hash string that is too long', () => {
const invalidInput = `${B256_DECODED}0`;

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

it('should throw an error when decoding an encoded 256 bit hash string that is too long', () => {
const invalidInput = new Uint8Array(Array.from(Array(32).keys()));

expect(() => {
coder.decode(invalidInput, 1);
}).toThrow('Invalid size for b256');
});

it('should throw an error when encoding a 256 bit hash string that is not a hex string', () => {
const invalidInput = 'not a hex string';

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