diff --git a/ext/fiddle/SqliteTestUtil.js b/ext/fiddle/SqliteTestUtil.js new file mode 100644 index 0000000000..964e60b215 --- /dev/null +++ b/ext/fiddle/SqliteTestUtil.js @@ -0,0 +1,54 @@ +/** + Helpers for writing sqlite3-specific tests. +*/ +self/*window or worker*/.SqliteTestUtil = { + /** Running total of the number of tests run via + this API. */ + counter: 0, + /** + If expr is a function, it is called and its result + is returned, coerced to a bool, else expr, coerced to + a bool, is returned. + */ + toBool: function(expr){ + return (expr instanceof Function) ? !!expr() : !!expr; + }, + /** abort() if expr is false. If expr is a function, it + is called and its result is evaluated. + */ + assert: function(expr, msg){ + ++this.counter; + if(!this.toBool(expr)) abort(msg || "Assertion failed."); + return this; + }, + /** Identical to assert() but throws instead of calling + abort(). */ + affirm: function(expr, msg){ + ++this.counter; + if(!this.toBool(expr)) throw new Error(msg || "Affirmation failed."); + return this; + }, + /** Calls f() and squelches any exception it throws. If it + does not throw, this function throws. */ + mustThrow: function(f, msg){ + ++this.counter; + let err; + try{ f(); } catch(e){err=e;} + if(!err) throw new Error(msg || "Expected exception."); + return this; + }, + /** Throws if expr is truthy or expr is a function and expr() + returns truthy. */ + throwIf: function(expr, msg){ + ++this.counter; + if(this.toBool(expr)) throw new Error(msg || "throwIf() failed"); + return this; + }, + /** Throws if expr is falsy or expr is a function and expr() + returns falsy. */ + throwUnless: function(expr, msg){ + ++this.counter; + if(!this.toBool(expr)) throw new Error(msg || "throwUnless() failed"); + return this; + } +}; diff --git a/ext/fiddle/testing-common.js b/ext/fiddle/testing-common.js index 79bb0ba9d9..2701481289 100644 --- a/ext/fiddle/testing-common.js +++ b/ext/fiddle/testing-common.js @@ -37,10 +37,10 @@ postRun: [], //onRuntimeInitialized: function(){}, print: function(){ - console.log(Array.prototype.slice.call(arguments)); + console.log.apply(console, Array.prototype.slice.call(arguments)); }, printErr: function(){ - console.error(Array.prototype.slice.call(arguments)); + console.error.apply(console, Array.prototype.slice.call(arguments)); }, setStatus: function f(text){ if(!f.last) f.last = { time: Date.now(), text: '' }; @@ -74,8 +74,8 @@ /* Loads sqlite3-api.js and calls the given callback (if provided), passing it an object which contains the sqlite3 and SQLite3 modules. Whether this is synchronous or async - depends on whether it's run in the main thread or a - worker.*/ + depends on whether it's run in the main thread (async) or a + worker (synchronous). */ loadSqliteAPI: function(callback){ const theScript = 'sqlite3-api.js'; if(self.importScripts){/*worker*/ @@ -96,60 +96,5 @@ } } }; - - /** - Helpers for writing sqlite3-specific tests. - */ - self.SqliteTester = { - /** Running total of the number of tests run via - this API. */ - counter: 0, - /** - If expr is a function, it is called and its result - is returned, coerced to a bool, else expr, coerced to - a bool, is returned. - */ - toBool: function(expr){ - return (expr instanceof Function) ? !!expr() : !!expr; - }, - /** abort() if expr is false. If expr is a function, it - is called and its result is evaluated. - */ - assert: function(expr, msg){ - ++this.counter; - if(!this.toBool(expr)) abort(msg || "Assertion failed."); - return this; - }, - /** Identical to assert() but throws instead of calling - abort(). */ - affirm: function(expr, msg){ - ++this.counter; - if(!this.toBool(expr)) throw new Error(msg || "Affirmation failed."); - return this; - }, - /** Calls f() and squelches any exception it throws. If it - does not throw, this function throws. */ - mustThrow: function(f, msg){ - ++this.counter; - let err; - try{ f(); } catch(e){err=e;} - if(!err) throw new Error(msg || "Expected exception."); - return this; - }, - /** Throws if expr is truthy or expr is a function and expr() - returns truthy. */ - throwIf: function(expr, msg){ - ++this.counter; - if(this.toBool(expr)) throw new Error(msg || "throwIf() failed"); - return this; - }, - /** Throws if expr is falsy or expr is a function and expr() - returns falsy. */ - throwUnless: function(expr, msg){ - ++this.counter; - if(!this.toBool(expr)) throw new Error(msg || "throwUnless() failed"); - return this; - } - }; })(self/*window or worker*/); diff --git a/ext/fiddle/testing1.html b/ext/fiddle/testing1.html index 56d2cb53b3..d428f12f65 100644 --- a/ext/fiddle/testing1.html +++ b/ext/fiddle/testing1.html @@ -25,6 +25,7 @@