bare-script 3.2.3 → 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,229 @@
1
+ # Licensed under the MIT License
2
+ # https://github.com/craigahobbs/markdown-up/blob/main/LICENSE
3
+
4
+
5
+ # Include sentinel
6
+ if systemGlobalGet('unittestSentinel'):
7
+ return
8
+ endif
9
+ unittestSentinel = true
10
+
11
+
12
+ include 'diff.bare'
13
+
14
+
15
+ # Test statistics
16
+ unittestTests = objectNew()
17
+ unittestWarnings = arrayNew()
18
+
19
+
20
+ # $function: unittestRunTest
21
+ # $group: unittest.bare
22
+ # $doc: Run a unit test
23
+ # $arg testName: The test function name
24
+ function unittestRunTest(testName):
25
+ testFn = unittestRunTestHelper(testName)
26
+ if testFn:
27
+ testFn()
28
+ systemGlobalSet('unittestTestName', null)
29
+ endif
30
+ endfunction
31
+
32
+
33
+ # $function: unittestRunTestAsync
34
+ # $group: unittest.bare
35
+ # $doc: Run an asyncronous unit test
36
+ # $arg testName: The test function name
37
+ async function unittestRunTestAsync(testName):
38
+ testFn = unittestRunTestHelper(testName)
39
+ if testFn:
40
+ testFn()
41
+ systemGlobalSet('unittestTestName', null)
42
+ endif
43
+ endfunction
44
+
45
+
46
+ # unittestRunTest helper function
47
+ function unittestRunTestHelper(testName):
48
+ # Single test argument?
49
+ if vTest != null && vTest != testName:
50
+ return null
51
+ endif
52
+
53
+ # Test run multiple times?
54
+ if objectHas(unittestTests, testName):
55
+ arrayPush(unittestWarnings, 'Test "' + testName + '" run multiple times')
56
+ return null
57
+ endif
58
+
59
+ # Get the test func
60
+ testFn = systemGlobalGet(testName)
61
+ if testFn == null:
62
+ arrayPush(unittestWarnings, 'Test "' + testName + '" not found')
63
+ return null
64
+ endif
65
+
66
+ # Add the unit test result array
67
+ testFailures = arrayNew()
68
+ objectSet(unittestTests, testName, testFailures)
69
+ systemGlobalSet('unittestTestName', testName)
70
+
71
+ return testFn
72
+ endfunction
73
+
74
+
75
+ # $function: unittestReport
76
+ # $group: unittest.bare
77
+ # $doc: Render the unit test report
78
+ # $return: The number of unit test failures
79
+ function unittestReport():
80
+ # Compute test statistics
81
+ testNames = arraySort(objectKeys(unittestTests))
82
+ testCount = arrayLength(testNames)
83
+ testFailCount = 0
84
+ for testName in testNames:
85
+ testFailures = objectGet(unittestTests, testName)
86
+ if arrayLength(testFailures):
87
+ testFailCount = testFailCount + 1
88
+ endif
89
+ endfor
90
+ testPassCount = testCount - testFailCount
91
+
92
+ # Report statistics
93
+ markdownPrint('', 'Ran ' + testCount + ' tests - ' + testPassCount + ' passed, ' + testFailCount + ' failed')
94
+ if vTest != null:
95
+ testURL = if(vTestArgs != null, '#' + vTestArgs, '#var=')
96
+ markdownPrint('([all tests](' + testURL + '))')
97
+ endif
98
+
99
+ # Report any warnings
100
+ testWarningCount = arrayLength(unittestWarnings)
101
+ if testWarningCount:
102
+ markdownPrint('', '## Warnings')
103
+ for warning in unittestWarnings:
104
+ markdownPrint('', '- ' + markdownEscape(warning))
105
+ endfor
106
+ endif
107
+
108
+ # Report the failing tests
109
+ if testFailCount:
110
+ markdownPrint('', '## Failing Tests')
111
+ for testName in testNames:
112
+ testFailures = objectGet(unittestTests, testName)
113
+ if arrayLength(testFailures):
114
+ testURL = '#' + if(vTestArgs != null, vTestArgs + '&', '') + "var.vTest='" + urlEncodeComponent(testName) + "'"
115
+ failureLines = arrayNew('', '[' + markdownEscape(testName) + '](' + testURL + ") - FAIL")
116
+ for errorLines in testFailures:
117
+ for errorLine, ixErrorLine in errorLines:
118
+ if ixErrorLine == 0:
119
+ arrayPush(failureLines, '')
120
+ arrayPush(failureLines, '- ' + errorLine)
121
+ else:
122
+ arrayPush(failureLines, if(errorLine, ' ' + errorLine, ''))
123
+ endif
124
+ endfor
125
+ endfor
126
+ markdownPrint(failureLines)
127
+ endif
128
+ endfor
129
+ endif
130
+
131
+ # Report the passing tests
132
+ if testPassCount:
133
+ markdownPrint('', '## Passing Tests')
134
+ for testName in testNames:
135
+ testFailures = objectGet(unittestTests, testName)
136
+ if !arrayLength(testFailures):
137
+ testURL = '#' + if(vTestArgs != null, vTestArgs + '&', '') + "var.vTest='" + urlEncodeComponent(testName) + "'"
138
+ markdownPrint('', '[' + markdownEscape(testName) + '](' + testURL + ") - OK")
139
+ endif
140
+ endfor
141
+ endif
142
+
143
+ return testWarningCount + testFailCount
144
+ endfunction
145
+
146
+
147
+ # $function: unittestEqual
148
+ # $group: unittest.bare
149
+ # $doc: Assert an actual value is equal to the expected value
150
+ # $arg actual: The actual value
151
+ # $arg expected: The expected value
152
+ # $arg description: The description of the assertion
153
+ function unittestEqual(actual, expected, description):
154
+ if actual == expected:
155
+ return
156
+ endif
157
+
158
+ # Add the test failure error lines
159
+ testFailures = objectGet(unittestTests, unittestTestName)
160
+ errorLines = arrayNew( \
161
+ 'Equal:', \
162
+ '', \
163
+ '```', \
164
+ jsonStringify(actual), \
165
+ '```', \
166
+ '', \
167
+ '```', \
168
+ jsonStringify(expected), \
169
+ '```' \
170
+ )
171
+ if description != null:
172
+ arrayPush(testFailures, arrayExtend(arrayNew(markdownEscape(description), errorLines)))
173
+ else:
174
+ arrayPush(testFailures, errorLines)
175
+ endif
176
+ endfunction
177
+
178
+
179
+ # $function: unittestDeepEqual
180
+ # $group: unittest.bare
181
+ # $doc: Assert an actual value is *deeply* equal to the expected value
182
+ # $arg actual: The actual value
183
+ # $arg expected: The expected value
184
+ # $arg description: The description of the assertion
185
+ function unittestDeepEqual(actual, expected, description):
186
+ # Serialize as JSON
187
+ actualJSON = if(systemType(actual) == 'string', actual, jsonStringify(actual, 4))
188
+ expectedJSON = if(systemType(expected) == 'string', expected, jsonStringify(expected, 4))
189
+ if actualJSON == expectedJSON:
190
+ return
191
+ endif
192
+
193
+ # Compute the difference lines
194
+ errorLines = arrayNew()
195
+ diffErrorLines = arrayNew()
196
+ for diff in diffLines(actualJSON, expectedJSON):
197
+ diffType = objectGet(diff, 'type')
198
+ if diffType == 'Remove':
199
+ for diffLine in objectGet(diff, 'lines'):
200
+ arrayPush(diffErrorLines, '--- ' + diffLine)
201
+ endfor
202
+ elif diffType == 'Add':
203
+ for diffLine in objectGet(diff, 'lines'):
204
+ arrayPush(diffErrorLines, '+++ ' + diffLine)
205
+ endfor
206
+ else:
207
+ # diffType == 'Identical'
208
+ if diffErrorLines:
209
+ arrayExtend(errorLines, arrayNew('Deep-equal:', '', '```'))
210
+ arrayExtend(errorLines, diffErrorLines)
211
+ arrayPush(errorLines, '```')
212
+ diffErrorLines = arrayNew()
213
+ endif
214
+ endif
215
+ endfor
216
+ if diffErrorLines:
217
+ arrayExtend(errorLines, arrayNew('Deep-equal:', '', '```'))
218
+ arrayExtend(errorLines, diffErrorLines)
219
+ arrayPush(errorLines, '```')
220
+ endif
221
+
222
+ # Add the test failure error lines
223
+ testFailures = objectGet(unittestTests, unittestTestName)
224
+ if description != null:
225
+ arrayPush(testFailures, arrayExtend(arrayNew(markdownEscape(description), errorLines)))
226
+ else:
227
+ arrayPush(testFailures, errorLines)
228
+ endif
229
+ endfunction
@@ -6,224 +6,7 @@
6
6
  if systemGlobalGet('unittestSentinel'):
