Last active
January 5, 2016 11:16
-
-
Save bedeabza/c2a6e7e10c6197b76483 to your computer and use it in GitHub Desktop.
JS Simple Assert
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
.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; | |
} |
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
/** | |
* 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