Skip to content

Instantly share code, notes, and snippets.

@jfsiii
Created December 7, 2011 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfsiii/1445116 to your computer and use it in GitHub Desktop.
Save jfsiii/1445116 to your computer and use it in GitHub Desktop.
First cut at a simple capability testing
(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