142
St
startsInvasion / files
startsInvasion / libs / node_modules / qwery / build / sink / src / sink.js
4.5 KB
1/*! 2 * Sink - Browser & Headless JavaScript Unit Tester 3 * copyright Dustin Diaz & Jacob Thornton 4 * https://github.com/ded/sink-test 5 * License MIT 6 */ 7!function(context) { 8 var total = 0, 9 fail = false, 10 modules = [], 11 tests = [], 12 item, 13 setPasses = true, 14 allPass = true, 15 beforeMethods = [], 16 afterMethods = [], 17 currentSetName, 18 isHeadless = (typeof module !== 'undefined' && module.exports); 19 20 isHeadless ? (require('colors')) : String.prototype.__defineGetter__ && !function () { 21 each(['red', 'green', 'magenta', 'rainbow', 'yellow'], function (color) { 22 String.prototype.__defineGetter__(color, function () { 23 return this.replace(/( )/, '$1'); // stupid workaround to not log an object 24 }); 25 String.prototype.__defineSetter__(color, function (v) {}); 26 }); 27 }(); 28 29 !isHeadless && window && !('console' in window) && !function () { 30 context.console = {log: function () {}}; 31 }(); 32 33 function reset() { 34 total = 0; 35 fail = false; 36 init(); 37 } 38 39 function failure(li, check) { 40 setPasses = false; 41 allPass = false; 42 if (!isHeadless) { 43 check.innerHTML = '✗'; 44 li.className = 'fail'; 45 } 46 reset(); 47 } 48 49 function each(items, fn) { 50 for (var i = 0; i < items.length; i++) { 51 fn(items[i]); 52 } 53 } 54 55 function pass(li, check) { 56 if (!isHeadless) { 57 check.innerHTML = '✓'; 58 li.className = 'pass'; 59 } 60 reset(); 61 } 62 63 function before(fn) { 64 fn ? beforeMethods.push(fn) : each(beforeMethods, function (f) { 65 f(); 66 }); 67 } 68 69 function after(fn) { 70 fn ? afterMethods.push(fn) : each(afterMethods, function (f) { 71 f(); 72 }); 73 } 74 75 function bind(li) { 76 li.onclick = function() { 77 this.getElementsByTagName('ul')[0].className = 'show'; 78 }; 79 } 80 81 function _test(name, expect, fn) { 82 before(); 83 total = expect; 84 var li, check; 85 if (!isHeadless) { 86 li = document.createElement('li'); 87 li.innerHTML = name + ' ... <span>o</span><ul></ul>'; 88 item = li.getElementsByTagName('ul')[0]; 89 bind(li); 90 check = li.getElementsByTagName('span')[0]; 91 document.getElementById('tests').appendChild(li); 92 } else { 93 console.log((name + '...').yellow); 94 } 95 96 var start = +new Date; 97 fn(); 98 setTimeout(function() { 99 if (+new Date - start > 10000) { 100 failure(li, check); 101 after(); 102 } else { 103 if (fail) { 104 failure(li, check); 105 after(); 106 } else if (!total) { 107 after(); 108 pass(li, check); 109 } else { 110 setTimeout(arguments.callee, 10); 111 } 112 } 113 }, 10); 114 } 115 116 function test(name, expect, fn) { 117 tests.push({ 118 name: name, 119 expect: expect, 120 fn: fn 121 }); 122 } 123 124 function init() { 125 if (tests.length) { 126 var o = tests.shift(); 127 _test(o.name, o.expect, o.fn); 128 } else { 129 setPasses = true; 130 start(); 131 } 132 } 133 134 function ok(b, message) { 135 if (isHeadless) { 136 if (b) { 137 console.log((message + ' ✓').green); 138 } else { 139 console.log((message + ' ✗').red); 140 } 141 } else { 142 var li = document.createElement('li'); 143 li.innerHTML = message + ' ' + (b ? '✓' : '✗'); 144 item.appendChild(li); 145 } 146 147 if (b) { 148 total--; 149 } else { 150 fail = true; 151 } 152 } 153 154 function sink(name, fn) { 155 modules.push({ 156 name: name, 157 fn: fn 158 }); 159 } 160 161 function nextGroup(name, fn) { 162 beforeMethods = []; 163 afterMethods = []; 164 var mod = ('MODULE: ' + name); 165 if (isHeadless) { 166 console.log(mod.magenta); 167 } else { 168 var li = document.createElement('li'); 169 li.innerHTML = mod; 170 document.getElementById('tests').appendChild(li); 171 li.className = 'mod'; 172 } 173 fn(test, ok, before, after); 174 currentSetName = name; 175 init(); 176 } 177 178 function start() { 179 var current = modules.shift(); 180 current ? nextGroup(current.name, current.fn) : !function () { 181 var message = [ 182 'Congratulations! All tests have passed!', 183 'There were some errors! The suite has failed.' 184 ]; 185 var color = allPass ? 'rainbow' : 'red', 186 status = allPass ? 'sink-pass' : 'sink-failure'; 187 message = message[allPass ? 0 : 1].toUpperCase(); 188 isHeadless ? 189 console.log(message[color]) : 190 (document.getElementById('tests').className = status); 191 }(); 192 } 193 194 if (isHeadless) { 195 exports.sink = sink; 196 exports.start = start; 197 } else { 198 context.sink = sink 199 context.start = start; 200 } 201 202}(this);