-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
danielbate
wants to merge
5
commits into
FuelLabs:db/test/refactor-abi-coder-coders-tests
from
danielbate:db/test/refactor-string-coder-tests
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0aad645
test: refactor and increase verbosity of string coder tests
568a1c6
test: remove redundant import
9cd6087
chore: changeset
d86bb27
Merge branch 'db/test/refactor-abi-coder-coders-tests' of https://git…
cedcd0d
chore: force rebuild
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
// }); | ||
}); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.