From 7d4aee31bb202e9b050fc15c56cfc33c46b9eaf5 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Wed, 16 Feb 2011 19:18:35 -0500 Subject: Auto create $inject property form the argument names. Any arg starting with $ or _ will be injected --- test/InjectorSpec.js | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) (limited to 'test/InjectorSpec.js') diff --git a/test/InjectorSpec.js b/test/InjectorSpec.js index 765003cf..c876f188 100644 --- a/test/InjectorSpec.js +++ b/test/InjectorSpec.js @@ -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(); + }); + + }); }); -- cgit v1.2.3