Skip to content
This repository has been archived by the owner on Jan 7, 2020. It is now read-only.

Commit

Permalink
Added tests for matchers.
Browse files Browse the repository at this point in the history
  • Loading branch information
SQiShER committed Sep 4, 2013
1 parent 3d7dcf9 commit 38de72e
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 26 deletions.
26 changes: 26 additions & 0 deletions lib/PropertyMatcher.js
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);
}
}
};
27 changes: 1 addition & 26 deletions lib/RequestMatchers.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,4 @@
var _ = require('underscore');

var 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);
}
}
};
var PropertyMatcher = require('./PropertyMatcher').PropertyMatcher;

var createMatcher = function (matcherConfig) {
if (matcherConfig['matcherType'] === 'property') {
Expand Down
71 changes: 71 additions & 0 deletions spec/PropertyMatcher.spec.js
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
});
});

});
26 changes: 26 additions & 0 deletions spec/RequestMatchers.spec.js
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);
});
});
});
});

0 comments on commit 38de72e

Please sign in to comment.