This repository has been archived by the owner on Jan 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
124 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
var _ = require('underscore'); | ||
|
||
exports.PropertyMatcher = function (config) { | ||
this.callCount = 0; | ||
this.matches = function (body) { | ||
var matchesProperties = function (object, config) { | ||
var matchesAllProperties = true; | ||
_.forEach(config['properties'], function (value, key) { | ||
matchesAllProperties = matchesAllProperties && object[key] === value; | ||
}); | ||
return matchesAllProperties; | ||
}; | ||
if (_.isArray(body)) { | ||
var matchesAnyItem = false; | ||
_.forEach(body, function (item) { | ||
if (matchesProperties(item, config) === true) { | ||
matchesAnyItem = true; | ||
} | ||
}); | ||
return matchesAnyItem; | ||
} | ||
else { | ||
return matchesProperties(body, config); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
var PropertyMatcher = require('../lib/PropertyMatcher.js').PropertyMatcher; | ||
|
||
describe('PropertyMatcher', function () { | ||
var matcher; | ||
var matchingObject = { | ||
foo: 'bar', | ||
random: 'noise', | ||
ww: 'heisenberg' | ||
}; | ||
var nonMatchingObject = { | ||
random: 'noise' | ||
}; | ||
|
||
describe('when constructed', function () { | ||
beforeEach(function () { | ||
matcher = new PropertyMatcher({ | ||
matcherType: 'property', | ||
properties: { | ||
foo: 'bar' | ||
} | ||
}); | ||
}); | ||
it('should have callCount of 0', function () { | ||
expect(matcher.callCount).toEqual(0); | ||
}); | ||
}); | ||
|
||
describe('when configured to match single property', function () { | ||
beforeEach(function () { | ||
matcher = new PropertyMatcher({ | ||
matcherType: 'property', | ||
properties: { | ||
foo: 'bar' | ||
} | ||
}); | ||
}); | ||
it('should match array containing matching object', function () { | ||
expect(matcher.matches([ matchingObject, nonMatchingObject ])).toBeTruthy(); | ||
}); | ||
it("should not match array that doesn't contain matching object", function () { | ||
expect(matcher.matches([ nonMatchingObject ])).toBeFalsy(); | ||
}); | ||
it('should match matching object', function () { | ||
expect(matcher.matches(matchingObject)).toBeTruthy(); | ||
}); | ||
it('should not match non-matching object', function () { | ||
expect(matcher.matches(nonMatchingObject)).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
describe('when configured to match multiple properties', function () { | ||
beforeEach(function () { | ||
matcher = new PropertyMatcher({ | ||
matcherType: 'property', | ||
properties: { | ||
foo: 'bar', | ||
ww: 'heisenberg' | ||
} | ||
}); | ||
}); | ||
it('should only match objects containing all matcher properties', function () { | ||
expect(matcher.matches({foo: 'bar'})).toBeFalsy(); | ||
expect(matcher.matches({foo: 'bar', ww: 'heisenberg'})).toBeTruthy(); | ||
expect(matcher.matches({foo: 'bar', ww: 'heisenberg', noise: true})).toBeTruthy(); | ||
expect(matcher.matches([ | ||
{foo: 'bar', ww: 'heisenberg'} | ||
])).toBeTruthy(); // yes, arrays too | ||
}); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
var RequestMatchers = require('../lib/RequestMatchers'); | ||
var PropertyMatcher = require('../lib/PropertyMatcher').PropertyMatcher; | ||
|
||
describe('RequestMatchers', function () { | ||
describe('createMatcher', function () { | ||
describe('when given configuration with matcherType "property"', function () { | ||
it('should return instance of PropertyMatcher', function () { | ||
var matcher = RequestMatchers.createMatcher({ | ||
matcherType: 'property', | ||
properties: { | ||
foo: 'bar' | ||
} | ||
}); | ||
expect(matcher instanceof PropertyMatcher).toBeTruthy(); | ||
}); | ||
}); | ||
describe('when given configuration with unknown matcherType', function () { | ||
it('should return null', function () { | ||
var matcher = RequestMatchers.createMatcher({ | ||
matcherType: 'unknown' | ||
}); | ||
expect(matcher).toBe(null); | ||
}); | ||
}); | ||
}); | ||
}); |