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

@@ -22,12 +22,12 @@ module.exports = {
jasmine: {
src: [
'src/core/requireCore.js',
'src/core/matchers/requireMatchers.js',
'src/core/base.js',
'src/core/util.js',
'src/core/Spec.js',
'src/core/Env.js',
'src/core/JsApiReporter.js',
'src/core/Matchers',
'src/core/PrettyPrinter',
'src/core/Suite',
'src/core/**/*.js',

View File

@@ -42,7 +42,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$);
@@ -52,9 +51,40 @@ getJasmineRequireObj().core = function(jRequire) {
j$.Suite = jRequire.Suite();
j$.version = jRequire.version();
j$.matchers = jRequire.requireMatchers(jRequire);
return j$;
};
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;
};
getJasmineRequireObj().base = function(j$) {
j$.unimplementedMethod_ = function() {
throw new Error("unimplemented method");
@@ -1536,275 +1566,6 @@ if (typeof window == void 0 && typeof exports == "object") {
exports.Suite = jasmineRequire.Suite;
}
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;
};
getJasmineRequireObj().matchersUtil = function(j$) {
// TODO: what to do about jasmine.pp not being inject? move to JSON.stringify? gut PrettyPrinter?
@@ -1982,6 +1743,344 @@ getJasmineRequireObj().matchersUtil = function(j$) {
}
}
};
getJasmineRequireObj().toBe = function() {
function toBe() {
return {
compare: function(actual, expected) {
return {
pass: actual === expected
};
}
};
}
return toBe;
};
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;
};
getJasmineRequireObj().toBeDefined = function() {
function toBeDefined() {
return {
compare: function(actual) {
return {
pass: (void 0 !== actual)
};
}
};
}
return toBeDefined;
};
getJasmineRequireObj().toBeFalsy = function() {
function toBeFalsy() {
return {
compare: function(actual) {
return {
pass: !!!actual
};
}
};
}
return toBeFalsy;
};
getJasmineRequireObj().toBeGreaterThan = function() {
function toBeGreaterThan() {
return {
compare: function(actual, expected) {
return {
pass: actual > expected
};
}
};
}
return toBeGreaterThan;
};
getJasmineRequireObj().toBeLessThan = function() {
function toBeLessThan() {
return {
compare: function(actual, expected) {
return {
pass: actual < expected
};
}
};
}
return toBeLessThan;
};
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;
};
getJasmineRequireObj().toBeNull = function() {
function toBeNull() {
return {
compare: function(actual) {
return {
pass: actual === null
};
}
};
}
return toBeNull;
};
getJasmineRequireObj().toBeTruthy = function() {
function toBeTruthy() {
return {
compare: function(actual) {
return {
pass: !!actual
};
}
};
}
return toBeTruthy;
};
getJasmineRequireObj().toBeUndefined = function() {
function toBeUndefined() {
return {
compare: function(actual) {
return {
pass: void 0 === actual
};
}
};
}
return toBeUndefined;
};
getJasmineRequireObj().toContain = function() {
function toContain(util, customEqualityTesters) {
customEqualityTesters = customEqualityTesters || [];
return {
compare: function(actual, expected) {
return {
pass: util.contains(actual, expected, customEqualityTesters)
};
}
};
}
return toContain;
};
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;
};
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;
};
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;
};
getJasmineRequireObj().toMatch = function() {
function toMatch() {
return {
compare: function(actual, expected) {
var regexp = new RegExp(expected);
return {
pass: regexp.test(actual)
};
}
};
}
return toMatch;
};
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;
};
getJasmineRequireObj().version = function() {
return "2.0.0-alpha";
};

View File

