Made ExpectationFilterChain immutable
This commit is contained in:
@@ -1,4 +1,32 @@
|
|||||||
describe('ExpectationFilterChainSpec', function() {
|
describe('ExpectationFilterChain', function() {
|
||||||
|
describe('#addFilter', function() {
|
||||||
|
it('returns a new filter chain with the added filter', function() {
|
||||||
|
var first = jasmine.createSpy('first'),
|
||||||
|
second = jasmine.createSpy('second'),
|
||||||
|
orig = new jasmineUnderTest.ExpectationFilterChain({
|
||||||
|
modifyFailureMessage: first
|
||||||
|
}),
|
||||||
|
added = orig.addFilter({selectComparisonFunc: second});
|
||||||
|
|
||||||
|
added.modifyFailureMessage();
|
||||||
|
expect(first).toHaveBeenCalled();
|
||||||
|
debugger;
|
||||||
|
added.selectComparisonFunc();
|
||||||
|
expect(second).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not modify the original filter chain', function() {
|
||||||
|
var orig = new jasmineUnderTest.ExpectationFilterChain({}),
|
||||||
|
f = jasmine.createSpy('f');
|
||||||
|
|
||||||
|
|
||||||
|
orig.addFilter({selectComparisonFunc: f});
|
||||||
|
|
||||||
|
orig.selectComparisonFunc();
|
||||||
|
expect(f).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('#selectComparisonFunc', function() {
|
describe('#selectComparisonFunc', function() {
|
||||||
describe('When no filters have #selectComparisonFunc', function() {
|
describe('When no filters have #selectComparisonFunc', function() {
|
||||||
it('returns undefined', function() {
|
it('returns undefined', function() {
|
||||||
@@ -10,14 +38,14 @@ describe('ExpectationFilterChainSpec', function() {
|
|||||||
|
|
||||||
describe('When some filters have #selectComparisonFunc', function() {
|
describe('When some filters have #selectComparisonFunc', function() {
|
||||||
it('calls the first filter that has #selectComparisonFunc', function() {
|
it('calls the first filter that has #selectComparisonFunc', function() {
|
||||||
var chain = new jasmineUnderTest.ExpectationFilterChain(),
|
var first = jasmine.createSpy('first').and.returnValue('first'),
|
||||||
first = jasmine.createSpy('first').and.returnValue('first'),
|
|
||||||
second = jasmine.createSpy('second').and.returnValue('second'),
|
second = jasmine.createSpy('second').and.returnValue('second'),
|
||||||
|
chain = new jasmineUnderTest.ExpectationFilterChain()
|
||||||
|
.addFilter({selectComparisonFunc: first})
|
||||||
|
.addFilter({selectComparisonFunc: second}),
|
||||||
matcher = {},
|
matcher = {},
|
||||||
result;
|
result;
|
||||||
|
|
||||||
chain.addFilter({selectComparisonFunc: first});
|
|
||||||
chain.addFilter({selectComparisonFunc: second});
|
|
||||||
result = chain.selectComparisonFunc(matcher);
|
result = chain.selectComparisonFunc(matcher);
|
||||||
|
|
||||||
expect(first).toHaveBeenCalledWith(matcher);
|
expect(first).toHaveBeenCalledWith(matcher);
|
||||||
@@ -38,17 +66,17 @@ describe('ExpectationFilterChainSpec', function() {
|
|||||||
|
|
||||||
describe('When some filters have #buildFailureMessage', function() {
|
describe('When some filters have #buildFailureMessage', function() {
|
||||||
it('calls the first filter that has #buildFailureMessage', function() {
|
it('calls the first filter that has #buildFailureMessage', function() {
|
||||||
var chain = new jasmineUnderTest.ExpectationFilterChain(),
|
var first = jasmine.createSpy('first').and.returnValue('first'),
|
||||||
first = jasmine.createSpy('first').and.returnValue('first'),
|
|
||||||
second = jasmine.createSpy('second').and.returnValue('second'),
|
second = jasmine.createSpy('second').and.returnValue('second'),
|
||||||
|
chain = new jasmineUnderTest.ExpectationFilterChain()
|
||||||
|
.addFilter({buildFailureMessage: first})
|
||||||
|
.addFilter({buildFailureMessage: second}),
|
||||||
matcherResult = {pass: false},
|
matcherResult = {pass: false},
|
||||||
matcherName = 'foo',
|
matcherName = 'foo',
|
||||||
args = [],
|
args = [],
|
||||||
util = {},
|
util = {},
|
||||||
result;
|
result;
|
||||||
|
|
||||||
chain.addFilter({buildFailureMessage: first});
|
|
||||||
chain.addFilter({buildFailureMessage: second});
|
|
||||||
result = chain.buildFailureMessage(matcherResult, matcherName, args, util);
|
result = chain.buildFailureMessage(matcherResult, matcherName, args, util);
|
||||||
|
|
||||||
expect(first).toHaveBeenCalledWith(matcherResult, matcherName, args, util);
|
expect(first).toHaveBeenCalledWith(matcherResult, matcherName, args, util);
|
||||||
@@ -60,19 +88,20 @@ describe('ExpectationFilterChainSpec', function() {
|
|||||||
|
|
||||||
describe('#modifyFailureMessage', function() {
|
describe('#modifyFailureMessage', function() {
|
||||||
it('calls each filter with the return value of the next previously added filter', function() {
|
it('calls each filter with the return value of the next previously added filter', function() {
|
||||||
var chain = new jasmineUnderTest.ExpectationFilterChain(),
|
var first = jasmine.createSpy('first').and.returnValue('first'),
|
||||||
first = jasmine.createSpy('first').and.returnValue('first'),
|
|
||||||
third = jasmine.createSpy('third').and.returnValue('third'),
|
third = jasmine.createSpy('third').and.returnValue('third'),
|
||||||
|
chain = new jasmineUnderTest.ExpectationFilterChain()
|
||||||
|
.addFilter({modifyFailureMessage: first})
|
||||||
|
.addFilter({})
|
||||||
|
.addFilter({modifyFailureMessage: third}),
|
||||||
result;
|
result;
|
||||||
|
|
||||||
chain.addFilter({modifyFailureMessage: first});
|
debugger;
|
||||||
chain.addFilter({})
|
|
||||||
chain.addFilter({modifyFailureMessage: third});
|
|
||||||
result = chain.modifyFailureMessage('original');
|
result = chain.modifyFailureMessage('original');
|
||||||
|
|
||||||
expect(third).toHaveBeenCalledWith('original');
|
expect(first).toHaveBeenCalledWith('original');
|
||||||
expect(first).toHaveBeenCalledWith('third');
|
expect(third).toHaveBeenCalledWith('first');
|
||||||
expect(result).toEqual('first');
|
expect(result).toEqual('third');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,42 +1,52 @@
|
|||||||
getJasmineRequireObj().ExpectationFilterChain = function() {
|
getJasmineRequireObj().ExpectationFilterChain = function() {
|
||||||
function ExpectationFilterChain() {
|
function ExpectationFilterChain(maybeFilter, prev) {
|
||||||
this.filters_ = [];
|
this.filter_ = maybeFilter;
|
||||||
|
this.prev_ = prev;
|
||||||
}
|
}
|
||||||
|
|
||||||
ExpectationFilterChain.prototype.addFilter = function(filter) {
|
ExpectationFilterChain.prototype.addFilter = function(filter) {
|
||||||
this.filters_.push(filter);
|
return new ExpectationFilterChain(filter, this);
|
||||||
};
|
};
|
||||||
|
|
||||||
ExpectationFilterChain.prototype.selectComparisonFunc = function(matcher) {
|
ExpectationFilterChain.prototype.selectComparisonFunc = function(matcher) {
|
||||||
return this.callFirst_('selectComparisonFunc', arguments);
|
return this.callFirst_('selectComparisonFunc', arguments).result;
|
||||||
};
|
};
|
||||||
|
|
||||||
ExpectationFilterChain.prototype.buildFailureMessage = function(result, matcherName, args, util) {
|
ExpectationFilterChain.prototype.buildFailureMessage = function(result, matcherName, args, util) {
|
||||||
return this.callFirst_('buildFailureMessage', arguments);
|
return this.callFirst_('buildFailureMessage', arguments).result;
|
||||||
};
|
};
|
||||||
|
|
||||||
ExpectationFilterChain.prototype.modifyFailureMessage = function(msg) {
|
ExpectationFilterChain.prototype.modifyFailureMessage = function(msg) {
|
||||||
var i;
|
if (this.prev_) {
|
||||||
|
msg = this.prev_.modifyFailureMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
for (i = this.filters_.length - 1; i >= 0; i--) {
|
if (this.filter_ && this.filter_.modifyFailureMessage) {
|
||||||
if (this.filters_[i].modifyFailureMessage) {
|
msg = this.filter_.modifyFailureMessage(msg);
|
||||||
msg = this.filters_[i].modifyFailureMessage(msg);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return msg;
|
return msg;
|
||||||
};
|
};
|
||||||
|
|
||||||
ExpectationFilterChain.prototype.callFirst_ = function(fname, args) {
|
ExpectationFilterChain.prototype.callFirst_ = function(fname, args) {
|
||||||
var i, filter;
|
var prevResult;
|
||||||
|
|
||||||
for (i = 0; i < this.filters_.length; i++) {
|
if (this.prev_) {
|
||||||
filter = this.filters_[i];
|
prevResult = this.prev_.callFirst_(fname, args);
|
||||||
|
|
||||||
if (filter[fname]) {
|
if (prevResult.found) {
|
||||||
return filter[fname].apply(filter, args);
|
return prevResult;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.filter_ && this.filter_[fname]) {
|
||||||
|
return {
|
||||||
|
found: true,
|
||||||
|
result: this.filter_[fname].apply(this.filter_, args)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {found: false};
|
||||||
};
|
};
|
||||||
|
|
||||||
return ExpectationFilterChain;
|
return ExpectationFilterChain;
|
||||||
|
|||||||
Reference in New Issue
Block a user