Revert removal of compare nesting

Since we want the user to be able to pass a negative comparison function, the extra layer of wrapping is now needed
This commit is contained in:
Kyriacos Souroullas and Sheel Choksi
2013-10-28 13:49:54 -07:00
parent dd8a455f91
commit e346e7dcc1
37 changed files with 493 additions and 435 deletions

View File

@@ -22,8 +22,7 @@ getJasmineRequireObj().Expectation = function() {
args.unshift(this.actual);
var matcherComparator = matcherFactory(this.util, this.customEqualityTesters),
result = matcherComparator.apply(null, args);
var result = matcherFactory(this.util, this.customEqualityTesters).compare.apply(null, args);
if (this.isNot) {
result.pass = !result.pass;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,18 +1,20 @@
getJasmineRequireObj().toBeNaN = function(j$) {
function toBeNaN() {
return function(actual) {
var result = {
pass: (actual !== actual)
};
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.";
if (result.pass) {
result.message = "Expected actual not to be NaN.";
} else {
result.message = "Expected " + j$.pp(actual) + " to be NaN.";
}
return result;
}
return result;
};
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,24 +1,26 @@
getJasmineRequireObj().toHaveBeenCalled = function(j$) {
function toHaveBeenCalled() {
return function(actual) {
var result = {};
return {
compare: function(actual) {
var result = {};
if (!j$.isSpy(actual)) {
throw new Error('Expected a spy, but got ' + j$.pp(actual) + '.');
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.calls.any();
result.message = result.pass ?
"Expected spy " + actual.and.identity() + " not to have been called." :
"Expected spy " + actual.and.identity() + " to have been called.";
return result;
}
if (arguments.length > 1) {
throw new Error('toHaveBeenCalled does not take arguments, use toHaveBeenCalledWith');
}
result.pass = actual.calls.any();
result.message = result.pass ?
"Expected spy " + actual.and.identity() + " not to have been called." :
"Expected spy " + actual.and.identity() + " to have been called.";
return result;
};
}

View File

@@ -1,29 +1,31 @@
getJasmineRequireObj().toHaveBeenCalledWith = function(j$) {
function toHaveBeenCalledWith(util) {
return function() {
var args = Array.prototype.slice.call(arguments, 0),
actual = args[0],
expectedArgs = args.slice(1),
result = { pass: false };
return {
compare: function() {
var args = Array.prototype.slice.call(arguments, 0),
actual = args[0],
expectedArgs = args.slice(1),
result = { pass: false };
if (!j$.isSpy(actual)) {
throw new Error('Expected a spy, but got ' + j$.pp(actual) + '.');
}
if (!j$.isSpy(actual)) {
throw new Error('Expected a spy, but got ' + j$.pp(actual) + '.');
}
if (!actual.calls.any()) {
result.message = "Expected spy " + actual.and.identity() + " to have been called with " + j$.pp(expectedArgs) + " but it was never called.";
return result;
}
if (util.contains(actual.calls.allArgs(), expectedArgs)) {
result.pass = true;
result.message = "Expected spy " + actual.and.identity() + " not to have been called with " + j$.pp(expectedArgs) + " but it was.";
} else {
result.message = "Expected spy " + actual.and.identity() + " to have been called with " + j$.pp(expectedArgs) + " but actual calls were " + j$.pp(actual.calls.allArgs()).replace(/^\[ | \]$/g, '') + ".";
}
if (!actual.calls.any()) {
result.message = "Expected spy " + actual.and.identity() + " to have been called with " + j$.pp(expectedArgs) + " but it was never called.";
return result;
}
if (util.contains(actual.calls.allArgs(), expectedArgs)) {
result.pass = true;
result.message = "Expected spy " + actual.and.identity() + " not to have been called with " + j$.pp(expectedArgs) + " but it was.";
} else {
result.message = "Expected spy " + actual.and.identity() + " to have been called with " + j$.pp(expectedArgs) + " but actual calls were " + j$.pp(actual.calls.allArgs()).replace(/^\[ | \]$/g, '') + ".";
}
return result;
};
}

View File

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

View File

@@ -1,42 +1,44 @@
getJasmineRequireObj().toThrow = function(j$) {
function toThrow(util) {
return function(actual, expected) {
var result = { pass: false },
threw = false,
thrown;
return {
compare: function(actual, expected) {
var result = { pass: false },
threw = false,
thrown;
if (typeof actual != "function") {
throw new Error("Actual is not a Function");
}
if (typeof actual != "function") {
throw new Error("Actual is not a Function");
}
try {
actual();
} catch (e) {
threw = true;
thrown = e;
}
try {
actual();
} catch (e) {
threw = true;
thrown = e;
}
if (!threw) {
result.message = "Expected function to throw an exception.";
return result;
}
if (!threw) {
result.message = "Expected function to throw an exception.";
return result;
}
if (arguments.length == 1) {
result.pass = true;
result.message = "Expected function not to throw, but it threw " + j$.pp(thrown) + ".";
if (arguments.length == 1) {
result.pass = true;
result.message = "Expected function not to throw, but it threw " + j$.pp(thrown) + ".";
return result;
}
if (util.equals(thrown, expected)) {
result.pass = true;
result.message = "Expected function not to throw " + j$.pp(expected) + ".";
} else {
result.message = "Expected function to throw " + j$.pp(expected) + ", but it threw " + j$.pp(thrown) + ".";
}
return result;
}
if (util.equals(thrown, expected)) {
result.pass = true;
result.message = "Expected function not to throw " + j$.pp(expected) + ".";
} else {
result.message = "Expected function to throw " + j$.pp(expected) + ", but it threw " + j$.pp(thrown) + ".";
}
return result;
};
}

View File

@@ -1,150 +1,152 @@
getJasmineRequireObj().toThrowError = function(j$) {
function toThrowError (util) {
return function(actual) {
var threw = false,
thrown,
errorType,
message,
regexp,
name,
constructorName;
return {
compare: function(actual) {
var threw = false,
thrown,
errorType,
message,
regexp,
name,
constructorName;
if (typeof actual != "function") {
throw new Error("Actual is not a Function");
}
extractExpectedParams.apply(null, arguments);
try {
actual();
} catch (e) {
threw = true;
thrown = e;
}
if (!threw) {
return fail("Expected function to throw an Error.");
}
if (!(thrown instanceof Error)) {
return fail("Expected function to throw an Error, but it threw " + thrown + ".");
}
if (arguments.length == 1) {
return pass("Expected function not to throw an Error, but it threw " + fnNameFor(thrown) + ".");
}
if (errorType) {
name = fnNameFor(errorType);
constructorName = fnNameFor(thrown.constructor);
}
if (errorType && message) {
if (thrown.constructor == errorType && util.equals(thrown.message, message)) {
return pass("Expected function not to throw " + name + " with message \"" + message + "\".");
} else {
return fail("Expected function to throw " + name + " with message \"" + message +
"\", but it threw " + constructorName + " with message \"" + thrown.message + "\".");
if (typeof actual != "function") {
throw new Error("Actual is not a Function");
}
}
if (errorType && regexp) {
if (thrown.constructor == errorType && regexp.test(thrown.message)) {
return pass("Expected function not to throw " + name + " with message matching " + regexp + ".");
} else {
return fail("Expected function to throw " + name + " with message matching " + regexp +
", but it threw " + constructorName + " with message \"" + thrown.message + "\".");
extractExpectedParams.apply(null, arguments);
try {
actual();
} catch (e) {
threw = true;
thrown = e;
}
}
if (errorType) {
if (thrown.constructor == errorType) {
return pass("Expected function not to throw " + name + ".");
} else {
return fail("Expected function to throw " + name + ", but it threw " + constructorName + ".");
if (!threw) {
return fail("Expected function to throw an Error.");
}
}
if (message) {
if (thrown.message == message) {
return pass("Expected function not to throw an exception with message " + j$.pp(message) + ".");
} else {
return fail("Expected function to throw an exception with message " + j$.pp(message) +
", but it threw an exception with message " + j$.pp(thrown.message) + ".");
if (!(thrown instanceof Error)) {
return fail("Expected function to throw an Error, but it threw " + thrown + ".");
}
}
if (regexp) {
if (regexp.test(thrown.message)) {
return pass("Expected function not to throw an exception with a message matching " + j$.pp(regexp) + ".");
} else {
return fail("Expected function to throw an exception with a message matching " + j$.pp(regexp) +
", but it threw an exception with message " + j$.pp(thrown.message) + ".");
}
}
function fnNameFor(func) {
return func.name || func.toString().match(/^\s*function\s*(\w*)\s*\(/)[1];
}
function pass(notMessage) {
return {
pass: true,
message: notMessage
};
}
function fail(message) {
return {
pass: false,
message: message
};
}
function extractExpectedParams() {
if (arguments.length == 1) {
return;
return pass("Expected function not to throw an Error, but it threw " + fnNameFor(thrown) + ".");
}
if (arguments.length == 2) {
var expected = arguments[1];
if (errorType) {
name = fnNameFor(errorType);
constructorName = fnNameFor(thrown.constructor);
}
if (expected instanceof RegExp) {
regexp = expected;
} else if (typeof expected == "string") {
message = expected;
} else if (checkForAnErrorType(expected)) {
errorType = expected;
}
if (!(errorType || message || regexp)) {
throw new Error("Expected is not an Error, string, or RegExp.");
}
} else {
if (checkForAnErrorType(arguments[1])) {
errorType = arguments[1];
if (errorType && message) {
if (thrown.constructor == errorType && util.equals(thrown.message, message)) {
return pass("Expected function not to throw " + name + " with message \"" + message + "\".");
} else {
throw new Error("Expected error type is not an Error.");
return fail("Expected function to throw " + name + " with message \"" + message +
"\", but it threw " + constructorName + " with message \"" + thrown.message + "\".");
}
}
if (arguments[2] instanceof RegExp) {
regexp = arguments[2];
} else if (typeof arguments[2] == "string") {
message = arguments[2];
if (errorType && regexp) {
if (thrown.constructor == errorType && regexp.test(thrown.message)) {
return pass("Expected function not to throw " + name + " with message matching " + regexp + ".");
} else {
throw new Error("Expected error message is not a string or RegExp.");
return fail("Expected function to throw " + name + " with message matching " + regexp +
", but it threw " + constructorName + " with message \"" + thrown.message + "\".");
}
}
}
function checkForAnErrorType(type) {
if (typeof type !== "function") {
return false;
if (errorType) {
if (thrown.constructor == errorType) {
return pass("Expected function not to throw " + name + ".");
} else {
return fail("Expected function to throw " + name + ", but it threw " + constructorName + ".");
}
}
var Surrogate = function() {};
Surrogate.prototype = type.prototype;
return (new Surrogate()) instanceof Error;
if (message) {
if (thrown.message == message) {
return pass("Expected function not to throw an exception with message " + j$.pp(message) + ".");
} else {
return fail("Expected function to throw an exception with message " + j$.pp(message) +
", but it threw an exception with message " + j$.pp(thrown.message) + ".");
}
}
if (regexp) {
if (regexp.test(thrown.message)) {
return pass("Expected function not to throw an exception with a message matching " + j$.pp(regexp) + ".");
} else {
return fail("Expected function to throw an exception with a message matching " + j$.pp(regexp) +
", but it threw an exception with message " + j$.pp(thrown.message) + ".");
}
}
function fnNameFor(func) {
return func.name || func.toString().match(/^\s*function\s*(\w*)\s*\(/)[1];
}
function pass(notMessage) {
return {
pass: true,
message: notMessage
};
}
function fail(message) {
return {
pass: false,
message: message
};
}
function extractExpectedParams() {
if (arguments.length == 1) {
return;
}
if (arguments.length == 2) {
var expected = arguments[1];
if (expected instanceof RegExp) {
regexp = expected;
} else if (typeof expected == "string") {
message = expected;
} else if (checkForAnErrorType(expected)) {
errorType = expected;
}
if (!(errorType || message || regexp)) {
throw new Error("Expected is not an Error, string, or RegExp.");
}
} else {
if (checkForAnErrorType(arguments[1])) {
errorType = arguments[1];
} else {
throw new Error("Expected error type is not an Error.");
}
if (arguments[2] instanceof RegExp) {
regexp = arguments[2];
} else if (typeof arguments[2] == "string") {
message = arguments[2];
} else {
throw new Error("Expected error message is not a string or RegExp.");
}
}
}
function checkForAnErrorType(type) {
if (typeof type !== "function") {
return false;
}
var Surrogate = function() {};
Surrogate.prototype = type.prototype;
return (new Surrogate()) instanceof Error;
}
}
};
}