Skip to content

Commit

Permalink
Merge pull request #13356 from rwjblue/beef-up-regexp
Browse files Browse the repository at this point in the history
[BUGFIX beta] Update Registry#has to always return true/false.
  • Loading branch information
dgeb committed Apr 18, 2016
2 parents 5389442 + d0619b8 commit 97a2d96
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
13 changes: 10 additions & 3 deletions packages/container/lib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import EmptyObject from 'ember-metal/empty_object';
import assign from 'ember-metal/assign';
import Container from './container';

var VALID_FULL_NAME_REGEXP = /^[^:]+.+:[^:]+$/;
var VALID_FULL_NAME_REGEXP = /^[^:]+:[^:]+$/;

/**
A registry used to store factory and option information keyed
Expand Down Expand Up @@ -345,7 +345,9 @@ Registry.prototype = {
@return {Boolean}
*/
has(fullName, options) {
assert('fullName must be a proper full name', this.validateFullName(fullName));
if (!this.isValidFullName(fullName)) {
return false;
}

let source;
if (isEnabled('ember-htmlbars-local-lookup')) {
Expand Down Expand Up @@ -693,12 +695,17 @@ Registry.prototype = {
},

validateFullName(fullName) {
if (!VALID_FULL_NAME_REGEXP.test(fullName)) {
if (!this.isValidFullName(fullName)) {
throw new TypeError('Invalid Fullname, expected: `type:name` got: ' + fullName);
}

return true;
},

isValidFullName(fullName) {
return !!VALID_FULL_NAME_REGEXP.test(fullName);
},

validateInjections(injections) {
if (!injections) { return; }

Expand Down
16 changes: 15 additions & 1 deletion packages/container/tests/registry_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ QUnit.test('The registry normalizes names when checking if the factory is regist
});

QUnit.test('validateFullName throws an error if name is incorrect', function() {
expect(2);

var registry = new Registry();
var PostController = factory();

Expand All @@ -133,8 +135,12 @@ QUnit.test('validateFullName throws an error if name is incorrect', function() {

registry.register('controller:post', PostController);
throws(function() {
registry.resolve('post');
registry.validateFullName('post');
}, /TypeError: Invalid Fullname, expected: `type:name` got: post/);

throws(function() {
registry.validateFullName('route:http://foo.bar.com/baz');
}, /TypeError: Invalid Fullname, expected: `type:name` got: route:http:\/\/foo.bar.com\/baz/);
});

QUnit.test('The registry normalizes names when injecting', function() {
Expand Down Expand Up @@ -205,6 +211,14 @@ QUnit.test('registry.has should not accidentally cause injections on that factor
ok(registry.has('controller:apple'));
});

QUnit.test('registry.has should not error for invalid fullNames)', function() {
expect(1);

var registry = new Registry();

ok(!registry.has('foo:bar:baz'));
});

QUnit.test('once resolved, always return the same result', function() {
expect(1);

Expand Down

0 comments on commit 97a2d96

Please sign in to comment.