Skip to content
This repository has been archived by the owner on Jun 17, 2021. It is now read-only.

Commit

Permalink
add tests for toType
Browse files Browse the repository at this point in the history
add error if output number is greater than MAX_SAFE_INTEGER
grammar clarification: alternative => alternate
  • Loading branch information
ryanio committed Mar 3, 2021
1 parent 243fd47 commit 88db47f
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export enum TypeOutput {
}

export type TypeOutputReturnType = {
[TypeOutput.Number]: Number
[TypeOutput.Number]: number
[TypeOutput.BN]: BN
[TypeOutput.Buffer]: Buffer
[TypeOutput.PrefixedHexString]: PrefixedHexString
Expand All @@ -94,7 +94,7 @@ export function toType<T extends TypeOutput>(
throw new Error(`A string must be provided with a 0x-prefix, given: ${input}`)
} else if (typeof input === 'number' && !Number.isSafeInteger(input)) {
throw new Error(
'The provided number is greater than MAX_SAFE_INTEGER (please use an alternative input type)'
'The provided number is greater than MAX_SAFE_INTEGER (please use an alternate input type)'
)
}

Expand All @@ -105,7 +105,14 @@ export function toType<T extends TypeOutput>(
} else if (outputType === TypeOutput.BN) {
return new BN(input) as any
} else if (outputType === TypeOutput.Number) {
return new BN(input).toNumber() as any
const bn = new BN(input)
const max = new BN(Number.MAX_SAFE_INTEGER.toString())
if (bn.gt(max)) {
throw new Error(
'The provided number is greater than MAX_SAFE_INTEGER (please use an alternate output type)'
)
}
return bn.toNumber() as any
} else {
// outputType === TypeOutput.PrefixedHexString
return `0x${input.toString('hex')}` as any
Expand Down
2 changes: 1 addition & 1 deletion test/account.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ describe('.toChecksumAddress()', function() {
},
{
message:
'The provided number is greater than MAX_SAFE_INTEGER (please use an alternative input type)'
'The provided number is greater than MAX_SAFE_INTEGER (please use an alternate input type)'
}
)
})
Expand Down
112 changes: 112 additions & 0 deletions test/types.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import assert from 'assert'
import BN from 'bn.js'
import { toType, TypeOutput, intToBuffer, bufferToHex, intToHex, bnToHex, toBuffer } from '../src'

describe('toType', function() {
describe('from Number', function() {
const num = 1000
it('should convert to Number', function() {
const result = toType(num, TypeOutput.Number)
assert.strictEqual(result, num)
})
it('should convert to BN', function() {
const result = toType(num, TypeOutput.BN)
assert.ok(result.eq(new BN(num)))
})
it('should convert to Buffer', function() {
const result = toType(num, TypeOutput.Buffer)
assert.ok(result.equals(intToBuffer(num)))
})
it('should convert to PrefixedHexString', function() {
const result = toType(num, TypeOutput.PrefixedHexString)
assert.strictEqual(result, bufferToHex(new BN(num).toArrayLike(Buffer)))
})
it('should throw an error if greater than MAX_SAFE_INTEGER', function() {
assert.throws(
() => {
const num = Number.MAX_SAFE_INTEGER + 1
toType(num, TypeOutput.BN)
},
{
message:
'The provided number is greater than MAX_SAFE_INTEGER (please use an alternate input type)'
}
)
})
})
describe('from BN', function() {
const num = new BN(1000)
it('should convert to Number', function() {
const result = toType(num, TypeOutput.Number)
assert.strictEqual(result, num.toNumber())
})
it('should convert to BN', function() {
const result = toType(num, TypeOutput.BN)
assert.ok(result.eq(num))
})
it('should convert to Buffer', function() {
const result = toType(num, TypeOutput.Buffer)
assert.ok(result.equals(num.toArrayLike(Buffer)))
})
it('should convert to PrefixedHexString', function() {
const result = toType(num, TypeOutput.PrefixedHexString)
assert.strictEqual(result, bufferToHex(num.toArrayLike(Buffer)))
})
it('should throw an error if converting to Number and greater than MAX_SAFE_INTEGER', function() {
const num = new BN(Number.MAX_SAFE_INTEGER).addn(1)
assert.throws(
() => {
toType(num, TypeOutput.Number)
},
{
message:
'The provided number is greater than MAX_SAFE_INTEGER (please use an alternate output type)'
}
)
})
})
describe('from Buffer', function() {
const num = intToBuffer(1000)
it('should convert to Number', function() {
const result = toType(num, TypeOutput.Number)
assert.ok(intToBuffer(result).equals(num))
})
it('should convert to BN', function() {
const result = toType(num, TypeOutput.BN)
assert.ok(result.eq(new BN(num)))
})
it('should convert to Buffer', function() {
const result = toType(num, TypeOutput.Buffer)
assert.ok(result.equals(num))
})
it('should convert to PrefixedHexString', function() {
const result = toType(num, TypeOutput.PrefixedHexString)
assert.strictEqual(result, bufferToHex(num))
})
})
describe('from HexPrefixedString', function() {
const num = intToHex(1000)
it('should convert to Number', function() {
const result = toType(num, TypeOutput.Number)
assert.strictEqual(intToHex(result), num)
})
it('should convert to BN', function() {
const result = toType(num, TypeOutput.BN)
assert.strictEqual(bnToHex(result), num)
})
it('should convert to Buffer', function() {
const result = toType(num, TypeOutput.Buffer)
assert.ok(result.equals(toBuffer(num)))
})
it('should throw an error if is not 0x-prefixed', function() {
assert.throws(
() => {
toType('1', TypeOutput.Number)
},
{
message: 'A string must be provided with a 0x-prefix, given: 1'
}
)
})
})
})

0 comments on commit 88db47f

Please sign in to comment.