Last active
October 19, 2015 04:21
-
-
Save buren/ba8bad40021e2d57832d to your computer and use it in GitHub Desktop.
Minimal JS test DSL
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
function describe(label, testFunction) { | |
return new test(label, testFunction); | |
} | |
function test(label, testFunction) { | |
var self = this; | |
self.testLabel = label; | |
self.itLabel; | |
var label = function() { return self.testLabel + ' ' + self.itLabel; }; | |
var passed = function() { console.log(label(), true); }; | |
var failed = function(result, expected) { | |
console.error(label(), 'Expected:', expected, 'but was', result); | |
}; | |
self.it = function(label, itFunction) { | |
self.itLabel = label; | |
return itFunction(self.assert); | |
}; | |
self.assert = function(result, expected) { | |
if (result === expected) { | |
passed(); | |
} else { | |
failed(result, expected); | |
} | |
}; | |
testFunction(self.it); | |
} |
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
describe('describe', function(it) { | |
var emptyFunction = function() {}; | |
var itFunction = function(it) { | |
it('it label', emptyFunction); | |
}; | |
var assertFunction = function(it) { | |
it('', function(assert) { | |
assert(true, true); | |
}) | |
}; | |
it('has a label', function(assert) { | |
var label = 'a label'; | |
var result = describe(label, emptyFunction) | |
assert(result.testLabel, label); | |
}); | |
describe('it', function(it) { | |
it('has a label', function(assert) { | |
var result = describe('', itFunction); | |
assert(result.itLabel, 'it label'); | |
}); | |
}); | |
describe('assert', function(it) { | |
it('should have assert function', function(assert) { | |
var result = describe('label', itFunction); | |
assert(result.assert instanceof Function, true); | |
}); | |
}); | |
}); |
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
describe('test', function(it) { | |
it('can assert true', function(assert) { | |
assert(true, true); | |
}); | |
it('should really NOT work', function(assert) { | |
assert(false, true); | |
}); | |
describe('nested describes', function(it) { | |
it('should work again', function(assert) { | |
assert(true, true); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment