-
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(asymmetric): add asymmetric matchers
- Loading branch information
1 parent
6319e4d
commit 7cc229b
Showing
39 changed files
with
1,206 additions
and
486 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,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') | ||
} | ||
}; |
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,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); | ||
} | ||
}; | ||
}; | ||
} |
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,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; | ||
} |
This file was deleted.
Oops, something went wrong.
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
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,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'))); | ||
}); | ||
}); |
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,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()); | ||
}); | ||
}); |
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,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()); | ||
}); | ||
}); |
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,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()); | ||
}); | ||
}); |
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,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)); | ||
}); | ||
}); |
Oops, something went wrong.