From 2f10d24ecdb617e232d51078657082867c4c423d Mon Sep 17 00:00:00 2001 From: Wilco Fiers Date: Fri, 30 Aug 2019 12:47:51 +0200 Subject: [PATCH] feat: Add axe.utils.assert method --- lib/core/utils/assert.js | 12 ++++++++++++ test/core/utils/assert.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 lib/core/utils/assert.js create mode 100644 test/core/utils/assert.js diff --git a/lib/core/utils/assert.js b/lib/core/utils/assert.js new file mode 100644 index 0000000000..a6663ccf7c --- /dev/null +++ b/lib/core/utils/assert.js @@ -0,0 +1,12 @@ +/* global axe*/ + +/** + * If the first argument is falsey, throw an error using the second argument as a message. + * @param {boolean} bool + * @param {string} message + */ +axe.utils.assert = function assert (bool, message) { + if (!bool) { + throw new Error(message) + } +} diff --git a/test/core/utils/assert.js b/test/core/utils/assert.js new file mode 100644 index 0000000000..0aad56f635 --- /dev/null +++ b/test/core/utils/assert.js @@ -0,0 +1,36 @@ +describe('axe.utils.assert', function () { + it('does nothing when passed a truthy value', function () { + assert.doesNotThrow(function () { + axe.utils.assert(true) + axe.utils.assert('foo') + axe.utils.assert(123) + axe.utils.assert([]) + axe.utils.assert({}) + }) + }) + + it('throws an error when passed a falsey value', function () { + assert.throws(function () { + axe.utils.assert(false) + }) + assert.throws(function () { + axe.utils.assert(0) + }) + assert.throws(function () { + axe.utils.assert(null) + }) + assert.throws(function () { + axe.utils.assert(undefined) + }) + }) + + it('sets second argument as the error message', function () { + var message = 'Something went wrong' + try { + axe.utils.assert(false, message) + } catch (e) { + assert.instanceOf(e, Error) + assert.equal(e.message, message) + } + }) +}) \ No newline at end of file