Skip to content

Commit

Permalink
Export MetadataReader (#544)
Browse files Browse the repository at this point in the history
* Expose metadata reader

* Expose metadata reader
  • Loading branch information
remojansen authored Apr 25, 2017
1 parent 3553c79 commit 67f17b1
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "inversify",
"version": "4.0.0",
"version": "4.1.0",
"description": "A powerful and lightweight inversion of control container for JavaScript and Node.js apps powered by TypeScript.",
"main": "lib/inversify.js",
"jsnext:main": "es/inversify.js",
Expand Down
1 change: 1 addition & 0 deletions src/inversify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export { optional } from "./annotation/optional";
export { unmanaged } from "./annotation/unmanaged";
export { multiInject } from "./annotation/multi_inject";
export { targetName } from "./annotation/target_name";
export { MetadataReader } from "./planning/metadata_reader";
export { guid } from "./utils/guid";
export { interfaces } from "./interfaces/interfaces";
export { decorate } from "./annotation/decorator_utils";
Expand Down
106 changes: 105 additions & 1 deletion test/features/metadata_reader.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai";
import { Container } from "../../src/inversify";
import { Container, injectable, inject, MetadataReader } from "../../src/inversify";
import { interfaces } from "../../src/interfaces/interfaces";
import { Metadata } from "../../src/planning/metadata";
import * as METADATA_KEY from "../../src/constants/metadata_keys";
Expand Down Expand Up @@ -210,4 +210,108 @@ describe("Custom Metadata Reader", () => {

});


it("Should be able to use extend the default metadata reader", () => {

const constructorMetadataLog: interfaces.ConstructorMetadata[] = [];
const propertyMetadataLog: interfaces.MetadataMap[] = [];

class CustomMetadataReader extends MetadataReader {
public getConstructorMetadata(constructorFunc: Function): interfaces.ConstructorMetadata {
const constructorMetadata = super.getConstructorMetadata(constructorFunc);
constructorMetadataLog.push(constructorMetadata);
return constructorMetadata;
}
public getPropertiesMetadata(constructorFunc: Function): interfaces.MetadataMap {
const propertyMetadata = super.getPropertiesMetadata(constructorFunc);
propertyMetadataLog.push(propertyMetadata);
return propertyMetadata;
}
}

interface Ninja {
fight(): string;
sneak(): string;
}

interface Katana {
hit(): string;
}

interface Shuriken {
throw(): string;
}

@injectable()
class Katana implements Katana {
public hit() {
return "cut!";
}
}

@injectable()
class Shuriken implements Shuriken {
public throw() {
return "hit!";
}
}

@injectable()
class Ninja implements Ninja {

private _katana: Katana;
private _shuriken: Shuriken;

public constructor(
@inject("Katana") katana: Katana,
@inject("Shuriken") shuriken: Shuriken
) {
this._katana = katana;
this._shuriken = shuriken;
}

public fight() {return this._katana.hit(); }
public sneak() { return this._shuriken.throw(); }

}

let container = new Container();
container.applyCustomMetadataReader(new CustomMetadataReader());

container.bind<Ninja>("Ninja").to(Ninja);
container.bind<Katana>("Katana").to(Katana);
container.bind<Shuriken>("Shuriken").to(Shuriken);

let ninja = container.get<Ninja>("Ninja");

expect(ninja.fight()).eql("cut!");
expect(ninja.sneak()).eql("hit!");

expect(Array.isArray(constructorMetadataLog)).eq(true);
expect(constructorMetadataLog.length).eq(3);

let compilerGeneratedMetadata0 = constructorMetadataLog[0].compilerGeneratedMetadata;

if (compilerGeneratedMetadata0) {
expect(compilerGeneratedMetadata0[0]).eq(Katana);
expect(compilerGeneratedMetadata0[1]).eq(Shuriken);
}

expect(constructorMetadataLog[0].userGeneratedMetadata["0"][0].key).eq("inject");
expect(constructorMetadataLog[0].userGeneratedMetadata["0"][0].value).eq("Katana");
expect(constructorMetadataLog[0].userGeneratedMetadata["1"][0].key).eq("inject");
expect(constructorMetadataLog[0].userGeneratedMetadata["1"][0].value).eq("Shuriken");

expect(JSON.stringify(constructorMetadataLog[1].compilerGeneratedMetadata)).eq(JSON.stringify([]));
expect(JSON.stringify(constructorMetadataLog[2].compilerGeneratedMetadata)).eq(JSON.stringify([]));
expect(JSON.stringify(constructorMetadataLog[1].userGeneratedMetadata)).eq(JSON.stringify({}));
expect(JSON.stringify(constructorMetadataLog[2].userGeneratedMetadata)).eq(JSON.stringify({}));

expect(propertyMetadataLog.length).eq(3);
expect(propertyMetadataLog[0].length).eq(0);
expect(propertyMetadataLog[1].length).eq(0);
expect(propertyMetadataLog[2].length).eq(0);

});

});

0 comments on commit 67f17b1

Please sign in to comment.