Javascript: Assertion
ul {
padding: 10px;
background: #feff93;
}
li {
margin: 0;
font-weight: bold;
font-family: Arial;
}
p {
margin: 0;
}
.PASS strong {
color: green;
}
.FAIL strong {
color: red;
}
function assert (value, desc) {
var li = document.createElement("li");
var bold = document.createElement("strong");
li.className = value ? "PASS" : "FAIL";
bold.appendChild(document.createTextNode(li.className+": "));
li.appendChild(bold);
li.appendChild(document.createTextNode(desc));
document.getElementById("results").appendChild(li);
}
function log(desc) {
var li = document.createElement("li");
li.appendChild(document.createTextNode("LOG: " + desc));
document.getElementById("results").appendChild(li);
}
function error(desc) {
var li = document.createElement("li");
var bold = document.createElement("strong");
li.className = "FAIL";
bold.appendChild(document.createTextNode("ERROR: "));
li.appendChild(bold);
li.appendChild(document.createTextNode(desc));
document.getElementById("results").appendChild(li);
}
assert(true, 'test passed');
assert(false, 'test failed');
log('testowy log');
error('this is error');
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Assertion" />
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<ol id="results"></ol>
</body>
</html>