|
| 1 | +import { ErrorCode, FuelError } from '@fuel-ts/errors'; |
| 2 | +import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils'; |
| 3 | + |
1 | 4 | import { B256Coder } from './b256';
|
2 | 5 |
|
3 | 6 | /**
|
@@ -46,52 +49,67 @@ describe('B256Coder', () => {
|
46 | 49 | expect(actualLength).toBe(expectedLength);
|
47 | 50 | });
|
48 | 51 |
|
49 |
| - it('should throw an error when encoding a 256 bit hash string that is too short', () => { |
| 52 | + it('should throw an error when encoding a 256 bit hash string that is too short', async () => { |
50 | 53 | const invalidInput = B256_DECODED.slice(0, B256_DECODED.length - 1);
|
51 | 54 |
|
52 |
| - expect(() => { |
53 |
| - coder.encode(invalidInput); |
54 |
| - }).toThrow('Invalid b256'); |
| 55 | + await expectToThrowFuelError( |
| 56 | + () => coder.encode(invalidInput), |
| 57 | + new FuelError(ErrorCode.ENCODE_ERROR, 'Invalid b256.') |
| 58 | + ); |
55 | 59 | });
|
56 | 60 |
|
57 |
| - it('should throw an error when decoding an encoded 256 bit hash string that is too short', () => { |
| 61 | + it('should throw an error when decoding an encoded 256 bit hash string that is too short', async () => { |
58 | 62 | const invalidInput = B256_ENCODED.slice(0, B256_ENCODED.length - 1);
|
59 | 63 |
|
60 |
| - expect(() => { |
61 |
| - coder.decode(invalidInput, 0); |
62 |
| - }).toThrow(); |
| 64 | + await expectToThrowFuelError( |
| 65 | + () => coder.decode(invalidInput, 0), |
| 66 | + new FuelError(ErrorCode.DECODE_ERROR, 'Invalid b256 data size.') |
| 67 | + ); |
63 | 68 | });
|
64 | 69 |
|
65 |
| - it('should throw an error when encoding a 256 bit hash string that is too long', () => { |
| 70 | + it('should throw an error when encoding a 256 bit hash string that is too long', async () => { |
66 | 71 | const invalidInput = `${B256_DECODED}0`;
|
67 | 72 |
|
68 |
| - expect(() => { |
69 |
| - coder.encode(invalidInput); |
70 |
| - }).toThrow('Invalid b256'); |
| 73 | + await expectToThrowFuelError( |
| 74 | + () => coder.encode(invalidInput), |
| 75 | + new FuelError(ErrorCode.ENCODE_ERROR, 'Invalid b256.') |
| 76 | + ); |
71 | 77 | });
|
72 | 78 |
|
73 |
| - it('should throw an error when encoding a 512 bit hash string', () => { |
| 79 | + it('should throw an error when encoding a 512 bit hash string', async () => { |
74 | 80 | const B512 =
|
75 | 81 | '0x8e9dda6f7793745ac5aacf9e907cae30b2a01fdf0d23b7750a85c6a44fca0c29f0906f9d1f1e92e6a1fb3c3dcef3cc3b3cdbaae27e47b9d9a4c6a4fce4cf16b2';
|
76 | 82 |
|
77 |
| - expect(() => { |
78 |
| - coder.encode(B512); |
79 |
| - }).toThrow('Invalid b256'); |
| 83 | + await expectToThrowFuelError( |
| 84 | + () => coder.encode(B512), |
| 85 | + new FuelError(ErrorCode.ENCODE_ERROR, 'Invalid b256.') |
| 86 | + ); |
80 | 87 | });
|
81 | 88 |
|
82 |
| - it('should throw an error when decoding an encoded 256 bit hash string that is too long', () => { |
83 |
| - const invalidInput = new Uint8Array(Array.from(Array(32).keys())); |
| 89 | + it('should throw an error when encoding a 256 bit hash string that is not a hex string', async () => { |
| 90 | + const invalidInput = 'not a hex string'; |
84 | 91 |
|
85 |
| - expect(() => { |
86 |
| - coder.decode(invalidInput, 1); |
87 |
| - }).toThrow('Invalid size for b256'); |
| 92 | + await expectToThrowFuelError( |
| 93 | + () => coder.encode(invalidInput), |
| 94 | + new FuelError(ErrorCode.ENCODE_ERROR, 'Invalid b256.') |
| 95 | + ); |
88 | 96 | });
|
89 | 97 |
|
90 |
| - it('should throw an error when encoding a 256 bit hash string that is not a hex string', () => { |
91 |
| - const invalidInput = 'not a hex string'; |
| 98 | + it('throws when decoding empty bytes', async () => { |
| 99 | + const input = new Uint8Array(0); |
| 100 | + |
| 101 | + await expectToThrowFuelError( |
| 102 | + () => coder.decode(input, 0), |
| 103 | + new FuelError(ErrorCode.DECODE_ERROR, 'Invalid b256 data size.') |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should throw an error when decoding an encoded b256 bit hash string that is too long', async () => { |
| 108 | + const invalidInput = new Uint8Array(Array.from(Array(65).keys())); |
92 | 109 |
|
93 |
| - expect(() => { |
94 |
| - coder.encode(invalidInput); |
95 |
| - }).toThrow('Invalid b256'); |
| 110 | + await expectToThrowFuelError( |
| 111 | + () => coder.decode(invalidInput, 62), |
| 112 | + new FuelError(ErrorCode.DECODE_ERROR, 'Invalid b256 byte data size.') |
| 113 | + ); |
96 | 114 | });
|
97 | 115 | });
|
0 commit comments