Skip to content

Commit

Permalink
feat(asymmetric): add asymmetric matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMason committed Aug 20, 2016
1 parent 6319e4d commit 7cc229b
Show file tree
Hide file tree
Showing 39 changed files with 1,206 additions and 486 deletions.
935 changes: 563 additions & 372 deletions dist/jasmine-matchers.js

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
'use strict';

module.exports = {
asymmetricMatcher: [{
name: 'after',
matcher: 'toBeAfter'
}, {
name: 'arrayOfBooleans',
matcher: 'toBeArrayOfBooleans'
}, {
name: 'arrayOfNumbers',
matcher: 'toBeArrayOfNumbers'
}, {
name: 'arrayOfObjects',
matcher: 'toBeArrayOfObjects'
}, {
name: 'arrayOfSize',
matcher: 'toBeArrayOfSize'
}, {
name: 'arrayOfStrings',
matcher: 'toBeArrayOfStrings'
}, {
name: 'before',
matcher: 'toBeBefore'
}, {
name: 'calculable',
matcher: 'toBeCalculable'
}, {
name: 'emptyArray',
matcher: 'toBeEmptyArray'
}, {
name: 'emptyObject',
matcher: 'toBeEmptyObject'
}, {
name: 'evenNumber',
matcher: 'toBeEvenNumber'
}, {
name: 'greaterThanOrEqualTo',
matcher: 'toBeGreaterThanOrEqualTo'
}, {
name: 'iso8601',
matcher: 'toBeIso8601'
}, {
name: 'jsonString',
matcher: 'toBeJsonString'
}, {
name: 'lessThanOrEqualTo',
matcher: 'toBeLessThanOrEqualTo'
}, {
name: 'longerThan',
matcher: 'toBeLongerThan'
}, {
name: 'nonEmptyArray',
matcher: 'toBeNonEmptyArray'
}, {
name: 'nonEmptyObject',
matcher: 'toBeNonEmptyObject'
}, {
name: 'nonEmptyString',
matcher: 'toBeNonEmptyString'
}, {
name: 'oddNumber',
matcher: 'toBeOddNumber'
}, {
name: 'sameLengthAs',
matcher: 'toBeSameLengthAs'
}, {
name: 'shorterThan',
matcher: 'toBeShorterThan'
}, {
name: 'whitespace',
matcher: 'toBeWhitespace'
}, {
name: 'wholeNumber',
matcher: 'toBeWholeNumber'
}, {
name: 'withinRange',
matcher: 'toBeWithinRange'
}, {
name: 'endingWith',
matcher: 'toEndWith'
}, {
name: 'startingWith',
matcher: 'toStartWith'
}],
matcher: {
toBeAfter: require('./toBeAfter'),
toBeArray: require('./toBeArray'),
toBeArrayOfBooleans: require('./toBeArrayOfBooleans'),
toBeArrayOfNumbers: require('./toBeArrayOfNumbers'),
toBeArrayOfObjects: require('./toBeArrayOfObjects'),
toBeArrayOfSize: require('./toBeArrayOfSize'),
toBeArrayOfStrings: require('./toBeArrayOfStrings'),
toBeBefore: require('./toBeBefore'),
toBeBoolean: require('./toBeBoolean'),
toBeCalculable: require('./toBeCalculable'),
toBeDate: require('./toBeDate'),
toBeEmptyArray: require('./toBeEmptyArray'),
toBeEmptyObject: require('./toBeEmptyObject'),
toBeEmptyString: require('./toBeEmptyString'),
toBeEvenNumber: require('./toBeEvenNumber'),
toBeFalse: require('./toBeFalse'),
toBeFunction: require('./toBeFunction'),
toBeGreaterThanOrEqualTo: require('./toBeGreaterThanOrEqualTo'),
toBeHtmlString: require('./toBeHtmlString'),
toBeIso8601: require('./toBeIso8601'),
toBeJsonString: require('./toBeJsonString'),
toBeLessThanOrEqualTo: require('./toBeLessThanOrEqualTo'),
toBeLongerThan: require('./toBeLongerThan'),
toBeNonEmptyArray: require('./toBeNonEmptyArray'),
toBeNonEmptyObject: require('./toBeNonEmptyObject'),
toBeNonEmptyString: require('./toBeNonEmptyString'),
toBeNumber: require('./toBeNumber'),
toBeObject: require('./toBeObject'),
toBeOddNumber: require('./toBeOddNumber'),
toBeSameLengthAs: require('./toBeSameLengthAs'),
toBeShorterThan: require('./toBeShorterThan'),
toBeString: require('./toBeString'),
toBeTrue: require('./toBeTrue'),
toBeWhitespace: require('./toBeWhitespace'),
toBeWholeNumber: require('./toBeWholeNumber'),
toBeWithinRange: require('./toBeWithinRange'),
toEndWith: require('./toEndWith'),
toHaveArray: require('./toHaveArray'),
toHaveArrayOfBooleans: require('./toHaveArrayOfBooleans'),
toHaveArrayOfNumbers: require('./toHaveArrayOfNumbers'),
toHaveArrayOfObjects: require('./toHaveArrayOfObjects'),
toHaveArrayOfSize: require('./toHaveArrayOfSize'),
toHaveArrayOfStrings: require('./toHaveArrayOfStrings'),
toHaveBoolean: require('./toHaveBoolean'),
toHaveCalculable: require('./toHaveCalculable'),
toHaveDate: require('./toHaveDate'),
toHaveDateAfter: require('./toHaveDateAfter'),
toHaveDateBefore: require('./toHaveDateBefore'),
toHaveEmptyArray: require('./toHaveEmptyArray'),
toHaveEmptyObject: require('./toHaveEmptyObject'),
toHaveEmptyString: require('./toHaveEmptyString'),
toHaveEvenNumber: require('./toHaveEvenNumber'),
toHaveFalse: require('./toHaveFalse'),
toHaveHtmlString: require('./toHaveHtmlString'),
toHaveIso8601: require('./toHaveIso8601'),
toHaveJsonString: require('./toHaveJsonString'),
toHaveMember: require('./toHaveMember'),
toHaveMethod: require('./toHaveMethod'),
toHaveNonEmptyArray: require('./toHaveNonEmptyArray'),
toHaveNonEmptyObject: require('./toHaveNonEmptyObject'),
toHaveNonEmptyString: require('./toHaveNonEmptyString'),
toHaveNumber: require('./toHaveNumber'),
toHaveNumberWithinRange: require('./toHaveNumberWithinRange'),
toHaveObject: require('./toHaveObject'),
toHaveOddNumber: require('./toHaveOddNumber'),
toHaveString: require('./toHaveString'),
toHaveStringLongerThan: require('./toHaveStringLongerThan'),
toHaveStringSameLengthAs: require('./toHaveStringSameLengthAs'),
toHaveStringShorterThan: require('./toHaveStringShorterThan'),
toHaveTrue: require('./toHaveTrue'),
toHaveWhitespaceString: require('./toHaveWhitespaceString'),
toHaveWholeNumber: require('./toHaveWholeNumber'),
toImplement: require('./toImplement'),
toStartWith: require('./toStartWith'),
toThrowAnyError: require('./toThrowAnyError'),
toThrowErrorOfType: require('./toThrowErrorOfType')
}
};
27 changes: 27 additions & 0 deletions src/asymmetricMatchers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

