Skip to content

Commit

Permalink
We now throw an error if any arguments are left unverified.
Browse files Browse the repository at this point in the history
  • Loading branch information
dchambers committed Oct 24, 2014
1 parent 354cf68 commit 6f6792a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Typester is inspired by the [check-types](https://www.npmjs.org/package/check-ty
* Allow a single `isA()` check to be used for what would otherwise have to be done with both `instanceof` and `typeof()`checks, as appropriate.
* Provide a `fulfills()` method that can verify any object using shape-based duck typing.
* Allow `isA()` checks to efficiently verify multiple inheritance (e.g. sand-boxed mix-ins and implemented interfaces) for applications that themselves used [topiarist](https://github.com/BladeRunnerJS/topiarist) to set these up.
* Failing if a method is invoked with more arguments than are expected.

Here's how you might use Typester to implement `addEventListener()`:

Expand All @@ -37,6 +38,7 @@ Some things to note here:
* The `fulfills()` method can be used do shape-based checks.
* The `optionally` modifier can be used for optional arguments.
* It's the developer's responsibility to ensure the `verify()` statements are performed in the correct order — the provided argument name is only used for informational purposes when an error is thrown.
* it's the developer's responsibility to ensure that verifications are provided for all arguments — otherwise an error will be thrown.

## Verifiers

Expand Down
4 changes: 4 additions & 0 deletions lib/ArgVerifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ function verifierMethod(verifier, methodName) {
verifier[methodName].apply(this, arguments);
}

if(this.argsVerifier.argIndex < this.argsVerifier.arguments.length) {
this.argsVerifier.constructor.pendingVerifier = this.argsVerifier;
}

return this.argsVerifier;
};
}
Expand Down
11 changes: 11 additions & 0 deletions lib/ArgsVerifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@ var ArgVerifier = require('./ArgVerifier');
var ArgumentError = require('./ArgumentError');

function ArgsVerifier(arguments) {
if(ArgsVerifier.pendingVerifier) throw pendingVerifierError();
if(arguments === undefined) throw new ArgumentError('arguments argument must be provided');

this.arguments = arguments;
this.argIndex = 0;
}

ArgsVerifier.prototype.verify = function(argName) {
ArgsVerifier.pendingVerifier = null;
if(typeof(argName) != 'string') throw new TypeError('argName argument must be a String');

return new ArgVerifier(this, argName, this.arguments[this.argIndex++]);
};

function pendingVerifierError() {
var pendingVerifier = ArgsVerifier.pendingVerifier;
var pendingVerifierArgs = Array.prototype.slice.call(pendingVerifier.arguments);
ArgsVerifier.pendingVerifier = null;

return new ArgumentError('only ' + pendingVerifier.argIndex + ' argument(s) verified but ' + pendingVerifierArgs.length +
' were provided: [' + pendingVerifierArgs.join(', ') + ']');
}

module.exports = ArgsVerifier;
18 changes: 18 additions & 0 deletions spec/test/typester.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,23 @@ describe('typester', function() {
func.bind(func, '10', true).should.throw(TypeError);
func.bind(func, '10', true).should.throw('num argument must be a Number');
});

it('throws an error if not all of the arguments from the previous verification were accounted for', function() {
function func() {
using(arguments)
.verify('bool').isA(Boolean)
.verify('num').optionally.isA(Number);
}

function nextVerification() {
using([]);
}

func(true, 10, 'text');
nextVerification.should.throw(ArgumentError);

func(true, 10, 'text');
nextVerification.should.throw('only 2 argument(s) verified but 3 were provided: [true, 10, text]');
});
});
});

0 comments on commit 6f6792a

Please sign in to comment.