Auto create $inject property form the argument names. Any arg starting with $ or _ will be injected

This commit is contained in:
Misko Hevery
2011-02-16 19:18:35 -05:00
parent 7a54d2791f
commit 7d4aee31bb
4 changed files with 110 additions and 23 deletions

View File

@@ -53,9 +53,56 @@ describe('injector', function(){
it('should autostart eager services', function(){
var log = '';
providers('eager', function(){log += 'eager;'; return 'foo'}, {$eager: true});
providers('eager', function(){log += 'eager;'; return 'foo';}, {$eager: true});
inject();
expect(log).toEqual('eager;');
expect(inject('eager')).toBe('foo');
});
describe('annotation', function(){
it('should return $inject', function(){
function fn(){};
fn.$inject = ['a'];
expect(injectionArgs(fn)).toBe(fn.$inject);
expect(injectionArgs(function(){})).toEqual([]);
});
it('should create $inject', function(){
// keep the multi-line to make sure we can handle it
function $f_n0(
$a, // x, <-- looks like an arg but it is a comment
b_, /* z, <-- looks like an arg but it is a
multi-line comment
function (a, b){}
*/
/* {some type} */ c){ extraParans();};
expect(injectionArgs($f_n0)).toEqual(['$a', 'b']);
expect($f_n0.$inject).toEqual(['$a', 'b']);
});
it('should handle no arg functions', function(){
function $f_n0(){};
expect(injectionArgs($f_n0)).toEqual([]);
expect($f_n0.$inject).toEqual([]);
});
it('should handle args with both $ and _', function(){
function $f_n0($a_){};
expect(injectionArgs($f_n0)).toEqual(['$a']);
expect($f_n0.$inject).toEqual(['$a']);
});
it('should throw on non function arg', function(){
expect(function(){
injectionArgs({});
}).toThrow();
});
it('should throw on injectable after non-injectable arg', function(){
expect(function(){
injectionArgs(function($a, b_, nonInject, d_){});
}).toThrow();
});
});
});