.withContext() works with .not

This commit is contained in:
Steve Gravrock
2018-06-02 22:06:53 -07:00
parent 321f161ce5
commit 8a01e1f26c
2 changed files with 76 additions and 19 deletions
+56
View File
@@ -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",
})
);
});
}); });
}); });
+20 -19
View File
@@ -9,7 +9,7 @@ getJasmineRequireObj().Expectation = function(j$) {
this.customEqualityTesters = options.customEqualityTesters || []; this.customEqualityTesters = options.customEqualityTesters || [];
this.actual = options.actual; this.actual = options.actual;
this.addExpectationResult = options.addExpectationResult || function(){}; this.addExpectationResult = options.addExpectationResult || function(){};
this.isNot = options.isNot; this.filters = new j$.ExpectationFilterChain();
var customMatchers = options.customMatchers || {}; var customMatchers = options.customMatchers || {};
for (var matcherName in customMatchers) { for (var matcherName in customMatchers) {
@@ -32,12 +32,8 @@ getJasmineRequireObj().Expectation = function(j$) {
Expectation.prototype.instantiateMatcher = function(matcherFactory) { Expectation.prototype.instantiateMatcher = function(matcherFactory) {
var matcher = matcherFactory(this.util, this.customEqualityTesters); var matcher = matcherFactory(this.util, this.customEqualityTesters);
var comparisonFunc = this.filters.selectComparisonFunc(matcher);
if (this.filter && this.filter.selectComparisonFunc) { return comparisonFunc || matcher.compare;
return this.filter.selectComparisonFunc(matcher);
}
return matcher.compare;
}; };
Expectation.prototype.processResult = function(result, name, expected, args) { Expectation.prototype.processResult = function(result, name, expected, args) {
@@ -62,16 +58,16 @@ getJasmineRequireObj().Expectation = function(j$) {
}; };
Expectation.prototype.buildMessage = function(result, name, args) { Expectation.prototype.buildMessage = function(result, name, args) {
var util = this.util; var util = this.util,
msg;
if (result.pass) { if (result.pass) {
return ''; 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() { function defaultMessage() {
if (!result.message) { if (!result.message) {
args = args.slice(); 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) { Expectation.addCoreMatchers = function(matchers) {
var prototype = Expectation.prototype; var prototype = Expectation.prototype;
for (var matcherName in matchers) { for (var matcherName in matchers) {
@@ -96,13 +98,12 @@ getJasmineRequireObj().Expectation = function(j$) {
Expectation.Factory = function(options) { Expectation.Factory = function(options) {
var expect = new Expectation(options || {}); var expect = new Expectation(options || {});
expect.not = Object.create(expect); expect.not = expect.addFilter(negatingFilter);
expect.not.filter = negatingFilter;
expect.withContext = function(message) { expect.withContext = function(message) {
var filteredExpect = Object.create(expect); var result = this.addFilter(new ContextAddingFilter(message));
filteredExpect.filter = new ContextAddingFilter(message); result.not = result.addFilter(negatingFilter);
return filteredExpect; return result;
}; };
return expect; return expect;
@@ -140,8 +141,8 @@ getJasmineRequireObj().Expectation = function(j$) {
this.message = message; this.message = message;
} }
ContextAddingFilter.prototype.buildFailureMessage = function(result, matcherName, args, util, getDefaultMessage) { ContextAddingFilter.prototype.modifyFailureMessage = function(msg) {
return this.message + ': ' + getDefaultMessage(); return this.message + ': ' + msg;
}; };
return Expectation; return Expectation;