From 60aaba56c325356a86cfabd6bc42706d60f59140 Mon Sep 17 00:00:00 2001 From: remojansen Date: Mon, 26 Sep 2016 01:58:13 +0100 Subject: [PATCH] Working on test coverage --- test/constraints.test.ts | 146 +++++++++++++++++++++++++++++ test/decorators.test.ts | 70 ++++++++++++++ test/register.test.ts | 193 --------------------------------------- 3 files changed, 216 insertions(+), 193 deletions(-) create mode 100644 test/constraints.test.ts create mode 100644 test/decorators.test.ts diff --git a/test/constraints.test.ts b/test/constraints.test.ts new file mode 100644 index 0000000..1f51983 --- /dev/null +++ b/test/constraints.test.ts @@ -0,0 +1,146 @@ +import { expect } from "chai"; +import { Kernel, interfaces } from "inversify"; +import { helpers } from "../src/index"; + +describe("Register helper constraints", () => { + + it("Should allow to register a named class", () => { + + interface Weapon { + name: string; + } + + interface Warrior { + primaryWeapon: Weapon; + secondaryWeapon: Weapon; + } + + class Katana implements Weapon { + public name: string; + public constructor() { + this.name = "Katana"; + } + } + + class Shuriken implements Weapon { + public name: string; + public constructor() { + this.name = "Shuriken"; + } + } + + class Ninja { + public primaryWeapon: Weapon; + public secondaryWeapon: Weapon; + public constructor(primaryWeapon: Weapon, secondaryWeapon: Weapon) { + this.primaryWeapon = primaryWeapon; + this.secondaryWeapon = secondaryWeapon; + } + } + + let kernel = new Kernel(); + let register = helpers.register(kernel); + + register( + "Weapon", + [], + (b: interfaces.BindingInWhenOnSyntax) => { b.whenTargetNamed("not-throwable"); } + )(Katana); + + register( + "Weapon", + [], + (b: interfaces.BindingInWhenOnSyntax) => { b.whenTargetNamed("throwable"); } + )(Shuriken); + + register( + "Warrior", + [ + { named: "not-throwable", type: "Weapon" }, + { named: "throwable", type: "Weapon" } + ] + )(Ninja); + + let ninja = kernel.get("Warrior"); + expect(ninja.primaryWeapon.name).to.eql("Katana"); + expect(ninja.secondaryWeapon.name).to.eql("Shuriken"); + + }); + + it("Should allow to register a tagged class", () => { + + interface Weapon { + name: string; + } + + interface Warrior { + primaryWeapon: Weapon; + secondaryWeapon: Weapon; + } + + class Katana implements Weapon { + public name: string; + public constructor() { + this.name = "Katana"; + } + } + + class Shuriken implements Weapon { + public name: string; + public constructor() { + this.name = "Shuriken"; + } + } + + class Ninja { + public primaryWeapon: Weapon; + public secondaryWeapon: Weapon; + public constructor(primaryWeapon: Weapon, secondaryWeapon: Weapon) { + this.primaryWeapon = primaryWeapon; + this.secondaryWeapon = secondaryWeapon; + } + } + + let kernel = new Kernel(); + let register = helpers.register(kernel); + let TYPE = { + Warrior: "Warrior", + Weapon: "Weapon" + }; + + register( + TYPE.Weapon, + [], + (b: interfaces.BindingInWhenOnSyntax) => { b.whenTargetTagged("throwable", false); } + )(Katana); + + register( + TYPE.Weapon, + [], + (b: interfaces.BindingInWhenOnSyntax) => { b.whenTargetTagged("throwable", true); } + )(Shuriken); + + register( + TYPE.Warrior, + [ + { tagged: { key: "throwable", value: false }, type: TYPE.Weapon }, + { tagged: { key: "throwable", value: true }, type: TYPE.Weapon } + ] + )(Ninja); + + let ninja = kernel.get(TYPE.Warrior); + expect(ninja.primaryWeapon.name).to.eql("Katana"); + expect(ninja.secondaryWeapon.name).to.eql("Shuriken"); + + }); + + it("Should allow to apply constraints to registerSelf"); + it("Should allow to apply constraints to registerConstructor"); + it("Should allow to apply constraints to registerConstantValue"); + it("Should allow to apply constraints to registerDynamicValue"); + it("Should allow to apply constraints to registerFunction"); + it("Should allow to apply constraints to registerAutoFactory"); + it("Should allow to apply constraints to registerFactory"); + it("Should allow to apply constraints to registerProvider"); + +}); diff --git a/test/decorators.test.ts b/test/decorators.test.ts new file mode 100644 index 0000000..da505db --- /dev/null +++ b/test/decorators.test.ts @@ -0,0 +1,70 @@ +import { expect } from "chai"; +import { Kernel, interfaces } from "inversify"; +import { helpers } from "../src/index"; + +describe("Register helper as a decorator", () => { + + it("Should allow to use register helper as a class decorator", () => { + + let kernel = new Kernel(); + let register = helpers.register(kernel); + + let TYPE = { + Warrior: "Warrior", + Weapon: "Weapon" + }; + + interface Weapon { + name: string; + } + + interface Warrior { + primaryWeapon: Weapon; + secondaryWeapon: Weapon; + } + + @register( + TYPE.Weapon, [], + (b: interfaces.BindingInWhenOnSyntax) => { b.whenTargetTagged("throwable", false); } + ) + class Katana implements Weapon { + public name: string; + public constructor() { + this.name = "Katana"; + } + } + + @register( + TYPE.Weapon, [], + (b: interfaces.BindingInWhenOnSyntax) => { b.whenTargetTagged("throwable", true); } + ) + class Shuriken implements Weapon { + public name: string; + public constructor() { + this.name = "Shuriken"; + } + } + + @register( + TYPE.Warrior, + [ + { tagged: { key: "throwable", value: false }, type: TYPE.Weapon }, + { tagged: { key: "throwable", value: true }, type: TYPE.Weapon } + ] + ) + class Ninja { + public primaryWeapon: Weapon; + public secondaryWeapon: Weapon; + public constructor(primaryWeapon: Weapon, secondaryWeapon: Weapon) { + this.primaryWeapon = primaryWeapon; + this.secondaryWeapon = secondaryWeapon; + } + } + + let ninja = kernel.get(TYPE.Warrior); + expect(ninja.primaryWeapon.name).to.eql("Katana"); + expect(ninja.secondaryWeapon.name).to.eql("Shuriken"); + + }); + +}); diff --git a/test/register.test.ts b/test/register.test.ts index 6be6a5b..dc41ac0 100644 --- a/test/register.test.ts +++ b/test/register.test.ts @@ -488,197 +488,4 @@ describe("Register Helper", () => { }); - it("Should allow to register a named class", () => { - - interface Weapon { - name: string; - } - - interface Warrior { - primaryWeapon: Weapon; - secondaryWeapon: Weapon; - } - - class Katana implements Weapon { - public name: string; - public constructor() { - this.name = "Katana"; - } - } - - class Shuriken implements Weapon { - public name: string; - public constructor() { - this.name = "Shuriken"; - } - } - - class Ninja { - public primaryWeapon: Weapon; - public secondaryWeapon: Weapon; - public constructor(primaryWeapon: Weapon, secondaryWeapon: Weapon) { - this.primaryWeapon = primaryWeapon; - this.secondaryWeapon = secondaryWeapon; - } - } - - let kernel = new Kernel(); - let register = helpers.register(kernel); - - register( - "Weapon", - [], - (b: interfaces.BindingInWhenOnSyntax) => { b.whenTargetNamed("not-throwable"); } - )(Katana); - - register( - "Weapon", - [], - (b: interfaces.BindingInWhenOnSyntax) => { b.whenTargetNamed("throwable"); } - )(Shuriken); - - register( - "Warrior", - [ - { named: "not-throwable", type: "Weapon" }, - { named: "throwable", type: "Weapon" } - ] - )(Ninja); - - let ninja = kernel.get("Warrior"); - expect(ninja.primaryWeapon.name).to.eql("Katana"); - expect(ninja.secondaryWeapon.name).to.eql("Shuriken"); - - }); - - it("Should allow to register a tagged class", () => { - - interface Weapon { - name: string; - } - - interface Warrior { - primaryWeapon: Weapon; - secondaryWeapon: Weapon; - } - - class Katana implements Weapon { - public name: string; - public constructor() { - this.name = "Katana"; - } - } - - class Shuriken implements Weapon { - public name: string; - public constructor() { - this.name = "Shuriken"; - } - } - - class Ninja { - public primaryWeapon: Weapon; - public secondaryWeapon: Weapon; - public constructor(primaryWeapon: Weapon, secondaryWeapon: Weapon) { - this.primaryWeapon = primaryWeapon; - this.secondaryWeapon = secondaryWeapon; - } - } - - let kernel = new Kernel(); - let register = helpers.register(kernel); - let TYPE = { - Warrior: "Warrior", - Weapon: "Weapon" - }; - - register( - TYPE.Weapon, - [], - (b: interfaces.BindingInWhenOnSyntax) => { b.whenTargetTagged("throwable", false); } - )(Katana); - - register( - TYPE.Weapon, - [], - (b: interfaces.BindingInWhenOnSyntax) => { b.whenTargetTagged("throwable", true); } - )(Shuriken); - - register( - TYPE.Warrior, - [ - { tagged: { key: "throwable", value: false }, type: TYPE.Weapon }, - { tagged: { key: "throwable", value: true }, type: TYPE.Weapon } - ] - )(Ninja); - - let ninja = kernel.get(TYPE.Warrior); - expect(ninja.primaryWeapon.name).to.eql("Katana"); - expect(ninja.secondaryWeapon.name).to.eql("Shuriken"); - - }); - - it("Should allow to use register helper as a class decorator", () => { - - let kernel = new Kernel(); - let register = helpers.register(kernel); - - let TYPE = { - Warrior: "Warrior", - Weapon: "Weapon" - }; - - interface Weapon { - name: string; - } - - interface Warrior { - primaryWeapon: Weapon; - secondaryWeapon: Weapon; - } - - @register( - TYPE.Weapon, [], - (b: interfaces.BindingInWhenOnSyntax) => { b.whenTargetTagged("throwable", false); } - ) - class Katana implements Weapon { - public name: string; - public constructor() { - this.name = "Katana"; - } - } - - @register( - TYPE.Weapon, [], - (b: interfaces.BindingInWhenOnSyntax) => { b.whenTargetTagged("throwable", true); } - ) - class Shuriken implements Weapon { - public name: string; - public constructor() { - this.name = "Shuriken"; - } - } - - @register( - TYPE.Warrior, - [ - { tagged: { key: "throwable", value: false }, type: TYPE.Weapon }, - { tagged: { key: "throwable", value: true }, type: TYPE.Weapon } - ] - ) - class Ninja { - public primaryWeapon: Weapon; - public secondaryWeapon: Weapon; - public constructor(primaryWeapon: Weapon, secondaryWeapon: Weapon) { - this.primaryWeapon = primaryWeapon; - this.secondaryWeapon = secondaryWeapon; - } - } - - let ninja = kernel.get(TYPE.Warrior); - expect(ninja.primaryWeapon.name).to.eql("Katana"); - expect(ninja.secondaryWeapon.name).to.eql("Shuriken"); - - }); - });