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 StringCoder tests #942

Closed
wants to merge 5 commits into from
Closed
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/strange-sloths-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
92 changes: 92 additions & 0 deletions packages/abi-coder/src/coders/string.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { U8_MAX } from '../../test/utils/constants';

import StringCoder from './string';

describe('StringCoder', () => {
const STRING_MIN_DECODED = '';
const STRING_MIN_ENCODED = new Uint8Array();
const STRING_MAX_DECODED = 'a'.repeat(U8_MAX);
const STRING_MAX_ENCODED = new Uint8Array([...Array.from(Array(U8_MAX + 1).fill(97, 0, U8_MAX))]);

it('should encode an empty string', () => {
const coder = new StringCoder(0);
const expected = STRING_MIN_ENCODED;
const actual = coder.encode(STRING_MIN_DECODED);

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

it('should encode a max len string', () => {
const coder = new StringCoder(U8_MAX);
const expected = STRING_MAX_ENCODED;
const actual = coder.encode(STRING_MAX_DECODED);

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

it('should decode an empty string', () => {
const coder = new StringCoder(0);
const expectedValue = STRING_MIN_DECODED;
const expectedLength = STRING_MIN_ENCODED.length;
const [actualValue, actualLength] = coder.decode(STRING_MIN_ENCODED, 0);

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

it('should decode a max len string', () => {
const coder = new StringCoder(U8_MAX);
const expectedValue = STRING_MAX_DECODED;
const expectedLength = STRING_MAX_ENCODED.length;
const [actualValue, actualLength] = coder.decode(STRING_MAX_ENCODED, 0);

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

it('should not completely encode a string that is too big for the coder', () => {
const coderLength = 0;
const coder = new StringCoder(coderLength);
const invalidInput = STRING_MAX_DECODED;
const actual = coder.encode(invalidInput);

expect(actual).not.toStrictEqual(STRING_MAX_ENCODED);
});

it('should not completely encode a string that is too small for the coder', () => {
const coder = new StringCoder(1);
const invalidInput = STRING_MIN_DECODED;
const actual = coder.encode(invalidInput);

expect(actual).not.toStrictEqual(STRING_MIN_ENCODED);
});

it('should not completely decode a string that is too big for the coder', () => {
const coder = new StringCoder(1);
const invalidInput = STRING_MAX_ENCODED;
const [actualValue, actualLength] = coder.decode(invalidInput, 0);

expect(actualValue).not.toBe(STRING_MAX_DECODED);
expect(actualLength).toBe(8);
});

// TODO: StringCoder should throw for these conditions?

// it('should throw when encoding a string that is too big', () => {
// const coder = new StringCoder(0);
// const invalidInput = STRING_MAX_DECODED;

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

// it('should throw when encoding a string that is too small', () => {
// const coder = new StringCoder(1);
// const invalidInput = STRING_MIN_DECODED;

// expect(() => {
// coder.encode(invalidInput);
// }).toThrow();
// });
});
Comment on lines +73 to +92
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FuelLabs/sdk-ts should we be throwing for these conditions? Other coders throw for this behaviour, wheras StringCoder will instead just slice the input by the length that is past to the construtor.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My instinct says yes — we should throw.

Not sure how the Rust SDK handles this.

cc @digorithm.