Wrap base matchers from jasmine.Matcher prototype automatically at env instantiation time rather than manually.

This commit is contained in:
Christian Williams 2009-11-13 14:32:08 -05:00
parent 2fc78a0fe2
commit 1a53838d97
4 changed files with 121 additions and 153 deletions

View File

@ -8,7 +8,7 @@ describe("jasmine.Matchers", function() {
});
function match(value) {
return new jasmine.Matchers(env, value, mockSpec);
return new env.matchersClass(env, value, mockSpec);
}
it("toEqual with primitives, objects, dates, html nodes, etc.", function() {
@ -435,9 +435,9 @@ describe("jasmine.Matchers", function() {
});
it("toThrow", function() {
var expected = new jasmine.Matchers(env, function() {
var expected = match(function() {
throw new Error("Fake Error");
}, mockSpec);
});
expect(expected.toThrow()).toEqual(true);
expect(expected.toThrow("Fake Error")).toEqual(true);
expect(expected.toThrow(new Error("Fake Error"))).toEqual(true);

View File

@ -10,7 +10,7 @@ jasmine.Env = function() {
this.reporter = new jasmine.MultiReporter();
this.updateInterval = jasmine.DEFAULT_UPDATE_INTERVAL
this.updateInterval = jasmine.DEFAULT_UPDATE_INTERVAL;
this.lastUpdate = 0;
this.specFilter = function() {
return true;
@ -19,6 +19,17 @@ jasmine.Env = function() {
this.nextSpecId_ = 0;
this.nextSuiteId_ = 0;
this.equalityTesters_ = [];
// wrap matchers
this.matchersClass = function() {
jasmine.Matchers.apply(this, arguments);
};
jasmine.util.inherit(this.matchersClass, jasmine.Matchers);
for (var methodName in jasmine.Matchers.prototype) {
var orig = jasmine.Matchers.prototype[methodName];
this.matchersClass.prototype[methodName] = jasmine.Matchers.matcherFn_(methodName, orig);
}
};

View File

@ -24,11 +24,10 @@ jasmine.Matchers.prototype.report = function(result, failing_message, details) {
return result;
};
jasmine.Matchers.matcherFn_ = function(matcherName, options) {
jasmine.Matchers.matcherFn_ = function(matcherName, matcherFunction) {
return function() {
jasmine.util.extend(this, options);
var matcherArgs = jasmine.util.argsToArray(arguments);
var result = options.test.apply(this, arguments);
var result = matcherFunction.apply(this, arguments);
var message;
if (!result) {
if (this.message) {
@ -65,21 +64,17 @@ jasmine.Matchers.matcherFn_ = function(matcherName, options) {
* @param expected
*/
jasmine.Matchers.prototype.toBe = jasmine.Matchers.matcherFn_('toBe', {
test: function(expected) {
jasmine.Matchers.prototype.toBe = function(expected) {
return this.actual === expected;
}
});
};
/**
* toNotBe: compares the actual to the expected using !==
* @param expected
*/
jasmine.Matchers.prototype.toNotBe = jasmine.Matchers.matcherFn_('toNotBe', {
test: function(expected) {
jasmine.Matchers.prototype.toNotBe = function(expected) {
return this.actual !== expected;
}
});
};
/**
* toEqual: compares the actual to the expected using common sense equality. Handles Objects, Arrays, etc.
@ -87,21 +82,17 @@ jasmine.Matchers.prototype.toNotBe = jasmine.Matchers.matcherFn_('toNotBe', {
* @param expected
*/
jasmine.Matchers.prototype.toEqual = jasmine.Matchers.matcherFn_('toEqual', {
test: function(expected) {
jasmine.Matchers.prototype.toEqual = function(expected) {
return this.env.equals_(this.actual, expected);
}
});
};
/**
* toNotEqual: compares the actual to the expected using the ! of jasmine.Matchers.toEqual
* @param expected
*/
jasmine.Matchers.prototype.toNotEqual = jasmine.Matchers.matcherFn_('toNotEqual', {
test: function(expected) {
jasmine.Matchers.prototype.toNotEqual = function(expected) {
return !this.env.equals_(this.actual, expected);
}
});
};
/**
* Matcher that compares the actual to the expected using a regular expression. Constructs a RegExp, so takes
@ -109,74 +100,58 @@ jasmine.Matchers.prototype.toNotEqual = jasmine.Matchers.matcherFn_('toNotEqual'
*
* @param reg_exp
*/
jasmine.Matchers.prototype.toMatch = jasmine.Matchers.matcherFn_('toMatch', {
test: function(expected) {
jasmine.Matchers.prototype.toMatch = function(expected) {
return new RegExp(expected).test(this.actual);
}
});
};
/**
* Matcher that compares the actual to the expected using the boolean inverse of jasmine.Matchers.toMatch
* @param reg_exp
*/
jasmine.Matchers.prototype.toNotMatch = jasmine.Matchers.matcherFn_('toNotMatch', {
test: function(expected) {
jasmine.Matchers.prototype.toNotMatch = function(expected) {
return !(new RegExp(expected).test(this.actual));
}
});
};
/**
* Matcher that compares the actual to undefined.
*/
jasmine.Matchers.prototype.toBeDefined = jasmine.Matchers.matcherFn_('toBeDefined', {
test: function() {
jasmine.Matchers.prototype.toBeDefined = function() {
return (this.actual !== undefined);
}
});
};
/**
* Matcher that compares the actual to undefined.
*/
jasmine.Matchers.prototype.toBeUndefined = jasmine.Matchers.matcherFn_('toBeUndefined', {
test: function() {
jasmine.Matchers.prototype.toBeUndefined = function() {
return (this.actual === undefined);
}
});
};
/**
* Matcher that compares the actual to null.
*/
jasmine.Matchers.prototype.toBeNull = jasmine.Matchers.matcherFn_('toBeNull', {
test: function() {
jasmine.Matchers.prototype.toBeNull = function() {
return (this.actual === null);
}
});
};
/**
* Matcher that boolean not-nots the actual.
*/
jasmine.Matchers.prototype.toBeTruthy = jasmine.Matchers.matcherFn_('toBeTruthy', {
test: function() {
jasmine.Matchers.prototype.toBeTruthy = function() {
return !!this.actual;
}
});
};
/**
* Matcher that boolean nots the actual.
*/
jasmine.Matchers.prototype.toBeFalsy = jasmine.Matchers.matcherFn_('toBeFalsy', {
test: function() {
jasmine.Matchers.prototype.toBeFalsy = function() {
return !this.actual;
}
});
};
/**
* Matcher that checks to see if the actual, a Jasmine spy, was called.
*/
jasmine.Matchers.prototype.wasCalled = jasmine.Matchers.matcherFn_('wasCalled', {
test: function() {
jasmine.Matchers.prototype.wasCalled = function() {
if (arguments.length > 0) {
throw new Error('wasCalled does not take arguments, use wasCalledWith');
}
@ -190,14 +165,12 @@ jasmine.Matchers.prototype.wasCalled = jasmine.Matchers.matcherFn_('wasCalled',
};
return this.actual.wasCalled;
}
});
};
/**
* Matcher that checks to see if the actual, a Jasmine spy, was not called.
*/
jasmine.Matchers.prototype.wasNotCalled = jasmine.Matchers.matcherFn_('wasNotCalled', {
test: function() {
jasmine.Matchers.prototype.wasNotCalled = function() {
if (arguments.length > 0) {
throw new Error('wasNotCalled does not take arguments');
}
@ -211,11 +184,15 @@ jasmine.Matchers.prototype.wasNotCalled = jasmine.Matchers.matcherFn_('wasNotCal
};
return !this.actual.wasCalled;
}
});
};
jasmine.Matchers.prototype.wasCalledWith = jasmine.Matchers.matcherFn_('wasCalledWith', {
test: function() {
/**
* Matcher that checks to see if the actual, a Jasmine spy, was called with a set of parameters.
*
* @example
*
*/
jasmine.Matchers.prototype.wasCalledWith = function() {
if (!jasmine.isSpy(this.actual)) {
throw new Error('Expected a spy, but got ' + jasmine.Matchers.pp(this.actual) + '.');
}
@ -225,58 +202,41 @@ jasmine.Matchers.prototype.wasCalledWith = jasmine.Matchers.matcherFn_('wasCalle
};
return this.env.contains_(this.actual.argsForCall, jasmine.util.argsToArray(arguments));
}
});
/**
* Matcher that checks to see if the actual, a Jasmine spy, was called with a set of parameters.
*
* @example
*
*/
};
/**
* Matcher that checks that the expected item is an element in the actual Array.
*
* @param {Object} item
*/
jasmine.Matchers.prototype.toContain = jasmine.Matchers.matcherFn_('toContain', {
test: function(expected) {
jasmine.Matchers.prototype.toContain = function(expected) {
return this.env.contains_(this.actual, expected);
}
});
};
/**
* Matcher that checks that the expected item is NOT an element in the actual Array.
*
* @param {Object} item
*/
jasmine.Matchers.prototype.toNotContain = jasmine.Matchers.matcherFn_('toNotContain', {
test: function(expected) {
jasmine.Matchers.prototype.toNotContain = function(expected) {
return !this.env.contains_(this.actual, expected);
}
});
};
jasmine.Matchers.prototype.toBeLessThan = jasmine.Matchers.matcherFn_('toBeLessThan', {
test: function(expected) {
jasmine.Matchers.prototype.toBeLessThan = function(expected) {
return this.actual < expected;
}
});
};
jasmine.Matchers.prototype.toBeGreaterThan = jasmine.Matchers.matcherFn_('toBeGreaterThan', {
test: function(expected) {
jasmine.Matchers.prototype.toBeGreaterThan = function(expected) {
return this.actual > expected;
}
});
};
/**
* Matcher that checks that the expected exception was thrown by the actual.
*
* @param {String} expectedException
*/
jasmine.Matchers.prototype.toThrow = jasmine.Matchers.matcherFn_('toThrow', {
getException_: function(actual, expected) {
jasmine.Matchers.prototype.toThrow = function(expected) {
function getException_(actual, expected) {
var exception;
if (typeof actual != 'function') {
throw new Error('Actual is not a function');
@ -287,16 +247,16 @@ jasmine.Matchers.prototype.toThrow = jasmine.Matchers.matcherFn_('toThrow', {
exception = e;
}
return exception;
},
test: function(expected) {
}
var result = false;
var exception = this.getException_(this.actual, expected);
var exception = getException_(this.actual, expected);
if (exception) {
result = (expected === undefined || this.env.equals_(exception.message || exception, expected.message || expected));
}
this.message = function(expected) {
var exception = this.getException_(this.actual, expected);
var exception = getException_(this.actual, expected);
if (exception && (expected === undefined || !this.env.equals_(exception.message || exception, expected.message || expected))) {
return ["Expected function to throw", expected.message || expected, ", but it threw", exception.message || exception ].join(' ');
} else {
@ -305,8 +265,7 @@ jasmine.Matchers.prototype.toThrow = jasmine.Matchers.matcherFn_('toThrow', {
};
return result;
}
});
};
jasmine.Matchers.Any = function(expectedClass) {
this.expectedClass = expectedClass;

View File

@ -10,11 +10,9 @@ jasmine.Spec = function(env, suite, description) {
if (!env) {
throw new Error('jasmine.Env() required');
}
;
if (!suite) {
throw new Error('jasmine.Suite() required');
}
;
var spec = this;
spec.id = env.nextSpecId ? env.nextSpecId() : null;
spec.env = env;
@ -91,7 +89,7 @@ jasmine.Spec.prototype.fail = function (e) {
};
jasmine.Spec.prototype.getMatchersClass_ = function() {
return this.matchersClass || jasmine.Matchers;
return this.matchersClass || this.env.matchersClass;
};
jasmine.Spec.prototype.addMatchers = function(matchersPrototype) {