Skip to content

Commit

Permalink
implemented whenAnyAncestorTagged and whenNoAncestorTagged
Browse files Browse the repository at this point in the history
  • Loading branch information
remojansen committed Mar 23, 2016
1 parent 5f459ac commit c2b84d7
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 40 deletions.
10 changes: 5 additions & 5 deletions src/syntax/binding_when_syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ class BindingWhenSyntax<T> implements IBindingWhenSyntax<T> {
return new BindingOnSyntax<T>(this._binding);
}

public whenAnyAncestorTagged(tag: string, value: any): IBindingOnSyntax<T> {
public whenNoAncestorNamed(name: string): IBindingOnSyntax<T> {

this._binding.constraint = (request: IRequest) => {
return traverseAncerstors(request, taggedConstraint(tag)(name));
return !traverseAncerstors(request, namedConstraint(name));
};

return new BindingOnSyntax<T>(this._binding);
}

public whenNoAncestorNamed(name: string): IBindingOnSyntax<T> {
public whenAnyAncestorTagged(tag: string, value: any): IBindingOnSyntax<T> {

this._binding.constraint = (request: IRequest) => {
return !traverseAncerstors(request, namedConstraint(name));
return traverseAncerstors(request, taggedConstraint(tag)(value));
};

return new BindingOnSyntax<T>(this._binding);
Expand All @@ -91,7 +91,7 @@ class BindingWhenSyntax<T> implements IBindingWhenSyntax<T> {
public whenNoAncestorTagged(tag: string, value: any): IBindingOnSyntax<T> {

this._binding.constraint = (request: IRequest) => {
return !traverseAncerstors(request, taggedConstraint(tag)(name));
return !traverseAncerstors(request, taggedConstraint(tag)(value));
};

return new BindingOnSyntax<T>(this._binding);
Expand Down
31 changes: 15 additions & 16 deletions test/inversify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,22 +719,6 @@ describe("InversifyJS", () => {

});

// TODO
it("Should support contextual bindings with a type constraint the request target");
it("Should support contextual bindings with a named constraint the request target");
it("Should support contextual bindings with a taget constraint the request target");
it("Should support contextual bindings with a type constraint the request parent");
it("Should support contextual bindings with a type named the target of the request parent");
it("Should support contextual bindings with a type tagged the target of the request parent");
it("Should support contextual bindings with a type constraint to some of its ancestors");
it("Should support contextual bindings with a type constraint to none of its ancestors");
it("Should support contextual bindings with a named constraint to some of its ancestors");
it("Should support contextual bindings with a named constraint to none of its ancestors");
it("Should support contextual bindings with a tagged constraint to some of its ancestors");
it("Should support contextual bindings with a tagged constraint to none of its ancestors");
it("Should support contextual bindings with a custom constraint to some of its ancestors");
it("Should support contextual bindings with a custom constraint to none of its ancestors");

it("Should throw if circular dependencies found", () => {

interface IA {}
Expand Down Expand Up @@ -785,4 +769,19 @@ describe("InversifyJS", () => {

});

it("Should support contextual bindings with a type constraint the request target");
it("Should support contextual bindings with a named constraint the request target");
it("Should support contextual bindings with a taget constraint the request target");
it("Should support contextual bindings with a type constraint the request parent");
it("Should support contextual bindings with a type named the target of the request parent");
it("Should support contextual bindings with a type tagged the target of the request parent");
it("Should support contextual bindings with a type constraint to some of its ancestors");
it("Should support contextual bindings with a type constraint to none of its ancestors");
it("Should support contextual bindings with a named constraint to some of its ancestors");
it("Should support contextual bindings with a named constraint to none of its ancestors");
it("Should support contextual bindings with a tagged constraint to some of its ancestors");
it("Should support contextual bindings with a tagged constraint to none of its ancestors");
it("Should support contextual bindings with a custom constraint to some of its ancestors");
it("Should support contextual bindings with a custom constraint to none of its ancestors");

});
171 changes: 152 additions & 19 deletions test/syntax/binding_when_syntax.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Target from "../../src/planning/target";
import Metadata from "../../src/planning/metadata";
import BindingWhenSyntax from "../../src/syntax/binding_when_syntax";
import * as METADATA_KEY from "../../src/constants/metadata_keys";
import { typeConstraint } from "../../src/syntax/constraint_helpers";

describe("BindingWhenSyntax", () => {

Expand Down Expand Up @@ -428,25 +429,14 @@ describe("BindingWhenSyntax", () => {
let ironKatanaRequest = new Request("IWeapon", null, samuraiMasterRequest, katanaBinding, katanaTarget);
let woodKatanaRequest = new Request("IWeapon", null, samuraiStudentRequest, katanaBinding, katanaTarget);

// Shuriken
let shurikenBinding = new Binding<IWeapon>("IWeapon");
shurikenBinding.implementationType = Shuriken;
let shurikenBindingWhenSyntax = new BindingWhenSyntax<IWeapon>(shurikenBinding);
let shurikenTarget = new Target("shuriken", "IWeapon");
let ironShurikenRequest = new Request("IWeapon", null, ninjaMasterRequest, shurikenBinding, shurikenTarget);
let woodShurikenRequest = new Request("IWeapon", null, ninjaStudentRequest, shurikenBinding, shurikenTarget);
/*
katanaBindingWhenSyntax.whenParentTagged("sneaky", true);
shurikenBindingWhenSyntax.whenParentTagged("sneaky", true);
expect(katanaBinding.constraint(katanaRequest)).eql(false);
expect(shurikenBinding.constraint(shurikenRequest)).eql(true);

katanaBindingWhenSyntax.whenParentTagged("sneaky", false);
shurikenBindingWhenSyntax.whenParentTagged("sneaky", false);
expect(katanaBinding.constraint(katanaRequest)).eql(true);
expect(shurikenBinding.constraint(shurikenRequest)).eql(false);
*/

// TODO
it("Should be able to apply a type constraint to some of its ancestors", () => {

shurikenBindingWhenSyntax.whenAnyAncestorIs(NinjaMaster);
Expand All @@ -467,13 +457,156 @@ describe("BindingWhenSyntax", () => {

});

it("Should be able to apply a type constraint to none of its ancestors");
it("Should be able to apply a named constraint to some of its ancestors");
it("Should be able to apply a named constraint to none of its ancestors");
it("Should be able to apply a tagged constraint to some of its ancestors");
it("Should be able to apply a tagged constraint to none of its ancestors");
it("Should be able to apply a custom constraint to some of its ancestors");
it("Should be able to apply a custom constraint to none of its ancestors");
it("Should be able to apply a type constraint to none of its ancestors", () => {

shurikenBindingWhenSyntax.whenNoAncestorIs(NinjaMaster);
expect(shurikenBinding.constraint(woodShurikenRequest)).eql(true);
expect(shurikenBinding.constraint(ironShurikenRequest)).eql(false);

shurikenBindingWhenSyntax.whenNoAncestorIs(NinjaStudent);
expect(shurikenBinding.constraint(woodShurikenRequest)).eql(false);
expect(shurikenBinding.constraint(ironShurikenRequest)).eql(true);

katanaBindingWhenSyntax.whenNoAncestorIs(SamuraiMaster);
expect(katanaBinding.constraint(woodKatanaRequest)).eql(true);
expect(katanaBinding.constraint(ironKatanaRequest)).eql(false);

katanaBindingWhenSyntax.whenNoAncestorIs(SamuraiStudent);
expect(katanaBinding.constraint(woodKatanaRequest)).eql(false);
expect(katanaBinding.constraint(ironKatanaRequest)).eql(true);

});

it("Should be able to apply a named constraint to some of its ancestors", () => {

shurikenBindingWhenSyntax.whenAnyAncestorNamed("chinese");
expect(shurikenBinding.constraint(woodShurikenRequest)).eql(false);
expect(shurikenBinding.constraint(ironShurikenRequest)).eql(false);

shurikenBindingWhenSyntax.whenAnyAncestorNamed("chinese");
expect(shurikenBinding.constraint(woodShurikenRequest)).eql(false);
expect(shurikenBinding.constraint(ironShurikenRequest)).eql(false);

katanaBindingWhenSyntax.whenAnyAncestorNamed("japonese");
expect(katanaBinding.constraint(woodKatanaRequest)).eql(false);
expect(katanaBinding.constraint(ironKatanaRequest)).eql(false);

katanaBindingWhenSyntax.whenAnyAncestorNamed("japonese");
expect(katanaBinding.constraint(woodKatanaRequest)).eql(false);
expect(katanaBinding.constraint(ironKatanaRequest)).eql(false);

});

it("Should be able to apply a named constraint to none of its ancestors", () => {

shurikenBindingWhenSyntax.whenNoAncestorNamed("chinese");
expect(shurikenBinding.constraint(woodShurikenRequest)).eql(true);
expect(shurikenBinding.constraint(ironShurikenRequest)).eql(true);

shurikenBindingWhenSyntax.whenNoAncestorNamed("chinese");
expect(shurikenBinding.constraint(woodShurikenRequest)).eql(true);
expect(shurikenBinding.constraint(ironShurikenRequest)).eql(true);

katanaBindingWhenSyntax.whenNoAncestorNamed("japonese");
expect(katanaBinding.constraint(woodKatanaRequest)).eql(true);
expect(katanaBinding.constraint(ironKatanaRequest)).eql(true);

katanaBindingWhenSyntax.whenNoAncestorNamed("japonese");
expect(katanaBinding.constraint(woodKatanaRequest)).eql(true);
expect(katanaBinding.constraint(ironKatanaRequest)).eql(true);

});

it("Should be able to apply a tagged constraint to some of its ancestors", () => {

shurikenBindingWhenSyntax.whenAnyAncestorTagged("sneaky", true);
expect(shurikenBinding.constraint(woodShurikenRequest)).eql(true);
expect(shurikenBinding.constraint(ironShurikenRequest)).eql(true);

shurikenBindingWhenSyntax.whenAnyAncestorTagged("sneaky", false);
expect(shurikenBinding.constraint(woodShurikenRequest)).eql(false);
expect(shurikenBinding.constraint(ironShurikenRequest)).eql(false);

katanaBindingWhenSyntax.whenAnyAncestorTagged("sneaky", true);
expect(katanaBinding.constraint(woodKatanaRequest)).eql(false);
expect(katanaBinding.constraint(ironKatanaRequest)).eql(false);

katanaBindingWhenSyntax.whenAnyAncestorTagged("sneaky", false);
expect(katanaBinding.constraint(woodKatanaRequest)).eql(true);
expect(katanaBinding.constraint(ironKatanaRequest)).eql(true);

});

it("Should be able to apply a tagged constraint to none of its ancestors", () => {

shurikenBindingWhenSyntax.whenNoAncestorTagged("sneaky", true);
expect(shurikenBinding.constraint(woodShurikenRequest)).eql(false);
expect(shurikenBinding.constraint(ironShurikenRequest)).eql(false);

shurikenBindingWhenSyntax.whenNoAncestorTagged("sneaky", false);
expect(shurikenBinding.constraint(woodShurikenRequest)).eql(true);
expect(shurikenBinding.constraint(ironShurikenRequest)).eql(true);

katanaBindingWhenSyntax.whenNoAncestorTagged("sneaky", true);
expect(katanaBinding.constraint(woodKatanaRequest)).eql(true);
expect(katanaBinding.constraint(ironKatanaRequest)).eql(true);

katanaBindingWhenSyntax.whenNoAncestorTagged("sneaky", false);
expect(katanaBinding.constraint(woodKatanaRequest)).eql(false);
expect(katanaBinding.constraint(ironKatanaRequest)).eql(false);

});

it("Should be able to apply a custom constraint to some of its ancestors", () => {

let anyAncestorIsNinjaMasterConstraint = typeConstraint(NinjaMaster);
let anyAncestorIsNinjaStudentConstraint = typeConstraint(NinjaStudent);

shurikenBindingWhenSyntax.whenAnyAncestorMatches(anyAncestorIsNinjaMasterConstraint);
expect(shurikenBinding.constraint(woodShurikenRequest)).eql(false);
expect(shurikenBinding.constraint(ironShurikenRequest)).eql(true);

shurikenBindingWhenSyntax.whenAnyAncestorMatches(anyAncestorIsNinjaStudentConstraint);
expect(shurikenBinding.constraint(woodShurikenRequest)).eql(true);
expect(shurikenBinding.constraint(ironShurikenRequest)).eql(false);

let anyAncestorIsSamuraiMasterConstraint = typeConstraint(SamuraiMaster);
let anyAncestorIsSamuraiStudentConstraint = typeConstraint(SamuraiStudent);

katanaBindingWhenSyntax.whenAnyAncestorMatches(anyAncestorIsSamuraiMasterConstraint);
expect(katanaBinding.constraint(woodKatanaRequest)).eql(false);
expect(katanaBinding.constraint(ironKatanaRequest)).eql(true);

katanaBindingWhenSyntax.whenAnyAncestorMatches(anyAncestorIsSamuraiStudentConstraint);
expect(katanaBinding.constraint(woodKatanaRequest)).eql(true);
expect(katanaBinding.constraint(ironKatanaRequest)).eql(false);

});

it("Should be able to apply a custom constraint to none of its ancestors", () => {

let anyAncestorIsNinjaMasterConstraint = typeConstraint(NinjaMaster);
let anyAncestorIsNinjaStudentConstraint = typeConstraint(NinjaStudent);

shurikenBindingWhenSyntax.whenNoAncestorMatches(anyAncestorIsNinjaMasterConstraint);
expect(shurikenBinding.constraint(woodShurikenRequest)).eql(true);
expect(shurikenBinding.constraint(ironShurikenRequest)).eql(false);

shurikenBindingWhenSyntax.whenNoAncestorMatches(anyAncestorIsNinjaStudentConstraint);
expect(shurikenBinding.constraint(woodShurikenRequest)).eql(false);
expect(shurikenBinding.constraint(ironShurikenRequest)).eql(true);

let anyAncestorIsSamuraiMasterConstraint = typeConstraint(SamuraiMaster);
let anyAncestorIsSamuraiStudentConstraint = typeConstraint(SamuraiStudent);

katanaBindingWhenSyntax.whenNoAncestorMatches(anyAncestorIsSamuraiMasterConstraint);
expect(katanaBinding.constraint(woodKatanaRequest)).eql(true);
expect(katanaBinding.constraint(ironKatanaRequest)).eql(false);

katanaBindingWhenSyntax.whenNoAncestorMatches(anyAncestorIsSamuraiStudentConstraint);
expect(katanaBinding.constraint(woodKatanaRequest)).eql(false);
expect(katanaBinding.constraint(ironKatanaRequest)).eql(true);
});

});

Expand Down

0 comments on commit c2b84d7

Please sign in to comment.