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
This commit is contained in:
parent
22427db882
commit
0fd1bc9cae
4 changed files with 40 additions and 1 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
_2018-xx-yy_
|
||||
<!-- Add new lines here. Version number will be decided later -->
|
||||
- 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_
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue