Skip to content

Instantly share code, notes, and snippets.

@bedeabza
Last active January 5, 2016 11:16
Show Gist options
  • Save bedeabza/c2a6e7e10c6197b76483 to your computer and use it in GitHub Desktop.
Save bedeabza/c2a6e7e10c6197b76483 to your computer and use it in GitHub Desktop.
JS Simple Assert
.assertions {
list-style-type: none;
margin: 0;
padding: 0;
border-top: 1px solid #000;
background-color: #fff;
position: fixed;
bottom: 0;
left: 0;
width: 100%;
min-height: 100px;
max-height: 300px;
overflow: auto;
}
.assertions li {
padding: 10px 10px;
border-bottom: 1px solid #ccc;
}
.assertions .error {
color: red;
font-weight: bold;
}
.assertions .success {
color: green;
}
/**
* Usage
* ------------------------------------------------------------------------
* assert('Description of assertion', condition, boolWhetherToDebugOnFail);
* ------------------------------------------------------------------------
*
* Example
* ------------------------------------------------------------------------
* assert('String "1" equals numeric 1', '1' == 1);
* ------------------------------------------------------------------------
*/
(function() {
var container = document.getElementById('assert-container');
this.assert = function(what, condition, debug) {
var li = document.createElement('li');
container.appendChild(li);
if (condition) {
li.classList.add('success');
what = '✓ ' + what;
} else {
li.classList.add('error');
what = '✘ ' + what;
}
li.innerText = what;
container.scrollTop = 999999;
if (!condition) {
if (debug) {
debugger;
} else {
throw new Error('Execution stopped because of failed assertion');
}
}
};
if (!container) {
container = document.createElement('ul');
container.id = 'assert-container';
container.classList.add('assertions');
document.body.appendChild(container);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment