parent
d61358d7cf
commit
e7ace6f172
@ -0,0 +1,32 @@ |
||||
// import BN from 'bn.js';
|
||||
|
||||
export const basicType = { |
||||
zero: 0, |
||||
float: 0.1, |
||||
text: 'testString', |
||||
jsonString: |
||||
'{"name":"@harmony/utils","version":"0.0.48","description":"utils for harmony"}', |
||||
hexNumber: 0x123, |
||||
hexString: '0x123', |
||||
bool: true, |
||||
undefined, |
||||
null: null, |
||||
// tslint:disable-next-line: no-empty
|
||||
function: () => {}, |
||||
array: [1, 2, 3], |
||||
object: {}, |
||||
}; |
||||
|
||||
export const advanceType = { |
||||
privateKey: |
||||
'0x97d2d3a21d829800eeb01aa7f244926f993a1427d9ba79d9dc3bf14fe04d9e37', |
||||
publicKey: |
||||
'0x028fe48b60c4511f31cf58906ddaa8422725d9313d4b994fab598d2cf220146228', |
||||
address: '0x84fece7d1f5629bc728c956ffd313dd0c3ac8f17', |
||||
hexAddress: '0x84fece7d1f5629bc728c956ffd313dd0c3ac8f17', |
||||
checkSumAddress: '0x84fece7d1f5629Bc728c956Ffd313dD0C3AC8f17', |
||||
hex: '0x8423', |
||||
hash: 'F5A5FD42D16A20302798EF6ED309979B43003D2320D9F0E8EA9831A92759FB4B', |
||||
byStrX: '0x84fece7d1f5629bc728c956ffd313dd0c3ac8f17', |
||||
url: 'https://www.zilliqa.com', |
||||
}; |
@ -0,0 +1,3 @@ |
||||
{ |
||||
"extends": "../tsconfig.test.json" |
||||
} |
@ -0,0 +1,83 @@ |
||||
import * as validators from '../src/validators'; |
||||
import { basicType, advanceType } from './fixture'; |
||||
|
||||
function expector(fun: any, val: any, bool: boolean) { |
||||
return expect(fun(val)).toEqual(bool); |
||||
} |
||||
|
||||
function mapTest(testObject: any, testTrue: string[], testFunc: any) { |
||||
const keys = Object.keys(testObject); |
||||
keys.forEach((k: string) => { |
||||
if (testTrue.includes(k)) { |
||||
expector(testFunc, testObject[k], true); |
||||
} else { |
||||
expector(testFunc, testObject[k], false); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
describe('test transformer', () => { |
||||
it('test isNumber', () => { |
||||
const beTrue = ['zero', 'float', 'hexNumber']; |
||||
mapTest(basicType, beTrue, validators.isNumber); |
||||
}); |
||||
it('test isInt', () => { |
||||
const beTrue = ['zero', 'hexNumber']; |
||||
mapTest(basicType, beTrue, validators.isInt); |
||||
}); |
||||
it('test isString', () => { |
||||
const beTrue = ['text', 'hexString', 'jsonString']; |
||||
mapTest(basicType, beTrue, validators.isString); |
||||
}); |
||||
it('test isBoolean', () => { |
||||
const beTrue = ['bool']; |
||||
mapTest(basicType, beTrue, validators.isBoolean); |
||||
}); |
||||
it('test isArray', () => { |
||||
const beTrue = ['array']; |
||||
mapTest(basicType, beTrue, validators.isArray); |
||||
}); |
||||
it('test isJsonString', () => { |
||||
const beTrue = ['jsonString']; |
||||
mapTest(basicType, beTrue, validators.isJsonString); |
||||
}); |
||||
it('test isObject', () => { |
||||
const beTrue = ['object']; |
||||
mapTest(basicType, beTrue, validators.isObject); |
||||
}); |
||||
it('test isFunction', () => { |
||||
const beTrue = ['function']; |
||||
mapTest(basicType, beTrue, validators.isFunction); |
||||
}); |
||||
|
||||
it('test isPubKey', () => { |
||||
const beTrue = ['publicKey']; |
||||
mapTest({ ...advanceType }, beTrue, validators.isPublicKey); |
||||
}); |
||||
it('test isAddress', () => { |
||||
const beTrue = ['address', 'hexAddress', 'checkSumAddress', 'byStrX']; |
||||
mapTest({ ...advanceType }, beTrue, validators.isAddress); |
||||
}); |
||||
it('test isPrivateKey', () => { |
||||
const beTrue = ['privateKey', 'hash']; |
||||
mapTest({ ...advanceType }, beTrue, validators.isPrivateKey); |
||||
}); |
||||
it('test isHex', () => { |
||||
const beTrue = [ |
||||
'privateKey', |
||||
'publicKey', |
||||
'address', |
||||
'hexString', |
||||
'hexAddress', |
||||
'checkSumAddress', |
||||
'hex', |
||||
'byStrX', |
||||
]; |
||||
mapTest({ ...advanceType }, beTrue, validators.isHex); |
||||
try { |
||||
validators.isHex(basicType.zero); |
||||
} catch (error) { |
||||
expect(error.message).toEqual(`${basicType.zero} is not string`); |
||||
} |
||||
}); |
||||
}); |
@ -0,0 +1,5 @@ |
||||
{ |
||||
"extends": "../../tsconfig.test.json", |
||||
"include": ["src", "test", "../../typings/**/*.d.ts"], |
||||
"references": [] |
||||
} |
Loading…
Reference in new issue