diff options
author | stephan <stephan@noemail.net> | 2022-05-23 19:38:57 +0000 |
---|---|---|
committer | stephan <stephan@noemail.net> | 2022-05-23 19:38:57 +0000 |
commit | e145136a4e2e0b76d3a64d3f47783dcf9c3143bb (patch) | |
tree | 33225b23dc4d980e5c65a0469fa927e3aeee106a /ext/fiddle | |
parent | 325b56b5049460c04ccf675630d620494f0b1cea (diff) | |
download | sqlite-e145136a4e2e0b76d3a64d3f47783dcf9c3143bb.tar.gz sqlite-e145136a4e2e0b76d3a64d3f47783dcf9c3143bb.zip |
wasm: minor refactoring and doc updates.
FossilOrigin-Name: 6044605b2a712da73600cabb967797a03ed1915dc0ab0b10edbd52525e548196
Diffstat (limited to 'ext/fiddle')
-rw-r--r-- | ext/fiddle/SqliteTestUtil.js | 54 | ||||
-rw-r--r-- | ext/fiddle/testing-common.js | 63 | ||||
-rw-r--r-- | ext/fiddle/testing1.html | 1 | ||||
-rw-r--r-- | ext/fiddle/testing1.js | 16 |
4 files changed, 69 insertions, 65 deletions
diff --git a/ext/fiddle/SqliteTestUtil.js b/ext/fiddle/SqliteTestUtil.js new file mode 100644 index 000000000..964e60b21 --- /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 79bb0ba9d..270148128 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 56d2cb53b..d428f12f6 100644 --- a/ext/fiddle/testing1.html +++ b/ext/fiddle/testing1.html @@ -25,6 +25,7 @@ </div><!-- /emscripten bits --> <div>Everything on this page happens in the dev console.</div> <script src="testing-common.js"></script> + <script src="SqliteTestUtil.js"></script> <script src="testing1.js"></script> <script src="sqlite3.js"></script> </body> diff --git a/ext/fiddle/testing1.js b/ext/fiddle/testing1.js index 7f024bf03..a59d2d2cc 100644 --- a/ext/fiddle/testing1.js +++ b/ext/fiddle/testing1.js @@ -16,12 +16,12 @@ const mainTest1 = function(namespace){ const S = namespace.sqlite3.api; const oo = namespace.sqlite3.SQLite3; - const T = self.SqliteTester; + const T = self.SqliteTestUtil; console.log("Loaded module:",S.sqlite3_libversion(), S.sqlite3_sourceid()); const db = new oo.DB(); + const log = console.log.bind(console); try { - const log = console.log.bind(console); T.assert(db._pDb); log("DB:",db.filename); log("Build options:",oo.compileOptionUsed()); @@ -95,7 +95,11 @@ INSERT INTO t(a,b) VALUES(1,2),(3,4),(?,?);`, } }; -self/*window or worker*/.Module.onRuntimeInitialized = function(){ - console.log("Loading sqlite3-api.js..."); - self.Module.loadSqliteAPI(mainTest1); -}; +self/*window or worker*/.Module.postRun.push(function(theModule){ + /** Use a timeout so that we are (hopefully) out from under the + module init stack when our setup gets run. */ + + setTimeout(function(){ + theModule.loadSqliteAPI(mainTest1); + },0); +}); |