|
| 1 | +import assert from 'assert' |
| 2 | +import BN from 'bn.js' |
| 3 | +import { toType, TypeOutput, intToBuffer, bufferToHex, intToHex, bnToHex, toBuffer } from '../src' |
| 4 | + |
| 5 | +describe('toType', function() { |
| 6 | + describe('from Number', function() { |
| 7 | + const num = 1000 |
| 8 | + it('should convert to Number', function() { |
| 9 | + const result = toType(num, TypeOutput.Number) |
| 10 | + assert.strictEqual(result, num) |
| 11 | + }) |
| 12 | + it('should convert to BN', function() { |
| 13 | + const result = toType(num, TypeOutput.BN) |
| 14 | + assert.ok(result.eq(new BN(num))) |
| 15 | + }) |
| 16 | + it('should convert to Buffer', function() { |
| 17 | + const result = toType(num, TypeOutput.Buffer) |
| 18 | + assert.ok(result.equals(intToBuffer(num))) |
| 19 | + }) |
| 20 | + it('should convert to PrefixedHexString', function() { |
| 21 | + const result = toType(num, TypeOutput.PrefixedHexString) |
| 22 | + assert.strictEqual(result, bufferToHex(new BN(num).toArrayLike(Buffer))) |
| 23 | + }) |
| 24 | + it('should throw an error if greater than MAX_SAFE_INTEGER', function() { |
| 25 | + assert.throws( |
| 26 | + () => { |
| 27 | + const num = Number.MAX_SAFE_INTEGER + 1 |
| 28 | + toType(num, TypeOutput.BN) |
| 29 | + }, |
| 30 | + { |
| 31 | + message: |
| 32 | + 'The provided number is greater than MAX_SAFE_INTEGER (please use an alternative input type)' |
| 33 | + } |
| 34 | + ) |
| 35 | + }) |
| 36 | + }) |
| 37 | + describe('from BN', function() { |
| 38 | + const num = new BN(1000) |
| 39 | + it('should convert to Number', function() { |
| 40 | + const result = toType(num, TypeOutput.Number) |
| 41 | + assert.strictEqual(result, num.toNumber()) |
| 42 | + }) |
| 43 | + it('should convert to BN', function() { |
| 44 | + const result = toType(num, TypeOutput.BN) |
| 45 | + assert.ok(result.eq(num)) |
| 46 | + }) |
| 47 | + it('should convert to Buffer', function() { |
| 48 | + const result = toType(num, TypeOutput.Buffer) |
| 49 | + assert.ok(result.equals(num.toArrayLike(Buffer))) |
| 50 | + }) |
| 51 | + it('should convert to PrefixedHexString', function() { |
| 52 | + const result = toType(num, TypeOutput.PrefixedHexString) |
| 53 | + assert.strictEqual(result, bufferToHex(num.toArrayLike(Buffer))) |
| 54 | + }) |
| 55 | + it('should throw an error if converting to Number and greater than MAX_SAFE_INTEGER', function() { |
| 56 | + const num = new BN(Number.MAX_SAFE_INTEGER).addn(1) |
| 57 | + assert.throws( |
| 58 | + () => { |
| 59 | + toType(num, TypeOutput.Number) |
| 60 | + }, |
| 61 | + { |
| 62 | + message: |
| 63 | + 'The provided number is greater than MAX_SAFE_INTEGER (please use an alternative output type)' |
| 64 | + } |
| 65 | + ) |
| 66 | + }) |
| 67 | + }) |
| 68 | + describe('from Buffer', function() { |
| 69 | + const num = intToBuffer(1000) |
| 70 | + it('should convert to Number', function() { |
| 71 | + const result = toType(num, TypeOutput.Number) |
| 72 | + assert.ok(intToBuffer(result).equals(num)) |
| 73 | + }) |
| 74 | + it('should convert to BN', function() { |
| 75 | + const result = toType(num, TypeOutput.BN) |
| 76 | + assert.ok(result.eq(new BN(num))) |
| 77 | + }) |
| 78 | + it('should convert to Buffer', function() { |
| 79 | + const result = toType(num, TypeOutput.Buffer) |
| 80 | + assert.ok(result.equals(num)) |
| 81 | + }) |
| 82 | + it('should convert to PrefixedHexString', function() { |
| 83 | + const result = toType(num, TypeOutput.PrefixedHexString) |
| 84 | + assert.strictEqual(result, bufferToHex(num)) |
| 85 | + }) |
| 86 | + }) |
| 87 | + describe('from HexPrefixedString', function() { |
| 88 | + const num = intToHex(1000) |
| 89 | + it('should convert to Number', function() { |
| 90 | + const result = toType(num, TypeOutput.Number) |
| 91 | + assert.strictEqual(intToHex(result), num) |
| 92 | + }) |
| 93 | + it('should convert to BN', function() { |
| 94 | + const result = toType(num, TypeOutput.BN) |
| 95 | + assert.strictEqual(bnToHex(result), num) |
| 96 | + }) |
| 97 | + it('should convert to Buffer', function() { |
| 98 | + const result = toType(num, TypeOutput.Buffer) |
| 99 | + assert.ok(result.equals(toBuffer(num))) |
| 100 | + }) |
| 101 | + it('should throw an error if is not 0x-prefixed', function() { |
| 102 | + assert.throws( |
| 103 | + () => { |
| 104 | + toType('1', TypeOutput.Number) |
| 105 | + }, |
| 106 | + { |
| 107 | + message: 'A string must be provided with a 0x-prefix, given: 1' |
| 108 | + } |
| 109 | + ) |
| 110 | + }) |
| 111 | + }) |
| 112 | +}) |
0 commit comments