Skip to content

Commit

Permalink
added tests for autowiring with hints
Browse files Browse the repository at this point in the history
  • Loading branch information
sakren committed Dec 13, 2013
1 parent a97f0e1 commit b0fbcb1
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 25 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,16 +243,16 @@ for DI container.

```
var someFunction = function(otherNameForApplicationService) {
{'@di:inject': ['@application']};
{'@di:inject': ['@application']}; // services' names are prepended with '@'
otherNameForApplicationService.run(); // this will call method run on service application
};
```

or you can also include services with their full paths
or you can also include services by their full paths
```
var someFunction = function(otherNameForApplicationService) {
{'@di:inject': ['$path/to/application/service']};
{'@di:inject': ['$path/to/application/service']}; // services' paths are prepended with '$'
otherNameForApplicationService.run();
};
Expand Down Expand Up @@ -390,6 +390,7 @@ $ npm test
+ Added method `getByPath`
+ Added basePath option
+ Better docs
+ Added hints for autowiring

* 1.8.0
+ Better tests (mocha does not need to be installed globally)
Expand Down
2 changes: 1 addition & 1 deletion lib/Helpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@
"test": "npm run test-node && npm run test-browser",
"test-build": "cd ./test/browser; simq build;",
"test-node": "mocha ./test/node/index.js --reporter spec",
"test-browser": "mocha-phantomjs ./test/browser/index.html"
"test-browser": "npm run test-build; mocha-phantomjs ./test/browser/index.html"
}
}
2 changes: 1 addition & 1 deletion src/Helpers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class Helpers
result.push(container.get(args[0]))

# link to another services via module path
else if args[0] != null && typeof args[0] == 'string' && args[0].match(/^$/) != null
else if args[0] != null && typeof args[0] == 'string' && args[0].match(/^\$/) != null
args[0] = args[0].substr(1)
result.push(container.getByPath(args[0]))

Expand Down
57 changes: 52 additions & 5 deletions test/browser/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@
if (args[0] !== null && typeof args[0] === 'string' && args[0].match(/^@/) !== null) {
args[0] = args[0].substr(1);
result.push(container.get(args[0]));
} else if (args[0] !== null && typeof args[0] === 'string' && args[0].match(/^$/) !== null) {
} else if (args[0] !== null && typeof args[0] === 'string' && args[0].match(/^\$/) !== null) {
args[0] = args[0].substr(1);
result.push(container.getByPath(args[0]));
} else {
Expand Down Expand Up @@ -523,6 +523,8 @@

DI.prototype.creating = null;

DI.prototype.basePath = null;

function DI() {
var di;
di = new Service(this, 'di', this);
Expand All @@ -534,6 +536,10 @@
this.creating = [];
}

DI.prototype.getPath = function(name) {
return (this.basePath === null ? '' : this.basePath + '/') + name;
};

DI.prototype.addService = function(name, service, args) {
if (args == null) {
args = [];
Expand All @@ -542,7 +548,7 @@
throw new Error("DI: name '" + name + "' is reserved by DI.");
}
if (typeof service === 'string') {
service = require.resolve(service);
service = require.resolve(this.getPath(service));
this.paths[service] = name;
}
this.services[name] = new Service(this, name, service, args);
Expand Down Expand Up @@ -615,7 +621,7 @@
var e, error;
error = false;
try {
path = require.resolve(path);
path = require.resolve(this.getPath(path));
} catch (_error) {
e = _error;
error = true;
Expand Down Expand Up @@ -1346,7 +1352,7 @@

/** code **/
(function() {
var Application, DI, Helpers, Http, Service, di, dir;
var Application, AutowirePath, DI, Helpers, Http, Service, di, dir;

DI = require('/lib/DI');

Expand All @@ -1358,6 +1364,8 @@

Http = require('/test/data/Http');

AutowirePath = require('/test/data/AutowirePath');

di = null;

dir = '/test/data';
Expand Down Expand Up @@ -1418,6 +1426,16 @@
di.addService('array', Array);
return expect(Helpers.autowireArguments(fn, ['@array'], di)).to.be.eql([[]]);
});
it('should inject service by full path', function() {
var fn;
fn = function(something) {
return {
'@di:inject': ['$/test/data/AutowirePath']
};
};
di.addService('someRandomName', '/test/data/AutowirePath');
return expect(Helpers.autowireArguments(fn, null, di)[0]).to.be.an["instanceof"](AutowirePath);
});
it('should inject services replaced with dots in the end', function() {
var fn;
fn = function(first, second, third) {
Expand Down Expand Up @@ -1619,6 +1637,35 @@
}).call(this);


}, '/test/data/AutowirePath.coffee': function(exports, module) {

/** node globals **/
var require = function(name) {return window.require(name, '/test/data/AutowirePath.coffee');};
require.resolve = function(name, parent) {if (parent === null) {parent = '/test/data/AutowirePath.coffee';} return window.require.resolve(name, parent);};
require.define = function(bundle) {window.require.define(bundle);};
require.cache = window.require.cache;
var __filename = '/test/data/AutowirePath.coffee';
var __dirname = '/test/data';
var process = {cwd: function() {return '/';}, argv: ['node', '/test/data/AutowirePath.coffee'], env: {}};

/** code **/
(function() {
var AutowirePath;

AutowirePath = (function() {
function AutowirePath() {}

AutowirePath.prototype.greeting = 'hello';

return AutowirePath;

})();

module.exports = AutowirePath;

}).call(this);


}, '/test/data/Http.coffee': function(exports, module) {

/** node globals **/
Expand Down Expand Up @@ -1770,7 +1817,7 @@
, 'recursive-merge': function(exports, module) { module.exports = window.require('recursive-merge/lib/Merge.js'); }

});
require.__setStats({"/lib/Service.js":{"atime":1386926442000,"mtime":1386926423000,"ctime":1386926423000},"/lib/Helpers.js":{"atime":1386926442000,"mtime":1386926423000,"ctime":1386926423000},"/lib/DI.js":{"atime":1386926442000,"mtime":1386926423000,"ctime":1386926423000},"easy-configuration/lib/EasyConfiguration.js":{"atime":1386923382000,"mtime":1385411214000,"ctime":1385450928000},"recursive-merge/lib/Merge.js":{"atime":1386923382000,"mtime":1385409966000,"ctime":1385450932000},"easy-configuration/lib/Extension.js":{"atime":1386923382000,"mtime":1385411214000,"ctime":1385450928000},"easy-configuration/lib/Helpers.js":{"atime":1386923382000,"mtime":1385411214000,"ctime":1385450928000},"/test/browser/tests/DI.coffee":{"atime":1386926495000,"mtime":1386926494000,"ctime":1386926494000},"/test/browser/tests/Helpers.coffee":{"atime":1386926501000,"mtime":1386926501000,"ctime":1386926501000},"/lib/DIConfigurator.js":{"atime":1386926442000,"mtime":1386926423000,"ctime":1386926423000},"/test/data/Application.coffee":{"atime":1386925844000,"mtime":1386925844000,"ctime":1386925844000},"/test/data/Http.coffee":{"atime":1386923382000,"mtime":1384940373000,"ctime":1384940373000},"/package.json":{"atime":1386923331000,"mtime":1386923331000,"ctime":1386923331000},"easy-configuration/package.json":{"atime":1386923382000,"mtime":1385450929000,"ctime":1385450929000}});
require.__setStats({"/lib/Service.js":{"atime":1386929552000,"mtime":1386929491000,"ctime":1386929491000},"/lib/Helpers.js":{"atime":1386934776000,"mtime":1386934774000,"ctime":1386934774000},"/lib/DI.js":{"atime":1386932242000,"mtime":1386932155000,"ctime":1386932155000},"easy-configuration/lib/EasyConfiguration.js":{"atime":1386923382000,"mtime":1385411214000,"ctime":1385450928000},"recursive-merge/lib/Merge.js":{"atime":1386923382000,"mtime":1385409966000,"ctime":1385450932000},"easy-configuration/lib/Extension.js":{"atime":1386923382000,"mtime":1385411214000,"ctime":1385450928000},"easy-configuration/lib/Helpers.js":{"atime":1386923382000,"mtime":1385411214000,"ctime":1385450928000},"/test/browser/tests/DI.coffee":{"atime":1386926495000,"mtime":1386926494000,"ctime":1386926494000},"/test/browser/tests/Helpers.coffee":{"atime":1386935157000,"mtime":1386935153000,"ctime":1386935153000},"/lib/DIConfigurator.js":{"atime":1386929554000,"mtime":1386929491000,"ctime":1386929491000},"/test/data/Application.coffee":{"atime":1386925844000,"mtime":1386925844000,"ctime":1386925844000},"/test/data/AutowirePath.coffee":{"atime":1386934815000,"mtime":1386934815000,"ctime":1386934815000},"/test/data/Http.coffee":{"atime":1386923382000,"mtime":1384940373000,"ctime":1384940373000},"/package.json":{"atime":1386935057000,"mtime":1386935054000,"ctime":1386935054000},"easy-configuration/package.json":{"atime":1386923382000,"mtime":1385450929000,"ctime":1385450929000}});
require.version = '5.5.1';

/** run section **/
Expand Down
7 changes: 7 additions & 0 deletions test/browser/tests/Helpers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Helpers = require '/lib/Helpers'

Application = require '/test/data/Application'
Http = require '/test/data/Http'
AutowirePath = require '/test/data/AutowirePath'

di = null
dir = '/test/data'
Expand Down Expand Up @@ -58,6 +59,12 @@ describe 'Helpers', ->
di.addService('array', Array)
expect(Helpers.autowireArguments(fn, ['@array'], di)).to.be.eql([[]])

it 'should inject service by full path', ->
fn = (something) -> {'@di:inject': ['$/test/data/AutowirePath']}

di.addService('someRandomName', '/test/data/AutowirePath')
expect(Helpers.autowireArguments(fn, null, di)[0]).to.be.an.instanceof(AutowirePath)

it 'should inject services replaced with dots in the end', ->
fn = (first, second, third) -> return arguments
di.addService('second', ['second item']).instantiate = false
Expand Down
2 changes: 1 addition & 1 deletion test/data/AutowirePath.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class AutowirePath



greeting: 'hello'


module.exports = AutowirePath
2 changes: 2 additions & 0 deletions test/data/AutowirePath.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions test/node/Helpers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Helpers = require '../../lib/Helpers'

Application = require '../data/Application'
Http = require '../data/Http'
console.log require.resolve '../data/AutowirePath'
AutowirePath = require '../data/AutowirePath'

di = null
dir = '/test/data'
Expand Down Expand Up @@ -63,13 +63,11 @@ describe 'Helpers', ->
di.addService('array', Array)
expect(Helpers.autowireArguments(fn, ['@array'], di)).to.be.eql([[]])

it.skip 'should inject service by full path', ->
fn = (something) ->
{'@di:inject': ['$test/data/AutowirePath']}
return something
it 'should inject service by full path', ->
fn = (something) -> {'@di:inject': ['$test/data/AutowirePath']}

di.addService('someRandomName', 'test/data/AutowirePath')
console.log(Helpers.autowireArguments(fn, null, di))
expect(Helpers.autowireArguments(fn, null, di)[0]).to.be.an.instanceof(AutowirePath)

it 'should inject services replaced with dots in the end', ->
fn = (first, second, third) -> return arguments
Expand Down
13 changes: 6 additions & 7 deletions test/node/Helpers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b0fbcb1

Please sign in to comment.