bare-script 3.2.2 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,462 @@
1
+ # Licensed under the MIT License
2
+ # https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
3
+
4
+
5
+ # Include sentinel
6
+ if systemGlobalGet('unittestMockSentinel'):
7
+ return
8
+ endif
9
+ unittestMockSentinel = true
10
+
11
+
12
+ # Constants
13
+ unittestMockPixelsPerPoint = 4 / 3
14
+ unittestMockDefaultFontSizePx = 12 * unittestMockPixelsPerPoint
15
+ unittestMockFontWidthRatio = 0.6
16
+ unittestMockWindowHeight = 768
17
+ unittestMockWindowWidth = 1024
18
+
19
+
20
+ # The mocked MarkdownUp state
21
+ unittestMockCalls = arrayNew()
22
+ unittestMockFunctions = objectNew()
23
+ unittestMockStateDefault = objectNew( \
24
+ 'drawingFontSizePx', unittestMockDefaultFontSizePx, \
25
+ 'drawingHeight', 480, \
26
+ 'drawingWidth', 640, \
27
+ 'localStorage', objectNew(), \
28
+ 'sessionStorage', objectNew(), \
29
+ 'windowClipboard', '' \
30
+ )
31
+ unittestMockState = objectCopy(unittestMockStateDefault)
32
+
33
+
34
+ # $function: unittestMockAll
35
+ # $group: unittestMock.bare
36
+ # $doc: Start mocking all BareScript and MarkdownUp library functions with externalities.
37
+ # $doc: To stop mocking, call the [unittestMockEnd](#var.vGroup='unittestMock.bare'&unittestmockend) function.
38
+ # $arg data: Optional (default is null). The map of function name to mock function data.
39
+ # $arg data: The following functions make use of mock data:
40
+ # $arg data: - **documentInputValue** - map of id to return value
41
+ # $arg data: - **markdownParse** - array of return values
42
+ # $arg data: - **markdownTitle** - array of return values
43
+ # $arg data: - **systemFetch** - map of URL to response text
44
+ function unittestMockAll(data):
45
+ # Data
46
+ unittestMockOneGeneric('dataLineChart')
47
+ unittestMockOneGeneric('dataTable')
48
+
49
+ # Document
50
+ unittestMockOne('documentFontSize', unittestMock_documentFontSize)
51
+ unittestMockOne('documentInputValue', systemPartial(unittestMock_documentInputValue, if(data != null, objectGet(data, 'documentInputValue'))))
52
+ unittestMockOneGeneric('documentSetFocus')
53
+ unittestMockOneGeneric('documentSetReset')
54
+ unittestMockOneGeneric('documentSetTitle')
55
+ unittestMockOne('documentURL', unittestMock_documentURL)
56
+
57
+ # Drawing
58
+ unittestMockOneGeneric('drawArc')
59
+ unittestMockOneGeneric('drawCircle')
60
+ unittestMockOneGeneric('drawClose')
61
+ unittestMockOneGeneric('drawEllipse')
62
+ unittestMockOneGeneric('drawHLine')
63
+ unittestMockOne('drawHeight', unittestMock_drawHeight)
64
+ unittestMockOneGeneric('drawImage')
65
+ unittestMockOneGeneric('drawLine')
66
+ unittestMockOneGeneric('drawMove')
67
+ unittestMockOne('drawNew', unittestMock_drawNew)
68
+ unittestMockOneGeneric('drawOnClick')
69
+ unittestMockOneGeneric('drawPathRect')
70
+ unittestMockOneGeneric('drawRect')
71
+ unittestMockOneGeneric('drawStyle')
72
+ unittestMockOneGeneric('drawText')
73
+ unittestMockOne('drawTextHeight', unittestMock_drawTextHeight)
74
+ unittestMockOne('drawTextStyle', unittestMock_drawTextStyle)
75
+ unittestMockOne('drawTextWidth', unittestMock_drawTextWidth)
76
+ unittestMockOneGeneric('drawVLine')
77
+ unittestMockOne('drawWidth', unittestMock_drawWidth)
78
+
79
+ # Element Model
80
+ unittestMockOneGeneric('elementModelRender')
81
+
82
+ # Local Storage
83
+ unittestMockOne('localStorageClear', unittestMock_localStorageClear)
84
+ unittestMockOne('localStorageGet', unittestMock_localStorageGet)
85
+ unittestMockOne('localStorageRemove', unittestMock_localStorageRemove)
86
+ unittestMockOne('localStorageSet', unittestMock_localStorageSet)
87
+
88
+ # Markdown
89
+ unittestMockOne('markdownEscape', unittestMock_markdownEscape)
90
+ unittestMockOne('markdownHeaderId', unittestMock_markdownHeaderId)
91
+ unittestMockOne('markdownParse', systemPartial(unittestMock_markdownParse, if(data != null, objectGet(data, 'markdownParse'))))
92
+ unittestMockOneGeneric('markdownPrint')
93
+ unittestMockOne('markdownTitle', systemPartial(unittestMock_markdownTitle, if(data != null, objectGet(data, 'markdownTitle'))))
94
+
95
+ # Schema
96
+ unittestMockOne('schemaElements', unittestMock_schemaElements)
97
+
98
+ # Session Storage
99
+ unittestMockOne('sessionStorageClear', unittestMock_sessionStorageClear)
100
+ unittestMockOne('sessionStorageGet', unittestMock_sessionStorageGet)
101
+ unittestMockOne('sessionStorageRemove', unittestMock_sessionStorageRemove)
102
+ unittestMockOne('sessionStorageSet', unittestMock_sessionStorageSet)
103
+
104
+ # System
105
+ unittestMockOne('systemFetch', systemPartial(unittestMock_systemFetch, if(data != null, objectGet(data, 'systemFetch'))))
106
+ unittestMockOneGeneric('systemLog')
107
+ unittestMockOneGeneric('systemLogDebug')
108
+
109
+ # URL
110
+ unittestMockOne('urlObjectCreate', unittestMock_urlObjectCreate)
111
+
112
+ # Window
113
+ unittestMockOne('windowClipboardRead', unittestMock_windowClipboardRead)
114
+ unittestMockOne('windowClipboardWrite', unittestMock_windowClipboardWrite)
115
+ unittestMockOne('windowHeight', unittestMock_windowHeight)
116
+ unittestMockOneGeneric('windowSetLocation')
117
+ unittestMockOneGeneric('windowSetResize')
118
+ unittestMockOneGeneric('windowSetTimeout')
119
+ unittestMockOne('windowWidth', unittestMock_windowWidth)
120
+ endfunction
121
+
122
+
123
+ # $function: unittestMockOne
124
+ # $group: unittestMock.bare
125
+ # $doc: Start a function mock.
126
+ # $doc: To stop mocking, call the [unittestMockEnd](#var.vGroup='unittestMock.bare'&unittestmockend) function.
127
+ # $arg funcName: The name of the function to mock
128
+ # $arg mockFunc: The mock function
129
+ function unittestMockOne(funcName, mockFunc):
130
+ # Replace the function withi the mocked function
131
+ objectSet(unittestMockFunctions, funcName, systemGlobalGet(funcName))
132
+ systemGlobalSet(funcName, mockFunc)
133
+ endfunction
134
+
135
+
136
+ # $function: unittestMockOneGeneric
137
+ # $group: unittestMock.bare
138
+ # $doc: Start a generic function mock.
139
+ # $doc: To stop mocking, call the [unittestMockEnd](#var.vGroup='unittestMock.bare'&unittestmockend) function.
140
+ # $arg funcName: The name of the function to mock
141
+ function unittestMockOneGeneric(funcName):
142
+ return unittestMockOne(funcName, systemPartial(unittestMockGeneric, funcName))
143
+ endfunction
144
+
145
+ function unittestMockGeneric(funcName, args...):
146
+ # Record the mocked function call
147
+ arrayPush(unittestMockCalls, arrayNew(funcName, args))
148
+ endfunction
149
+
150
+
151
+ # $function: unittestMockEnd
152
+ # $group: unittestMock.bare
153
+ # $doc: Stop all function mocks
154
+ # $return: The array of mock function call tuples of the form (function name, function argument array)
155
+ function unittestMockEnd():
156
+ # Restore the original functions
157
+ for funcName in objectKeys(unittestMockFunctions):
158
+ systemGlobalSet(funcName, objectGet(unittestMockFunctions, funcName))
159
+ endfor
160
+
161
+ # Reset the mock state
162
+ mockCalls = unittestMockCalls
163
+ systemGlobalSet('unittestMockCalls', arrayNew())
164
+ systemGlobalSet('unittestMockFunctions', objectNew())
165
+ systemGlobalSet('unittestMockState', objectCopy(unittestMockStateDefault))
166
+
167
+ return mockCalls
168
+ endfunction
169
+
170
+
171
+ #
172
+ # Document functions
173
+ #
174
+
175
+
176
+ function unittestMock_documentFontSize():
177
+ return unittestMockDefaultFontSizePx
178
+ endfunction
179
+
180
+
181
+ function unittestMock_documentInputValue(data, args...):
182
+ id = arrayGet(args, 0)
183
+
184
+ # Record the mocked function call
185
+ arrayPush(unittestMockCalls, arrayNew('documentInputValue', args))
186
+
187
+ # Return the mocked documentInputValue response
188
+ return if(data != null, objectGet(data, id))
189
+ endfunction
190
+
191
+
192
+ function unittestMock_documentURL(url):
193
+ return url
194
+ endfunction
195
+
196
+
197
+ #
198
+ # Drawing functions
199
+ #
200
+
201
+
202
+ function unittestMock_drawHeight():
203
+ return objectGet(unittestMockState, 'drawingHeight')
204
+ endfunction
205
+
206
+
207
+ function unittestMock_drawNew(args...):
208
+ width = arrayGet(args, 0)
209
+ height = arrayGet(args, 1)
210
+
211
+ # Record the mocked function call
212
+ arrayPush(unittestMockCalls, arrayNew('drawNew', args))
213
+
214
+ # Update the mock state
215
+ objectSet(unittestMockState, 'drawingWidth', width)
216
+ objectSet(unittestMockState, 'drawingHeight', height)
217
+ endfunction
218
+
219
+
220
+ function unittestMock_drawTextHeight(text, width):
221
+ if width > 0:
222
+ return width / (unittestMockFontWidthRatio * stringLength(text))
223
+ endif
224
+ return objectGet(unittestMockState, 'drawingFontSizePx')
225
+ endfunction
226
+
227
+
228
+ function unittestMock_drawTextStyle(args...):
229
+ fontSizePx = arrayGet(args, 0)
230
+
231
+ # Record the mocked function call
232
+ arrayPush(unittestMockCalls, arrayNew('drawTextStyle', args))
233
+
234
+ # Update the mock state
235
+ objectSet(unittestMockState, 'drawingFontSizePx', if(fontSizePx != null, fontSizePx, unittestMockDefaultFontSizePx))
236
+ endfunction
237
+
238
+
239
+ function unittestMock_drawTextWidth(text, fontSizePx):
240
+ return unittestMockFontWidthRatio * fontSizePx * stringLength(text)
241
+ endfunction
242
+
243
+
244
+ function unittestMock_drawWidth():
245
+ return objectGet(unittestMockState, 'drawingWidth')
246
+ endfunction
247
+
248
+
249
+ #
250
+ # Local Storage functions
251
+ #
252
+
253
+
254
+ function unittestMock_localStorageClear(args...):
255
+ # Record the mocked function call
256
+ arrayPush(unittestMockCalls, arrayNew('localStorageClear', args))
257
+
258
+ # Update the mock state
259
+ objectSet(unittestMockState, 'localStorage', objectNew())
260
+ endfunction
261
+
262
+
263
+ function unittestMock_localStorageGet(key):
264
+ return objectGet(objectGet(unittestMockState, 'localStorage'), key)
265
+ endfunction
266
+
267
+
268
+ function unittestMock_localStorageRemove(args...):
269
+ key = arrayGet(args, 0)
270
+
271
+ # Record the mocked function call
272
+ arrayPush(unittestMockCalls, arrayNew('localStorageRemove', args))
273
+
274
+ # Update the mock state
275
+ objectDelete(objectGet(unittestMockState, 'localStorage'), key)
276
+ endfunction
277
+
278
+
279
+ function unittestMock_localStorageSet(args...):
280
+ key = arrayGet(args, 0)
281
+ value = arrayGet(args, 1)
282
+
283
+ # Record the mocked function call
284
+ arrayPush(unittestMockCalls, arrayNew('localStorageSet', args))
285
+
286
+ # Update the mock state
287
+ objectSet(objectGet(unittestMockState, 'localStorage'), key, value)
288
+ endfunction
289
+
290
+
291
+ #
292
+ # Markdown functions
293
+ #
294
+
295
+
296
+ function unittestMock_markdownEscape(text):
297
+ return regexReplace(unittestMock_markdownEscapeRegex, text, '\\$1')
298
+ endfunction
299
+
300
+ unittestMock_markdownEscapeRegex = regexNew('([\\\\[\\]()<>"\'*_~`#=+|-])')
301
+
302
+
303
+ function unittestMock_markdownHeaderId(text):
304
+ result = stringLower(text)
305
+ result = regexReplace(unittestMock_markdownHeaderId_start, result, '')
306
+ result = regexReplace(unittestMock_markdownHeaderId_end, result, '')
307
+ result = regexReplace(unittestMock_markdownHeaderId_remove, result, '')
308
+ return regexReplace(unittestMock_markdownHeaderId_dash, result, '-')
309
+ endfunction
310
+
311
+ unittestMock_markdownHeaderId_start = regexNew('^[^a-z0-9]+')
312
+ unittestMock_markdownHeaderId_end = regexNew('[^a-z0-9]+$')
313
+ unittestMock_markdownHeaderId_remove = regexNew('[\'"]')
314
+ unittestMock_markdownHeaderId_dash = regexNew('[^a-z0-9]+')
315
+
316
+
317
+ function unittestMock_markdownParse(data, args...):
318
+ # Record the mocked function call
319
+ arrayPush(unittestMockCalls, arrayNew('markdownParse', args))
320
+
321
+ # Return the mocked markdownParse response
322
+ return if(data != null, arrayShift(data))
323
+ endfunction
324
+
325
+
326
+ function unittestMock_markdownTitle(data, args...):
327
+ # Record the mocked function call
328
+ arrayPush(unittestMockCalls, arrayNew('markdownTitle', args))
329
+
330
+ # Return the mocked markdownTitle response
331
+ return if(data != null, arrayShift(data))
332
+ endfunction
333
+
334
+
335
+ #
336
+ # Schema functions
337
+ #
338
+
339
+
340
+ function unittestMock_schemaElements(types, typeName):
341
+ userType = objectGet(types, typeName)
342
+ userTypeKey = arrayGet(objectKeys(userType), 0)
343
+ if userTypeKey == 'struct' && objectGet(objectGet(userType, 'struct'), 'union'):
344
+ userTypeKey = 'union'
345
+ endif
346
+ return arrayNew( \
347
+ arrayNew( \
348
+ objectNew('html', 'h1', 'elem', objectNew('text', userTypeKey + ' ' + typeName)) \
349
+ ) \
350
+ )
351
+ endfunction
352
+
353
+
354
+ #
355
+ # Session Storage functions
356
+ #
357
+
358
+
359
+ function unittestMock_sessionStorageClear(args...):
360
+ # Record the mocked function call
361
+ arrayPush(unittestMockCalls, arrayNew('sessionStorageClear', args))
362
+
363
+ # Update the mock state
364
+ objectSet(unittestMockState, 'sessionStorage', objectNew())
365
+ endfunction
366
+
367
+
368
+ function unittestMock_sessionStorageGet(key):
369
+ return objectGet(objectGet(unittestMockState, 'sessionStorage'), key)
370
+ endfunction
371
+
372
+
373
+ function unittestMock_sessionStorageRemove(args...):
374
+ key = arrayGet(args, 0)
375
+
376
+ # Record the mocked function call
377
+ arrayPush(unittestMockCalls, arrayNew('sessionStorageRemove', args))
378
+
379
+ # Update the mock state
380
+ objectDelete(objectGet(unittestMockState, 'sessionStorage'), key)
381
+ endfunction
382
+
383
+
384
+ function unittestMock_sessionStorageSet(args...):
385
+ key = arrayGet(args, 0)
386
+ value = arrayGet(args, 1)
387
+
388
+ # Record the mocked function call
389
+ arrayPush(unittestMockCalls, arrayNew('sessionStorageSet', args))
390
+
391
+ # Update the mock state
392
+ objectSet(objectGet(unittestMockState, 'sessionStorage'), key, value)
393
+ endfunction
394
+
395
+
396
+ #
397
+ # System functions
398
+ #
399
+
400
+
401
+ function unittestMock_systemFetch(data, args...):
402
+ url = arrayGet(args, 0)
403
+
404
+ # Record the mocked function call
405
+ arrayPush(unittestMockCalls, arrayNew('systemFetch', args))
406
+
407
+ # Array of URLs?
408
+ urlType = systemType(url)
409
+ if urlType == 'array':
410
+ result = arrayNew()
411
+ for urlItem in url:
412
+ urlActual = if(systemType(urlItem) == 'object', objectGet(urlItem, 'url'), urlItem)
413
+ arrayPush(result, if(data != null, objectGet(data, urlActual)))
414
+ endfor
415
+ return result
416
+ endif
417
+
418
+ # Return the mocked systemFetch response
419
+ urlActual = if(urlType == 'object', objectGet(url, 'url'), url)
420
+ return if(data != null, objectGet(data, urlActual))
421
+ endfunction
422
+
423
+
424
+ #
425
+ # URL functions
426
+ #
427
+
428
+
429
+ function unittestMock_urlObjectCreate(data, contentType):
430
+ return 'blob:' + urlEncode(contentType) + '-' + urlEncode(if(stringLength(data) < 20, data, stringSlice(data, 0, 20)))
431
+ endfunction
432
+
433
+
434
+ #
435
+ # Window functions
436
+ #
437
+
438
+
439
+ function unittestMock_windowClipboardRead():
440
+ return objectGet(unittestMockState, 'windowClipboard')
441
+ endfunction
442
+
443
+
444
+ function unittestMock_windowClipboardWrite(args...):
445
+ text = arrayGet(args, 0)
446
+
447
+ # Record the mocked function call
448
+ arrayPush(unittestMockCalls, arrayNew('windowClipboardWrite', args))
449
+
450
+ # Update the mock state
451
+ objectSet(unittestMockState, 'windowClipboard', text)
452
+ endfunction
453
+
454
+
455
+ function unittestMock_windowHeight():
456
+ return unittestMockWindowHeight
457
+ endfunction
458
+
459
+
460
+ function unittestMock_windowWidth():
461
+ return unittestMockWindowWidth
462
+ endfunction