@@ -0,0 +1,51 @@
describe("toBeCloseTo", function() {
it("passes when within two decimal places by default", function() {
var matcher = j$.matchers.toBeCloseTo(),
result;
result = matcher.compare(0, 0);
expect(result.pass).toBe(true);
result = matcher.compare(0, 0.001);
expect(result.pass).toBe(true);
});
it("fails when not within two decimal places by default", function() {
var matcher = j$.matchers.toBeCloseTo(),
result;
result = matcher.compare(0, 0.01);
expect(result.pass).toBe(false);
});
it("accepts an optional precision argument", function() {
var matcher = j$.matchers.toBeCloseTo(),
result;
result = matcher.compare(0, 0.1, 0);
expect(result.pass).toBe(true);
result = matcher.compare(0, 0.0001, 3);
expect(result.pass).toBe(true);
});
it("rounds expected values", function() {
var matcher = j$.matchers.toBeCloseTo(),
result;
result = matcher.compare(1.23, 1.229);
expect(result.pass).toBe(true);
result = matcher.compare(1.23, 1.226);
expect(result.pass).toBe(true);
result = matcher.compare(1.23, 1.225);
expect(result.pass).toBe(true);
result = matcher.compare(1.23, 1.2249999);
expect(result.pass).toBe(false);
result = matcher.compare(1.23, 1.234);
expect(result.pass).toBe(true);
});
});

View File

@@ -0,0 +1,18 @@
describe("toBeDefined", function() {
it("matches for defined values", function() {
var matcher = j$.matchers.toBeDefined(),
result;
result = matcher.compare('foo');
expect(result.pass).toBe(true);
});
it("fails when matching undefined values", function() {
var matcher = j$.matchers.toBeDefined(),
result;
result = matcher.compare(void 0);
expect(result.pass).toBe(false);
})
});

View File

@@ -0,0 +1,38 @@
describe("toBeFalsy", function() {
it("passes for 'falsy' values", function() {
var matcher = j$.matchers.toBeFalsy(),
result;
result = matcher.compare(false);
expect(result.pass).toBe(true);
result = matcher.compare(0);
expect(result.pass).toBe(true);
result = matcher.compare('');
expect(result.pass).toBe(true);
result = matcher.compare(null);
expect(result.pass).toBe(true);
result = matcher.compare(void 0);
expect(result.pass).toBe(true);
});
it("fails for 'truthy' values", function() {
var matcher = j$.matchers.toBeFalsy(),
result;
result = matcher.compare(true);
expect(result.pass).toBe(false);
result = matcher.compare(1);
expect(result.pass).toBe(false);
result = matcher.compare("foo");
expect(result.pass).toBe(false);
result = matcher.compare({});
expect(result.pass).toBe(false);
});
});

View File

@@ -0,0 +1,19 @@
describe("toBeGreaterThan", function() {
it("passes when actual > expected", function() {
var matcher = j$.matchers.toBeGreaterThan(),
result;
result = matcher.compare(2, 1);
expect(result.pass).toBe(true);
});
it("fails when actual <= expected", function() {
var matcher = j$.matchers.toBeGreaterThan();
result = matcher.compare(1, 1);
expect(result.pass).toBe(false);
result = matcher.compare(1, 2);
expect(result.pass).toBe(false);
});
});

View File

@@ -0,0 +1,20 @@
describe("toBeLessThan", function() {
it("passes when actual < expected", function() {
var matcher = j$.matchers.toBeLessThan(),
result;
result = matcher.compare(1, 2);
expect(result.pass).toBe(true);
});
it("fails when actual <= expected", function() {
var matcher = j$.matchers.toBeLessThan(),
result;
result = matcher.compare(1, 1);
expect(result.pass).toBe(false);
result = matcher.compare(2, 1);
expect(result.pass).toBe(false);
});
});

View File

@@ -0,0 +1,36 @@
describe("toBeNaN", function() {
it("passes for NaN with a custom .not fail", function() {
var matcher = j$.matchers.toBeNaN(),
result;
result = matcher.compare(Number.NaN);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected actual not to be NaN.");
});
it("fails for anything not a NaN", function() {
var matcher = j$.matchers.toBeNaN();
result = matcher.compare(1);
expect(result.pass).toBe(false);
result = matcher.compare(null);
expect(result.pass).toBe(false);
result = matcher.compare(void 0);
expect(result.pass).toBe(false);
result = matcher.compare('');
expect(result.pass).toBe(false);
result = matcher.compare(Number.POSITIVE_INFINITY);
expect(result.pass).toBe(false);
});
it("has a custom message on failure", function() {
var matcher = j$.matchers.toBeNaN(),
result = matcher.compare(0);
expect(result.message).toEqual("Expected 0 to be NaN.");
});
});

View File

