diff --git a/test/function-guards.js b/test/function-guards.js index 0a74b93..42412e7 100644 --- a/test/function-guards.js +++ b/test/function-guards.js @@ -64,4 +64,61 @@ describe("function guards", () => { guardedFunction("hello world"); }); + + let SomeType = dm.createType("SomeType", { + value: dm.string() + }); + + let guardedTypeFunction = dm.guard([SomeType], dm.nothing(), function (instance) { + expect(instance.value).to.equal("foo"); + }); + + it("should allow correct instances for a type rule", () => { + guardedTypeFunction(SomeType({ + value: "foo" + })); + }); + + it("should reject other values for that rule", () => { + expect(() => { + guardedTypeFunction("hello world"); + }).to.throw("Expected an instance of SomeType, got a string instead"); + }); + + let SomeTraitType = dm.createType("SomeTraitType", { + value: dm.string() + }); + + let SomeTrait = dm.createTrait("SomeTrait", { + traitValue: dm.string() + }); + + SomeTraitType.implements(SomeTrait, { + traitValue: "SomeTraitType" + }); + + let guardedTraitFunction = dm.guard([SomeTrait], dm.nothing(), function (instance) { + expect(instance.value).to.equal("foo"); + expect(instance.traitValue).to.equal("SomeTraitType"); + }); + + it("should allow correct instances for a trait rule", () => { + guardedTraitFunction(SomeTraitType({ + value: "foo" + })); + }); + + it ("should reject instances that do not have the trait", () => { + expect(() => { + guardedTraitFunction(SomeType({ + value: "foo" + })); + }).to.throw("Expected object of a type with the SomeTrait trait, got an instance of SomeType instead"); + }); + + it("should reject other values for that rule", () => { + expect(() => { + guardedTraitFunction("hello world"); + }).to.throw("Expected object of a type with the SomeTrait trait, got a string instead"); + }); });