Skip to content

Commit

Permalink
feat(UrlMatcher): validates whole interface
Browse files Browse the repository at this point in the history
urlMatcherFactoryProvider.isMatcher() now validates the entire UrlMatcher interface.
  • Loading branch information
nateabele committed Apr 16, 2014
1 parent a3e2136 commit 32b27db
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,13 +449,22 @@ function $UrlMatcherFactory() {
* @methodOf ui.router.util.$urlMatcherFactory
*
* @description
* Returns true if the specified object is a UrlMatcher, or false otherwise.
* Returns true if the specified object is a `UrlMatcher`, or false otherwise.
*
* @param {Object} object The object to perform the type check against.
* @returns {Boolean} Returns `true` if the object has the following functions: `exec`, `format`, and `concat`.
* @returns {Boolean} Returns `true` if the object matches the `UrlMatcher` interface, by
* implementing all the same methods.
*/
this.isMatcher = function (o) {
return isObject(o) && isFunction(o.exec) && isFunction(o.format) && isFunction(o.concat);
if (!isObject(o)) return false;
var result = true;

forEach(UrlMatcher.prototype, function(val, name) {
if (isFunction(val)) {
result = result && (isDefined(o[name]) && isFunction(o[name]));
}
});
return result;
};

this.type = function (name, def) {
Expand Down
16 changes: 15 additions & 1 deletion test/urlMatcherFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ describe("UrlMatcher", function () {
provider.strictMode(false);
expect(provider.compile('/hello').exec('/hello/')).toEqual({});
});

it("should correctly validate UrlMatcher interface", function () {
var m = new UrlMatcher("/");
expect(provider.isMatcher(m)).toBe(true);

m.validates = null;
expect(provider.isMatcher(m)).toBe(false);
});
});

it("should match static URLs", function () {
Expand Down Expand Up @@ -177,7 +185,13 @@ describe("urlMatcherFactory", function () {
it("recognizes matchers", function () {
expect($umf.isMatcher(new UrlMatcher('/'))).toBe(true);

var custom = { format: angular.noop, exec: angular.noop, concat: angular.noop };
var custom = {
format: angular.noop,
exec: angular.noop,
concat: angular.noop,
validates: angular.noop,
parameters: angular.noop
};
expect($umf.isMatcher(custom)).toBe(true);
});

Expand Down

0 comments on commit 32b27db

Please sign in to comment.