Created
March 9, 2014 19:49
-
-
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
This file contains hidden or 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
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); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See my blog post which goes with this gist: http://comp-phil.blogspot.com/2014/03/bird-watching-with-javascript-kestrel.html