From 0fd1bc9caeb2bea05857c75d4d34495382f88810 Mon Sep 17 00:00:00 2001 From: "Robin (Robert) Thomas" Date: Wed, 21 Nov 2018 14:46:34 -0600 Subject: [PATCH] Add 'throws'/'notThrows' assertion to Ospec for error reporting (#2255) * Ospec: added assertion that function does/doesnt throw error * Ospec.throws passes npm test * Ospec.throws: Address requested changes * Ospec: message comparison support for .throws/.notthrows Credit to @maranomynet, #2227 --- ospec/README.md | 18 +++++++++++++++++- ospec/change-log.md | 1 + ospec/ospec.js | 14 ++++++++++++++ ospec/tests/test-ospec.js | 8 ++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/ospec/README.md b/ospec/README.md index 3cd3bcf6..1fccb1ae 100644 --- a/ospec/README.md +++ b/ospec/README.md @@ -429,7 +429,7 @@ If an argument is defined for the `assertions` function, the test is deemed to b ### Assertion o(any value) -Starts an assertion. There are four types of assertion: `equals`, `notEquals`, `deepEquals` and `notDeepEquals`. +Starts an assertion. There are six types of assertion: `equals`, `notEquals`, `deepEquals`, `notDeepEquals`, `throws`, `notThrows`. Assertions have this form: @@ -467,6 +467,22 @@ Asserts that two values are recursively equal Asserts that two values are not recursively equal +#### Function(String description) o(Function fn).throws(Object constructor) + +Asserts that a function throws an instance of the provided constructo + +#### Function(String description) o(Function fn).throws(String message) + +Asserts that a function throws an Error with the provided message + +#### Function(String description) o(Function fn).notThrows(Object constructor) + +Asserts that a function does not throw an instance of the provided constructor + +#### Function(String description) o(Function fn).notThrows(String message) + +Asserts that a function does not throw an Error with the provided message + --- ### void o.before(Function([Function done [, Function timeout]]) setup) diff --git a/ospec/change-log.md b/ospec/change-log.md index 04884e99..b355dd20 100644 --- a/ospec/change-log.md +++ b/ospec/change-log.md @@ -5,6 +5,7 @@ _2018-xx-yy_ - Add `spy.calls` array property to get the `this` and `arguments` values for any arbitrary call. +- Added `.throws` and `.notThrows` assertions to ospec. (#2255 @robertakarobin) ## 3.0.1 _2018-06-30_ diff --git a/ospec/ospec.js b/ospec/ospec.js index fb91f1a6..91483ead 100644 --- a/ospec/ospec.js +++ b/ospec/ospec.js @@ -220,6 +220,8 @@ else window.o = m() define("notEquals", "should not equal", function(a, b) {return a !== b}) define("deepEquals", "should deep equal", deepEqual) define("notDeepEquals", "should not deep equal", function(a, b) {return !deepEqual(a, b)}) + define("throws", "should throw a", throws) + define("notThrows", "should not throw a", function(a, b) {return !throws(a, b)}) function isArguments(a) { if ("callee" in a) { @@ -260,6 +262,18 @@ else window.o = m() } return false } + function throws(a, b){ + try{ + a() + }catch(e){ + if(typeof b === "string"){ + return (e.message === b) + }else{ + return (e instanceof b) + } + } + return false + } function isRunning() {return results != null} function Assert(value) { diff --git a/ospec/tests/test-ospec.js b/ospec/tests/test-ospec.js index 3ab3fa99..19c7af68 100644 --- a/ospec/tests/test-ospec.js +++ b/ospec/tests/test-ospec.js @@ -163,6 +163,14 @@ o.spec("ospec", function() { o(a).notEquals(2) o({a: [1, 2], b: 3}).deepEquals({a: [1, 2], b: 3}) o([{a: 1, b: 2}, {c: 3}]).deepEquals([{a: 1, b: 2}, {c: 3}]) + o(function(){throw new Error()}).throws(Error) + o(function(){"ayy".foo()}).throws(TypeError) + o(function(){Math.PI.toFixed(Math.pow(10,20))}).throws(RangeError) + o(function(){decodeURIComponent("%")}).throws(URIError) + + o(function(){"ayy".foo()}).notThrows(SyntaxError) + o(function(){throw new Error("foo")}).throws("foo") + o(function(){throw new Error("foo")}).notThrows("bar") var undef1 = {undef: void 0} var undef2 = {UNDEF: void 0}