From 8a01e1f26cb8e7b1e7939515e3c07a0e1e8c8b88 Mon Sep 17 00:00:00 2001 From: Steve Gravrock Date: Sat, 2 Jun 2018 22:06:53 -0700 Subject: [PATCH] .withContext() works with .not --- spec/core/ExpectationSpec.js | 56 ++++++++++++++++++++++++++++++++++++ src/core/Expectation.js | 39 +++++++++++++------------ 2 files changed, 76 insertions(+), 19 deletions(-) diff --git a/spec/core/ExpectationSpec.js b/spec/core/ExpectationSpec.js index 66e60e7d..78088013 100644 --- a/spec/core/ExpectationSpec.js +++ b/spec/core/ExpectationSpec.js @@ -570,6 +570,62 @@ describe("Expectation", function() { }) ); }); + + it("works with #not", function() { + var matchers = { + toFoo: function() { + return { + compare: function() { return { pass: true }; } + }; + } + }, + addExpectationResult = jasmine.createSpy("addExpectationResult"), + expectation = jasmineUnderTest.Expectation.Factory({ + customMatchers: matchers, + util: jasmineUnderTest.matchersUtil, + actual: "an actual", + addExpectationResult: addExpectationResult + }); + + expectation.withContext("Some context").not.toFoo(); + + expect(addExpectationResult).toHaveBeenCalledWith(false, + jasmine.objectContaining({ + message: "Some context: Expected 'an actual' not to foo." + }) + ); + }); + + it("works with #not and a custom message", function() { + var customError = new Error("I am a custom error"); + var matchers = { + toFoo: function() { + return { + compare: function() { + return { + pass: true, + message: function() { return "I am a custom message"; }, + error: customError + }; + } + }; + } + }, + addExpectationResult = jasmine.createSpy("addExpectationResult"), + expectation = jasmineUnderTest.Expectation.Factory({ + actual: "an actual", + customMatchers: matchers, + addExpectationResult: addExpectationResult + }); + + expectation.withContext("Some context").not.toFoo("hello"); + + expect(addExpectationResult).toHaveBeenCalledWith(false, + jasmine.objectContaining({ + message: "Some context: I am a custom message", + }) + ); + }); }); }); diff --git a/src/core/Expectation.js b/src/core/Expectation.js index c8d09a02..d5690d67 100644 --- a/src/core/Expectation.js +++ b/src/core/Expectation.js @@ -9,7 +9,7 @@ getJasmineRequireObj().Expectation = function(j$) { this.customEqualityTesters = options.customEqualityTesters || []; this.actual = options.actual; this.addExpectationResult = options.addExpectationResult || function(){}; - this.isNot = options.isNot; + this.filters = new j$.ExpectationFilterChain(); var customMatchers = options.customMatchers || {}; for (var matcherName in customMatchers) { @@ -32,12 +32,8 @@ getJasmineRequireObj().Expectation = function(j$) { Expectation.prototype.instantiateMatcher = function(matcherFactory) { var matcher = matcherFactory(this.util, this.customEqualityTesters); - - if (this.filter && this.filter.selectComparisonFunc) { - return this.filter.selectComparisonFunc(matcher); - } - - return matcher.compare; + var comparisonFunc = this.filters.selectComparisonFunc(matcher); + return comparisonFunc || matcher.compare; }; Expectation.prototype.processResult = function(result, name, expected, args) { @@ -62,16 +58,16 @@ getJasmineRequireObj().Expectation = function(j$) { }; Expectation.prototype.buildMessage = function(result, name, args) { - var util = this.util; + var util = this.util, + msg; if (result.pass) { return ''; - } else if (this.filter && this.filter.buildFailureMessage) { - return this.filter.buildFailureMessage(result, name, args, util, defaultMessage); - } else { - return defaultMessage(); } + msg = this.filters.buildFailureMessage(result, name, args, util, defaultMessage); + return this.filters.modifyFailureMessage(msg || defaultMessage()); + function defaultMessage() { if (!result.message) { args = args.slice(); @@ -86,6 +82,12 @@ getJasmineRequireObj().Expectation = function(j$) { } }; + Expectation.prototype.addFilter = function(filter) { + var result = Object.create(this); + result.filters = this.filters.addFilter(filter); + return result; + }; + Expectation.addCoreMatchers = function(matchers) { var prototype = Expectation.prototype; for (var matcherName in matchers) { @@ -96,13 +98,12 @@ getJasmineRequireObj().Expectation = function(j$) { Expectation.Factory = function(options) { var expect = new Expectation(options || {}); - expect.not = Object.create(expect); - expect.not.filter = negatingFilter; + expect.not = expect.addFilter(negatingFilter); expect.withContext = function(message) { - var filteredExpect = Object.create(expect); - filteredExpect.filter = new ContextAddingFilter(message); - return filteredExpect; + var result = this.addFilter(new ContextAddingFilter(message)); + result.not = result.addFilter(negatingFilter); + return result; }; return expect; @@ -140,8 +141,8 @@ getJasmineRequireObj().Expectation = function(j$) { this.message = message; } - ContextAddingFilter.prototype.buildFailureMessage = function(result, matcherName, args, util, getDefaultMessage) { - return this.message + ': ' + getDefaultMessage(); + ContextAddingFilter.prototype.modifyFailureMessage = function(msg) { + return this.message + ': ' + msg; }; return Expectation;