Skip to content

Instantly share code, notes, and snippets.

@GZShi
Last active August 29, 2015 13:57
Show Gist options
  • Save GZShi/9502278 to your computer and use it in GitHub Desktop.
Save GZShi/9502278 to your computer and use it in GitHub Desktop.
function assert(expectation, fn) {
var args = Array.prototype.slice.call(arguments, 2);
var ret = fn.apply(fn, args);
if(ret === expectation) {
return ret;
} else {
throw new TypeError("调用" + fn.name +
"函数时,返回值" + ret + "与预期的" + expectation + "不同");
}
}
// test case
function plus(a, b) { return a + b;}
// ok
assert(10, plus, 5, 5);
// throw error
assert(11, plus, 5, 5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment