Skip to content

Instantly share code, notes, and snippets.

@ryanlid
Created August 19, 2019 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanlid/bbb0b365c837aeb1cf5d215a8bea7ce6 to your computer and use it in GitHub Desktop.
Save ryanlid/bbb0b365c837aeb1cf5d215a8bea7ce6 to your computer and use it in GitHub Desktop.
JavaScript 断言的简单实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Test Suite</title>
<style>
#results li.pass {
color: green;
}
#results li.fail {
color: red;
}
</style>
</head>
<body>
<ul id="results"></ul>
<script>
// 创建一个assert方法
function assert(value, desc) {
var li = document.createElement("li");
li.className = value ? "pass" : "fail";
li.appendChild(document.createTextNode(desc));
document.getElementById("results").appendChild(li);
}
function report(text) {
assert(true, text);
}
window.onload = function() {
// 通过断言执行测试用例
assert(true, "The test suite is running.");
assert(false, "Fail!");
console.assert(true, "The test suite is running.");
console.assert(false, "Fail!");
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment