From 6862c59a4a3e068b6108fa521089e97a99254a1e Mon Sep 17 00:00:00 2001 From: Andrew Higgs Date: Mon, 13 Jul 2020 18:07:29 +0200 Subject: [PATCH] Add unit tests for isHex and isHexStrict. (#3622) Co-authored-by: Gregory Markou <16929357+GregTheGreek@users.noreply.github.com> --- test/utils.isHex.js | 34 ++++++++++++++++++++++++++++++++++ test/utils.isHexStrict.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 test/utils.isHex.js create mode 100644 test/utils.isHexStrict.js diff --git a/test/utils.isHex.js b/test/utils.isHex.js new file mode 100644 index 00000000000..e5a868b9576 --- /dev/null +++ b/test/utils.isHex.js @@ -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); + }); + }); + }); +}); diff --git a/test/utils.isHexStrict.js b/test/utils.isHexStrict.js new file mode 100644 index 00000000000..12c88b5f2a2 --- /dev/null +++ b/test/utils.isHexStrict.js @@ -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); + }); + }); + }); +});