Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MikeMKH/9453471 to your computer and use it in GitHub Desktop.
Save MikeMKH/9453471 to your computer and use it in GitHub Desktop.
Characterization tests using Mocha and expect.js to understand how Underscore.js' constant function works
var _ = require("underscore"),
expect = require("expect.js");
// Characterization tests using Mocha and expect.js to understand how
// underscore.js' constant function works
// See http://comp-phil.blogspot.com/2014/03/bird-watching-with-javascript-kestrel.html
describe("Verify that we understand how underscore.js' constant works", function(){
it("Constat will return a function", function(){
expect(_.constant("Hello world")).to.be.a("function");
}),
it("Constat will return a function even when given no value", function(){
expect(_.constant()).to.be.a("function");
}),
it("Constat will return a function even when given undefined", function(){
expect(_.constant(void 0)).to.be.a("function");
}),
it("Constat will return a function even when given null", function(){
expect(_.constant(null)).to.be.a("function");
}),
it("Given two values constant will return the first", function(){
var alwaysFirst = _.constant("First");
expect(alwaysFirst("Second")).to.be.equal("First");
}),
it("Given three values constant will return the first", function(){
var alwaysFirst = _.constant("First");
expect(alwaysFirst("Second", "Third")).to.be.equal("First");
}),
it("Given one value constant will always return that value", function(){
var always42 = _.constant(42);
expect(always42()).to.be.equal(42);
expect(always42()).to.be.equal(42);
expect(always42()).to.be.equal(42);
}),
it("Given a function constant will return that function", function(){
var theTruth = function(){return true;},
nothingButTheTruth = _.constant(theTruth);
expect(nothingButTheTruth()).to.be.a("function");
expect(nothingButTheTruth(false)()).to.be.ok();
expect(nothingButTheTruth(_.identity(42))()).to.be.equal(true);
});
});
@MikeMKH
Copy link
Author

MikeMKH commented Mar 16, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment