-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
2 changed files
with
43 additions
and
3 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 |
---|---|---|
@@ -1,9 +1,12 @@ | ||
export default function requiredArgs(required, args) { | ||
if (args.length < required) { | ||
throw new TypeError( | ||
required + ' argument' + required > 1 | ||
? 's' | ||
: '' + ' required, but only ' + args.length + ' present' | ||
required + | ||
' argument' + | ||
(required > 1 ? 's' : '') + | ||
' required, but only ' + | ||
args.length + | ||
' present' | ||
) | ||
} | ||
} |
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,37 @@ | ||
// @flow | ||
/* eslint-env mocha */ | ||
|
||
import assert from 'power-assert' | ||
import requiredArgs from '.' | ||
|
||
describe('requiredArgs', function() { | ||
function wrapperFn(required) { | ||
// $ExpectedMistake | ||
return function() { | ||
requiredArgs(required, arguments) | ||
} | ||
} | ||
const twoArgsRequired = wrapperFn(2) | ||
|
||
context('with correct number of passed arguments', function() { | ||
it('does not throw an error', function() { | ||
assert.doesNotThrow(() => twoArgsRequired(1, 2)) | ||
}) | ||
}) | ||
|
||
context('with wrong number of arguments', function() { | ||
it('throws correct error message', function() { | ||
assert.throws( | ||
function() { | ||
twoArgsRequired(1) | ||
}, | ||
function(err) { | ||
return ( | ||
err instanceof TypeError && | ||
err.message === '2 arguments required, but only 1 present' | ||
) | ||
} | ||
) | ||
}) | ||
}) | ||
}) |