Snapshot of upstream SQLite 3.39.4
[sqlcipher.git] / ext / fiddle / SqliteTestUtil.js
blobcf78946dc720f5558275622c1165efc309717765
1 /*
2 2022-05-22
4 The author disclaims copyright to this source code. In place of a
5 legal notice, here is a blessing:
7 * May you do good and not evil.
8 * May you find forgiveness for yourself and forgive others.
9 * May you share freely, never taking more than you give.
11 ***********************************************************************
13 This file contains bootstrapping code used by various test scripts
14 which live in this file's directory.
16 (function(){
17 /* querySelectorAll() proxy */
18 const EAll = function(/*[element=document,] cssSelector*/){
19 return (arguments.length>1 ? arguments[0] : document)
20 .querySelectorAll(arguments[arguments.length-1]);
22 /* querySelector() proxy */
23 const E = function(/*[element=document,] cssSelector*/){
24 return (arguments.length>1 ? arguments[0] : document)
25 .querySelector(arguments[arguments.length-1]);
28 /**
29 Helpers for writing sqlite3-specific tests.
31 self/*window or worker*/.SqliteTestUtil = {
32 /** Running total of the number of tests run via
33 this API. */
34 counter: 0,
35 /**
36 If expr is a function, it is called and its result
37 is returned, coerced to a bool, else expr, coerced to
38 a bool, is returned.
40 toBool: function(expr){
41 return (expr instanceof Function) ? !!expr() : !!expr;
43 /** abort() if expr is false. If expr is a function, it
44 is called and its result is evaluated.
46 assert: function f(expr, msg){
47 if(!f._){
48 f._ = ('undefined'===typeof abort
49 ? (msg)=>{throw new Error(msg)}
50 : abort);
52 ++this.counter;
53 if(!this.toBool(expr)){
54 f._(msg || "Assertion failed.");
56 return this;
58 /** Identical to assert() but throws instead of calling
59 abort(). */
60 affirm: function(expr, msg){
61 ++this.counter;
62 if(!this.toBool(expr)) throw new Error(msg || "Affirmation failed.");
63 return this;
65 /** Calls f() and squelches any exception it throws. If it
66 does not throw, this function throws. */
67 mustThrow: function(f, msg){
68 ++this.counter;
69 let err;
70 try{ f(); } catch(e){err=e;}
71 if(!err) throw new Error(msg || "Expected exception.");
72 return this;
74 /** Throws if expr is truthy or expr is a function and expr()
75 returns truthy. */
76 throwIf: function(expr, msg){
77 ++this.counter;
78 if(this.toBool(expr)) throw new Error(msg || "throwIf() failed");
79 return this;
81 /** Throws if expr is falsy or expr is a function and expr()
82 returns falsy. */
83 throwUnless: function(expr, msg){
84 ++this.counter;
85 if(!this.toBool(expr)) throw new Error(msg || "throwUnless() failed");
86 return this;
91 /**
92 This is a module object for use with the emscripten-installed
93 initSqlite3Module() factory function.
95 self.sqlite3TestModule = {
96 postRun: [
97 /* function(theModule){...} */
99 //onRuntimeInitialized: function(){},
100 /* Proxy for C-side stdout output. */
101 print: function(){
102 console.log.apply(console, Array.prototype.slice.call(arguments));
104 /* Proxy for C-side stderr output. */
105 printErr: function(){
106 console.error.apply(console, Array.prototype.slice.call(arguments));
109 Called by the module init bits to report loading
110 progress. It gets passed an empty argument when loading is
111 done (after onRuntimeInitialized() and any this.postRun
112 callbacks have been run).
114 setStatus: function f(text){
115 if(!f.last){
116 f.last = { text: '', step: 0 };
117 f.ui = {
118 status: E('#module-status'),
119 progress: E('#module-progress'),
120 spinner: E('#module-spinner')
123 if(text === f.last.text) return;
124 f.last.text = text;
125 if(f.ui.progress){
126 f.ui.progress.value = f.last.step;
127 f.ui.progress.max = f.last.step + 1;
129 ++f.last.step;
130 if(text) {
131 f.ui.status.classList.remove('hidden');
132 f.ui.status.innerText = text;
133 }else{
134 if(f.ui.progress){
135 f.ui.progress.remove();
136 f.ui.spinner.remove();
137 delete f.ui.progress;
138 delete f.ui.spinner;
140 f.ui.status.classList.add('hidden');
144 })(self/*window or worker*/);