@@ -0,0 +1,17 @@
describe("toBeNull", function() {
it("passes for null", function() {
var matcher = j$.matchers.toBeNull(),
result;
result = matcher.compare(null);
expect(result.pass).toBe(true);
});
it("fails for non-null", function() {
var matcher = j$.matchers.toBeNull(),
result;
result = matcher.compare('foo');
expect(result.pass).toBe(false);
});
});

View File

@@ -0,0 +1,17 @@
describe("toBe", function() {
it("passes when actual === expected", function() {
var matcher = j$.matchers.toBe(),
result;
result = matcher.compare(1, 1);
expect(result.pass).toBe(true);
});
it("fails when actual !== expected", function() {
var matcher = j$.matchers.toBe(),
result;
result = matcher.compare(1, 2);
expect(result.pass).toBe(false);
});
});

View File

@@ -0,0 +1,38 @@
describe("toBeTruthy", function() {
it("passes for 'truthy' values", function() {
var matcher = j$.matchers.toBeTruthy(),
result;
result = matcher.compare(true);
expect(result.pass).toBe(true);
result = matcher.compare(1);
expect(result.pass).toBe(true);
result = matcher.compare("foo");
expect(result.pass).toBe(true);
result = matcher.compare({});
expect(result.pass).toBe(true);
});
it("fails for 'falsy' values", function() {
var matcher = j$.matchers.toBeTruthy(),
result;
result = matcher.compare(false);
expect(result.pass).toBe(false);
result = matcher.compare(0);
expect(result.pass).toBe(false);
result = matcher.compare('');
expect(result.pass).toBe(false);
result = matcher.compare(null);
expect(result.pass).toBe(false);
result = matcher.compare(void 0);
expect(result.pass).toBe(false);
});
});

View File

@@ -0,0 +1,17 @@
describe("toBeUndefined", function() {
it("passes for undefined values", function() {
var matcher = j$.matchers.toBeUndefined(),
result;
result = matcher.compare(void 0);
expect(result.pass).toBe(true);
});
it("fails when matching defined values", function() {
var matcher = j$.matchers.toBeUndefined();
result = matcher.compare('foo');
expect(result.pass).toBe(false);
})
});

View File

@@ -0,0 +1,24 @@
describe("toContain", function() {
it("delegates to j$.matchersUtil.contains", function() {
var util = {
contains: j$.createSpy('delegated-contains').andReturn(true)
},
matcher = j$.matchers.toContain(util);
result = matcher.compare("ABC", "B");
expect(util.contains).toHaveBeenCalledWith("ABC", "B", []);
expect(result.pass).toBe(true);
});
it("delegates to j$.matchersUtil.contains, passing in equality testers if present", function() {
var util = {
contains: j$.createSpy('delegated-contains').andReturn(true)
},
customEqualityTesters = ['a', 'b'],
matcher = j$.matchers.toContain(util, customEqualityTesters);
result = matcher.compare("ABC", "B");
expect(util.contains).toHaveBeenCalledWith("ABC", "B", ['a', 'b']);
expect(result.pass).toBe(true);
});
});

View File

@@ -0,0 +1,28 @@
describe("toEqual", function() {
it("delegates to equals function", function() {
var util = {
equals: j$.createSpy('delegated-equals').andReturn(true)
},
matcher = j$.matchers.toEqual(util),
result;
result = matcher.compare(1, 1);
expect(util.equals).toHaveBeenCalledWith(1, 1, []);
expect(result.pass).toBe(true);
});
it("delegates custom equality testers, if present", function() {
var util = {
equals: j$.createSpy('delegated-equals').andReturn(true)
},
customEqualityTesters = ['a', 'b'],
matcher = j$.matchers.toEqual(util, customEqualityTesters),
result;
result = matcher.compare(1, 1);
expect(util.equals).toHaveBeenCalledWith(1, 1, ['a', 'b']);
expect(result.pass).toBe(true);
});
});

View File

@@ -0,0 +1,46 @@
describe("toHaveBeenCalled", function() {
it("passes when the actual was called, with a custom .not fail message", function() {
var matcher = j$.matchers.toHaveBeenCalled(),
calledSpy = j$.createSpy('called-spy'),
result;
calledSpy();
result = matcher.compare(calledSpy);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected spy called-spy not to have been called.");
});
it("fails when the actual was not called", function() {
var matcher = j$.matchers.toHaveBeenCalled(),
uncalledSpy = j$.createSpy('uncalled spy');
result = matcher.compare(uncalledSpy);
expect(result.pass).toBe(false);
});
it("throws an exception when the actual is not a spy", function() {
var matcher = j$.matchers.toHaveBeenCalled(),
fn = function() {};
expect(function() { matcher.compare(fn) }).toThrow(new Error("Expected a spy, but got Function."));
});
it("throws an exception when invoked with any arguments", function() {
var matcher = j$.matchers.toHaveBeenCalled(),
spy = j$.createSpy('sample spy');
expect(function() { matcher.compare(spy, 'foo') }).toThrow(new Error("toHaveBeenCalled does not take arguments, use toHaveBeenCalledWith"));
});
it("has a custom message on failure", function() {
var matcher = j$.matchers.toHaveBeenCalled(),
spy = j$.createSpy('sample-spy'),
result;
result = matcher.compare(spy);
expect(result.message).toEqual("Expected spy sample-spy to have been called.");
});
});

View File

@@ -0,0 +1,57 @@
describe("toHaveBeenCalledWith", function() {
it("passes when the actual was called with matching parameters", function() {
var util = {
contains: j$.createSpy('delegated-contains').andReturn(true)
},
matcher = j$.matchers.toHaveBeenCalledWith(util)
calledSpy = j$.createSpy('called-spy'),
result;
calledSpy('a', 'b');
result = matcher.compare(calledSpy, 'a', 'b');
expect(result.pass).toBe(true);
});
it("fails when the actual was not called", function() {
var util = {
contains: j$.createSpy('delegated-contains').andReturn(false)
},
matcher = j$.matchers.toHaveBeenCalledWith(util),
uncalledSpy = j$.createSpy('uncalled spy'),
result;
result = matcher.compare(uncalledSpy);
expect(result.pass).toBe(false);
});
it("fails when the actual was called with different parameters", function() {
var util = {
contains: j$.createSpy('delegated-contains').andReturn(false)
},
matcher = j$.matchers.toHaveBeenCalledWith(util),
calledSpy = j$.createSpy('called spy'),
result;
calledSpy('a');
result = matcher.compare(calledSpy, 'a', 'b');
expect(result.pass).toBe(false);
});
it("throws an exception when the actual is not a spy", function() {
var matcher = j$.matchers.toHaveBeenCalledWith(),
fn = function() {};
expect(function() { matcher.compare(fn) }).toThrow(new Error("Expected a spy, but got Function."));
});
it("has a custom message on failure", function() {
var matcher = j$.matchers.toHaveBeenCalledWith(),
spy = j$.createSpy('sample-spy'),
messages = matcher.message(spy);
expect(messages.affirmative).toEqual("Expected spy sample-spy to have been called.")
expect(messages.negative).toEqual("Expected spy sample-spy not to have been called.")
});
});

View File

@@ -0,0 +1,34 @@
describe("toMatch", function() {
it("passes when RegExps are equivalent", function() {
var matcher = j$.matchers.toMatch(),
result;
result = matcher.compare(/foo/, /foo/);
expect(result.pass).toBe(true);
});
it("fails when RegExps are not equivalent", function() {
var matcher = j$.matchers.toMatch(),
result;
result = matcher.compare(/bar/, /foo/);
expect(result.pass).toBe(false);
});
it("passes when the actual matches the expected string as a pattern", function() {
var matcher = j$.matchers.toMatch(),
result;
result = matcher.compare('foosball', 'foo');
expect(result.pass).toBe(true);
});
it("fails when the actual matches the expected string as a pattern", function() {
var matcher = j$.matchers.toMatch(),
result;
result = matcher.compare('bar', 'foo');
expect(result.pass).toBe(false);
});
});

View File

@@ -0,0 +1,151 @@
describe("toThrow", function() {
it("throw an error when the acutal is not a function ", function() {
var matcher = j$.matchers.toThrow();
expect(function() {
matcher.compare({});
}).toThrow(new Error("Actual is not a Function"));
});
it("throws an error when the expected can't be turned into an exception", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "foo";
},
result;
expect(function() {
matcher.compare(fn, 1);
}).toThrow(new Error("Expected cannot be treated as an exception."));
});
it("passes if the actual throws any exception", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "foo";
},
result;
result = matcher.compare(fn);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception.");
});
it("fails if the actual does not throw an exception", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
return 0;
},
result;
result = matcher.compare(fn);
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw an exception.");
});
it("passes if the actual throws an exception with the expected message", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "foo";
},
result;
result = matcher.compare(fn, "foo");
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception \"foo\".");
});
it("fails if the actual throws an exception with a different message", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "foo";
},
result;
result = matcher.compare(fn, "bar");
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw an exception \"bar\".");
});
it("passes if the actual throws an exception and matches the message of the expected exception", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "foo";
},
result;
result = matcher.compare(fn, new Error("foo"));
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception \"foo\".");
});
it("fails if the actual throws an exception and it does not match the message of the expected exception with a custom message", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "foo";
},
result;
result = matcher.compare(fn, new Error("bar"));
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw an exception \"bar\".");
});
it("passes if the actual throws an exception and the message matches the expected regular expression", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "a long message";
},
result;
result = matcher.compare(fn, /long/);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception matching /long/.");
});
it("fails if the actual throws an exception and the message does not match the expected regular expression", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "a long message";
},
result;
result = matcher.compare(fn, /short/);
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw an exception matching /short/.");
});
it("passes if the actual throws an exception with an undefined message", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw void 0;
},
result;
result = matcher.compare(fn);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception.");
});
it("passes if the actual throws an exception with an empty message", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "";
},
result;
result = matcher.compare(fn);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception.");
});
});

View File

