-
-
Save joepie91/af8154fc4e1c8b405f86378db9fc12ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const expect = require("chai").expect; | |
const dm = require("../src"); | |
let GitSource = dm.createTrait("GitSource", { | |
stringify: dm.function([], dm.string()) | |
}); | |
let GithubSource, CrytoGitSource, sourceOne, sourceTwo; | |
describe("traits", () => { | |
it("should allow valid trait implementations", () => { | |
GithubSource = dm.createType("GithubSource", { | |
username: dm.string(), | |
repository: dm.string() | |
}).implements(GitSource, { | |
stringify: dm.guard([], dm.string(), function () { | |
return `https://github.com/${this.username}/${this.repository}.git`; | |
}) | |
}); | |
CrytoGitSource = dm.createType("CrytoGitSource", { | |
username: dm.string(), | |
repository: dm.string() | |
}).implements(GitSource, { | |
stringify: dm.guard([], dm.string(), function () { | |
return `http://git.cryto.net/${this.username}/${this.repository}.git`; | |
}) | |
}); | |
}); | |
it("should produce working trait functionality", () => { | |
sourceOne = GithubSource({ | |
username: "joepie91", | |
repository: "node-bhttp" | |
}); | |
expect(sourceOne.stringify()).to.equal("https://github.com/joepie91/node-bhttp.git"); | |
sourceTwo = CrytoGitSource({ | |
username: "joepie91", | |
repository: "node-bhttp" | |
}); | |
expect(sourceTwo.stringify()).to.equal("http://git.cryto.net/joepie91/node-bhttp.git"); | |
}); | |
let guardedFunc = dm.guard([GitSource], dm.nothing(), function (source) { | |
expect(source.stringify()).to.be.a("string"); | |
}); | |
it("should allow an instance of any type with the correct trait to be passed into a trait-guarded function", () => { | |
guardedFunc(sourceOne); | |
guardedFunc(sourceTwo); | |
}); | |
it("should disallow an instance of a type that does not have the correct trait", () => { | |
let SomeType = dm.createType("SomeType", { | |
value: dm.string() | |
}); | |
let instance = SomeType({ | |
value: "foo" | |
}); | |
expect(() => { | |
guardedFunc(instance); | |
}).to.throw("Expected object of a type with the GitSource trait, got an instance of SomeType instead"); | |
}); | |
it(" ... even if that type has an otherwise compatible-looking method", () => { | |
let SomeOtherType = dm.createType("SomeOtherType", { | |
value: dm.string(), | |
stringify: dm.function([], dm.string()) | |
}); | |
let instance = SomeOtherType({ | |
value: "foo", | |
stringify: dm.guard([], dm.string(), function () { | |
return "this is not a URL at all"; | |
}) | |
}); | |
expect(() => { | |
guardedFunc(instance); | |
}).to.throw("Expected object of a type with the GitSource trait, got an instance of SomeOtherType instead"); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment