Skip to content

Instantly share code, notes, and snippets.

@nphyx
Created February 28, 2016 17:24
Show Gist options
  • Save nphyx/c6634aae73e29523635a to your computer and use it in GitHub Desktop.
Save nphyx/c6634aae73e29523635a to your computer and use it in GitHub Desktop.
A simple should.js extension for testing approximate values for array-like things. Works like approximately, but with arrays. Usage: [9.3567, 1.23456, 1.11112].should.be.nearly([9.36, 1.24, 1.12], 0.01);
const should = require("should");
should.Assertion.add("nearly", function(arr, delta) {
try {
for(let i = 0, len = arr.length; i < len; i++) {
this.obj[i].should.be.approximately(arr[i], delta);
}
}
catch(e) {
throw new Error("expected ["+this.obj.toString()+"] to be approximately ["+arr.toString()+"] ± "+delta);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment