2009-05-29 03:02:15 +00:00
|
|
|
describe("jasmine.Matchers", function() {
|
2009-12-24 22:01:13 +00:00
|
|
|
var env, spec;
|
2009-10-30 00:03:24 +00:00
|
|
|
|
2009-05-29 03:02:15 +00:00
|
|
|
beforeEach(function() {
|
|
|
|
env = new jasmine.Env();
|
2009-10-06 05:36:10 +00:00
|
|
|
env.updateInterval = 0;
|
2009-12-24 22:01:13 +00:00
|
|
|
|
|
|
|
var suite = env.describe("suite", function() {
|
|
|
|
spec = env.it("spec", function() {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
spyOn(spec, 'addMatcherResult');
|
2009-05-29 03:02:15 +00:00
|
|
|
});
|
|
|
|
|
2009-10-30 00:03:24 +00:00
|
|
|
function match(value) {
|
2009-12-24 22:01:13 +00:00
|
|
|
return spec.expect(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
function lastResult() {
|
|
|
|
return spec.addMatcherResult.mostRecentCall.args[0];
|
2009-05-29 03:02:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
it("toEqual with primitives, objects, dates, html nodes, etc.", function() {
|
|
|
|
expect(match(true).toEqual(true)).toEqual(true);
|
|
|
|
|
|
|
|
expect(match({foo:'bar'}).toEqual(null)).toEqual(false);
|
|
|
|
|
2009-10-30 00:03:24 +00:00
|
|
|
var functionA = function() {
|
|
|
|
return 'hi';
|
|
|
|
};
|
|
|
|
var functionB = function() {
|
|
|
|
return 'hi';
|
|
|
|
};
|
2009-05-29 03:02:15 +00:00
|
|
|
expect(match({foo:functionA}).toEqual({foo:functionB})).toEqual(false);
|
|
|
|
expect(match({foo:functionA}).toEqual({foo:functionA})).toEqual(true);
|
|
|
|
|
|
|
|
expect((match(false).toEqual(true))).toEqual(false);
|
|
|
|
|
|
|
|
var circularGraph = {};
|
|
|
|
circularGraph.referenceToSelf = circularGraph;
|
|
|
|
expect((match(circularGraph).toEqual(circularGraph))).toEqual(true);
|
|
|
|
|
|
|
|
var nodeA = document.createElement('div');
|
|
|
|
var nodeB = document.createElement('div');
|
|
|
|
expect((match(nodeA).toEqual(nodeA))).toEqual(true);
|
|
|
|
expect((match(nodeA).toEqual(nodeB))).toEqual(false);
|
|
|
|
|
|
|
|
expect((match(new Date(2008, 1, 3, 15, 17, 19, 1234)).toEqual(new Date(2009, 1, 3, 15, 17, 19, 1234)))).toEqual(false);
|
|
|
|
expect((match(new Date(2008, 1, 3, 15, 17, 19, 1234)).toEqual(new Date(2008, 1, 3, 15, 17, 19, 1234)))).toEqual(true);
|
|
|
|
|
|
|
|
|
|
|
|
expect(match(true).toNotEqual(false)).toEqual(true);
|
|
|
|
expect((match(true).toNotEqual(true))).toEqual(false);
|
|
|
|
|
2009-11-26 16:12:06 +00:00
|
|
|
expect((match(['a', 'b']).toEqual(['a', jasmine.undefined]))).toEqual(false);
|
|
|
|
expect((match(['a', 'b']).toEqual(['a', 'b', jasmine.undefined]))).toEqual(false);
|
2009-05-29 03:02:15 +00:00
|
|
|
});
|
|
|
|
|
2009-10-30 00:03:24 +00:00
|
|
|
it("toEqual to build an Expectation Result", function() {
|
2009-10-30 03:33:01 +00:00
|
|
|
var actual = 'a';
|
|
|
|
var matcher = match(actual);
|
|
|
|
var expected = 'b';
|
|
|
|
matcher.toEqual(expected);
|
2009-10-30 00:03:24 +00:00
|
|
|
|
2009-12-24 22:01:13 +00:00
|
|
|
var result = lastResult();
|
2009-10-30 00:03:24 +00:00
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toEqual");
|
|
|
|
expect(result.passed()).toEqual(false);
|
2009-10-30 03:33:01 +00:00
|
|
|
expect(result.message).toMatch(jasmine.pp(actual));
|
|
|
|
expect(result.message).toMatch(jasmine.pp(expected));
|
|
|
|
expect(result.expected).toEqual(expected);
|
|
|
|
expect(result.actual).toEqual(actual);
|
2009-10-30 00:03:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("toNotEqual to build an Expectation Result", function() {
|
2009-10-30 03:33:01 +00:00
|
|
|
var str = 'a';
|
|
|
|
var matcher = match(str);
|
|
|
|
matcher.toNotEqual(str);
|
2009-10-30 00:03:24 +00:00
|
|
|
|
2009-12-24 22:01:13 +00:00
|
|
|
var result = lastResult();
|
2009-10-30 00:03:24 +00:00
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toNotEqual");
|
|
|
|
expect(result.passed()).toEqual(false);
|
2009-10-30 03:33:01 +00:00
|
|
|
expect(result.message).toMatch(jasmine.pp(str));
|
|
|
|
expect(result.message).toMatch('not');
|
|
|
|
expect(result.expected).toEqual(str);
|
|
|
|
expect(result.actual).toEqual(str);
|
2009-10-30 00:03:24 +00:00
|
|
|
});
|
|
|
|
|
2009-05-29 03:02:15 +00:00
|
|
|
it('toBe should return true only if the expected and actual items === each other', function() {
|
|
|
|
var a = {};
|
|
|
|
var b = {};
|
|
|
|
//noinspection UnnecessaryLocalVariableJS
|
|
|
|
var c = a;
|
|
|
|
expect((match(a).toBe(b))).toEqual(false);
|
|
|
|
expect((match(a).toBe(a))).toEqual(true);
|
|
|
|
expect((match(a).toBe(c))).toEqual(true);
|
|
|
|
expect((match(a).toNotBe(b))).toEqual(true);
|
|
|
|
expect((match(a).toNotBe(a))).toEqual(false);
|
|
|
|
expect((match(a).toNotBe(c))).toEqual(false);
|
|
|
|
});
|
|
|
|
|
2009-10-30 00:03:24 +00:00
|
|
|
it("toBe to build an ExpectationResult", function() {
|
|
|
|
var expected = 'b';
|
|
|
|
var actual = 'a';
|
|
|
|
var matcher = match(actual);
|
|
|
|
matcher.toBe(expected);
|
|
|
|
|
2009-12-24 22:01:13 +00:00
|
|
|
var result = lastResult();
|
2009-10-30 00:03:24 +00:00
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toBe");
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
expect(result.message).toMatch(jasmine.pp(actual));
|
|
|
|
expect(result.message).toMatch(jasmine.pp(expected));
|
2009-10-30 03:33:01 +00:00
|
|
|
expect(result.expected).toEqual(expected);
|
2009-10-30 00:03:24 +00:00
|
|
|
expect(result.actual).toEqual(actual);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("toNotBe to build an ExpectationResult", function() {
|
|
|
|
var str = 'a';
|
|
|
|
var matcher = match(str);
|
|
|
|
matcher.toNotBe(str);
|
|
|
|
|
2009-12-24 22:01:13 +00:00
|
|
|
var result = lastResult();
|
2009-10-30 00:03:24 +00:00
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toNotBe");
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
expect(result.message).toMatch(str);
|
2009-10-30 03:33:01 +00:00
|
|
|
expect(result.expected).toEqual(str);
|
2009-10-30 00:03:24 +00:00
|
|
|
expect(result.actual).toEqual(str);
|
|
|
|
});
|
2009-05-29 03:02:15 +00:00
|
|
|
|
|
|
|
it("toMatch and #toNotMatch should perform regular expression matching on strings", function() {
|
|
|
|
expect((match('foobarbel').toMatch(/bar/))).toEqual(true);
|
|
|
|
expect((match('foobazbel').toMatch(/bar/))).toEqual(false);
|
|
|
|
|
|
|
|
expect((match('foobarbel').toMatch("bar"))).toEqual(true);
|
|
|
|
expect((match('foobazbel').toMatch("bar"))).toEqual(false);
|
|
|
|
|
|
|
|
expect((match('foobarbel').toNotMatch(/bar/))).toEqual(false);
|
|
|
|
expect((match('foobazbel').toNotMatch(/bar/))).toEqual(true);
|
|
|
|
|
|
|
|
expect((match('foobarbel').toNotMatch("bar"))).toEqual(false);
|
|
|
|
expect((match('foobazbel').toNotMatch("bar"))).toEqual(true);
|
|
|
|
});
|
|
|
|
|
2009-10-30 00:03:24 +00:00
|
|
|
it("toMatch w/ RegExp to build an ExpectationResult", function() {
|
2009-10-30 03:33:01 +00:00
|
|
|
var actual = 'a';
|
|
|
|
var matcher = match(actual);
|
|
|
|
var expected = /b/;
|
|
|
|
matcher.toMatch(expected);
|
2009-10-30 00:03:24 +00:00
|
|
|
|
2009-12-24 22:01:13 +00:00
|
|
|
var result = lastResult();
|
2009-10-30 00:03:24 +00:00
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toMatch");
|
|
|
|
expect(result.passed()).toEqual(false);
|
2009-10-30 03:33:01 +00:00
|
|
|
expect(result.message).toMatch(jasmine.pp(actual));
|
|
|
|
expect(result.message).toMatch(expected.toString());
|
|
|
|
expect(result.expected).toEqual(expected);
|
|
|
|
expect(result.actual).toEqual(actual);
|
2009-10-30 00:03:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("toMatch w/ String to build an ExpectationResult", function() {
|
2009-10-30 03:33:01 +00:00
|
|
|
var actual = 'a';
|
|
|
|
var matcher = match(actual);
|
|
|
|
var expected = 'b';
|
|
|
|
matcher.toMatch(expected);
|
2009-10-30 00:03:24 +00:00
|
|
|
|
2009-12-24 22:01:13 +00:00
|
|
|
var result = lastResult();
|
2009-10-30 00:03:24 +00:00
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toMatch");
|
|
|
|
expect(result.passed()).toEqual(false);
|
2009-11-12 20:43:49 +00:00
|
|
|
expect(result.message).toEqual("Expected 'a' to match 'b'.");
|
2009-10-30 03:33:01 +00:00
|
|
|
expect(result.expected).toEqual(expected);
|
|
|
|
expect(result.actual).toEqual(actual);
|
2009-10-30 00:03:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("toNotMatch w/ RegExp to build an ExpectationResult", function() {
|
2009-10-30 03:33:01 +00:00
|
|
|
var actual = 'a';
|
|
|
|
var matcher = match(actual);
|
|
|
|
var expected = /a/;
|
|
|
|
matcher.toNotMatch(expected);
|
2009-10-30 00:03:24 +00:00
|
|
|
|
2009-12-24 22:01:13 +00:00
|
|
|
var result = lastResult();
|
2009-10-30 00:03:24 +00:00
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toNotMatch");
|
|
|
|
expect(result.passed()).toEqual(false);
|
2009-11-12 20:43:49 +00:00
|
|
|
expect(result.message).toEqual("Expected 'a' to not match /a/.");
|
2009-10-30 03:33:01 +00:00
|
|
|
expect(result.expected).toEqual(expected);
|
|
|
|
expect(result.actual).toEqual(actual);
|
2009-10-30 00:03:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("toNotMatch w/ String to build an ExpectationResult", function() {
|
2009-10-30 03:33:01 +00:00
|
|
|
var str = 'a';
|
|
|
|
var matcher = match(str);
|
|
|
|
matcher.toNotMatch(str);
|
2009-10-30 00:03:24 +00:00
|
|
|
|
2009-12-24 22:01:13 +00:00
|
|
|
var result = lastResult();
|
2009-10-30 00:03:24 +00:00
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toNotMatch");
|
|
|
|
expect(result.passed()).toEqual(false);
|
2009-11-12 20:43:49 +00:00
|
|
|
expect(result.message).toEqual("Expected 'a' to not match 'a'.");
|
2009-10-30 03:33:01 +00:00
|
|
|
expect(result.expected).toEqual(str);
|
|
|
|
expect(result.actual).toEqual(str);
|
2009-10-30 00:03:24 +00:00
|
|
|
});
|
|
|
|
|
2009-05-29 03:02:15 +00:00
|
|
|
it("toBeDefined", function() {
|
|
|
|
expect(match('foo').toBeDefined()).toEqual(true);
|
2009-11-26 16:12:06 +00:00
|
|
|
expect(match(jasmine.undefined).toBeDefined()).toEqual(false);
|
2009-05-29 03:02:15 +00:00
|
|
|
});
|
|
|
|
|
2009-10-30 00:03:24 +00:00
|
|
|
it("toBeDefined to build an ExpectationResult", function() {
|
2009-11-26 16:12:06 +00:00
|
|
|
var matcher = match(jasmine.undefined);
|
2009-10-30 00:03:24 +00:00
|
|
|
matcher.toBeDefined();
|
|
|
|
|
2009-12-24 22:01:13 +00:00
|
|
|
var result = lastResult();
|
2009-10-30 00:03:24 +00:00
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toBeDefined");
|
|
|
|
expect(result.passed()).toEqual(false);
|
2009-11-12 19:36:58 +00:00
|
|
|
expect(result.message).toEqual('Expected undefined to be defined.');
|
2009-11-26 16:12:06 +00:00
|
|
|
expect(result.actual).toEqual(jasmine.undefined);
|
2009-10-30 00:03:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("toBeUndefined", function() {
|
|
|
|
expect(match('foo').toBeUndefined()).toEqual(false);
|
2009-11-26 16:12:06 +00:00
|
|
|
expect(match(jasmine.undefined).toBeUndefined()).toEqual(true);
|
2009-10-30 00:03:24 +00:00
|
|
|
});
|
|
|
|
|
2009-05-29 03:02:15 +00:00
|
|
|
it("toBeNull", function() {
|
|
|
|
expect(match(null).toBeNull()).toEqual(true);
|
2009-11-26 16:12:06 +00:00
|
|
|
expect(match(jasmine.undefined).toBeNull()).toEqual(false);
|
2009-05-29 03:02:15 +00:00
|
|
|
expect(match("foo").toBeNull()).toEqual(false);
|
|
|
|
});
|
|
|
|
|
2009-10-30 00:03:24 +00:00
|
|
|
it("toBeNull w/ String to build an ExpectationResult", function() {
|
|
|
|
var actual = 'a';
|
|
|
|
var matcher = match(actual);
|
|
|
|
matcher.toBeNull();
|
|
|
|
|
2009-12-24 22:01:13 +00:00
|
|
|
var result = lastResult();
|
2009-10-30 00:03:24 +00:00
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toBeNull");
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
expect(result.message).toMatch(jasmine.pp(actual));
|
|
|
|
expect(result.message).toMatch('null');
|
|
|
|
expect(result.actual).toEqual(actual);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("toBeNull w/ Object to build an ExpectationResult", function() {
|
|
|
|
var actual = {a: 'b'};
|
|
|
|
var matcher = match(actual);
|
|
|
|
matcher.toBeNull();
|
|
|
|
|
2009-12-24 22:01:13 +00:00
|
|
|
var result = lastResult();
|
2009-10-30 00:03:24 +00:00
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toBeNull");
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
expect(result.message).toMatch(jasmine.pp(actual));
|
|
|
|
expect(result.message).toMatch('null');
|
|
|
|
expect(result.actual).toEqual(actual);
|
|
|
|
});
|
|
|
|
|
2009-05-29 03:02:15 +00:00
|
|
|
it("toBeFalsy", function() {
|
|
|
|
expect(match(false).toBeFalsy()).toEqual(true);
|
|
|
|
expect(match(true).toBeFalsy()).toEqual(false);
|
2009-11-26 16:12:06 +00:00
|
|
|
expect(match(jasmine.undefined).toBeFalsy()).toEqual(true);
|
2009-05-29 03:02:15 +00:00
|
|
|
expect(match(0).toBeFalsy()).toEqual(true);
|
|
|
|
expect(match("").toBeFalsy()).toEqual(true);
|
|
|
|
});
|
|
|
|
|
2009-10-30 00:03:24 +00:00
|
|
|
it("toBeFalsy to build an ExpectationResult", function() {
|
|
|
|
var actual = 'a';
|
|
|
|
var matcher = match(actual);
|
|
|
|
matcher.toBeFalsy();
|
|
|
|
|
2009-12-24 22:01:13 +00:00
|
|
|
var result = lastResult();
|
2009-10-30 00:03:24 +00:00
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toBeFalsy");
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
expect(result.message).toMatch(jasmine.pp(actual));
|
|
|
|
expect(result.message).toMatch('falsy');
|
|
|
|
expect(result.actual).toEqual(actual);
|
|
|
|
});
|
|
|
|
|
2009-05-29 03:02:15 +00:00
|
|
|
it("toBeTruthy", function() {
|
|
|
|
expect(match(false).toBeTruthy()).toEqual(false);
|
|
|
|
expect(match(true).toBeTruthy()).toEqual(true);
|
2009-11-26 16:12:06 +00:00
|
|
|
expect(match(jasmine.undefined).toBeTruthy()).toEqual(false);
|
2009-05-29 03:02:15 +00:00
|
|
|
expect(match(0).toBeTruthy()).toEqual(false);
|
|
|
|
expect(match("").toBeTruthy()).toEqual(false);
|
|
|
|
expect(match("hi").toBeTruthy()).toEqual(true);
|
|
|
|
expect(match(5).toBeTruthy()).toEqual(true);
|
|
|
|
expect(match({foo: 1}).toBeTruthy()).toEqual(true);
|
|
|
|
});
|
|
|
|
|
2009-10-30 00:03:24 +00:00
|
|
|
it("toBeTruthy to build an ExpectationResult", function() {
|
|
|
|
var matcher = match(false);
|
|
|
|
matcher.toBeTruthy();
|
|
|
|
|
2009-12-24 22:01:13 +00:00
|
|
|
var result = lastResult();
|
2009-10-30 00:03:24 +00:00
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toBeTruthy");
|
|
|
|
expect(result.passed()).toEqual(false);
|
2009-11-12 19:36:58 +00:00
|
|
|
expect(result.message).toEqual("Expected false to be truthy.");
|
2009-10-30 00:03:24 +00:00
|
|
|
expect(result.actual).toEqual(false);
|
|
|
|
});
|
|
|
|
|
2009-05-29 03:02:15 +00:00
|
|
|
it("toEqual", function() {
|
2009-11-26 16:12:06 +00:00
|
|
|
expect(match(jasmine.undefined).toEqual(jasmine.undefined)).toEqual(true);
|
2009-05-29 03:02:15 +00:00
|
|
|
expect(match({foo:'bar'}).toEqual({foo:'bar'})).toEqual(true);
|
2009-11-26 16:12:06 +00:00
|
|
|
expect(match("foo").toEqual({bar: jasmine.undefined})).toEqual(false);
|
|
|
|
expect(match({foo: jasmine.undefined}).toEqual("goo")).toEqual(false);
|
|
|
|
expect(match({foo: {bar :jasmine.undefined}}).toEqual("goo")).toEqual(false);
|
2009-05-29 03:02:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("toEqual with jasmine.any()", function() {
|
|
|
|
expect(match("foo").toEqual(jasmine.any(String))).toEqual(true);
|
|
|
|
expect(match(3).toEqual(jasmine.any(Number))).toEqual(true);
|
|
|
|
expect(match("foo").toEqual(jasmine.any(Function))).toEqual(false);
|
|
|
|
expect(match("foo").toEqual(jasmine.any(Object))).toEqual(false);
|
|
|
|
expect(match({someObj:'foo'}).toEqual(jasmine.any(Object))).toEqual(true);
|
|
|
|
expect(match({someObj:'foo'}).toEqual(jasmine.any(Function))).toEqual(false);
|
2009-10-30 00:03:24 +00:00
|
|
|
expect(match(function() {
|
|
|
|
}).toEqual(jasmine.any(Object))).toEqual(false);
|
2009-05-29 03:02:15 +00:00
|
|
|
expect(match(["foo", "goo"]).toEqual(["foo", jasmine.any(String)])).toEqual(true);
|
2009-10-30 00:03:24 +00:00
|
|
|
expect(match(function() {
|
|
|
|
}).toEqual(jasmine.any(Function))).toEqual(true);
|
|
|
|
expect(match(["a", function() {
|
|
|
|
}]).toEqual(["a", jasmine.any(Function)])).toEqual(true);
|
2009-05-29 03:02:15 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("toEqual handles circular objects ok", function() {
|
2009-11-26 16:12:06 +00:00
|
|
|
expect(match({foo: "bar", baz: jasmine.undefined}).toEqual({foo: "bar", baz: jasmine.undefined})).toEqual(true);
|
2009-05-29 03:02:15 +00:00
|
|
|
expect(match({foo:['bar','baz','quux']}).toEqual({foo:['bar','baz','quux']})).toEqual(true);
|
|
|
|
expect(match({foo: {bar:'baz'}, quux:'corge'}).toEqual({foo:{bar:'baz'}, quux:'corge'})).toEqual(true);
|
|
|
|
|
|
|
|
var circularObject = {};
|
|
|
|
var secondCircularObject = {};
|
|
|
|
circularObject.field = circularObject;
|
|
|
|
secondCircularObject.field = secondCircularObject;
|
|
|
|
expect(match(circularObject).toEqual(secondCircularObject)).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("toNotEqual as slightly surprising behavior, but is it intentional?", function() {
|
|
|
|
expect(match({x:"x", y:"y", z:"w"}).toNotEqual({x:"x", y:"y", z:"z"})).toEqual(true);
|
|
|
|
expect(match({x:"x", y:"y", w:"z"}).toNotEqual({x:"x", y:"y", z:"z"})).toEqual(true);
|
|
|
|
expect(match({x:"x", y:"y", z:"z"}).toNotEqual({w: "w", x:"x", y:"y", z:"z"})).toEqual(true);
|
|
|
|
expect(match({w: "w", x:"x", y:"y", z:"z"}).toNotEqual({x:"x", y:"y", z:"z"})).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("toEqual handles arrays", function() {
|
|
|
|
expect(match([1, "A"]).toEqual([1, "A"])).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("toContain and toNotContain", function() {
|
|
|
|
expect(match('ABC').toContain('A')).toEqual(true);
|
|
|
|
expect(match('ABC').toContain('X')).toEqual(false);
|
2009-10-30 00:03:24 +00:00
|
|
|
|
2009-05-29 03:02:15 +00:00
|
|
|
expect(match(['A', 'B', 'C']).toContain('A')).toEqual(true);
|
|
|
|
expect(match(['A', 'B', 'C']).toContain('F')).toEqual(false);
|
|
|
|
expect(match(['A', 'B', 'C']).toNotContain('F')).toEqual(true);
|
|
|
|
expect(match(['A', 'B', 'C']).toNotContain('A')).toEqual(false);
|
|
|
|
|
|
|
|
expect(match(['A', {some:'object'}, 'C']).toContain({some:'object'})).toEqual(true);
|
|
|
|
expect(match(['A', {some:'object'}, 'C']).toContain({some:'other object'})).toEqual(false);
|
2009-10-30 00:03:24 +00:00
|
|
|
});
|
2009-05-29 03:02:15 +00:00
|
|
|
|
2009-10-30 00:03:24 +00:00
|
|
|
it("toContain to build an ExpectationResult", function() {
|
|
|
|
var actual = ['a','b','c'];
|
|
|
|
var matcher = match(actual);
|
|
|
|
var expected = 'x';
|
|
|
|
matcher.toContain(expected);
|
|
|
|
|
2009-12-24 22:01:13 +00:00
|
|
|
var result = lastResult();
|
2009-10-30 00:03:24 +00:00
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toContain");
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
expect(result.message).toMatch(jasmine.pp(actual));
|
|
|
|
expect(result.message).toMatch('contain');
|
|
|
|
expect(result.message).toMatch(jasmine.pp(expected));
|
|
|
|
expect(result.actual).toEqual(actual);
|
2009-10-30 03:33:01 +00:00
|
|
|
expect(result.expected).toEqual(expected);
|
2009-10-30 00:03:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("toNotContain to build an ExpectationResult", function() {
|
|
|
|
var actual = ['a','b','c'];
|
|
|
|
var matcher = match(actual);
|
|
|
|
var expected = 'b';
|
|
|
|
matcher.toNotContain(expected);
|
|
|
|
|
2009-12-24 22:01:13 +00:00
|
|
|
var result = lastResult();
|
2009-10-30 00:03:24 +00:00
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toNotContain");
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
expect(result.message).toMatch(jasmine.pp(actual));
|
|
|
|
expect(result.message).toMatch('not contain');
|
|
|
|
expect(result.message).toMatch(jasmine.pp(expected));
|
|
|
|
expect(result.actual).toEqual(actual);
|
2009-10-30 03:33:01 +00:00
|
|
|
expect(result.expected).toEqual(expected);
|
2009-05-29 03:02:15 +00:00
|
|
|
});
|
|
|
|
|
2009-08-14 22:39:28 +00:00
|
|
|
it("toBeLessThan should pass if actual is less than expected", function() {
|
|
|
|
expect(match(37).toBeLessThan(42)).toEqual(true);
|
|
|
|
expect(match(37).toBeLessThan(-42)).toEqual(false);
|
|
|
|
expect(match(37).toBeLessThan(37)).toEqual(false);
|
|
|
|
});
|
|
|
|
|
2009-10-30 00:03:24 +00:00
|
|
|
it("toBeLessThan to build an ExpectationResult", function() {
|
|
|
|
var actual = 3;
|
|
|
|
var matcher = match(actual);
|
|
|
|
var expected = 1;
|
|
|
|
matcher.toBeLessThan(expected);
|
|
|
|
|
2009-12-24 22:01:13 +00:00
|
|
|
var result = lastResult();
|
2009-10-30 00:03:24 +00:00
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toBeLessThan");
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
expect(result.message).toMatch(jasmine.pp(actual) + ' to be less than');
|
|
|
|
expect(result.message).toMatch(jasmine.pp(expected));
|
|
|
|
expect(result.actual).toEqual(actual);
|
2009-10-30 03:33:01 +00:00
|
|
|
expect(result.expected).toEqual(expected);
|
2009-10-30 00:03:24 +00:00
|
|
|
});
|
|
|
|
|
2009-08-14 22:39:28 +00:00
|
|
|
it("toBeGreaterThan should pass if actual is greater than expected", function() {
|
|
|
|
expect(match(37).toBeGreaterThan(42)).toEqual(false);
|
|
|
|
expect(match(37).toBeGreaterThan(-42)).toEqual(true);
|
|
|
|
expect(match(37).toBeGreaterThan(37)).toEqual(false);
|
|
|
|
});
|
|
|
|
|
2009-10-30 00:03:24 +00:00
|
|
|
it("toBeGreaterThan to build an ExpectationResult", function() {
|
|
|
|
var actual = 1;
|
|
|
|
var matcher = match(actual);
|
|
|
|
var expected = 3;
|
|
|
|
matcher.toBeGreaterThan(expected);
|
|
|
|
|
2009-12-24 22:01:13 +00:00
|
|
|
var result = lastResult();
|
2009-10-30 00:03:24 +00:00
|
|
|
|
|
|
|
expect(result.matcherName).toEqual("toBeGreaterThan");
|
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
expect(result.message).toMatch(jasmine.pp(actual) + ' to be greater than');
|
|
|
|
expect(result.message).toMatch(jasmine.pp(expected));
|
|
|
|
expect(result.actual).toEqual(actual);
|
2009-10-30 03:33:01 +00:00
|
|
|
expect(result.expected).toEqual(expected);
|
2009-10-30 00:03:24 +00:00
|
|
|
});
|
|
|
|
|
2009-05-29 03:02:15 +00:00
|
|
|
it("toThrow", function() {
|
2009-11-13 19:32:08 +00:00
|
|
|
var expected = match(function() {
|
2009-05-29 03:02:15 +00:00
|
|
|
throw new Error("Fake Error");
|
2009-11-13 19:32:08 +00:00
|
|
|
});
|
2009-05-29 03:02:15 +00:00
|
|
|
expect(expected.toThrow()).toEqual(true);
|
|
|
|
expect(expected.toThrow("Fake Error")).toEqual(true);
|
|
|
|
expect(expected.toThrow(new Error("Fake Error"))).toEqual(true);
|
|
|
|
|
2009-10-30 00:03:24 +00:00
|
|
|
expect(expected.toThrow("Other Error")).toEqual(false);
|
2009-12-24 22:01:13 +00:00
|
|
|
var result = lastResult();
|
2009-10-30 00:03:24 +00:00
|
|
|
expect(result.message).toMatch("Other Error");
|
2009-05-29 03:02:15 +00:00
|
|
|
|
2009-10-30 00:03:24 +00:00
|
|
|
expect(expected.toThrow(new Error("Other Error"))).toEqual(false);
|
2009-12-24 22:01:13 +00:00
|
|
|
result = lastResult();
|
2009-10-30 00:03:24 +00:00
|
|
|
expect(result.message).toMatch("Other Error");
|
2009-05-29 03:02:15 +00:00
|
|
|
|
2009-10-30 00:03:24 +00:00
|
|
|
var exception;
|
|
|
|
try {
|
2009-10-30 03:33:01 +00:00
|
|
|
(function () {
|
2009-12-24 22:01:13 +00:00
|
|
|
new jasmine.Matchers(env, 'not-a-function', spec).toThrow();
|
2009-10-30 03:33:01 +00:00
|
|
|
})();
|
2009-10-30 00:03:24 +00:00
|
|
|
} catch (e) {
|
|
|
|
exception = e;
|
2009-10-30 03:33:01 +00:00
|
|
|
}
|
2009-11-12 17:47:44 +00:00
|
|
|
|
2009-10-30 00:03:24 +00:00
|
|
|
expect(exception).toBeDefined();
|
|
|
|
expect(exception.message).toEqual('Actual is not a function');
|
2009-05-29 03:02:15 +00:00
|
|
|
|
|
|
|
|
2009-10-30 00:03:24 +00:00
|
|
|
expect(match(function() {
|
|
|
|
}).toThrow()).toEqual(false);
|
2009-12-24 22:01:13 +00:00
|
|
|
result = lastResult();
|
2009-10-30 00:03:24 +00:00
|
|
|
expect(result.message).toEqual('Expected function to throw an exception.');
|
2009-12-24 22:01:13 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe(".not.matcher", function() {
|
|
|
|
it("should invert the sense of any matcher", function() {
|
|
|
|
expect(match(37).not.toBeGreaterThan(42)).toEqual(true);
|
|
|
|
expect(match(42).not.toBeGreaterThan(37)).toEqual(false);
|
|
|
|
expect(match("abc").not.toEqual("def")).toEqual(true);
|
|
|
|
expect(match("abc").not.toEqual("abc")).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should provide an inverted default message", function() {
|
|
|
|
match(37).not.toBeGreaterThan(42);
|
|
|
|
expect(lastResult().message).toEqual("Passed.");
|
|
|
|
|
|
|
|
match(42).not.toBeGreaterThan(37);
|
|
|
|
expect(lastResult().message).toEqual("Expected 42 not to be greater than 37.");
|
|
|
|
});
|
2009-05-29 03:02:15 +00:00
|
|
|
|
2009-12-24 22:01:13 +00:00
|
|
|
it("should use the second message when the matcher sets an array of custom messages", function() {
|
|
|
|
spec.addMatchers({
|
|
|
|
custom: function() {
|
|
|
|
this.message = function() {
|
|
|
|
return ['Expected it was called.', 'Expected it wasn\'t called.'];
|
|
|
|
};
|
|
|
|
return this.actual;
|
|
|
|
}
|
|
|
|
});
|
2009-05-29 03:02:15 +00:00
|
|
|
|
2009-12-24 22:01:13 +00:00
|
|
|
match(true).custom();
|
|
|
|
expect(lastResult().message).toEqual("Passed.");
|
|
|
|
match(false).custom();
|
|
|
|
expect(lastResult().message).toEqual("Expected it was called.");
|
|
|
|
match(true).not.custom();
|
|
|
|
expect(lastResult().message).toEqual("Expected it wasn't called.");
|
|
|
|
match(false).not.custom();
|
|
|
|
expect(lastResult().message).toEqual("Passed.");
|
|
|
|
});
|
2009-05-29 03:02:15 +00:00
|
|
|
});
|
|
|
|
|
2009-11-13 17:40:04 +00:00
|
|
|
describe("spy matchers >>", function() {
|
2009-10-30 00:03:24 +00:00
|
|
|
var TestClass;
|
|
|
|
beforeEach(function() {
|
2009-11-13 17:21:34 +00:00
|
|
|
TestClass = {
|
|
|
|
normalFunction: function() {
|
|
|
|
},
|
|
|
|
spyFunction: jasmine.createSpy("My spy")
|
|
|
|
};
|
2009-10-30 00:03:24 +00:00
|
|
|
});
|
2009-11-12 17:47:44 +00:00
|
|
|
|
2009-11-13 17:40:04 +00:00
|
|
|
function shouldThrowAnExceptionWhenInvokedOnANonSpy(methodName) {
|
|
|
|
return function() {
|
|
|
|
expect(function() {
|
|
|
|
match(TestClass.normalFunction)[methodName]();
|
|
|
|
}).toThrow('Expected a spy, but got Function.');
|
|
|
|
|
|
|
|
expect(function() {
|
2009-11-26 16:12:06 +00:00
|
|
|
match(jasmine.undefined)[methodName]();
|
2009-11-13 17:40:04 +00:00
|
|
|
}).toThrow('Expected a spy, but got undefined.');
|
|
|
|
|
|
|
|
expect(function() {
|
|
|
|
match({some:'object'})[methodName]();
|
|
|
|
}).toThrow('Expected a spy, but got { some : \'object\' }.');
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-11-13 17:30:22 +00:00
|
|
|
describe("wasCalled", function() {
|
|
|
|
it("should pass iff the spy was called", function() {
|
|
|
|
expect(match(TestClass.spyFunction).wasCalled()).toEqual(false);
|
2009-11-12 17:47:44 +00:00
|
|
|
|
2009-11-13 17:30:22 +00:00
|
|
|
TestClass.spyFunction();
|
|
|
|
expect(match(TestClass.spyFunction).wasCalled()).toEqual(true);
|
|
|
|
});
|
2009-10-30 03:33:01 +00:00
|
|
|
|
2009-11-13 17:30:22 +00:00
|
|
|
it("should throw an exception when invoked with any arguments", function() {
|
2009-11-12 17:47:44 +00:00
|
|
|
expect(function() {
|
2009-11-13 17:30:22 +00:00
|
|
|
match(TestClass.normalFunction).wasCalled("unwanted argument");
|
|
|
|
}).toThrow('wasCalled does not take arguments, use wasCalledWith');
|
|
|
|
});
|
2009-11-12 17:47:44 +00:00
|
|
|
|
2009-11-13 17:40:04 +00:00
|
|
|
it('should throw an exception when invoked on a non-spy', shouldThrowAnExceptionWhenInvokedOnANonSpy('wasCalled'));
|
2009-10-30 00:03:24 +00:00
|
|
|
});
|
2009-05-29 03:02:15 +00:00
|
|
|
|
2009-11-13 17:30:22 +00:00
|
|
|
describe("wasNotCalled", function() {
|
|
|
|
it("should pass iff the spy was not called", function() {
|
|
|
|
expect(match(TestClass.spyFunction).wasNotCalled()).toEqual(true);
|
2009-11-13 17:21:34 +00:00
|
|
|
|
2009-11-13 17:30:22 +00:00
|
|
|
TestClass.spyFunction();
|
|
|
|
expect(match(TestClass.spyFunction).wasNotCalled()).toEqual(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should throw an exception when invoked with any arguments", function() {
|
|
|
|
expect(function() {
|
|
|
|
match(TestClass.normalFunction).wasNotCalled("unwanted argument");
|
|
|
|
}).toThrow('wasNotCalled does not take arguments');
|
|
|
|
});
|
|
|
|
|
2009-11-13 17:40:04 +00:00
|
|
|
it('should throw an exception when invoked on a non-spy', shouldThrowAnExceptionWhenInvokedOnANonSpy('wasNotCalled'));
|
2009-11-13 17:30:22 +00:00
|
|
|
});
|
2009-10-30 03:33:01 +00:00
|
|
|
|
2009-11-13 17:30:22 +00:00
|
|
|
describe("wasCalledWith", function() {
|
|
|
|
it('wasCalledWith should return true if it was called with the expected args', function() {
|
|
|
|
TestClass.spyFunction('a', 'b', 'c');
|
|
|
|
expect(match(TestClass.spyFunction).wasCalledWith('a', 'b', 'c')).toEqual(true);
|
2009-10-30 03:33:01 +00:00
|
|
|
});
|
|
|
|
|
2009-11-13 17:30:22 +00:00
|
|
|
it('should return false if it was not called with the expected args', function() {
|
|
|
|
TestClass.spyFunction('a', 'b', 'c');
|
|
|
|
var expected = match(TestClass.spyFunction);
|
|
|
|
expect(expected.wasCalledWith('c', 'b', 'a')).toEqual(false);
|
2009-12-24 22:01:13 +00:00
|
|
|
var result = lastResult();
|
2009-11-13 17:30:22 +00:00
|
|
|
expect(result.passed()).toEqual(false);
|
|
|
|
expect(result.expected).toEqual(['c', 'b', 'a']);
|
|
|
|
expect(result.actual.mostRecentCall.args).toEqual(['a', 'b', 'c']);
|
|
|
|
});
|
2009-11-13 17:21:34 +00:00
|
|
|
|
2009-11-13 17:30:22 +00:00
|
|
|
it('should allow matches across multiple calls', function() {
|
|
|
|
var expected = match(TestClass.spyFunction);
|
|
|
|
TestClass.spyFunction('a', 'b', 'c');
|
|
|
|
TestClass.spyFunction('d', 'e', 'f');
|
|
|
|
expect(expected.wasCalledWith('a', 'b', 'c')).toEqual(true);
|
|
|
|
expect(expected.wasCalledWith('d', 'e', 'f')).toEqual(true);
|
|
|
|
expect(expected.wasCalledWith('x', 'y', 'z')).toEqual(false);
|
|
|
|
});
|
2009-10-30 03:33:01 +00:00
|
|
|
|
2009-11-13 17:40:04 +00:00
|
|
|
it('should throw an exception when invoked on a non-spy', shouldThrowAnExceptionWhenInvokedOnANonSpy('wasCalledWith'));
|
2009-10-30 03:33:01 +00:00
|
|
|
|
2009-11-13 17:30:22 +00:00
|
|
|
describe("to build an ExpectationResult", function () {
|
|
|
|
beforeEach(function() {
|
|
|
|
var currentSuite;
|
|
|
|
var spec;
|
|
|
|
currentSuite = env.describe('default current suite', function() {
|
|
|
|
spec = env.it();
|
|
|
|
}, spec);
|
|
|
|
TestClass = { someFunction: function(a, b) {
|
|
|
|
} };
|
|
|
|
spec.spyOn(TestClass, 'someFunction');
|
2009-11-12 20:03:55 +00:00
|
|
|
});
|
|
|
|
|
2009-11-13 17:30:22 +00:00
|
|
|
it("should should handle the case of a spy", function() {
|
|
|
|
TestClass.someFunction('a', 'c');
|
|
|
|
var matcher = match(TestClass.someFunction);
|
|
|
|
matcher.wasCalledWith('a', 'b');
|
|
|
|
|
2009-12-24 22:01:13 +00:00
|
|
|
var result = lastResult();
|
2009-11-13 17:30:22 +00:00
|
|
|
expect(result.matcherName).toEqual("wasCalledWith");
|
2009-11-12 20:03:55 +00:00
|
|
|
expect(result.passed()).toEqual(false);
|
2009-11-13 17:30:22 +00:00
|
|
|
expect(result.message).toMatch("['a', 'b']");
|
|
|
|
expect(result.message).toMatch("['a', 'c']");
|
|
|
|
expect(result.actual).toEqual(TestClass.someFunction);
|
|
|
|
expect(result.expected).toEqual(['a','b']);
|
2009-11-12 20:03:55 +00:00
|
|
|
});
|
2009-10-30 03:33:01 +00:00
|
|
|
});
|
2009-10-30 00:03:24 +00:00
|
|
|
});
|
|
|
|
});
|
2009-11-12 17:47:44 +00:00
|
|
|
});
|