// modules
var reduce = require('./lib/reduce');
var api = require('./api');

// implementation
module.exports = reduce(api.asymmetricMatcher, register, {});

function register(any, asymMatcher) {
var matcher = api.matcher[asymMatcher.matcher];
any[asymMatcher.name] = createFactory(matcher);
return any;
}

function createFactory(matcher) {
return function asymmetricMatcherFactory() {
var args = [].slice.call(arguments);
return {
asymmetricMatch: function asymmetricMatcher(actual) {
var clone = args.slice();
clone.push(actual);
return matcher.apply(this, clone);
}
};
};
}
15 changes: 11 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,16 @@
* SOFTWARE.
*/

var jasmineMatchers = require('jasmine-matchers-loader');
var matchers = require('./matchers');
// 3rd party modules
var loader = require('jasmine-matchers-loader');

jasmineMatchers.add(matchers);
// modules
var api = require('./api');
var asymmetricMatchers = require('./asymmetricMatchers');

module.exports = matchers;
// public
module.exports = api.matcher;

// implementation
loader.add(api.matcher);
global.any = asymmetricMatchers;
20 changes: 20 additions & 0 deletions src/lib/reduce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

var is = require('./is');

module.exports = reduce;

function reduce(collection, fn, memo) {
if (is(collection, 'Array')) {
for (var i = 0, len = collection.length; i < len; i++) {
memo = fn(memo, collection[i], i, collection);
}
} else {
for (var key in collection) {
if (collection.hasOwnProperty(key)) {
memo = fn(memo, collection[key], key, collection);
}
}
}
return memo;
}
80 changes: 0 additions & 80 deletions src/matchers.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/toBeGreaterThanOrEqualTo.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ var toBeNumber = require('./toBeNumber');
module.exports = toBeGreaterThanOrEqualTo;

