Skip to content

Commit

Permalink
Add ability to update ExpectationResults after creating them.
Browse files Browse the repository at this point in the history
This is to support chained matcher functions. A chain
of matcher functions needs to produce a single result.
So when a chained matcher executes, it needs to update
the message and pass/fail status of an existing expectation
result.

Three new methods have been added:
- ExpectationResult#update. 
- NestedResults#updateResult
- Spec#updateExpectationResult

The last of these functions will be called from inside
chained matcher functions, instead of #addExpectationResult.
  • Loading branch information
maxbrunsfeld committed Mar 3, 2012
1 parent c87cf71 commit 59bdf0d
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 5 deletions.
47 changes: 47 additions & 0 deletions spec/core/BaseSpec.js
Expand Up @@ -24,4 +24,51 @@ describe("base.js", function() {
expect(jasmine.getGlobal()).toBe(globalObject);
});
});

describe("jasmine.ExpectationResult", function() {
var result;

beforeEach(function() {
result = new jasmine.ExpectationResult({
passed: true,
message: "some message"
});
});

describe("#update", function() {
it("updates the passing status", function() {
result.update({ passed: false });
expect(result.passed()).toBeFalsy();
});

describe("when the result is passing", function() {
it("sets the message to 'Passed.'", function() {
result.update({
passed: true,
message: "some message"
});

expect(result.message).toBe("Passed.");
});
});

describe("when the result is failing", function() {
beforeEach(function() {
result.update({
passed: false,
message: "some message"
});
});

it("updates the message", function() {
expect(result.message).toBe("some message");
});

it("creates a stack trace with the message", function() {
expect(result.trace instanceof Error).toBeTruthy();
expect(result.trace.message).toBe("some message");
});
});
});
});
});
52 changes: 52 additions & 0 deletions spec/core/NestedResultsSpec.js
Expand Up @@ -51,4 +51,56 @@ describe('jasmine.NestedResults', function() {
expect(branchResults.failedCount).toEqual(2);
});

describe("#updateResult", function() {
var results, result1, result2;

beforeEach(function() {
results = new jasmine.NestedResults();
result1 = new jasmine.ExpectationResult({
passed: true,
message: "Passed."
});
result2 = new jasmine.ExpectationResult({
passed: false,
message: "fail."
});

results.addResult(result1);
results.addResult(result2);
});

describe("when a result that was passing is updated to fail", function() {
beforeEach(function() {
results.updateResult(result1, { passed: false, message: "nope. failed." });
});

it("increments the failed count and decrements the passed count", function() {
expect(results.totalCount).toEqual(2);
expect(results.passedCount).toEqual(0);
expect(results.failedCount).toEqual(2);
});

it("updates the message and passing status of the result", function() {
expect(result1.passed()).toBeFalsy();
expect(result1.message).toBe("nope. failed.");
});
});

describe("when a result that was failing is updated to pass", function() {
beforeEach(function() {
results.updateResult(result2, { passed: true });
});

it("increments the failed count and decrements the passed count", function() {
expect(results.totalCount).toEqual(2);
expect(results.passedCount).toEqual(2);
expect(results.failedCount).toEqual(0);
});

it("updates the message and passing status of the result", function() {
expect(result2.passed()).toBeTruthy();
expect(result2.message).toBe("Passed.");
});
});
});
});
18 changes: 18 additions & 0 deletions src/core/NestedResults.js
Expand Up @@ -72,6 +72,24 @@ jasmine.NestedResults.prototype.addResult = function(result) {
this.items_.push(result);
};

/**
* Updates a result, tracking counts (passed & failed)
* @param {jasmine.ExpectationResult} result
* @param {Object} params
*/
jasmine.NestedResults.prototype.updateResult = function(result, params) {
var wasPassing = result.passed_,
isPassing = params.passed;
if (wasPassing && !isPassing) {
this.passedCount--;
this.failedCount++;
} else if (isPassing && !wasPassing) {
this.passedCount++;
this.failedCount--;
}
result.update(params);
};

/**
* @returns {Boolean} True if <b>everything</b> below passed
*/
Expand Down
4 changes: 4 additions & 0 deletions src/core/Spec.js
Expand Up @@ -67,6 +67,10 @@ jasmine.Spec.prototype.addMatcherResult = function(result) {
this.results_.addResult(result);
};

jasmine.Spec.prototype.updateMatcherResult = function(result, params) {
this.results_.updateResult(result, params);
};

jasmine.Spec.prototype.expect = function(actual) {
var positive = new (this.getMatchersClass_())(this.env, actual, this);
positive.not = new (this.getMatchersClass_())(this.env, actual, this, true);
Expand Down
19 changes: 14 additions & 5 deletions src/core/base.js
Expand Up @@ -94,13 +94,9 @@ jasmine.MessageResult.prototype.toString = function() {
jasmine.ExpectationResult = function(params) {
this.type = 'expect';
this.matcherName = params.matcherName;
this.passed_ = params.passed;
this.expected = params.expected;
this.actual = params.actual;
this.message = this.passed_ ? 'Passed.' : params.message;

var trace = (params.trace || new Error(this.message));
this.trace = this.passed_ ? '' : trace;
this.update(params);
};

jasmine.ExpectationResult.prototype.toString = function () {
Expand All @@ -111,6 +107,19 @@ jasmine.ExpectationResult.prototype.passed = function () {
return this.passed_;
};

jasmine.ExpectationResult.prototype.update = function(params) {
this.passed_ = params.passed;

if (this.passed_) {
this.message = "Passed.";
this.trace = "";
} else {
this.message = params.message;
this.trace = params.trace || new Error(this.message);
}
};


/**
* Getter for the Jasmine environment. Ensures one gets created
*/
Expand Down

0 comments on commit 59bdf0d

Please sign in to comment.