7
7
  return
8
8
  endif
9
- unittestSentinel = true
10
9
 
11
10
 
12
- include 'diff.bare'
13
-
14
-
15
- # Test statistics
16
- unittestTests = objectNew()
17
- unittestWarnings = arrayNew()
18
-
19
-
20
- # $function: unittestRunTest
21
- # $group: unittest.mds
22
- # $doc: Run a unit test
23
- # $arg testName: The test function name
24
- function unittestRunTest(testName):
25
- testFn = unittestRunTestHelper(testName)
26
- if testFn:
27
- testFn()
28
- systemGlobalSet('unittestTestName', null)
29
- endif
30
- endfunction
31
-
32
-
33
- # $function: unittestRunTestAsync
34
- # $group: unittest.mds
35
- # $doc: Run an asyncronous unit test
36
- # $arg testName: The test function name
37
- async function unittestRunTestAsync(testName):
38
- testFn = unittestRunTestHelper(testName)
39
- if testFn:
40
- testFn()
41
- systemGlobalSet('unittestTestName', null)
42
- endif
43
- endfunction
44
-
45
-
46
- # unittestRunTest helper function
47
- function unittestRunTestHelper(testName):
48
- # Single test argument?
49
- if vTest != null && vTest != testName:
50
- return null
51
- endif
52
-
53
- # Test run multiple times?
54
- if objectHas(unittestTests, testName):
55
- arrayPush(unittestWarnings, 'Test "' + testName + '" run multiple times')
56
- return null
57
- endif
58
-
59
- # Get the test func
60
- testFn = systemGlobalGet(testName)
61
- if testFn == null:
62
- arrayPush(unittestWarnings, 'Test "' + testName + '" not found')
63
- return null
64
- endif
65
-
66
- # Add the unit test result array
67
- testFailures = arrayNew()
68
- objectSet(unittestTests, testName, testFailures)
69
- systemGlobalSet('unittestTestName', testName)
70
-
71
- return testFn
72
- endfunction
73
-
74
-
75
- # $function: unittestReport
76
- # $group: unittest.mds
77
- # $doc: Render the unit test report
78
- # $return: The number of unit test failures
79
- function unittestReport():
80
- # Compute test statistics
81
- testNames = arraySort(objectKeys(unittestTests))
82
- testCount = arrayLength(testNames)
83
- testFailCount = 0
84
- for testName in testNames:
85
- testFailures = objectGet(unittestTests, testName)
86
- if arrayLength(testFailures):
87
- testFailCount = testFailCount + 1
88
- endif
89
- endfor
90
- testPassCount = testCount - testFailCount
91
-
92
- # Report statistics
93
- markdownPrint('', 'Ran ' + testCount + ' tests - ' + testPassCount + ' passed, ' + testFailCount + ' failed')
94
- if vTest != null:
95
- testURL = if(vTestArgs != null, '#' + vTestArgs, '#var=')
96
- markdownPrint('([all tests](' + testURL + '))')
97
- endif
98
-
99
- # Report any warnings
100
- testWarningCount = arrayLength(unittestWarnings)
101
- if testWarningCount:
102
- markdownPrint('', '## Warnings')
103
- for warning in unittestWarnings:
104
- markdownPrint('', '- ' + markdownEscape(warning))
105
- endfor
106
- endif
107
-
108
- # Report the failing tests
109
- if testFailCount:
110
- markdownPrint('', '## Failing Tests')
111
- for testName in testNames:
112
- testFailures = objectGet(unittestTests, testName)
113
- if arrayLength(testFailures):
114
- testURL = '#' + if(vTestArgs != null, vTestArgs + '&', '') + "var.vTest='" + urlEncodeComponent(testName) + "'"
115
- failureLines = arrayNew('', '[' + markdownEscape(testName) + '](' + testURL + ") - FAIL")
116
- for errorLines in testFailures:
117
- for errorLine, ixErrorLine in errorLines:
118
- if ixErrorLine == 0:
119
- arrayPush(failureLines, '')
120
- arrayPush(failureLines, '- ' + errorLine)
121
- else:
122
- arrayPush(failureLines, if(errorLine, ' ' + errorLine, ''))
123
- endif
124
- endfor
125
- endfor
126
- markdownPrint(failureLines)
127
- endif
128
- endfor
129
- endif
130
-
131
- # Report the passing tests
132
- if testPassCount:
133
- markdownPrint('', '## Passing Tests')
134
- for testName in testNames:
135
- testFailures = objectGet(unittestTests, testName)
136
- if !arrayLength(testFailures):
137
- testURL = '#' + if(vTestArgs != null, vTestArgs + '&', '') + "var.vTest='" + urlEncodeComponent(testName) + "'"
138
- markdownPrint('', '[' + markdownEscape(testName) + '](' + testURL + ") - OK")
139
- endif
140
- endfor
141
- endif
142
-
143
- return testWarningCount + testFailCount
144
- endfunction
145
-
146
-
147
- # $function: unittestEqual
148
- # $group: unittest.mds
149
- # $doc: Assert an actual value is equal to the expected value
150
- # $arg actual: The actual value
151
- # $arg expected: The expected value
152
- # $arg description: The description of the assertion
153
- function unittestEqual(actual, expected, description):
154
- if actual == expected:
155
- return
156
- endif
157
-
158
- # Add the test failure error lines
159
- testFailures = objectGet(unittestTests, unittestTestName)
160
- errorLines = arrayNew( \
161
- 'Equal:', \
162
- '', \
163
- '```', \
164
- jsonStringify(actual), \
165
- '```', \
166
- '', \
167
- '```', \
168
- jsonStringify(expected), \
169
- '```' \
170
- )
171
- if description != null:
172
- arrayPush(testFailures, arrayExtend(arrayNew(markdownEscape(description), errorLines)))
173
- else:
174
- arrayPush(testFailures, errorLines)
175
- endif
176
- endfunction
177
-
178
-
179
- # $function: unittestDeepEqual
180
- # $group: unittest.mds
181
- # $doc: Assert an actual value is *deeply* equal to the expected value
182
- # $arg actual: The actual value
183
- # $arg expected: The expected value
184
- # $arg description: The description of the assertion
185
- function unittestDeepEqual(actual, expected, description):
186
- # Serialize as JSON
187
- actualJSON = if(systemType(actual) == 'string', actual, jsonStringify(actual, 4))
188
- expectedJSON = if(systemType(expected) == 'string', expected, jsonStringify(expected, 4))
189
- if actualJSON == expectedJSON:
190
- return
191
- endif
192
-
193
- # Compute the difference lines
194
- errorLines = arrayNew()
195
- diffErrorLines = arrayNew()
196
- for diff in diffLines(actualJSON, expectedJSON):
197
- diffType = objectGet(diff, 'type')
198
- if diffType == 'Remove':
199
- for diffLine in objectGet(diff, 'lines'):
200
- arrayPush(diffErrorLines, '--- ' + diffLine)
201
- endfor
202
- elif diffType == 'Add':
203
- for diffLine in objectGet(diff, 'lines'):
204
- arrayPush(diffErrorLines, '+++ ' + diffLine)
205
- endfor
206
- else:
207
- # diffType == 'Identical'
208
- if diffErrorLines:
209
- arrayExtend(errorLines, arrayNew('Deep-equal:', '', '```'))
210
- arrayExtend(errorLines, diffErrorLines)
211
- arrayPush(errorLines, '```')
212
- diffErrorLines = arrayNew()
213
- endif
214
- endif
215
- endfor
216
- if diffErrorLines:
217
- arrayExtend(errorLines, arrayNew('Deep-equal:', '', '```'))
218
- arrayExtend(errorLines, diffErrorLines)
219
- arrayPush(errorLines, '```')
220
- endif
221
-
222
- # Add the test failure error lines
223
- testFailures = objectGet(unittestTests, unittestTestName)
224
- if description != null:
225
- arrayPush(testFailures, arrayExtend(arrayNew(markdownEscape(description), errorLines)))
226
- else:
227
- arrayPush(testFailures, errorLines)
228
- endif
229
- endfunction
11
+ systemLog('MarkdownUp - unittest.mds: unittest.mds is now unittest.bare - please update before 2025-06-01')
12
+ include 'unittest.bare'