Reject duplicate type/trait registrations

master
Sven Slootweg 6 years ago
parent 277f0c7f79
commit 0d9e9c83c5

@ -10,6 +10,7 @@ module.exports = function createRegistry() {
_types: new Map(), _types: new Map(),
_traits: new Map(), _traits: new Map(),
createType: function (name, schema, options = {}) { createType: function (name, schema, options = {}) {
if (!this._types.has(name)) {
let combinedOptions = Object.assign({ let combinedOptions = Object.assign({
_registry: this _registry: this
}, options); }, options);
@ -17,8 +18,12 @@ module.exports = function createRegistry() {
let type = moduleAPI.createType(name, schema, combinedOptions); let type = moduleAPI.createType(name, schema, combinedOptions);
this._types.set(name, type); this._types.set(name, type);
return type; return type;
} else {
throw new Error(`A type named ${name} already exists in this registry`);
}
}, },
createTrait: function (name, schema, options = {}) { createTrait: function (name, schema, options = {}) {
if (!this._traits.has(name)) {
let combinedOptions = Object.assign({ let combinedOptions = Object.assign({
_registry: this _registry: this
}, options); }, options);
@ -26,6 +31,9 @@ module.exports = function createRegistry() {
let trait = moduleAPI.createTrait(name, schema, combinedOptions); let trait = moduleAPI.createTrait(name, schema, combinedOptions);
this._traits.set(name, trait); this._traits.set(name, trait);
return trait; return trait;
} else {
throw new Error(`A trait named ${name} already exists in this registry`);
}
}, },
guardFunction: function (args, returnType, func) { guardFunction: function (args, returnType, func) {
return moduleAPI.guardFunction(args, returnType, func, this); return moduleAPI.guardFunction(args, returnType, func, this);

@ -77,31 +77,31 @@ describe("registry", () => {
it("should work correctly for traits", () => { it("should work correctly for traits", () => {
let Givable = registry.createTrait("Givable", { let Givable = registry.createTrait("Givable", {
from: registry.type("Person"), from: registry.type("AlsoPerson"),
to: registry.type("Person") to: registry.type("AlsoPerson")
}); });
let Person = registry.createType("Person", { let AlsoPerson = registry.createType("AlsoPerson", {
name: dm.string(), name: dm.string(),
favouriteGift: registry.trait("Givable").optional() favouriteGift: registry.trait("Givable").optional()
}); });
let Gift = registry.createType("Gift", { let AlsoGift = registry.createType("AlsoGift", {
description: dm.string() description: dm.string()
}).implements(Givable, { }).implements(Givable, {
from: dm.slot(), from: dm.slot(),
to: dm.slot() to: dm.slot()
}); });
let joe = Person({ let joe = AlsoPerson({
name: "Joe" name: "Joe"
}); });
let jane = Person({ let jane = AlsoPerson({
name: "Jane" name: "Jane"
}); });
let flowers = Gift({ let flowers = AlsoGift({
description: "Flowers", description: "Flowers",
from: jane, from: jane,
to: joe to: joe
@ -119,11 +119,11 @@ describe("registry", () => {
expect(() => { expect(() => {
flowers.to = "not a person"; flowers.to = "not a person";
}).to.throw("Expected an instance of Person, got a string instead"); }).to.throw("Expected an instance of AlsoPerson, got a string instead");
expect(() => { expect(() => {
flowers.to = flowers; flowers.to = flowers;
}).to.throw("Expected an instance of Person, got an instance of Gift instead"); }).to.throw("Expected an instance of AlsoPerson, got an instance of AlsoGift instead");
}); });
it("should work correctly for type aliases", () => { it("should work correctly for type aliases", () => {
@ -147,20 +147,20 @@ describe("registry", () => {
}); });
it("should work correctly for guarded maps and sets", () => { it("should work correctly for guarded maps and sets", () => {
let SomeType = registry.createType("SomeType", { let SomeNewType = registry.createType("SomeNewType", {
value: dm.string() value: dm.string()
}); });
let SomeOtherType = registry.createType("SomeOtherType", { let SomeOtherType = registry.createType("SomeOtherType", {
things: dm.setOf(registry.type("SomeType")), things: dm.setOf(registry.type("SomeNewType")),
thingsMap: dm.mapOf(dm.string(), registry.type("SomeType")) thingsMap: dm.mapOf(dm.string(), registry.type("SomeNewType"))
}); });
let thingOne = SomeType({ let thingOne = SomeNewType({
value: "one" value: "one"
}); });
let thingTwo = SomeType({ let thingTwo = SomeNewType({
value: "two" value: "two"
}); });
@ -186,7 +186,7 @@ describe("registry", () => {
["two", thingTwo] ["two", thingTwo]
]) ])
}); });
}).to.throw("Expected an instance of SomeType, got a string instead"); }).to.throw("Expected an instance of SomeNewType, got a string instead");
expect(() => { expect(() => {
SomeOtherType({ SomeOtherType({
@ -199,7 +199,23 @@ describe("registry", () => {
["two", "things"] ["two", "things"]
]) ])
}); });
}).to.throw("Expected an instance of SomeType, got a string instead"); }).to.throw("Expected an instance of SomeNewType, got a string instead");
});
it("should reject duplicate type registrations", () => {
registry.createType("DuplicateType", {});
expect(() => {
registry.createType("DuplicateType", {});
}).to.throw("A type named DuplicateType already exists in this registry");
});
it("should reject duplicate trait registrations", () => {
registry.createTrait("DuplicateTrait", {});
expect(() => {
registry.createTrait("DuplicateTrait", {});
}).to.throw("A trait named DuplicateTrait already exists in this registry");
}); });
}); });
}); });

Loading…
Cancel
Save