-
Notifications
You must be signed in to change notification settings - Fork 1
/
QUnitGS2.gs
332 lines (257 loc) · 9.08 KB
/
QUnitGS2.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// JSHint - 20200716
/* jshint asi: true */
/* jshint esversion: 6*/
(function() {"use strict"})()
/**
* QUnitG2S
* ========
*
* <pre>
* QUnit2GS is a Google Apps Script Library that allows Apps Script projects to be tested
* using the QUnit JavaScript testing framework - qunitjs.com.
*
* << Usage >>
*
* - Publish a "doGet()" that calls the following functions:
*
* function doGet() {
* QUnitGS2.init()
* userDefinedtestFunctions()
* QUnit.start()
* return QUnitGS2.getHtml()
* }
*
* - Provide a getResultsFromServer() which simply returns QUnitGS2.getResultsFromServer()
*
* For example:
*
* function getResultsFromServer() {
* return QUnitGS2.getResultsFromServer()
* }
*
* Further examples can be seen in the QUnitGS2 Test project:
*
* script.google.com/d/1cmwYQ6H7k6v3xNoFhhcASR8K2_JBJcgJ2W0WFNE8Sy3fAJzfE2Kpbh_M
*
* << Differences from main QUnit library >>
*
* - QUnitGS2 provides a wrapper to the main QUnit library. It creates the HTML/CSS
* to display in an Apps Script web app based on the JSON test results
* returned by various QUnit callback functions. This is in contrast to how the
* QUnitGS library based on v1 worked, which was to update the main library to
* display the HTML as a web app.
*
* github.com/simula-innovation/qunit/tree/gas/gas
*
* - The published QUnit library functions are in a separate file for the library
* creation and API documentation generation to work.
*
* - The GUI is not displayed immediately, but rather only after the tests have run.
*
* - Tests are run on Google's servers, not in your browser. The browser only
* displays test results.
*
* - Asynchronous testing is limited. There is no setTimeout() function in Google
* Apps Script. If you create time-based triggers via the Script service, the
* doGet() function is more likely to timeout than your test.
*
* - HTML and client-side JavaScript is generated using Google's HtmlService.
*
* << Upgrading QUnitGS2 >>
*
* To upgrade to a later version of QUnit simply:
*
* - Copy the new version into qunitjs.gs
*
* - Ensure the main QUnit object is exported from the main Qunit library by
* adding to exportQUnit():
*
* if (typeof HtmlService !== undefined) global$1.QUnit = QUnit;
*
* - Create a new version of the script project. See:
*
* developers.google.com/apps-script/guides/versions
*
* << QUnit config >>
*
* Config settings are passed straight through to the main QUnit library.
* See https://api.qunitjs.com/config/QUnit.config for details.
* </pre>
*/
/**
* <pre>
* Main initialisation function for QUnitGS2, called from the doGet in the
* script project running the tests
* </pre>
*/
function init() {
CACHE_.remove('qunit_test_results')
QUnit.config.autostart = false;
QUnit.config.storage = {
store: {},
setItem: function(key, value) {this.store[key] = value},
getItem: function(key) {return this.store[key]},
removeItem: function(key) {delete this.store[key]},
clear: function() {this.store = {}},
}
// Set up callbacks that will store the results
QUnit.done(onAllTestsFinished)
QUnit.testDone(onTestDone)
QUnit.log(onAssertResult)
return
// Private Functions
// -----------------
function onAllTestsFinished(details) {
push('TESTS_RESULTS_ALL', details)
}
function onTestDone(details) {
push('TESTS_RESULTS_ONE', details)
}
function onAssertResult(details) {
push('TESTS_RESULTS_ASSERT', details)
}
function push(type, details) {
var cacheString = CACHE_.get('qunit_test_results')
cache = (cacheString === null) ? [] : JSON.parse(cacheString)
updateCacheWithTestDetails()
CACHE_.put(
'qunit_test_results',
JSON.stringify(cache)
)
return
// Private Functions
// -----------------
function updateCacheWithTestDetails() {
if (type === 'TESTS_RESULTS_ASSERT') {
details.expected = typeof details.expected === 'object' ? QUnit.dump.parse(details.expected) : details.expected
details.actual = typeof details.actual === 'object' ? QUnit.dump.parse(details.actual) : details.actual
details.diff = getDiff()
getTest(details.testId).value.assertions.push(details)
} else if (type === 'TESTS_RESULTS_ONE') {
getTest(details.testId).value.results = details
} else if (type === 'TESTS_RESULTS_ALL') {
getSummary().value = details
} else {
throw new Error('Bad result type: "' + type + '"')
}
return
// Private Functions
// -----------------
function getDiff() {
var diff
var showDiff
if (typeof details.actual === "number" && typeof details.expected === "number") {
if (!isNaN(details.actual) && !isNaN(details.expected)) {
showDiff = true
diff = details.actual - details.expected
diff = (diff > 0 ? "+" : "") + diff
}
} else if (typeof details.actual !== "boolean" && typeof details.expected !== "boolean") {
diff = QUnit.diff(details.expected, details.actual)
// don't show diff if there is zero overlap
showDiff = stripHtml(diff).length !== stripHtml(details.expected).length + stripHtml(details.actual).length;
}
return (showDiff) ? diff : ''
// Private Functions
// -----------------
function stripHtml(string) {
// Strip tags, html entity and whitespaces
return string.replace(/<\/?[^>]+(>|$)/g, "").replace(/"/g, "").replace(/\s+/g, "");
}
}
function getTest(testId) {
var test = null
cache.some(item => {
if (item.type === 'TESTS_RESULTS_ONE' && item.value.id === testId) {
test = item
return true
}
})
if (test === null) {
test = {
type: 'TESTS_RESULTS_ONE',
value: {
id: testId,
results: {},
assertions: []
},
}
cache.push(test)
}
return test
} // init.push.updateCacheWithTestDetails.getTest()
} // init.push.updateCacheWithTestDetails()
} // init.push()
function getSummary() {
var summary = null
cache.some(item => {
if (item.type === 'TESTS_RESULTS_ALL') {
summary = item
return true
}
})
if (summary === null) {
summary = {
type: 'TESTS_RESULTS_ALL',
value: {}
}
cache.push(summary)
}
return summary
}
} // init()
/**
* <pre>
* This is called by the project using the QUnitGS2 library to get the initial HTML
* displayed by the web app.
*
* Once the test results are returned by the main QUnit library client-side JS in
* the web app will display them appropriately
* </pre>
*/
function getHtml() {
var mainTemplate = HtmlService.createTemplateFromFile( "index" );
mainTemplate.title = QUnit.config.title ? QUnit.config.title : "QUnit v2.9.2 for Google Apps Script";
var toolbarTemplate = HtmlService.createTemplateFromFile( "qunit-toolbar" );
if (typeof QUnit.config.cssUrl === "string" ) {
mainTemplate.styles =
'<link rel="stylesheet" href="' + QUnit.config.cssUrl + '" type="text/css" media="screen" />'
} else {
mainTemplate.styles = '<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.9.2.css">';
}
toolbarTemplate.checked = QUnit.config.hidepassed ? "checked='checked'" : "";
// toolbarTemplate.urlConfigs = urlConfigHtml; // TODO
toolbarTemplate.urlConfigs = '';
var htmlCollection = {main: mainTemplate}
htmlCollection.toolbar = toolbarTemplate;
testHtml = HtmlService.createHtmlOutput();
htmlCollection.main.testUrl = htmlCollection.toolbar.testUrl = ScriptApp.getService().getUrl();
htmlCollection.main.hidepassed = QUnit.config.hidepassed;
htmlCollection.main.toolbar = htmlCollection.toolbar.evaluate().getContent();
return htmlCollection.main.evaluate().setTitle(htmlCollection.main.title);
} // getHtml()
/**
* <pre>
* Called by a function in the test project of the same name to get the results
* from the cache once the tests have completed
*
* For example:
*
* function getResultsFromServer() {
* return QUnitGS2.getResultsFromServer()
* }
* </pre>
*/
function getResultsFromServer() {
return CACHE_.get('qunit_test_results')
}
/**
* Not part of the public API, but has to be global so that the web app can see it
*/
function _include(filename) {
return HtmlService.createHtmlOutputFromFile(filename).getContent()
}
// Apps Script only runs sync, so setTimeoutis "stubbed out" and just runs the
// callback immediately
function setTimeout(callback, timeout) {return callback()} // TODO
var CACHE_ = CacheService.getUserCache()