diff --git a/package.json b/package.json index a331826d2..10d69ef1a 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "scripts": { "build": "polkadot-dev-build-babel && polkadot-dev-build-docs", "check": "eslint src && flow check", - "test": "jest src" + "test": "jest --coverage src" }, "devDependencies": { "@polkadot/api-jsonrpc": "^0.5.5", diff --git a/src/assert.js b/src/assert.js new file mode 100644 index 000000000..6f8600646 --- /dev/null +++ b/src/assert.js @@ -0,0 +1,24 @@ +// ISC, Copyright 2017 Jaco Greeff +// @flow + +const ExtError = require('./ext/error'); + +/** + @name assert + @signature assert (test: boolean, message: string, code: number = ExtError.CODES.ASSERT, data: any): void + @summary Checks for a valid test, if not ExtError is thrown. + @description + Checks that `test` is a true value. If value is `false` (or `false-ish`), it throws an ExtError with the supplied `message` and an optional `code` and `data`. When `test` passes, `true` is returned. + @example + const assert = require('@polkadot/util/assert'); + + assert(true === true, 'True should be true'); // true returned + assert(false === true, 'False should not be true'); // ExtError thrown +*/ +module.exports = function assert (test: boolean, message: string, code: number = ExtError.CODES.ASSERT, data: any): boolean { + if (!test) { + throw new ExtError(message, code, data); + } + + return true; +}; diff --git a/src/assert.spec.js b/src/assert.spec.js new file mode 100644 index 000000000..4d98732e7 --- /dev/null +++ b/src/assert.spec.js @@ -0,0 +1,27 @@ +// ISC, Copyright 2017 Jaco Greeff + +const { assert } = require('./index'); + +describe('assert', () => { + it('should not throw an error when test is true', () => { + expect( + assert(true, 'nothing should be thrown') + ).toEqual(true); + }); + + it('should throw an error when test is not true', () => { + expect( + () => assert(false, 'error thrown') + ).toThrow(/error thrown/); + }); + + it('throws a valid constructed ExtError', () => { + try { + assert(false, 'error', -666, { some: 'data' }); + } catch (error) { + expect(error.code).toEqual(-666); + expect(error.data).toEqual({ some: 'data' }); + expect(error.message).toEqual('error'); + } + }); +}); diff --git a/src/ext/error.js b/src/ext/error.js index 766790be8..ac4bcac7e 100644 --- a/src/ext/error.js +++ b/src/ext/error.js @@ -49,6 +49,7 @@ module.exports = class ExtError extends Error implements ExtErrorInterface { } static CODES = { + ASSERT: -90009, UNKNOWN, INVALID_JSONRPC: -99998, METHOD_NOT_FOUND: -32601 // Rust client diff --git a/src/index.js b/src/index.js index ca51c8f2e..c44344554 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,7 @@ // ISC, Copyright 2017 Jaco Greeff // @flow +const assert = require('./assert'); const bn = require('./bn'); const buffer = require('./buffer'); const ext = require('./ext'); @@ -16,5 +17,5 @@ const u8a = require('./u8a'); @summary Utility methods for this package are split into groups */ module.exports = Object.assign( - {}, bn, buffer, ext, hex, is, jsonrpc, keccak, { logger }, number, u8a + {}, { assert }, bn, buffer, ext, hex, is, jsonrpc, keccak, { logger }, number, u8a );