Matchers & Matchers specs now broken up into individual files. There is now a requireMatchers jasmineRequire function to attach matchers properly.

This commit is contained in:
Davis W. Frank
2013-06-02 22:22:25 -07:00
parent 3271dc8838
commit d53002c63a
42 changed files with 1341 additions and 1173 deletions

View File

@@ -0,0 +1,177 @@
getJasmineRequireObj().matchersUtil = function(j$) {
// TODO: what to do about jasmine.pp not being inject? move to JSON.stringify? gut PrettyPrinter?
return {
equals: function(a, b, customTesters) {
customTesters = customTesters || [];
return eq(a, b, [], [], customTesters);
},
contains: function(haystack, needle, customTesters) {
customTesters = customTesters || [];
if (Object.prototype.toString.apply(haystack) === "[object Array]") {
for (var i = 0; i < haystack.length; i++) {
if (eq(haystack[i], needle, [], [], customTesters)) {
return true;
}
}
return false;
}
return haystack.indexOf(needle) >= 0;
},
buildFailureMessage: function() {
var args = Array.prototype.slice.call(arguments, 0),
matcherName = args[0],
isNot = args[1],
actual = args[2],
expected = args.slice(3),
englishyPredicate = matcherName.replace(/[A-Z]/g, function(s) { return ' ' + s.toLowerCase(); });
var message = "Expected " +
j$.pp(actual) +
(isNot ? " not " : " ") +
englishyPredicate;
if (expected.length > 0) {
for (var i = 0; i < expected.length; i++) {
if (i > 0) message += ",";
message += " " + j$.pp(expected[i]);
}
}
return message + ".";
}
};
// Equality function lovingly adapted from isEqual in
// [Underscore](http://underscorejs.org)
function eq(a, b, aStack, bStack, customTesters) {
var result = true;
for (var i = 0; i < customTesters.length; i++) {
result = customTesters[i](a, b);
if (result) {
return true;
}
}
if (a instanceof j$.Any) {
result = a.jasmineMatches(b);
if (result) {
return true;
}
}
if (b instanceof j$.Any) {
result = b.jasmineMatches(a);
if (result) {
return true;
}
}
if (b instanceof j$.ObjectContaining) {
result = b.jasmineMatches(a);
if (result) {
return true;
}
}
// Identical objects are equal. `0 === -0`, but they aren't identical.
// See the [Harmony `egal` proposal](http://wiki.ecmascript.org/doku.php?id=harmony:egal).
if (a === b) return a !== 0 || 1 / a == 1 / b;
// A strict comparison is necessary because `null == undefined`.
if (a === null || b === null) return a === b;
var className = Object.prototype.toString.call(a);
if (className != Object.prototype.toString.call(b)) return false;
switch (className) {
// Strings, numbers, dates, and booleans are compared by value.
case '[object String]':
// Primitives and their corresponding object wrappers are equivalent; thus, `"5"` is
// equivalent to `new String("5")`.
return a == String(b);
case '[object Number]':
// `NaN`s are equivalent, but non-reflexive. An `egal` comparison is performed for
// other numeric values.
return a != +a ? b != +b : (a === 0 ? 1 / a == 1 / b : a == +b);
case '[object Date]':
case '[object Boolean]':
// Coerce dates and booleans to numeric primitive values. Dates are compared by their
// millisecond representations. Note that invalid dates with millisecond representations
// of `NaN` are not equivalent.
return +a == +b;
// RegExps are compared by their source patterns and flags.
case '[object RegExp]':
return a.source == b.source &&
a.global == b.global &&
a.multiline == b.multiline &&
a.ignoreCase == b.ignoreCase;
}
if (typeof a != 'object' || typeof b != 'object') return false;
// Assume equality for cyclic structures. The algorithm for detecting cyclic
// structures is adapted from ES 5.1 section 15.12.3, abstract operation `JO`.
var length = aStack.length;
while (length--) {
// Linear search. Performance is inversely proportional to the number of
// unique nested structures.
if (aStack[length] == a) return bStack[length] == b;
}
// Add the first object to the stack of traversed objects.
aStack.push(a);
bStack.push(b);
var size = 0;
// Recursively compare objects and arrays.
if (className == '[object Array]') {
// Compare array lengths to determine if a deep comparison is necessary.
size = a.length;
result = size == b.length;
if (result) {
// Deep compare the contents, ignoring non-numeric properties.
while (size--) {
if (!(result = eq(a[size], b[size], aStack, bStack, customTesters))) break;
}
}
} else {
// Objects with different constructors are not equivalent, but `Object`s
// from different frames are.
var aCtor = a.constructor, bCtor = b.constructor;
if (aCtor !== bCtor && !(isFunction(aCtor) && (aCtor instanceof aCtor) &&
isFunction(bCtor) && (bCtor instanceof bCtor))) {
return false;
}
// Deep compare objects.
for (var key in a) {
if (has(a, key)) {
// Count the expected number of properties.
size++;
// Deep compare each member.
if (!(result = has(b, key) && eq(a[key], b[key], aStack, bStack, customTesters))) break;
}
}
// Ensure that both objects contain the same number of properties.
if (result) {
for (key in b) {
if (has(b, key) && !(size--)) break;
}
result = !size;
}
}
// Remove the first object from the stack of traversed objects.
aStack.pop();
bStack.pop();
return result;
function has(obj, key) {
return obj.hasOwnProperty(key);
}
function isFunction(obj) {
return typeof obj === 'function';
}
}
};