function toBeGreaterThanOrEqualTo(otherNumber, actual) {
return toBeNumber(actual) && actual >= otherNumber;
return toBeNumber(actual) && actual >= otherNumber;
}
2 changes: 1 addition & 1 deletion src/toBeLessThanOrEqualTo.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ var toBeNumber = require('./toBeNumber');
module.exports = toBeLessThanOrEqualTo;

function toBeLessThanOrEqualTo(otherNumber, actual) {
return toBeNumber(actual) && actual <= otherNumber;
return toBeNumber(actual) && actual <= otherNumber;
}
13 changes: 13 additions & 0 deletions test/after.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

var callSpy = require('./lib/callSpy');

describe('any.after', function() {
var shared = {};
beforeEach(function() {
shared.spy = callSpy(new Date('2013-01-01T00:00:00.000Z'));
});
it('should confirm', function() {
expect(shared.spy).toHaveBeenCalledWith(any.after(new Date('1998-08-12T01:00:00.000Z')));
});
});
13 changes: 13 additions & 0 deletions test/arrayOfBooleans.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

var callSpy = require('./lib/callSpy');

describe('any.arrayOfBooleans', function() {
var shared = {};
beforeEach(function() {
shared.spy = callSpy([true, false, new Boolean(true), new Boolean(false)]);
});
it('should confirm', function() {
expect(shared.spy).toHaveBeenCalledWith(any.arrayOfBooleans());
});
});
13 changes: 13 additions & 0 deletions test/arrayOfNumbers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

var callSpy = require('./lib/callSpy');

describe('any.arrayOfNumbers', function() {
var shared = {};
beforeEach(function() {
shared.spy = callSpy([1, new Number(6)]);
});
it('should confirm', function() {
expect(shared.spy).toHaveBeenCalledWith(any.arrayOfNumbers());
});
});
13 changes: 13 additions & 0 deletions test/arrayOfObjects.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

var callSpy = require('./lib/callSpy');

describe('any.arrayOfObjects', function() {
var shared = {};
beforeEach(function() {
shared.spy = callSpy([{}, new Object()]);
});
it('should confirm', function() {
expect(shared.spy).toHaveBeenCalledWith(any.arrayOfObjects());
});
});
13 changes: 13 additions & 0 deletions test/arrayOfSize.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

var callSpy = require('./lib/callSpy');

describe('any.arrayOfSize', function() {
var shared = {};
beforeEach(function() {
shared.spy = callSpy(['a', 'b', 'c']);
});
it('should confirm', function() {
expect(shared.spy).toHaveBeenCalledWith(any.arrayOfSize(3));
});
});
Loading

0 comments on commit 7cc229b

Please sign in to comment.