bare-script 3.8.2__py3-none-any.whl

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