-
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
EnumCoder
tests (#938)
* test: refactor and increase verbosity of enum coder tests * chore: changeset * test: introduce test cases for native enums * test: add feature test in abi coder for native enum * test: remove abi coder test case for native enum as redundant --------- Co-authored-by: danielbate <--global>
- Loading branch information
1 parent
03e4988
commit 2527865
Showing
3 changed files
with
112 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
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,109 @@ | ||
/* eslint-disable @typescript-eslint/ban-ts-comment */ | ||
import { bn } from '@fuel-ts/math'; | ||
import { executionAsyncId } from 'async_hooks'; | ||
|
||
import { U64_MAX } from '../../test/utils/constants'; | ||
|
||
import type Coder from './abstract-coder'; | ||
import ArrayCoder from './array'; | ||
import BooleanCoder from './boolean'; | ||
import EnumCoder from './enum'; | ||
import NumberCoder from './number'; | ||
import U64Coder from './u64'; | ||
|
||
describe('EnumCoder', () => { | ||
const coder = new EnumCoder('TestEnum', { a: new BooleanCoder(), b: new U64Coder() }); | ||
|
||
it('should encode an enum containing a boolean', () => { | ||
const expected = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]); | ||
const actual = coder.encode({ a: true }); | ||
|
||
expect(actual).toStrictEqual(expected); | ||
}); | ||
|
||
it('should decode an enum containing a boolean', () => { | ||
const expectedValue = { a: true }; | ||
const expectedLength = 16; | ||
const [actualValue, actualLength] = coder.decode( | ||
new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]), | ||
0 | ||
); | ||
|
||
expect(actualValue).toStrictEqual(expectedValue); | ||
expect(actualLength).toBe(expectedLength); | ||
}); | ||
|
||
it('should encode an enum containing a u64', () => { | ||
const expected = new Uint8Array([ | ||
0, 0, 0, 0, 0, 0, 0, 1, 255, 255, 255, 255, 255, 255, 255, 255, | ||
]); | ||
const actual = coder.encode({ b: bn(U64_MAX) }); | ||
|
||
expect(actual).toStrictEqual(expected); | ||
}); | ||
|
||
it('should decode an enum containing a u64', () => { | ||
const expectedValue = { b: bn(U64_MAX) }; | ||
const expectedLength = 16; | ||
const [actualValue, actualLength] = coder.decode( | ||
new Uint8Array([0, 0, 0, 0, 0, 0, 0, 1, 255, 255, 255, 255, 255, 255, 255, 255]), | ||
0 | ||
); | ||
|
||
expect(actualValue).toStrictEqual(expectedValue); | ||
expect(actualLength).toBe(expectedLength); | ||
}); | ||
|
||
it('should throw an error when encoding if no enum key is provided', () => { | ||
const invalidCoder = new EnumCoder('TestEnum', {}); | ||
|
||
expect(() => invalidCoder.encode({} as never)).toThrow('A field for the case must be provided'); | ||
}); | ||
|
||
it.skip('should encode a native enum', () => { | ||
// TODO: complete encode native enum test case | ||
}); | ||
|
||
it.skip('should decode a native enum', () => { | ||
// TODO: complete decode native enum test case | ||
}); | ||
|
||
it('should throw an error when decoded value accesses an invalid index', () => { | ||
const input = new Uint8Array(Array.from(Array(3).keys())); | ||
expect(() => { | ||
coder.decode(input, 1); | ||
}).toThrow('Invalid caseIndex'); | ||
}); | ||
|
||
it('should not throw given correctly typed inputs', () => { | ||
expect(() => coder.encode({ a: true })).not.toThrow(); | ||
expect(() => coder.encode({ b: bn(1234) })).not.toThrow(); | ||
}); | ||
|
||
it('should throw when provided with extra inputs', () => { | ||
expect(() => | ||
coder.encode( | ||
// @ts-expect-error | ||
{ a: true, b: bn(1234), c: false } | ||
) | ||
).toThrow('Only one field must be provided'); | ||
}); | ||
|
||
it('should throw type error with invalid input for coder', () => { | ||
expect(() => | ||
coder.encode( | ||
// @ts-expect-error | ||
{ b: true } | ||
) | ||
).toThrow('Invalid u64'); | ||
}); | ||
|
||
it('should throw type error with invalid input key', () => { | ||
expect(() => | ||
coder.encode( | ||
// @ts-expect-error | ||
{ nope: 42 } | ||
) | ||
).toThrow(); | ||
}); | ||
}); |