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:
@@ -1,268 +0,0 @@
|
||||
getJasmineRequireObj().matchers = function() {
|
||||
matchers = {};
|
||||
|
||||
matchers.toBe = function() {
|
||||
return {
|
||||
compare: function(actual, expected) {
|
||||
return {
|
||||
pass: actual === expected
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
matchers.toBeCloseTo = function() {
|
||||
return {
|
||||
compare: function(actual, expected, precision) {
|
||||
if (precision !== 0) {
|
||||
precision = precision || 2;
|
||||
}
|
||||
|
||||
return {
|
||||
pass: Math.abs(expected - actual) < (Math.pow(10, -precision) / 2)
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
matchers.toBeDefined = function() {
|
||||
return {
|
||||
compare: function(actual) {
|
||||
return {
|
||||
pass: (void 0 !== actual)
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
matchers.toBeFalsy = function() {
|
||||
return {
|
||||
compare: function(actual) {
|
||||
return {
|
||||
pass: !!!actual
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
matchers.toBeGreaterThan = function() {
|
||||
return {
|
||||
compare: function(actual, expected) {
|
||||
return {
|
||||
pass: actual > expected
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
matchers.toBeLessThan = function() {
|
||||
return {
|
||||
|
||||
compare: function(actual, expected) {
|
||||
return {
|
||||
pass: actual < expected
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
matchers.toBeNaN = function() {
|
||||
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;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
matchers.toBeNull = function() {
|
||||
return {
|
||||
compare: function(actual) {
|
||||
return {
|
||||
pass: actual === null
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
matchers.toBeTruthy = function() {
|
||||
return {
|
||||
compare: function(actual) {
|
||||
return {
|
||||
pass: !!actual
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
matchers.toBeUndefined = function() {
|
||||
return {
|
||||
compare: function(actual) {
|
||||
return {
|
||||
pass: void 0 === actual
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
matchers.toEqual = function(util, customEqualityTesters) {
|
||||
customEqualityTesters = customEqualityTesters || [];
|
||||
|
||||
return {
|
||||
compare: function(actual, expected) {
|
||||
var result = {
|
||||
pass: false
|
||||
};
|
||||
|
||||
result.pass = util.equals(actual, expected, customEqualityTesters);
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
matchers.toHaveBeenCalled = function() {
|
||||
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;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
matchers.toHaveBeenCalledWith = function(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."
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
matchers.toMatch = function() {
|
||||
return {
|
||||
compare: function(actual, expected) {
|
||||
var regexp = new RegExp(expected);
|
||||
|
||||
return {
|
||||
pass: regexp.test(actual)
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
matchers.toThrow = function() {
|
||||
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)
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
matchers.toContain = function(util, customEqualityTesters) {
|
||||
customEqualityTesters = customEqualityTesters || [];
|
||||
|
||||
return {
|
||||
compare: function(actual, expected) {
|
||||
|
||||
return {
|
||||
pass: util.contains(actual, expected, customEqualityTesters)
|
||||
};
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
return matchers;
|
||||
};
|
||||
28
src/core/matchers/requireMatchers.js
Normal file
28
src/core/matchers/requireMatchers.js
Normal 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
13
src/core/matchers/toBe.js
Normal file
@@ -0,0 +1,13 @@
|
||||
getJasmineRequireObj().toBe = function() {
|
||||
function toBe() {
|
||||
return {
|
||||
compare: function(actual, expected) {
|
||||
return {
|
||||
pass: actual === expected
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return toBe;
|
||||
};
|
||||
18
src/core/matchers/toBeCloseTo.js
Normal file
18
src/core/matchers/toBeCloseTo.js
Normal 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;
|
||||
};
|
||||
13
src/core/matchers/toBeDefined.js
Normal file
13
src/core/matchers/toBeDefined.js
Normal file
@@ -0,0 +1,13 @@
|
||||
getJasmineRequireObj().toBeDefined = function() {
|
||||
function toBeDefined() {
|
||||
return {
|
||||
compare: function(actual) {
|
||||
return {
|
||||
pass: (void 0 !== actual)
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return toBeDefined;
|
||||
};
|
||||
13
src/core/matchers/toBeFalsy.js
Normal file
13
src/core/matchers/toBeFalsy.js
Normal file
@@ -0,0 +1,13 @@
|
||||
getJasmineRequireObj().toBeFalsy = function() {
|
||||
function toBeFalsy() {
|
||||
return {
|
||||
compare: function(actual) {
|
||||
return {
|
||||
pass: !!!actual
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return toBeFalsy;
|
||||
};
|
||||
15
src/core/matchers/toBeGreaterThan.js
Normal file
15
src/core/matchers/toBeGreaterThan.js
Normal file
@@ -0,0 +1,15 @@
|
||||
getJasmineRequireObj().toBeGreaterThan = function() {
|
||||
|
||||
function toBeGreaterThan() {
|
||||
return {
|
||||
compare: function(actual, expected) {
|
||||
return {
|
||||
pass: actual > expected
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return toBeGreaterThan;
|
||||
};
|
||||
|
||||
14
src/core/matchers/toBeLessThan.js
Normal file
14
src/core/matchers/toBeLessThan.js
Normal file
@@ -0,0 +1,14 @@
|
||||
getJasmineRequireObj().toBeLessThan = function() {
|
||||
function toBeLessThan() {
|
||||
return {
|
||||
|
||||
compare: function(actual, expected) {
|
||||
return {
|
||||
pass: actual < expected
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return toBeLessThan;
|
||||
};
|
||||
22
src/core/matchers/toBeNaN.js
Normal file
22
src/core/matchers/toBeNaN.js
Normal 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;
|
||||
};
|
||||
14
src/core/matchers/toBeNull.js
Normal file
14
src/core/matchers/toBeNull.js
Normal file
@@ -0,0 +1,14 @@
|
||||
getJasmineRequireObj().toBeNull = function() {
|
||||
|
||||
function toBeNull() {
|
||||
return {
|
||||
compare: function(actual) {
|
||||
return {
|
||||
pass: actual === null
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return toBeNull;
|
||||
};
|
||||
14
src/core/matchers/toBeTruthy.js
Normal file
14
src/core/matchers/toBeTruthy.js
Normal file
@@ -0,0 +1,14 @@
|
||||
getJasmineRequireObj().toBeTruthy = function() {
|
||||
|
||||
function toBeTruthy() {
|
||||
return {
|
||||
compare: function(actual) {
|
||||
return {
|
||||
pass: !!actual
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return toBeTruthy;
|
||||
};
|
||||
14
src/core/matchers/toBeUndefined.js
Normal file
14
src/core/matchers/toBeUndefined.js
Normal file
@@ -0,0 +1,14 @@
|
||||
getJasmineRequireObj().toBeUndefined = function() {
|
||||
|
||||
function toBeUndefined() {
|
||||
return {
|
||||
compare: function(actual) {
|
||||
return {
|
||||
pass: void 0 === actual
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return toBeUndefined;
|
||||
};
|
||||
16
src/core/matchers/toContain.js
Normal file
16
src/core/matchers/toContain.js
Normal 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;
|
||||
};
|
||||
20
src/core/matchers/toEqual.js
Normal file
20
src/core/matchers/toEqual.js
Normal 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;
|
||||
};
|
||||
28
src/core/matchers/toHaveBeenCalled.js
Normal file
28
src/core/matchers/toHaveBeenCalled.js
Normal 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;
|
||||
};
|
||||
28
src/core/matchers/toHaveBeenCalledWith.js
Normal file
28
src/core/matchers/toHaveBeenCalledWith.js
Normal 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;
|
||||
};
|
||||
16
src/core/matchers/toMatch.js
Normal file
16
src/core/matchers/toMatch.js
Normal 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;
|
||||
};
|
||||
65
src/core/matchers/toThrow.js
Normal file
65
src/core/matchers/toThrow.js
Normal 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;
|
||||
};
|
||||
@@ -20,7 +20,6 @@ getJasmineRequireObj().core = function(jRequire) {
|
||||
j$.Expectation = jRequire.Expectation();
|
||||
j$.buildExpectationResult = jRequire.buildExpectationResult();
|
||||
j$.JsApiReporter = jRequire.JsApiReporter();
|
||||
j$.matchers = jRequire.matchers(j$);
|
||||
j$.matchersUtil = jRequire.matchersUtil(j$);
|
||||
j$.ObjectContaining = jRequire.ObjectContaining(j$);
|
||||
j$.StringPrettyPrinter = jRequire.StringPrettyPrinter(j$);
|
||||
@@ -30,5 +29,7 @@ getJasmineRequireObj().core = function(jRequire) {
|
||||
j$.Suite = jRequire.Suite();
|
||||
j$.version = jRequire.version();
|
||||
|
||||
j$.matchers = jRequire.requireMatchers(jRequire);
|
||||
|
||||
return j$;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user