Skip to content

Commit

Permalink
test: Increase coverage of fuels-ts/abi-coder/coders (FuelLabs#762)
Browse files Browse the repository at this point in the history
* test: add test cases for b512coder

* chore: changeset

* test: add coverage of missing throw conditions and messages for coders

* chore: changeset

* chore: linting
  • Loading branch information
danielbate authored Feb 9, 2023
1 parent 9e9e356 commit b126037
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/chatty-news-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/abi-coder": patch
---

Increase test coverage of B512Coder and coverage for missing throw conditions for coders
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ exports[`B256Coder as a b256 can encode "0x0000000000000000000000000000000000000

exports[`B256Coder as a b256 can encode "0xd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b" then decode "0xd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b" 1`] = `"0xd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b"`;

exports[`B512Coder as a b512 can encode "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" then decode "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" 1`] = `"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"`;

exports[`B512Coder as a b512 can encode "0x8e9dda6f7793745ac5aacf9e907cae30b2a01fdf0d23b7750a85c6a44fca0c29f0906f9d1f1e92e6a1fb3c3dcef3cc3b3cdbaae27e47b9d9a4c6a4fce4cf16b2" then decode "0x8e9dda6f7793745ac5aacf9e907cae30b2a01fdf0d23b7750a85c6a44fca0c29f0906f9d1f1e92e6a1fb3c3dcef3cc3b3cdbaae27e47b9d9a4c6a4fce4cf16b2" 1`] = `"0x8e9dda6f7793745ac5aacf9e907cae30b2a01fdf0d23b7750a85c6a44fca0c29f0906f9d1f1e92e6a1fb3c3dcef3cc3b3cdbaae27e47b9d9a4c6a4fce4cf16b2"`;

exports[`BooleanCoder as a boolean can encode false then decode false 1`] = `"0x0000000000000000"`;

exports[`BooleanCoder as a boolean can encode true then decode true 1`] = `"0x0000000000000001"`;
Expand Down
82 changes: 82 additions & 0 deletions packages/abi-coder/src/coders/coders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { bn, toHex } from '@fuel-ts/math';
import type Coder from './abstract-coder';
import ArrayCoder from './array';
import B256Coder from './b256';
import B512Coder from './b512';
import BooleanCoder from './boolean';
import ByteCoder from './byte';
import EnumCoder from './enum';
Expand All @@ -17,6 +18,10 @@ import U64Coder from './u64';

const B256_ZERO = '0x0000000000000000000000000000000000000000000000000000000000000000';
const B256 = '0xd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b';
const B512_ZERO =
'0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000';
const B512 =
'0x8e9dda6f7793745ac5aacf9e907cae30b2a01fdf0d23b7750a85c6a44fca0c29f0906f9d1f1e92e6a1fb3c3dcef3cc3b3cdbaae27e47b9d9a4c6a4fce4cf16b2';
const U8_MAX = 2 ** 8 - 1;
const U16_MAX = 2 ** 16 - 1;
const U32_MAX = 2 ** 32 - 1;
Expand Down Expand Up @@ -74,6 +79,26 @@ const testCases = [
[new B256Coder(), 'whoops', 'whoops'],
],
],
[
'B512Coder',
[
[new B512Coder(), B512_ZERO, B512_ZERO],
[new B512Coder(), B512, B512],
],
[
// Under
[
new B512Coder(),
B512_ZERO.slice(0, B512_ZERO.length - 1),
B512_ZERO.slice(0, B512_ZERO.length - 1),
],
// Over
[new B512Coder(), `${B512_ZERO}0`, `${B512_ZERO}0`],
[new B512Coder(), `${B512_ZERO}00`, `${B512_ZERO}00`],
// Wrong
[new B512Coder(), 'whoops', 'whoops'],
],
],
[
'BooleanCoder',
[
Expand Down Expand Up @@ -297,6 +322,56 @@ describe.each(testCases)('%s', (coderName, goodCases, badCases) => {
});
});

describe('ArrayCoder', () => {
it('will throw when value to encode is not array', () => {
const coder = new ArrayCoder(new NumberCoder('u8'), 1);
const nonArrayInput = { ...[1] };
expect(() => {
coder.encode(nonArrayInput);
}).toThrow('expected array value');
});
});

describe('B256Coder', () => {
it('will throw when the decoded value length is not equal to 32 bytes', () => {
const coder = new B256Coder();
const input = new Uint8Array(Array.from(Array(32).keys()));
expect(() => {
coder.decode(input, 1);
}).toThrow('Invalid size for b256');
});
});

describe('B512Coder', () => {
it('will throw when the decoded value length is not equal to 64 bytes', () => {
const coder = new B512Coder();
const input = new Uint8Array(Array.from(Array(64).keys()));
expect(() => {
coder.decode(input, 1);
}).toThrow('Invalid size for b512');
});
});

describe('BooleanCoder', () => {
it('will throw when the decoded value is not a valid boolean value', () => {
const coder = new BooleanCoder();
const input = new Uint8Array(Array.from(Array(4).keys()));
expect(() => {
coder.decode(input, 1);
}).toThrow('Invalid boolean value');
});
});

describe('ByteCoder', () => {
it('will throw when the decoded value length is invalid', () => {
const coder = new ByteCoder();
const input = new Uint8Array(Array.from(Array(4).keys()));
expect(() => {
coder.decode(input, 1);
}).toThrow('Invalid Byte');
});
});

describe('EnumCoder', () => {
it('is typed correctly', () => {
const coder = new EnumCoder('TestEnum', { a: new BooleanCoder(), b: new U64Coder() });
Expand Down Expand Up @@ -338,6 +413,13 @@ describe('EnumCoder', () => {
)
).toThrow();
});
it('will throw when decoded value accesses an invalid coder index', () => {
const coder = new EnumCoder('TestEnum', { a: new BooleanCoder(), b: new U64Coder() });
const input = new Uint8Array(Array.from(Array(3).keys()));
expect(() => {
coder.decode(input, 1);
}).toThrow('Invalid caseIndex');
});
});

describe('StructCoder', () => {
Expand Down

0 comments on commit b126037

Please sign in to comment.