-
-
Notifications
You must be signed in to change notification settings - Fork 693
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #503 from astorije/astorije/expected-types
Checking that argument given to expect is of the right type when using with include
- Loading branch information
Showing
6 changed files
with
127 additions
and
4 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
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,41 @@ | ||
/*! | ||
* Chai - expectTypes utility | ||
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com> | ||
* MIT Licensed | ||
*/ | ||
|
||
/** | ||
* ### expectTypes(obj, types) | ||
* | ||
* Ensures that the object being tested against is of a valid type. | ||
* | ||
* utils.expectTypes(this, ['array', 'object', 'string']); | ||
* | ||
* @param {Mixed} obj constructed Assertion | ||
* @param {Array} type A list of allowed types for this assertion | ||
* @name expectTypes | ||
* @api public | ||
*/ | ||
|
||
var AssertionError = require('assertion-error'); | ||
var flag = require('./flag'); | ||
var type = require('type-detect'); | ||
|
||
module.exports = function (obj, types) { | ||
var obj = flag(obj, 'object'); | ||
types = types.map(function (t) { return t.toLowerCase(); }); | ||
types.sort(); | ||
|
||
// Transforms ['lorem', 'ipsum'] into 'a lirum, or an ipsum' | ||
var str = types.map(function (t, index) { | ||
var art = ~[ 'a', 'e', 'i', 'o', 'u' ].indexOf(t.charAt(0)) ? 'an' : 'a'; | ||
var or = types.length > 1 && index === types.length - 1 ? 'or ' : ''; | ||
return or + art + ' ' + t; | ||
}).join(', '); | ||
|
||
if (!types.some(function (expected) { return type(obj) === expected; })) { | ||
throw new AssertionError( | ||
'object tested must be ' + str + ', but ' + type(obj) + ' given' | ||
); | ||
} | ||
}; |
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
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