From 0aad6453c234a0e15db726d3a5cb543236f6403c Mon Sep 17 00:00:00 2001 From: danielbate <--global> Date: Tue, 25 Apr 2023 15:11:15 +0100 Subject: [PATCH 1/4] test: refactor and increase verbosity of string coder tests --- packages/abi-coder/src/coders/string.test.ts | 94 ++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/packages/abi-coder/src/coders/string.test.ts b/packages/abi-coder/src/coders/string.test.ts index e69de29bb2d..c858270155a 100644 --- a/packages/abi-coder/src/coders/string.test.ts +++ b/packages/abi-coder/src/coders/string.test.ts @@ -0,0 +1,94 @@ +import * as fs from 'fs'; + +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(); + // }); +}); From 568a1c6cf71cab959fff2d84c280e0a4b670a069 Mon Sep 17 00:00:00 2001 From: danielbate <--global> Date: Tue, 25 Apr 2023 15:47:47 +0100 Subject: [PATCH 2/4] test: remove redundant import --- packages/abi-coder/src/coders/string.test.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/abi-coder/src/coders/string.test.ts b/packages/abi-coder/src/coders/string.test.ts index c858270155a..d4593b97c0e 100644 --- a/packages/abi-coder/src/coders/string.test.ts +++ b/packages/abi-coder/src/coders/string.test.ts @@ -1,5 +1,3 @@ -import * as fs from 'fs'; - import { U8_MAX } from '../../test/utils/constants'; import StringCoder from './string'; From 9cd60875ee3fd7fb930d425139b6069bde80b7f3 Mon Sep 17 00:00:00 2001 From: danielbate <--global> Date: Fri, 28 Apr 2023 13:02:32 +0100 Subject: [PATCH 3/4] chore: changeset --- .changeset/strange-sloths-fry.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .changeset/strange-sloths-fry.md diff --git a/.changeset/strange-sloths-fry.md b/.changeset/strange-sloths-fry.md new file mode 100644 index 00000000000..a49ba48448f --- /dev/null +++ b/.changeset/strange-sloths-fry.md @@ -0,0 +1,2 @@ +--- +--- \ No newline at end of file From cedcd0d5519bb1a9da2027aff22d089829c61961 Mon Sep 17 00:00:00 2001 From: danielbate <--global> Date: Tue, 2 May 2023 17:09:22 +0100 Subject: [PATCH 4/4] chore: force rebuild