Created
August 19, 2019 14:45
-
-
Save ryanlid/bbb0b365c837aeb1cf5d215a8bea7ce6 to your computer and use it in GitHub Desktop.
JavaScript 断言的简单实现
This file contains 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
<!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