View File

@@ -0,0 +1,28 @@
getJasmineRequireObj().requireMatchers = function(jRequire) {
var availableMatchers = [
"toBe",
"toBeCloseTo",
"toBeDefined",
"toBeFalsy",
"toBeGreaterThan",
"toBeLessThan",
"toBeNaN",
"toBeNull",
"toBeTruthy",
"toBeUndefined",
"toContain",
"toEqual",
"toHaveBeenCalled",
"toHaveBeenCalledWith",
"toMatch",
"toThrow"
],
matchers = {};
for (var i = 0; i < availableMatchers.length; i++) {
var name = availableMatchers[i];
matchers[name] = jRequire[name]();
}
return matchers;
};

13
src/core/matchers/toBe.js Normal file
View File

@@ -0,0 +1,13 @@
getJasmineRequireObj().toBe = function() {
function toBe() {
return {
compare: function(actual, expected) {
return {
pass: actual === expected
};
}
};
}
return toBe;
};

View File

@@ -0,0 +1,18 @@
getJasmineRequireObj().toBeCloseTo = function() {
function toBeCloseTo() {
return {
compare: function(actual, expected, precision) {
if (precision !== 0) {
precision = precision || 2;
}
return {
pass: Math.abs(expected - actual) < (Math.pow(10, -precision) / 2)
};
}
};
}
return toBeCloseTo;
};

View File

@@ -0,0 +1,13 @@
getJasmineRequireObj().toBeDefined = function() {
function toBeDefined() {
return {
compare: function(actual) {
return {
pass: (void 0 !== actual)
};
}
};
}
return toBeDefined;
};

View File

@@ -0,0 +1,13 @@
getJasmineRequireObj().toBeFalsy = function() {
function toBeFalsy() {
return {
compare: function(actual) {
return {
pass: !!!actual
};
}
};
}
return toBeFalsy;
};

View File

@@ -0,0 +1,15 @@
getJasmineRequireObj().toBeGreaterThan = function() {
function toBeGreaterThan() {
return {
compare: function(actual, expected) {
return {
pass: actual > expected
};
}
};
}
return toBeGreaterThan;
};

View File

@@ -0,0 +1,14 @@
getJasmineRequireObj().toBeLessThan = function() {
function toBeLessThan() {
return {
compare: function(actual, expected) {
return {
pass: actual < expected
};
}
};
}
return toBeLessThan;
};

View File

@@ -0,0 +1,22 @@
getJasmineRequireObj().toBeNaN = function() {
function toBeNaN() {
return {
compare: function(actual) {
var result = {
pass: (actual !== actual)
};
if (result.pass) {
result.message = "Expected actual not to be NaN.";
} else {
result.message = "Expected " + j$.pp(actual) + " to be NaN.";
}
return result;
}
};
}
return toBeNaN;
};

View File

@@ -0,0 +1,14 @@
getJasmineRequireObj().toBeNull = function() {
function toBeNull() {
return {
compare: function(actual) {
return {
pass: actual === null
};
}
};
}
return toBeNull;
};

View File

@@ -0,0 +1,14 @@
getJasmineRequireObj().toBeTruthy = function() {
function toBeTruthy() {
return {
compare: function(actual) {
return {
pass: !!actual
};
}
};
}
return toBeTruthy;
};

