Skip to content
This repository has been archived by the owner on Dec 8, 2020. It is now read-only.

Commit

Permalink
Adding value loader to allow for component injection in testing
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-donat committed Feb 27, 2017
1 parent e65aa7a commit 526e37f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 9 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ canister.Parser = require('./src/parser');
canister.ModuleLoader = require('./src/loader');
canister.definitionLoader = {
YAML: require('./src/definition-loader/yaml'),
Environment: require('./src/definition-loader/env')
Environment: require('./src/definition-loader/env'),
Value: require('./src/definition-loader/value')
};

module.exports = canister;
10 changes: 4 additions & 6 deletions src/definition-loader/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,24 @@ module.exports = class EnvironmentLoader {
return key.replace(prefix, '').toLowerCase().split('__').join('.');
},
castValue = value => {
if (value === "true") {
if (value === 'true') {
return true;
}
if (value === "false") {
if (value === 'false') {
return false;
}
if (isNaN(value)) {
return value;
}
return +value; //cast to number
return Number(value).valueOf();
}
} = {}) {

Object.keys(this.config).forEach(key => {
if (!prefix.test(key)) {
return;
}
this.dictionary[getKey(key, prefix)] = castValue(this.config[key]);
})

});
}

toJS() {
Expand Down
21 changes: 21 additions & 0 deletions src/definition-loader/value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = class ValueLoader {

constructor() {
this.dictionary = {
parameters: {},
components: {}
};
}

parameter(name, value) {
this.dictionary.parameters[name] = value;
}

component(name, value) {
this.dictionary.components[name] = {value};
}

toJS() {
return this.dictionary;
}
};
5 changes: 5 additions & 0 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ module.exports = class Parser {
if (Object.prototype.hasOwnProperty.call(dictionary.components, id)) {
let yieldValue = null;
let value = dictionary.components[id];

if (value.value) {
yieldValue = Definition.parameter(id, value.value);
}

if (value.module) {
let {module} = value;
yieldValue = Definition.module(id, this.__getPath(module));
Expand Down
15 changes: 15 additions & 0 deletions tests/component/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ const classFixture = require('./class');
describe('Canister.js', function() {
it('can build container from yml config file', function() {

const injectedComponent = String('ABC');
const injectedParameter = 12398;


const moduleLoader = new canister.ModuleLoader(__dirname);
const builder = new canister.Builder(moduleLoader);
const yamlLoader = new canister.definitionLoader.YAML();
const envLoader = new canister.definitionLoader.Environment({CONFIG_OVERRIDE: 1, CONFIG_NESTED_A_BIT__OVERRIDE: 2});
const valueLoader = new canister.definitionLoader.Value();


yamlLoader.fromFile(path.join(__dirname, './wiring.yml'));
envLoader.load();
valueLoader.parameter('injected.parameter', injectedParameter);
valueLoader.component('injected.component', injectedComponent);

const parser = new canister.Parser();

Expand All @@ -24,6 +32,10 @@ describe('Canister.js', function() {
builder.addDefinition(definition);
}

for (let definition of parser.parse(valueLoader.toJS())) {
builder.addDefinition(definition);
}

const container = builder.build();

expect(container.get('my.parameter')).to.equal('parameterValue');
Expand Down Expand Up @@ -84,5 +96,8 @@ describe('Canister.js', function() {

expect(container.get('override')).to.be.eql(1);
expect(container.get('nested_a_bit.override')).to.be.eql(2);

expect(container.get('injected.component')).to.be.equal(injectedComponent);
expect(container.get('injected.parameter')).to.be.equal(injectedParameter);
})
})
20 changes: 18 additions & 2 deletions tests/spec/parser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ describe('Parser', function() {
const compare = Definition.class('my.class', 'B', 'lib/smth', false);

expect(definition).to.be.eql(compare);
})
});

it('parses references to container', function() {
const parser = new Parser('/basePath');
Expand All @@ -288,5 +288,21 @@ describe('Parser', function() {
compare.constructWith(Definition.self());

expect(definition).to.be.eql(compare);
})
});

it('parses injected values as parameters', function() {
const parser = new Parser();

const definition = parser.parse({
components: { 'my.injected': {
value: parser,
}}
}).next().value;

expect(definition).to.be.an.instanceOf(Definition);

const compare = Definition.parameter('my.injected', parser);

expect(definition).to.be.eql(compare);
});
});

0 comments on commit 526e37f

Please sign in to comment.