98 lines
2.8 KiB
JavaScript
98 lines
2.8 KiB
JavaScript
getJasmineRequireObj().Expectation = function() {
|
|
|
|
/**
|
|
* Matchers that come with Jasmine out of the box.
|
|
* @namespace 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;
|
|
|
|
var customMatchers = options.customMatchers || {};
|
|
for (var matcherName in customMatchers) {
|
|
this[matcherName] = Expectation.prototype.wrapCompare(matcherName, customMatchers[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 matcher = matcherFactory(this.util, this.customEqualityTesters),
|
|
matcherCompare = matcher.compare;
|
|
|
|
function defaultNegativeCompare() {
|
|
var result = matcher.compare.apply(null, args);
|
|
result.pass = !result.pass;
|
|
return result;
|
|
}
|
|
|
|
if (this.isNot) {
|
|
matcherCompare = matcher.negativeCompare || defaultNegativeCompare;
|
|
}
|
|
|
|
var result = matcherCompare.apply(null, args);
|
|
|
|
if (!result.pass) {
|
|
if (!result.message) {
|
|
args.unshift(this.isNot);
|
|
args.unshift(name);
|
|
message = this.util.buildFailureMessage.apply(null, args);
|
|
} else {
|
|
if (Object.prototype.toString.apply(result.message) === '[object Function]') {
|
|
message = result.message();
|
|
} 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.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;
|
|
};
|