Permalink
Browse files

- add `assert()` method

- bump minor package version
  • Loading branch information...
1 parent 700a93b commit f8cd628e3b781c53762b8c138de99ac4634602ae @ded committed Sep 13, 2011
Showing with 139 additions and 112 deletions.
  1. +1 −0 .gitignore
  2. +1 −1 package.json
  3. +11 −0 src/sink.css
  4. +117 −110 src/sink.js
  5. +9 −1 tests/tests.js
View
@@ -0,0 +1 @@
+node_modules
View
@@ -1,7 +1,7 @@
{
"name": "sink-test"
, "description": "test your javascript - headless, or in the browser"
- , "version": "0.0.9"
+ , "version": "0.1.0"
, "author": "Dustin Diaz <polvero@gmail.com> (http://dustindiaz.com)"
, "keywords": ["testing", "unit tests", "headless", "tdd", "async"]
, "main": "./src/sink.js"
View
@@ -85,4 +85,15 @@ li ul li {
padding: 0;
margin: 2px 0;
list-style: none;
+ position: relative;
+}
+li b {
+ display: block;
+ margin-left: 40px;
+}
+li em.marker {
+ display: block;
+ position: absolute;
+ right: 5px;
+ top: 0;
}
View
@@ -5,214 +5,221 @@
* License MIT
*/
!function(context) {
- var total = 0,
- logKey = '',
- fail = false,
- modules = [],
- tests = [],
- item,
- setPasses = true,
- allPass = true,
- beforeMethods = [],
- afterMethods = [],
- currentSetName,
- isHeadless = (typeof module !== 'undefined' && module.exports);
+ var total = 0
+ , logKey = ''
+ , fail = false
+ , modules = []
+ , tests = []
+ , item
+ , setPasses = true
+ , allPass = true
+ , beforeMethods = []
+ , afterMethods = []
+ , currentSetName
+ , isHeadless = (typeof module !== 'undefined' && module.exports)
isHeadless ? (require('colors')) : String.prototype.__defineGetter__ && !function () {
each(['red', 'green', 'magenta', 'rainbow', 'yellow'], function (color) {
String.prototype.__defineGetter__(color, function () {
- return this.replace(/( )/, '$1'); // stupid workaround to not log an object
- });
- String.prototype.__defineSetter__(color, function (v) {});
- });
- }();
+ return this.replace(/( )/, '$1') // stupid workaround to not log an object
+ })
+ String.prototype.__defineSetter__(color, function (v) {})
+ })
+ }()
!isHeadless && window && !('console' in window) && !function () {
- context.console = {log: function () {}};
- }();
+ context.console = {log: function () {}}
+ }()
function reset() {
- total = 0;
- fail = false;
- init();
+ total = 0
+ fail = false
+ init()
}
function failure(li, check) {
- setPasses = false;
- allPass = false;
+ setPasses = false
+ allPass = false
if (!isHeadless) {
- check.innerHTML = '';
- li.className = 'fail';
+ check.innerHTML = ''
+ li.className = 'fail'
}
- reset();
+ reset()
}
function each(items, fn) {
- for (var i = 0; i < items.length; i++) {
- fn(items[i]);
- }
+ for (var i = 0; i < items.length; i++) fn(items[i])
}
function pass(li, check) {
if (!isHeadless) {
- check.innerHTML = '';
- li.className = 'pass';
+ check.innerHTML = ''
+ li.className = 'pass'
}
- reset();
+ reset()
}
function before(fn) {
fn ? beforeMethods.push(fn) : each(beforeMethods, function (f) {
- f();
- });
+ f()
+ })
}
function after(fn) {
fn ? afterMethods.push(fn) : each(afterMethods, function (f) {
- f();
- });
+ f()
+ })
}
function bind(li) {
li.onclick = function() {
- var ul;
- ul = this.getElementsByTagName('ul')[0];
- ul.className = (ul.className) ? '' : 'show';
- };
+ var ul = this.getElementsByTagName('ul')[0]
+ ul.className = (ul.className) ? '' : 'show'
+ }
}
function _test(name, expect, fn) {
- before();
- total = expect;
- var li, check;
+ before()
+ total = expect
+ var li, check, start = +new Date
if (!isHeadless) {
- li = document.createElement('li');
- li.innerHTML = name + ' ... <span>o</span><ul></ul>';
- item = li.getElementsByTagName('ul')[0];
- bind(li);
- check = li.getElementsByTagName('span')[0];
- document.getElementById('tests').appendChild(li);
+ li = document.createElement('li')
+ li.innerHTML = name + ' ... <span>o</span><ul></ul>'
+ item = li.getElementsByTagName('ul')[0]
+ bind(li)
+ check = li.getElementsByTagName('span')[0]
+ document.getElementById('tests').appendChild(li)
} else {
- console.log(logKey + (name + '...').yellow);
+ console.log(logKey + (name + '...').yellow)
}
- var start = +new Date;
- fn();
+ fn()
setTimeout(function() {
if (sink.timeout && (+new Date - start > sink.timeout)) {
- failure(li, check);
- after();
+ failure(li, check)
+ after()
} else {
if (fail) {
- failure(li, check);
- after();
+ failure(li, check)
+ after()
} else if (!total) {
- after();
- pass(li, check);
+ after()
+ pass(li, check)
} else {
- setTimeout(arguments.callee, 10);
+ setTimeout(arguments.callee, 10)
}
}
- }, 10);
+ }, 10)
}
function test(name, expect, fn) {
tests.push({
name: name,
expect: expect,
fn: fn
- });
+ })
}
function init() {
if (tests.length) {
- var o = tests.shift();
- _test(o.name, o.expect, o.fn);
+ var o = tests.shift()
+ _test(o.name, o.expect, o.fn)
} else {
- setPasses = true;
- start();
+ setPasses = true
+ start()
}
}
- function ok(b, message) {
+ function assert(actual, expected, msg) {
+ var b = actual === expected
+ , message = b ? '' : '<b>actual: ' + actual.toString() + '</b><b>expected: ' + expected.toString() + '</b>'
if (isHeadless) {
- if (b) {
- console.log(logKey + (message + '').green);
- } else {
- console.log(logKey + (message + '').red);
- }
+ message = b ? '' : '\n\tactual: ' + actual.toString() + '\n\texpected: ' + expected.toString()
+ if (b) console.log(logKey + msg + (message + '').green)
+ else console.log(logKey + msg + (message + '').red)
} else {
- var li = document.createElement('li');
- li.innerHTML = message + ' ' + (b ? '' : '');
- item.appendChild(li);
+ var li = document.createElement('li')
+ li.innerHTML = msg + ' ' + message + ' ' + '<em class="marker">' + (b ? '' : '') + '</em>'
+ item.appendChild(li)
}
- if (b) {
- total--;
+ if (b) total--
+ else fail = true
+ }
+
+ function ok(b, message) {
+ if (isHeadless) {
+ if (b) console.log(logKey + (message + '').green)
+ else console.log(logKey + (message + '').red)
} else {
- fail = true;
+ var li = document.createElement('li')
+ li.innerHTML = message + ' ' + (b ? '' : '')
+ item.appendChild(li)
}
+
+ if (b) total--
+ else fail = true
}
function sink(name, fn) {
modules.push({
name: name,
fn: fn
- });
+ })
}
function nextGroup(name, fn) {
- beforeMethods = [];
- afterMethods = [];
- var mod = ('MODULE: ' + name);
+ beforeMethods = []
+ afterMethods = []
+ var mod = ('MODULE: ' + name)
if (isHeadless) {
- console.log(logKey + mod.magenta);
+ console.log(logKey + mod.magenta)
} else {
- var li = document.createElement('li');
- li.innerHTML = mod;
- document.getElementById('tests').appendChild(li);
- li.className = 'mod';
+ var li = document.createElement('li')
+ li.innerHTML = mod
+ document.getElementById('tests').appendChild(li)
+ li.className = 'mod'
}
- fn(test, ok, before, after);
- currentSetName = name;
- init();
+ fn(test, ok, before, after, assert)
+ currentSetName = name
+ init()
}
function start() {
- var current = modules.shift();
+ var current = modules.shift()
current ? nextGroup(current.name, current.fn) : !function () {
var message = [
- 'Congratulations! All tests have passed!',
- 'There were some errors! The suite has failed.'
- ];
- var color = allPass ? 'rainbow' : 'red',
- status = allPass ? 'sink-pass' : 'sink-failure';
- message = message[allPass ? 0 : 1].toUpperCase();
+ 'Congratulations! All tests have passed!'
+ , 'There were some errors! The suite has failed.'
+ ]
+ , color = allPass ? 'rainbow' : 'red'
+ , status = allPass ? 'sink-pass' : 'sink-failure'
+ message = message[allPass ? 0 : 1].toUpperCase()
isHeadless ?
console.log(logKey + message[color]) :
- (document.getElementById('tests').className = status);
- }();
+ (document.getElementById('tests').className = status)
+ }()
}
function setLogKey (key) {
- var log = console.log;
- logKey = key || '$__sinkTest::';
+ var log = console.log
+ logKey = key || '$__sinkTest::'
console.log = function (msg) {
if (~(''+msg).indexOf(logKey)) {
- log(msg.replace(logKey, ''));
+ log(msg.replace(logKey, ''))
}
}
}
if (isHeadless) {
- exports.sink = sink;
- exports.start = start;
- exports.sink.timeout = 10000;
- exports.setLogKey = setLogKey;
+ exports.sink = sink
+ exports.start = start
+ exports.sink.timeout = 10000
+ exports.setLogKey = setLogKey
} else {
- context.sink = sink;
- context.start = start;
- context.sink.timeout = 10000;
+ context.sink = sink
+ context.start = start
+ context.sink.timeout = 10000
}
-}(this);
+}(this)
View
@@ -71,12 +71,20 @@ if (typeof module !== 'undefined' && module.exports) {
});
}
-sink('timeout tests (takes 20 seconds)', function (test, ok, before, after) {
+sink('timeout tests (takes 20 seconds)', function (test, ok, before, after, assert) {
before(function () {
sink.timeout = false
})
+ test('should be able to assert stuff. this shows fail', 1, function () {
+ assert(1, 6, 'should have same numbers')
+ })
+
+ test('should be able to assert stuff. this shows pass', 1, function () {
+ assert(2, 2, 'should have same numbers')
+ })
+
test('should pass a test thing or two', 1, function () {
setTimeout(function () {
ok(true, 'timeout successfully nulled!')

0 comments on commit f8cd628

Please sign in to comment.