Created
December 7, 2011 22:46
-
-
Save jfsiii/1445116 to your computer and use it in GitHub Desktop.
First cut at a simple capability testing
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 (global) { | |
// test: function to run OR value representing result | |
// root: object where results are stored | |
// name: key in storage object | |
// success: function to run on success | |
// failure: function to run on failure | |
// retest: should the test be re-run after a failure | |
// (eg. failure applied a polyfill) | |
// test must be a function to retest | |
function test(options) | |
{ | |
if (!options.root) options.root = {}; | |
test.results = options.root; | |
var run = function (options) | |
{ | |
var result = (typeof options.test === 'function') ? options.test() : options.test; | |
console.log('result', result) | |
options.root[options.name] = result; | |
([options.failure, options.success][+result])(); | |
return result; | |
}; | |
var result = run(options); | |
if (!result && options.retest) result = run(options); | |
return options.root[options.name] = result; | |
} | |
var results = {}; | |
var foo = test({ | |
name: 'global.foo', | |
root: results, | |
test: function () { | |
return 'foo' in global; | |
}, | |
success: function () { | |
console.log('success', global.foo); | |
}, | |
failure: function () { | |
console.log('failure. apply polyfill', global.foo = 'bar') | |
}, | |
retest: true | |
}); | |
console.log('foo', foo, 'test.results', test.results); | |
})(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment