-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: refactor
ArrayCoder
tests (#937)
* test: refactor and increase verbosity of array coder tests * chore: changeset * test: remove redundant describe block from array coder test * test: add missing throw condition testcase to array coder --------- Co-authored-by: danielbate <--global>
- Loading branch information
1 parent
98d2bb3
commit dae0c6c
Showing
2 changed files
with
105 additions
and
0 deletions.
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,103 @@ | ||
import { U8_MAX } from '../../test/utils/constants'; | ||
|
||
import ArrayCoder from './array'; | ||
import BooleanCoder from './boolean'; | ||
import EnumCoder from './enum'; | ||
import NumberCoder from './number'; | ||
|
||
describe('ArrayCoder', () => { | ||
it('should encode a number array with zero inputs', () => { | ||
const coder = new ArrayCoder(new NumberCoder('u8'), 0); | ||
const expected = new Uint8Array([]); | ||
const actual = coder.encode([]); | ||
|
||
expect(actual).toStrictEqual(expected); | ||
}); | ||
|
||
it('should decode a number array with zero inputs', () => { | ||
const coder = new ArrayCoder(new NumberCoder('u8'), 0); | ||
const expectedValue: number[] = []; | ||
const expectedLength = 0; | ||
const [actualValue, actualLength] = coder.decode(new Uint8Array([]), 0); | ||
|
||
expect(actualValue).toStrictEqual(expectedValue); | ||
expect(actualLength).toBe(expectedLength); | ||
}); | ||
|
||
it('should encode a number array with four inputs', () => { | ||
const coder = new ArrayCoder(new NumberCoder('u8'), 4); | ||
const expected = new Uint8Array([ | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 37, 0, 0, 0, 0, 0, 0, 0, | ||
255, | ||
]); | ||
const actual = coder.encode([0, 13, 37, U8_MAX]); | ||
|
||
expect(actual).toStrictEqual(expected); | ||
}); | ||
|
||
it('should decode a number array with four inputs', () => { | ||
const coder = new ArrayCoder(new NumberCoder('u8'), 4); | ||
const expectedValue = [0, 13, 37, U8_MAX]; | ||
const expectedLength = 32; | ||
const [actualValue, actualLength] = coder.decode( | ||
new Uint8Array([ | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 37, 0, 0, 0, 0, 0, 0, | ||
0, 255, | ||
]), | ||
0 | ||
); | ||
|
||
expect(actualValue).toStrictEqual(expectedValue); | ||
expect(actualLength).toBe(expectedLength); | ||
}); | ||
|
||
it('should encode an enum array with differently typed inputs', () => { | ||
const coder = new ArrayCoder( | ||
new EnumCoder('TestEnum', { a: new NumberCoder('u8'), b: new BooleanCoder() }), | ||
4 | ||
); | ||
const expected = new Uint8Array([ | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 255, | ||
]); | ||
const actual = coder.encode([{ a: 0 }, { b: false }, { b: true }, { a: U8_MAX }]); | ||
|
||
expect(actual).toStrictEqual(expected); | ||
}); | ||
|
||
it('should decode an enum array with differently typed inputs', () => { | ||
const coder = new ArrayCoder( | ||
new EnumCoder('TestEnum', { a: new NumberCoder('u8'), b: new BooleanCoder() }), | ||
4 | ||
); | ||
const expectedValue = [{ a: 0 }, { b: false }, { b: true }, { a: U8_MAX }]; | ||
const expectedLength = 64; | ||
const [actualValue, actualLength] = coder.decode( | ||
new Uint8Array([ | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 255, | ||
]), | ||
0 | ||
); | ||
|
||
expect(actualValue).toStrictEqual(expectedValue); | ||
expect(actualLength).toBe(expectedLength); | ||
}); | ||
|
||
it('should 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'); | ||
}); | ||
|
||
it('should throw when coder length is not match inputted array length', () => { | ||
const coder = new ArrayCoder(new NumberCoder('u8'), 1); | ||
expect(() => { | ||
coder.encode([1, 2]); | ||
}).toThrow('Types/values length mismatch'); | ||
}); | ||
}); |