@@ -1,626 +0,0 @@
describe("Matchers", function() {
describe("toBe", function() {
it("passes when actual === expected", function() {
var matcher = j$.matchers.toBe(),
result;
result = matcher.compare(1, 1);
expect(result.pass).toBe(true);
});
it("fails when actual !== expected", function() {
var matcher = j$.matchers.toBe(),
result;
result = matcher.compare(1, 2);
expect(result.pass).toBe(false);
});
});
describe("toBeCloseTo", function() {
it("passes when within two decimal places by default", function() {
var matcher = j$.matchers.toBeCloseTo(),
result;
result = matcher.compare(0, 0);
expect(result.pass).toBe(true);
result = matcher.compare(0, 0.001);
expect(result.pass).toBe(true);
});
it("fails when not within two decimal places by default", function() {
var matcher = j$.matchers.toBeCloseTo(),
result;
result = matcher.compare(0, 0.01);
expect(result.pass).toBe(false);
});
it("accepts an optional precision argument", function() {
var matcher = j$.matchers.toBeCloseTo(),
result;
result = matcher.compare(0, 0.1, 0);
expect(result.pass).toBe(true);
result = matcher.compare(0, 0.0001, 3);
expect(result.pass).toBe(true);
});
it("rounds expected values", function() {
var matcher = j$.matchers.toBeCloseTo(),
result;
result = matcher.compare(1.23, 1.229);
expect(result.pass).toBe(true);
result = matcher.compare(1.23, 1.226);
expect(result.pass).toBe(true);
result = matcher.compare(1.23, 1.225);
expect(result.pass).toBe(true);
result = matcher.compare(1.23, 1.2249999);
expect(result.pass).toBe(false);
result = matcher.compare(1.23, 1.234);
expect(result.pass).toBe(true);
});
});
describe("toBeDefined", function() {
it("matches for defined values", function() {
var matcher = j$.matchers.toBeDefined(),
result;
result = matcher.compare('foo');
expect(result.pass).toBe(true);
});
it("fails when matching undefined values", function() {
var matcher = j$.matchers.toBeDefined(),
result;
result = matcher.compare(void 0);
expect(result.pass).toBe(false);
})
});
describe("toBeFalsy", function() {
it("passes for 'falsy' values", function() {
var matcher = j$.matchers.toBeFalsy(),
result;
result = matcher.compare(false);
expect(result.pass).toBe(true);
result = matcher.compare(0);
expect(result.pass).toBe(true);
result = matcher.compare('');
expect(result.pass).toBe(true);
result = matcher.compare(null);
expect(result.pass).toBe(true);
result = matcher.compare(void 0);
expect(result.pass).toBe(true);
});
it("fails for 'truthy' values", function() {
var matcher = j$.matchers.toBeFalsy(),
result;
result = matcher.compare(true);
expect(result.pass).toBe(false);
result = matcher.compare(1);
expect(result.pass).toBe(false);
result = matcher.compare("foo");
expect(result.pass).toBe(false);
result = matcher.compare({});
expect(result.pass).toBe(false);
});
});
describe("toBeGreaterThan", function() {
it("passes when actual > expected", function() {
var matcher = j$.matchers.toBeGreaterThan(),
result;
result = matcher.compare(2, 1);
expect(result.pass).toBe(true);
});
it("fails when actual <= expected", function() {
var matcher = j$.matchers.toBeGreaterThan();
result = matcher.compare(1, 1);
expect(result.pass).toBe(false);
result = matcher.compare(1, 2);
expect(result.pass).toBe(false);
});
});
describe("toBeLessThan", function() {
it("passes when actual < expected", function() {
var matcher = j$.matchers.toBeLessThan(),
result;
result = matcher.compare(1, 2);
expect(result.pass).toBe(true);
});
it("fails when actual <= expected", function() {
var matcher = j$.matchers.toBeLessThan(),
result;
result = matcher.compare(1, 1);
expect(result.pass).toBe(false);
result = matcher.compare(2, 1);
expect(result.pass).toBe(false);
});
});
describe("toBeNaN", function() {
it("passes for NaN with a custom .not fail", function() {
var matcher = j$.matchers.toBeNaN(),
result;
result = matcher.compare(Number.NaN);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected actual not to be NaN.");
});
it("fails for anything not a NaN", function() {
var matcher = j$.matchers.toBeNaN();
result = matcher.compare(1);
expect(result.pass).toBe(false);
result = matcher.compare(null);
expect(result.pass).toBe(false);
result = matcher.compare(void 0);
expect(result.pass).toBe(false);
result = matcher.compare('');
expect(result.pass).toBe(false);
result = matcher.compare(Number.POSITIVE_INFINITY);
expect(result.pass).toBe(false);
});
it("has a custom message on failure", function() {
var matcher = j$.matchers.toBeNaN(),
result = matcher.compare(0);
expect(result.message).toEqual("Expected 0 to be NaN.");
});
});
describe("toBeNull", function() {
it("passes for null", function() {
var matcher = j$.matchers.toBeNull(),
result;
result = matcher.compare(null);
expect(result.pass).toBe(true);
});
it("fails for non-null", function() {
var matcher = j$.matchers.toBeNull(),
result;
result = matcher.compare('foo');
expect(result.pass).toBe(false);
});
});
describe("toBeTruthy", function() {
it("passes for 'truthy' values", function() {
var matcher = j$.matchers.toBeTruthy(),
result;
result = matcher.compare(true);
expect(result.pass).toBe(true);
result = matcher.compare(1);
expect(result.pass).toBe(true);
result = matcher.compare("foo");
expect(result.pass).toBe(true);
result = matcher.compare({});
expect(result.pass).toBe(true);
});
it("fails for 'falsy' values", function() {
var matcher = j$.matchers.toBeTruthy(),
result;
result = matcher.compare(false);
expect(result.pass).toBe(false);
result = matcher.compare(0);
expect(result.pass).toBe(false);
result = matcher.compare('');
expect(result.pass).toBe(false);
result = matcher.compare(null);
expect(result.pass).toBe(false);
result = matcher.compare(void 0);
expect(result.pass).toBe(false);
});
});
describe("toBeUndefined", function() {
it("passes for undefined values", function() {
var matcher = j$.matchers.toBeUndefined(),
result;
result = matcher.compare(void 0);
expect(result.pass).toBe(true);
});
it("fails when matching defined values", function() {
var matcher = j$.matchers.toBeUndefined();
result = matcher.compare('foo');
expect(result.pass).toBe(false);
})
});
describe("toContain", function() {
it("delegates to j$.matchersUtil.contains", function() {
var util = {
contains: j$.createSpy('delegated-contains').andReturn(true)
},
matcher = j$.matchers.toContain(util);
result = matcher.compare("ABC", "B");
expect(util.contains).toHaveBeenCalledWith("ABC", "B", []);
expect(result.pass).toBe(true);
});
it("delegates to j$.matchersUtil.contains, passing in equality testers if present", function() {
var util = {
contains: j$.createSpy('delegated-contains').andReturn(true)
},
customEqualityTesters = ['a', 'b'],
matcher = j$.matchers.toContain(util, customEqualityTesters);
result = matcher.compare("ABC", "B");
expect(util.contains).toHaveBeenCalledWith("ABC", "B", ['a', 'b']);
expect(result.pass).toBe(true);
});
});
describe("toEqual", function() {
it("delegates to equals function", function() {
var util = {
equals: j$.createSpy('delegated-equals').andReturn(true)
},
matcher = j$.matchers.toEqual(util),
result;
result = matcher.compare(1, 1);
expect(util.equals).toHaveBeenCalledWith(1, 1, []);
expect(result.pass).toBe(true);
});
it("delegates custom equality testers, if present", function() {
var util = {
equals: j$.createSpy('delegated-equals').andReturn(true)
},
customEqualityTesters = ['a', 'b'],
matcher = j$.matchers.toEqual(util, customEqualityTesters),
result;
result = matcher.compare(1, 1);
expect(util.equals).toHaveBeenCalledWith(1, 1, ['a', 'b']);
expect(result.pass).toBe(true);
});
});
describe("toHaveBeenCalled", function() {
it("passes when the actual was called, with a custom .not fail message", function() {
var matcher = j$.matchers.toHaveBeenCalled(),
calledSpy = j$.createSpy('called-spy'),
result;
calledSpy();
result = matcher.compare(calledSpy);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected spy called-spy not to have been called.");
});
it("fails when the actual was not called", function() {
var matcher = j$.matchers.toHaveBeenCalled(),
uncalledSpy = j$.createSpy('uncalled spy');
result = matcher.compare(uncalledSpy);
expect(result.pass).toBe(false);
});
it("throws an exception when the actual is not a spy", function() {
var matcher = j$.matchers.toHaveBeenCalled(),
fn = function() {};
expect(function() { matcher.compare(fn) }).toThrow(new Error("Expected a spy, but got Function."));
});
it("throws an exception when invoked with any arguments", function() {
var matcher = j$.matchers.toHaveBeenCalled(),
spy = j$.createSpy('sample spy');
expect(function() { matcher.compare(spy, 'foo') }).toThrow(new Error("toHaveBeenCalled does not take arguments, use toHaveBeenCalledWith"));
});
it("has a custom message on failure", function() {
var matcher = j$.matchers.toHaveBeenCalled(),
spy = j$.createSpy('sample-spy'),
result;
result = matcher.compare(spy);
expect(result.message).toEqual("Expected spy sample-spy to have been called.");
});
});
describe("toHaveBeenCalledWith", function() {
it("passes when the actual was called with matching parameters", function() {
var util = {
contains: j$.createSpy('delegated-contains').andReturn(true)
},
matcher = j$.matchers.toHaveBeenCalledWith(util)
calledSpy = j$.createSpy('called-spy'),
result;
calledSpy('a', 'b');
result = matcher.compare(calledSpy, 'a', 'b');
expect(result.pass).toBe(true);
});
it("fails when the actual was not called", function() {
var util = {
contains: j$.createSpy('delegated-contains').andReturn(false)
},
matcher = j$.matchers.toHaveBeenCalledWith(util),
uncalledSpy = j$.createSpy('uncalled spy'),
result;
result = matcher.compare(uncalledSpy);
expect(result.pass).toBe(false);
});
it("fails when the actual was called with different parameters", function() {
var util = {
contains: j$.createSpy('delegated-contains').andReturn(false)
},
matcher = j$.matchers.toHaveBeenCalledWith(util),
calledSpy = j$.createSpy('called spy'),
result;
calledSpy('a');
result = matcher.compare(calledSpy, 'a', 'b');
expect(result.pass).toBe(false);
});
it("throws an exception when the actual is not a spy", function() {
var matcher = j$.matchers.toHaveBeenCalledWith(),
fn = function() {};
expect(function() { matcher.compare(fn) }).toThrow(new Error("Expected a spy, but got Function."));
});
it("has a custom message on failure", function() {
var matcher = j$.matchers.toHaveBeenCalledWith(),
spy = j$.createSpy('sample-spy'),
messages = matcher.message(spy);
expect(messages.affirmative).toEqual("Expected spy sample-spy to have been called.")
expect(messages.negative).toEqual("Expected spy sample-spy not to have been called.")
});
});
describe("toMatch", function() {
it("passes when RegExps are equivalent", function() {
var matcher = j$.matchers.toMatch(),
result;
result = matcher.compare(/foo/, /foo/);
expect(result.pass).toBe(true);
});
it("fails when RegExps are not equivalent", function() {
var matcher = j$.matchers.toMatch(),
result;
result = matcher.compare(/bar/, /foo/);
expect(result.pass).toBe(false);
});
it("passes when the actual matches the expected string as a pattern", function() {
var matcher = j$.matchers.toMatch(),
result;
result = matcher.compare('foosball', 'foo');
expect(result.pass).toBe(true);
});
it("fails when the actual matches the expected string as a pattern", function() {
var matcher = j$.matchers.toMatch(),
result;
result = matcher.compare('bar', 'foo');
expect(result.pass).toBe(false);
});
});
describe("toThrow", function() {
it("throw an error when the acutal is not a function ", function() {
var matcher = j$.matchers.toThrow();
expect(function() {
matcher.compare({});
}).toThrow(new Error("Actual is not a Function"));
});
it("throws an error when the expected can't be turned into an exception", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "foo";
},
result;
expect(function() {
matcher.compare(fn, 1);
}).toThrow(new Error("Expected cannot be treated as an exception."));
});
it("passes if the actual throws any exception", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "foo";
},
result;
result = matcher.compare(fn);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception.");
});
it("fails if the actual does not throw an exception", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
return 0;
},
result;
result = matcher.compare(fn);
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw an exception.");
});
it("passes if the actual throws an exception with the expected message", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "foo";
},
result;
result = matcher.compare(fn, "foo");
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception \"foo\".");
});
it("fails if the actual throws an exception with a different message", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "foo";
},
result;
result = matcher.compare(fn, "bar");
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw an exception \"bar\".");
});
it("passes if the actual throws an exception and matches the message of the expected exception", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "foo";
},
result;
result = matcher.compare(fn, new Error("foo"));
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception \"foo\".");
});
it("fails if the actual throws an exception and it does not match the message of the expected exception with a custom message", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "foo";
},
result;
result = matcher.compare(fn, new Error("bar"));
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw an exception \"bar\".");
});
it("passes if the actual throws an exception and the message matches the expected regular expression", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "a long message";
},
result;
result = matcher.compare(fn, /long/);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception matching /long/.");
});
it("fails if the actual throws an exception and the message does not match the expected regular expression", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "a long message";
},
result;
result = matcher.compare(fn, /short/);
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw an exception matching /short/.");
});
it("passes if the actual throws an exception with an undefined message", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw void 0;
},
result;
result = matcher.compare(fn);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception.");
});
it("passes if the actual throws an exception with an empty message", function() {
var matcher = j$.matchers.toThrow(),
fn = function() {
throw "";
},
result;
result = matcher.compare(fn);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception.");
});
});
});

