Re-enable CustomMatchersSpec and update for current version of custom matchers

This commit is contained in:
Sheel Choksi
2013-10-20 21:58:16 -07:00
parent b6eb9a4d5e
commit 5f429fcb37
2 changed files with 128 additions and 96 deletions

View File

@@ -1,95 +1,127 @@
////TODO: matchers should be add-able to the env, not to the spec. describe("Custom Matchers (Integration)", function() {
//describe("Custom Matchers", function() { var env;
// var env; var fakeTimer;
// var fakeTimer;
// beforeEach(function() {
// beforeEach(function() { env = new j$.Env();
// env = new jasmine.Env(); });
// });
// it("allows adding more matchers local to a spec", function(done) {
// it("should be easy to add more matchers local to a spec, suite, etc.", function() { env.it('spec defining a custom matcher', function() {
// var spec1, spec2, spec1Matcher, spec2Matcher; env.addMatchers({
// var suite = env.describe('some suite', function() { matcherForSpec: function() {
// env.beforeEach(function() { return {
// this.addMatchers({ compare: function(actual, expected) {
// matcherForSuite: function(expected) { return { pass: false, message: "matcherForSpec: actual: " + actual + "; expected: " + expected };
// this.message = "matcherForSuite: actual: " + this.actual + "; expected: " + expected; }
// return true; }
// } }
// }); });
// });
// env.expect("zzz").matcherForSpec("yyy");
// spec1 = env.it('spec with an expectation').runs(function () { });
// this.addMatchers({
// matcherForSpec: function(expected) { env.it("spec without custom matcher defined", function() {
// this.message = "matcherForSpec: actual: " + this.actual + "; expected: " + expected; expect(env.expect("zzz").matcherForSpec).toBeUndefined();
// return true; });
// }
// }); var specDoneSpy = jasmine.createSpy("specDoneSpy");
// spec1Matcher = this.expect("xxx"); var expectations = function() {
// }); var firstSpecResult = specDoneSpy.calls.first().args[0];
// expect(firstSpecResult.status).toEqual("failed");
// spec2 = env.it('spec with failing expectation').runs(function () { expect(firstSpecResult.failedExpectations[0].message).toEqual("matcherForSpec: actual: zzz; expected: yyy");
// spec2Matcher = this.expect("yyy"); done();
// }); };
// }); env.addReporter({ specDone:specDoneSpy, jasmineDone: expectations});
//
// suite.execute(); env.execute();
// });
// spec1Matcher.matcherForSuite("expected");
// expect(spec1Matcher.message).toEqual("matcherForSuite: actual: xxx; expected: expected"); it("passes the spec if the custom matcher passes", function(done) {
// spec1Matcher.matcherForSpec("expected"); env.addMatchers({
// expect(spec1Matcher.message).toEqual("matcherForSpec: actual: xxx; expected: expected"); toBeReal: function() {
// return { compare: function() { return { pass: true }; } };
// spec2Matcher.matcherForSuite("expected"); }
// expect(spec2Matcher.message).toEqual("matcherForSuite: actual: yyy; expected: expected"); });
// expect(spec2Matcher.matcherForSpec).toBe(jasmine.undefined);
// }); env.it("spec using custom matcher", function() {
// env.expect(true).toBeReal();
// it("should generate messages with the same rules as for regular matchers when this.report() is not called", function() { });
// var spec;
// var suite = env.describe('some suite', function() { var specExpectations = function(result) {
// spec = env.it('spec with an expectation').runs(function () { expect(result.status).toEqual('passed');
// this.addMatchers({ };
// toBeTrue: function() {
// return this.actual === true; env.addReporter({ specDone: specExpectations, jasmineDone: done });
// } env.execute();
// }); });
// this.expect(true).toBeTrue();
// this.expect(false).toBeTrue(); it("generates messages with the same rules as built in matchers absent a custom message", function(done) {
// }); env.addMatchers({
// }); toBeReal: function() {
// return {
// suite.execute(); compare: function() {
// return { pass: false };
// var results = spec.results().getItems(); }
// expect(results[0].message).toEqual("Passed."); }
// expect(results[1].message).toEqual("Expected false to be true."); }
// }); });
//
// it("should pass args", function() { env.it('spec with an expectation', function() {
// var matcherCallArgs = []; env.expect("a").toBeReal();
// var spec; });
// var suite = env.describe('some suite', function() {
// spec = env.it('spec with an expectation').runs(function () { var specExpectations = function(result) {
// this.addMatchers({ expect(result.failedExpectations[0].message).toEqual("Expected 'a' to be real.");
// toBeTrue: function() { };
// matcherCallArgs.push(jasmine.util.argsToArray(arguments));
// return this.actual === true; env.addReporter({ specDone: specExpectations, jasmineDone: done });
// } env.execute();
// }); });
// this.expect(true).toBeTrue();
// this.expect(false).toBeTrue('arg'); it("passes the expected and actual arguments to the comparison function", function(done) {
// this.expect(true).toBeTrue('arg1', 'arg2'); var argumentSpy = jasmine.createSpy("argument spy").and.returnValue({ pass: true });
// }); env.addMatchers({
// }); toBeReal: function() {
// return { compare: argumentSpy };
// suite.execute(); }
// var results = spec.results().getItems(); });
// expect(results[0].expected).toEqual(jasmine.undefined);
// expect(results[1].expected).toEqual('arg'); env.it('spec with an expectation', function () {
// expect(results[2].expected).toEqual(['arg1', 'arg2']); env.expect(true).toBeReal();
// env.expect(true).toBeReal("arg");
// expect(matcherCallArgs).toEqual([[], ['arg'], ['arg1', 'arg2']]); env.expect(true).toBeReal("arg1", "arg2");
// }); });
//});
var specExpectations = function() {
expect(argumentSpy).toHaveBeenCalledWith(true);
expect(argumentSpy).toHaveBeenCalledWith(true, "arg");
expect(argumentSpy).toHaveBeenCalledWith(true, "arg1", "arg2");
};
env.addReporter({ specDone: specExpectations, jasmineDone: done });
env.execute();
});
it("passes the jasmine utility and current equality matchers to the expectation factory", function(done) {
var matcherFactory = function() { return { compare: function() { return {pass: true}; }}; },
argumentSpy = jasmine.createSpy("argument spy").and.returnValue(matcherFactory),
customEqualityFn = function() { return true; };
env.addCustomEqualityTester(customEqualityFn);
env.addMatchers({
toBeReal: argumentSpy
});
env.it("spec with expectation", function() {
env.expect(true).toBeReal();
});
var specExpectations = function() {
expect(argumentSpy).toHaveBeenCalledWith(j$.matchersUtil, [customEqualityFn]);
};
env.addReporter({ specDone: specExpectations, jasmineDone: done });
env.execute();
});
});

View File

@@ -169,7 +169,7 @@ getJasmineRequireObj().Env = function(j$) {
function specResultCallback(result) { function specResultCallback(result) {
removeAllSpies(); removeAllSpies();
j$.Expectation.resetMatchers(); j$.Expectation.resetMatchers();
customEqualityTesters.length = 0; customEqualityTesters = [];
self.currentSpec = null; self.currentSpec = null;
self.reporter.specDone(result); self.reporter.specDone(result);
} }