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 B512Coder tests #945

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/young-radios-fly.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/b512.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import B512Coder from './b512';

describe('B512Coder', () => {
const B512_DECODED =
'0x8e9dda6f7793745ac5aacf9e907cae30b2a01fdf0d23b7750a85c6a44fca0c29f0906f9d1f1e92e6a1fb3c3dcef3cc3b3cdbaae27e47b9d9a4c6a4fce4cf16b2';
const B512_ENCODED = new Uint8Array([
142, 157, 218, 111, 119, 147, 116, 90, 197, 170, 207, 158, 144, 124, 174, 48, 178, 160, 31, 223,
13, 35, 183, 117, 10, 133, 198, 164, 79, 202, 12, 41, 240, 144, 111, 157, 31, 30, 146, 230, 161,
251, 60, 61, 206, 243, 204, 59, 60, 219, 170, 226, 126, 71, 185, 217, 164, 198, 164, 252, 228,
207, 22, 178,
]);
const B512_ZERO_DECODED =
'0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000';
const B512_ZERO_ENCODED = new Uint8Array(64);

const coder = new B512Coder();

it('should encode zero as a 512 bit hash string', () => {
const expected = B512_ZERO_ENCODED;
const actual = coder.encode(B512_ZERO_DECODED);

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

it('should encode a 512 bit hash string', () => {
const expected = B512_ENCODED;
const actual = coder.encode(B512_DECODED);

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

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

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

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

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

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

expect(() => {
coder.encode(invalidInput);
}).toThrow(/Invalid struct B512/);
});

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

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

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

expect(() => {
coder.encode(invalidInput);
}).toThrow(/Invalid struct B512/);
});

it('should throw an error when encoding a 256 bit hash string', () => {
const B256 = '0xd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b';

expect(() => {
coder.encode(B256);
}).toThrow(/Invalid struct B512/);
});

it('should throw an error when decoding an encoded 512 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 b512');
});

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

expect(() => {
coder.encode(invalidInput);
}).toThrow(/Invalid struct B512/);
});
});