Implemented matcher negation as a filter

This commit is contained in:
Steve Gravrock
2018-05-13 23:01:55 -07:00
parent 5cc22740c9
commit 282c436463
4 changed files with 67 additions and 32 deletions

View File

@@ -2611,17 +2611,11 @@ getJasmineRequireObj().Expectation = function() {
Expectation.prototype.instantiateMatcher = function(matcherFactory) {
var matcher = matcherFactory(this.util, this.customEqualityTesters);
function defaultNegativeCompare() {
var result = matcher.compare.apply(null, arguments);
result.pass = !result.pass;
return result;
if (this.filter && this.filter.selectComparisonFunc) {
return this.filter.selectComparisonFunc(matcher);
}
if (this.isNot) {
return matcher.negativeCompare || defaultNegativeCompare;
} else {
return matcher.compare;
}
return matcher.compare;
};
Expectation.prototype.processResult = function(result, name, expected, args) {
@@ -2648,9 +2642,11 @@ getJasmineRequireObj().Expectation = function() {
Expectation.prototype.buildMessage = function(result, name, args) {
if (result.pass) {
return '';
} else if (this.filter && this.filter.buildFailureMessage) {
return this.filter.buildFailureMessage(result, name, args, this.util);
} else if (!result.message) {
args = args.slice();
args.unshift(this.isNot);
args.unshift(false);
args.unshift(name);
return this.util.buildFailureMessage.apply(null, args);
} else if (Object.prototype.toString.apply(result.message) === '[object Function]') {
@@ -2677,10 +2673,33 @@ getJasmineRequireObj().Expectation = function() {
// TODO: copy instead of mutate options
options.isNot = true;
expect.not = new Expectation(options);
expect.not.filter = negatingFilter;
return expect;
};
var negatingFilter = {
selectComparisonFunc: function(matcher) {
function defaultNegativeCompare() {
var result = matcher.compare.apply(null, arguments);
result.pass = !result.pass;
return result;
}
return matcher.negativeCompare || defaultNegativeCompare;
},
buildFailureMessage: function(result, matcherName, args, util) {
if (result.message) {
return result.message;
}
args = args.slice();
args.unshift(true);
args.unshift(matcherName);
return util.buildFailureMessage.apply(null, args);
}
};
return Expectation;
};