Skip to content

Commit

Permalink
Add unit tests for isHex and isHexStrict. (#3622)
Browse files Browse the repository at this point in the history
Co-authored-by: Gregory Markou <16929357+GregTheGreek@users.noreply.github.com>
  • Loading branch information
andrew-m-higgs and GregTheGreek authored Jul 13, 2020
1 parent 6ee7118 commit 6862c59
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
34 changes: 34 additions & 0 deletions test/utils.isHex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var chai = require('chai');
var utils = require('../packages/web3-utils');

var BigNumber = require('bignumber.js');
var BN = require('bn.js');

var assert = chai.assert;

var tests = [
{ value: 1, expected: true },
{ value: '1', expected: true },
{ value: '0xH', expected: false},
{ value: 'H', expected: false},
{ value: [1,2,3,{test: 'data'}], expected: false},
{ value: {test: 'test'}, expected: false},
{ value: '{"test": "test"}', expected: false},
{ value: 'myString', expected: false},
{ value: 'myString 34534!', expected: false},
{ value: new BN(15), expected: false},
{ value: new BigNumber(15), expected: false},
{ value: 'Heeäööä👅D34ɝɣ24Єͽ-.,äü+#/', expected: false},
{ value: true, expected: false},
{ value: false, expected: false}
];

describe('lib/utils/utils', function () {
describe('isHex', function () {
tests.forEach(function (test) {
it('should return ' + test.expected + ' for input ' + test.value, function () {
assert.strictEqual(utils.isHex(test.value), test.expected);
});
});
});
});
34 changes: 34 additions & 0 deletions test/utils.isHexStrict.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var chai = require('chai');
var utils = require('../packages/web3-utils');

var BigNumber = require('bignumber.js');
var BN = require('bn.js');

var assert = chai.assert;

var tests = [
{ value: 0x1, expected: false }, //0x1 would be tested as 1
{ value: '0x1', expected: true },
{ value: '0xH', expected: false},
{ value: 'H', expected: false},
{ value: [1,2,3,{test: 'data'}], expected: false},
{ value: {test: 'test'}, expected: false},
{ value: '{"test": "test"}', expected: false},
{ value: 'myString', expected: false},
{ value: 'myString 34534!', expected: false},
{ value: new BN(15), expected: false},
{ value: new BigNumber(15), expected: false},
{ value: 'Heeäööä👅D34ɝɣ24Єͽ-.,äü+#/', expected: false},
{ value: true, expected: false},
{ value: false, expected: false}
];

describe('lib/utils/utils', function () {
describe('isHexStrict', function () {
tests.forEach(function (test) {
it('should return ' + test.expected + ' for input ' + test.value, function () {
assert.strictEqual(utils.isHexStrict(test.value), test.expected);
});
});
});
});

0 comments on commit 6862c59

Please sign in to comment.