View File

@@ -0,0 +1,14 @@
getJasmineRequireObj().toBeUndefined = function() {
function toBeUndefined() {
return {
compare: function(actual) {
return {
pass: void 0 === actual
};
}
};
}
return toBeUndefined;
};

View File

@@ -0,0 +1,16 @@
getJasmineRequireObj().toContain = function() {
function toContain(util, customEqualityTesters) {
customEqualityTesters = customEqualityTesters || [];
return {
compare: function(actual, expected) {
return {
pass: util.contains(actual, expected, customEqualityTesters)
};
}
};
}
return toContain;
};

View File

@@ -0,0 +1,20 @@
getJasmineRequireObj().toEqual = function() {
function toEqual(util, customEqualityTesters) {
customEqualityTesters = customEqualityTesters || [];
return {
compare: function(actual, expected) {
var result = {
pass: false
};
result.pass = util.equals(actual, expected, customEqualityTesters);
return result;
}
};
}
return toEqual;
};

View File

@@ -0,0 +1,28 @@
getJasmineRequireObj().toHaveBeenCalled = function() {
function toHaveBeenCalled() {
return {
compare: function(actual) {
var result = {};
if (!j$.isSpy(actual)) {
throw new Error('Expected a spy, but got ' + j$.pp(actual) + '.');
}
if (arguments.length > 1) {
throw new Error('toHaveBeenCalled does not take arguments, use toHaveBeenCalledWith');
}
result.pass = actual.wasCalled;
result.message = result.pass ?
"Expected spy " + actual.identity + " not to have been called." :
"Expected spy " + actual.identity + " to have been called.";
return result;
}
};
}
return toHaveBeenCalled;
};

View File

@@ -0,0 +1,28 @@
getJasmineRequireObj().toHaveBeenCalledWith = function() {
function toHaveBeenCalledWith(util) {
return {
compare: function() {
var args = Array.prototype.slice.call(arguments, 0),
actual = args[0],
expectedArgs = args.slice(1);
if (!j$.isSpy(actual)) {
throw new Error('Expected a spy, but got ' + j$.pp(actual) + '.');
}
return {
pass: util.contains(actual.argsForCall, expectedArgs)
};
},
message: function(actual) {
return {
affirmative: "Expected spy " + actual.identity + " to have been called.",
negative: "Expected spy " + actual.identity + " not to have been called."
};
}
};
}
return toHaveBeenCalledWith;
};

View File

@@ -0,0 +1,16 @@
getJasmineRequireObj().toMatch = function() {
function toMatch() {
return {
compare: function(actual, expected) {
var regexp = new RegExp(expected);
return {
pass: regexp.test(actual)
};
}
};
}
return toMatch;
};

View File

@@ -0,0 +1,65 @@
getJasmineRequireObj().toThrow = function() {
function toThrow() {
return {
compare: function(actual, expected) {
var result = { pass: false },
exception;
if (typeof actual != "function") {
throw new Error("Actual is not a Function");
}
if (expectedCannotBeTreatedAsException()) {
throw new Error("Expected cannot be treated as an exception.");
}
try {
actual();
} catch (e) {
exception = new Error(e);
}
if (!exception) {
result.message = "Expected function to throw an exception.";
return result;
}
if (void 0 == expected) {
result.pass = true;
result.message = "Expected function not to throw an exception.";
} else if (exception.message == expected) {
result.pass = true;
result.message = "Expected function not to throw an exception \"" + expected + "\".";
} else if (exception.message == expected.message) {
result.pass = true;
result.message = "Expected function not to throw an exception \"" + expected.message + "\".";
} else if (expected instanceof RegExp) {
if (expected.test(exception.message)) {
result.pass = true;
result.message = "Expected function not to throw an exception matching " + expected + ".";
} else {
result.pass = false;
result.message = "Expected function to throw an exception matching " + expected + ".";
}
} else {
result.pass = false;
result.message = "Expected function to throw an exception \"" + (expected.message || expected) + "\".";
}
return result;
function expectedCannotBeTreatedAsException() {
return !(
(void 0 == expected) ||
(expected instanceof Error) ||
(typeof expected == "string") ||
(expected instanceof RegExp)
);
}
}
};
}
return toThrow;
};