View File

@@ -9,9 +9,9 @@ src_files:
- 'core/Spec.js'
- 'core/Env.js'
- 'core/JsApiReporter.js'
- 'core/Matchers.js'
- 'core/PrettyPrinter.js'
- 'core/Suite.js'
- 'core/**/*.js'
- 'html/**.js'
- '**/*.js'
stylesheets:

View File

@@ -86,25 +86,25 @@ function executeSpecs(specs, done, isVerbose, showColors) {
}
function getFiles(dir, matcher) {
specs = [];
var allFiles = [];
if (fs.statSync(dir).isFile() && dir.match(matcher)) {
specs.push(dir);
allFiles.push(dir);
} else {
var files = fs.readdirSync(dir);
for (var i = 0, len = files.length; i < len; ++i) {
var filename = dir + '/' + files[i];
if (fs.statSync(filename).isFile() && filename.match(matcher)) {
specs.push(filename);
allFiles.push(filename);
} else if (fs.statSync(filename).isDirectory()) {
var subfiles = getSpecFiles(filename);
var subfiles = getFiles(filename);
subfiles.forEach(function(result) {
specs.push(result);
allFiles.push(result);
});
}
}
}
return specs;
return allFiles;
}
function getSpecFiles(dir) {

View File

@@ -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;
};

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;
};

View File

@@ -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$;
};