diff --git a/ospec/change-log.md b/ospec/change-log.md index fd133ffb..01ca6f53 100644 --- a/ospec/change-log.md +++ b/ospec/change-log.md @@ -3,9 +3,6 @@ ## Upcoming... _2019-xx-yy_ - -## 3.1.0 -_2019-02-05_ - ospec: Test results now include `.message` and `.context` regardless of whether the test passed or failed. (#2227 @robertakarobin) - Add `spy.calls` array property to get the `this` and `arguments` values for any arbitrary call. (#2221 @isiahmeadows) diff --git a/ospec/ospec.mjs b/ospec/ospec.mjs index e6ea506d..115e0289 100644 --- a/ospec/ospec.mjs +++ b/ospec/ospec.mjs @@ -5,6 +5,9 @@ export default (function init(name) { var currentTestError = null if (name != null) spec[name] = ctx = {} + try {throw new Error} catch (e) { + var ospecFileName = e.stack && (/[\/\\](.*?):\d+:\d+/).test(e.stack) ? e.stack.match(/[\/\\](.*?):\d+:\d+/)[1] : null + } function o(subject, predicate) { if (predicate === undefined) { if (!isRunning()) throw new Error("Assertions should not occur outside test definitions") @@ -45,6 +48,7 @@ export default (function init(name) { var spy = function() { spy.this = this spy.args = [].slice.call(arguments) + spy.calls.push({this: this, args: spy.args}) spy.callCount++ if (fn) return fn.apply(this, arguments) @@ -55,6 +59,7 @@ export default (function init(name) { name: {value: fn.name} }) spy.args = [] + spy.calls = [] spy.callCount = 0 return spy } @@ -211,6 +216,8 @@ export default (function init(name) { 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) { @@ -251,6 +258,18 @@ export default (function init(name) { } 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) { @@ -264,16 +283,20 @@ export default (function init(name) { } function define(name, verb, compare) { Assert.prototype[name] = function assert(value) { - if (compare(this.value, value)) succeed(this) - else fail(this, serialize(this.value) + "\n " + verb + "\n" + serialize(value)) var self = this - return function(message) { - if (!self.pass) self.message = message + "\n\n" + self.message - } + var message = serialize(self.value) + "\n " + verb + "\n" + serialize(value) + if (compare(self.value, value)){ + succeed(self, message) + return function(message) { + if (!self.pass) self.message = message + "\n\n" + self.message + } + }else fail(self, message) } } - function succeed(assertion) { + function succeed(assertion, message) { results[assertion.i].pass = true + results[assertion.i].context = subjects.join(" > ") + results[assertion.i].message = message } function fail(assertion, message, error) { results[assertion.i].pass = false