-
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
OptionCoder
tests (#1006)
- Loading branch information
1 parent
75002fe
commit 41faf45
Showing
2 changed files
with
56 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,54 @@ | ||
/* eslint-disable @typescript-eslint/ban-ts-comment */ | ||
import { BN } from '@fuel-ts/math'; | ||
|
||
import { U8_MAX } from '../../test/utils/constants'; | ||
|
||
import OptionCoder from './option'; | ||
import U64Coder from './u64'; | ||
|
||
describe('OptionCoder', () => { | ||
it('should encode a some u64 option ', () => { | ||
const coder = new OptionCoder('test option', { Some: new U64Coder() }); | ||
const expected = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255]); | ||
// @ts-expect-error | ||
const actual = coder.encode(U8_MAX); | ||
|
||
expect(actual).toStrictEqual(expected); | ||
}); | ||
|
||
it('should decode a some u64 option', () => { | ||
const coder = new OptionCoder('test option', { Some: new U64Coder() }); | ||
const expectedValue = U8_MAX; | ||
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, 255]), | ||
0 | ||
); | ||
|
||
expect(actualValue).toBeInstanceOf(BN); | ||
expect((actualValue as unknown as BN).toNumber()).toBe(expectedValue); | ||
expect(actualLength).toBe(expectedLength); | ||
}); | ||
|
||
it('should encode a none u64 option', () => { | ||
const coder = new OptionCoder('test option', { None: new U64Coder() }); | ||
const expected = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); | ||
// @ts-expect-error | ||
const actual = coder.encode(undefined); | ||
|
||
expect(actual).toStrictEqual(expected); | ||
}); | ||
|
||
it('should decode a none u64 option', () => { | ||
const coder = new OptionCoder('test option', { None: new U64Coder() }); | ||
const expectedValue = undefined; | ||
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, 0]), | ||
0 | ||
); | ||
|
||
expect(actualValue).toBe(expectedValue); | ||
expect(actualLength).toBe(expectedLength); | ||
}); | ||
}); |