"use strict"; const expect = require("chai").expect; const dm = require("../src"); describe("registry", () => { let registry = dm.createRegistry(); describe("registry API", () => { it("should not expose registry-specific methods on the primary module API", () => { expect(dm.type).to.equal(undefined); expect(dm.trait).to.equal(undefined); }); it("should expose the createRegistry method on the primary module API", () => { expect(dm.createRegistry).to.be.a("function"); }); it("should expose registry-specific methods on the registry API", () => { expect(registry.type).to.be.a("function"); expect(registry.trait).to.be.a("function"); }); it("should not expose the createRegistry method on the registry API", () => { expect(registry.createRegistry).to.equal(undefined); }); }); describe("registry usage", () => { it("should work correctly for types", () => { let Person = registry.createType("Person", { name: dm.string(), favouriteGift: registry.type("Gift").optional() }); let Gift = registry.createType("Gift", { description: dm.string(), from: registry.type("Person"), to: registry.type("Person") }); let joe = Person({ name: "Joe" }); let jane = Person({ name: "Jane" }); let flowers = Gift({ description: "Flowers", from: jane, to: joe }); joe.favouriteGift = flowers; expect(joe.favouriteGift).to.equal(flowers); expect(flowers.to).to.equal(joe); expect(flowers.from).to.equal(jane); expect(() => { jane.favouriteGift = "not a gift"; }).to.throw("Expected an instance of Gift, got a string instead"); expect(() => { flowers.to = "not a person"; }).to.throw("Expected an instance of Person, got a string instead"); expect(() => { flowers.to = flowers; }).to.throw("Expected an instance of Person, got an instance of Gift instead"); }); it("should work correctly for traits", () => { let Givable = registry.createTrait("Givable", { from: registry.type("Person"), to: registry.type("Person") }); let Person = registry.createType("Person", { name: dm.string(), favouriteGift: registry.trait("Givable").optional() }); let Gift = registry.createType("Gift", { description: dm.string() }).implements(Givable, { from: dm.slot(), to: dm.slot() }); let joe = Person({ name: "Joe" }); let jane = Person({ name: "Jane" }); let flowers = Gift({ description: "Flowers", from: jane, to: joe }); joe.favouriteGift = flowers; expect(joe.favouriteGift).to.equal(flowers); expect(flowers.to).to.equal(joe); expect(flowers.from).to.equal(jane); expect(() => { jane.favouriteGift = "not a gift"; }).to.throw("Expected object of a type with the Givable trait, got a string instead"); expect(() => { flowers.to = "not a person"; }).to.throw("Expected an instance of Person, got a string instead"); expect(() => { flowers.to = flowers; }).to.throw("Expected an instance of Person, got an instance of Gift instead"); }); }); });