Squashed matchers refactor - matchers now unit-testable apart from Expectation and Spec.
This commit is contained in:
94
src/core/Expectation.js
Normal file
94
src/core/Expectation.js
Normal file
@@ -0,0 +1,94 @@
|
||||
getJasmineRequireObj().Expectation = function() {
|
||||
|
||||
var matchers = {};
|
||||
|
||||
function Expectation(options) {
|
||||
this.util = options.util || { buildFailureMessage: function() {} };
|
||||
this.customEqualityTesters = options.customEqualityTesters || [];
|
||||
this.actual = options.actual;
|
||||
this.addExpectationResult = options.addExpectationResult || function(){};
|
||||
this.isNot = options.isNot;
|
||||
|
||||
for (var matcherName in matchers) {
|
||||
this[matcherName] = matchers[matcherName];
|
||||
}
|
||||
}
|
||||
|
||||
Expectation.prototype.wrapCompare = function(name, matcherFactory) {
|
||||
return function() {
|
||||
var args = Array.prototype.slice.call(arguments, 0),
|
||||
expected = args.slice(0),
|
||||
message = "";
|
||||
|
||||
args.unshift(this.actual);
|
||||
|
||||
var result = matcherFactory(this.util, this.customEqualityTesters).compare.apply(null, args);
|
||||
|
||||
if (this.isNot) {
|
||||
result.pass = !result.pass;
|
||||
}
|
||||
|
||||
if (!result.pass) {
|
||||
if (!result.message) {
|
||||
args.unshift(this.isNot);
|
||||
args.unshift(name);
|
||||
message = this.util.buildFailureMessage.apply(null, args);
|
||||
} else {
|
||||
message = result.message;
|
||||
}
|
||||
}
|
||||
|
||||
if (expected.length == 1) {
|
||||
expected = expected[0];
|
||||
}
|
||||
|
||||
// TODO: how many of these params are needed?
|
||||
this.addExpectationResult(
|
||||
result.pass,
|
||||
{
|
||||
matcherName: name,
|
||||
passed: result.pass,
|
||||
message: message,
|
||||
actual: this.actual,
|
||||
expected: expected // TODO: this may need to be arrayified/sliced
|
||||
}
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
Expectation.addCoreMatchers = function(matchers) {
|
||||
var prototype = Expectation.prototype;
|
||||
for (var matcherName in matchers) {
|
||||
var matcher = matchers[matcherName];
|
||||
prototype[matcherName] = prototype.wrapCompare(matcherName, matcher);
|
||||
}
|
||||
};
|
||||
|
||||
Expectation.addMatchers = function(matchersToAdd) {
|
||||
for (var name in matchersToAdd) {
|
||||
var matcher = matchersToAdd[name];
|
||||
matchers[name] = Expectation.prototype.wrapCompare(name, matcher);
|
||||
}
|
||||
};
|
||||
|
||||
Expectation.resetMatchers = function() {
|
||||
for (var name in matchers) {
|
||||
delete matchers[name];
|
||||
}
|
||||
};
|
||||
|
||||
Expectation.Factory = function(options) {
|
||||
options = options || {};
|
||||
|
||||
var expect = new Expectation(options);
|
||||
|
||||
// TODO: this would be nice as its own Object - NegativeExpectation
|
||||
// TODO: copy instead of mutate options
|
||||
options.isNot = true;
|
||||
expect.not = new Expectation(options);
|
||||
|
||||
return expect;
|
||||
};
|
||||
|
||||
return Expectation;
|
||||
};
|
||||
Reference in New Issue
Block a user