-
Notifications
You must be signed in to change notification settings - Fork 5
/
UT.dyalog
398 lines (349 loc) · 11.8 KB
/
UT.dyalog
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
:NameSpace UT
⎕IO ← 1
sac ← 0
expect_orig ← expect ← ⎕NS⍬
exception ← ⍬
nexpect_orig ← nexpect ← ⎕NS⍬
print_passed ← 1
print_summary ← 1
print_header ← 0
∇ {Z}←{Conf}run Argument;PRE_test;POST_test;TEST_step;COVER_step;FromSpace
load_display_if_not_already_loaded
load_salt_scripts_into_current_namespace_if_configured
FromSpace←1⊃⎕RSI
PRE_test←{}
POST_test←{}
COVER_step←{}
:If 0≠⎕NC'Conf'
:If Conf has'cover_target'
PRE_test←{{}⎕PROFILE'start'}
POST_test←{{}⎕PROFILE'stop'}
:EndIf
:EndIf
:If is_function Argument
TEST_step←single_function_test_function
COVER_file←Argument,'_coverage.html'
:ElseIf is_list_of_functions Argument
TEST_step←list_of_functions_test_function
COVER_file←'list_coverage.html'
:ElseIf is_file Argument
TEST_step←file_test_function
COVER_file←(get_file_name Argument),'_coverage.html'
:ElseIf is_dir Argument
test_files←test_files_in_dir Argument
TEST_step←test_dir_function
Argument←test_files
:EndIf
:If 0≠⎕NC'Conf'
:If Conf has'cover_target'
COVER_step←{Conf,←⊂('cover_file'COVER_file)
generate_coverage_page Conf}
:EndIf
:EndIf
PRE_test ⍬
Z←FromSpace TEST_step Argument
POST_test ⍬
COVER_step ⍬
∇
∇ load_display_if_not_already_loaded
:If 0=⎕NC'#.DISPLAY'
'DISPLAY'#.⎕CY'display'
:EndIf
∇
∇ load_salt_scripts_into_current_namespace_if_configured
:If 0≠⎕NC'#.UT.appdir'
:If ⍬≢#.UT.appdir
⎕SE.SALT.Load #.UT.appdir,'src/*.dyalog -target=#'
⎕SE.SALT.Load #.UT.appdir,'test/*.dyalog -target=#'
:EndIf
:EndIf
∇
∇ Z←FromSpace single_function_test_function TestName
Z←run_ut FromSpace TestName
∇
∇ Z←FromSpace list_of_functions_test_function ListOfNames;t
t←⎕TS
Z←run_ut¨{FromSpace ⍵}¨ListOfNames
t←⎕TS-t
:If print_summary
('Test execution report')print_passed_crashed_failed Z t
:EndIf
∇
∇ Z←FromSpace file_test_function FilePath;FileNS;Functions;TestFunctions;t
FileNS←⎕SE.SALT.Load FilePath,' -target=#'
Functions←↓FileNS.⎕NL 3
TestFunctions←(is_test¨Functions)/Functions
:If (0/⍬,⊂0/'')≡TestFunctions
⎕←'No test functions found'
Z←⍬
:Else
:If print_header
⎕←'Testing from ',FilePath
:EndIf
t←⎕TS
Z←run_ut¨{FileNS ⍵}¨TestFunctions
t←⎕TS-t
:If print_summary
(FilePath,' tests')print_passed_crashed_failed Z t
:EndIf
:EndIf
⎕EX⍕FileNS
∇
∇ Z←FromSpace test_dir_function Test_files;old_sum;old_head
:If Test_files≡⍬/⍬,⊂''
⎕←'No test files found'
Z←⍬
:Else
old_sum←print_summary
old_head←print_header
print_summary←0
print_header←1
t←⎕TS
Z←#.UT.run¨Test_files
t←⎕TS-t
print_summary←old_sum
print_header←old_head
:If print_summary
'All tests'print_passed_crashed_failed (⊃,/Z)t
:EndIf
:EndIf
∇
∇ Z←get_file_name Argument;separator
separator←⊃⌽(Argument∊'/\')/⍳⍴Argument
Z←¯7↓separator↓Argument
∇
∇ generate_coverage_page Conf;ProfileData;CoverResults;HTML
ProfileData←⎕PROFILE'data'
ToCover←retrieve_coverables¨(⊃'cover_target'in Conf)
:If (⍴ToCover)≡(⍴⊂1)
ToCover←⊃ToCover
:EndIf
Representations←get_representation¨ToCover
CoverResults←ProfileData∘generate_cover_result¨↓ToCover,[1.5]Representations
HTML←generate_html CoverResults
Conf write_html_to_page HTML
⎕PROFILE'clear'
∇
∇ Z←retrieve_coverables Something;nc;functions
nc←⎕NC Something
:If nc=3
Z←Something
:ElseIf nc=9
functions←strip¨↓⍎Something,'.⎕NL 3'
Z←{(Something,'.',⍵)}¨functions
:EndIf
∇
∇ Z←strip input
Z←(input≠' ')/input
∇
∇ Z←get_representation Function;nc;rep
nc←⎕NC⊂Function
:If nc=3.1
rep←↓⎕CR Function
rep[1]←⊂'∇',⊃rep[1]
rep,←⊂'∇'
rep←↑rep
:Else
rep←⎕CR Function
:EndIf
Z←rep
∇
∇ Z←ProfileData generate_cover_result(name representation);Indices;lines;functionlines;covered_lines
Indices←({name≡⍵}¨ProfileData[;1])/⍳⍴ProfileData[;1]
lines←ProfileData[Indices;2]
nc←⎕NC⊂name
:If 3.1=nc
functionlines←¯2+⍴↓representation
:Else
functionlines←⊃⍴↓representation
:EndIf
covered_lines←(⍬∘≢¨lines)/lines
Z←(nc lines functionlines covered_lines representation)
∇
∇ Z←generate_html CoverResults;Covered;Total;Percentage;CoverageText;ColorizedCode;Timestamp;Page
Covered←⊃⊃+/{⍴4⊃⍵}¨CoverResults
Total←⊃⊃+/{3⊃⍵}¨CoverResults
Percentage←100×Covered÷Total
CoverageText←'Coverage: ',Percentage,'% (',Covered,'/',Total,')'
ColorizedCode←⊃,/{colorize_code_by_coverage ⍵}¨CoverResults
Timestamp←generate_timestamp_text
Page←⍬
Page,←⊂⍬,'<html>'
Page,←⊂⍬,'<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>'
Page,←⊂⍬,'<style>pre cov {line-height:80%;}'
Page,←⊂⍬,'pre cov {color: green;}'
Page,←⊂⍬,'pre uncov {line-height:80%;}'
Page,←⊂⍬,'pre uncov {color:red;}</style>'
Page,←⊂⍬,CoverageText
Page,←⊂⍬,'<pre>'
Page,←ColorizedCode
Page,←⊂⍬,'</pre>'
Page,←Timestamp
Page,←⊂⍬,'</html>'
Z←Page
∇
∇ Z←colorize_code_by_coverage CoverResult;Colors;Ends;Code
:If 3.1=⊃CoverResult
Colors←(2+3⊃CoverResult)⍴⊂'<uncov>'
Colors[1]←⊂''
Colors[⍴Colors]←⊂''
Ends←(2+3⊃CoverResult)⍴⊂'</uncov>'
Ends[1]←⊂''
Ends[⍴Ends]←⊂''
:Else
Colors←(3⊃CoverResult)⍴⊂'<uncov>'
Ends←(3⊃CoverResult)⍴⊂'</uncov>'
:EndIf
Colors[1+4⊃CoverResult]←⊂'<cov>'
Ends[1+4⊃CoverResult]←⊂'</cov>'
Code←↓5⊃CoverResult
Z←Colors,[1.5]Code
Z←{⍺,(⎕UCS 13),⍵}/Z,Ends
∇
∇ Z←generate_timestamp_text;TS;YYMMDD;HHMMSS
TS←⎕TS
YYMMDD←⊃{⍺,'-',⍵}/3↑TS
HHMMSS←⊃{⍺,':',⍵}/3↑3↓TS
Z←'Page generated: ',YYMMDD,'|',HHMMSS
∇
∇ Conf write_html_to_page Page;tie;filename
filename←(⊃'cover_out'in Conf),(⊃'cover_file'in Conf)
:Trap 22
tie←filename ⎕NTIE 0
filename ⎕NERASE tie
filename ⎕NCREATE tie
:Else
tie←filename ⎕NCREATE 0
:EndTrap
Simple_array←⍕⊃,/Page
(⎕UCS'UTF-8'⎕UCS Simple_array)⎕NAPPEND tie
∇
∇ Z←is_function Argument
Z←'_TEST'≡¯5↑Argument
∇
∇ Z←is_list_of_functions Argument
Z←2=≡Argument
∇
∇ Z←is_file Argument
Z←'.dyalog'≡¯7↑Argument
∇
∇ Z←is_dir Argument;attr
:If 'Mac' 'Linux'∨.≡3 5↑¨⊂⊃'.'⎕WG'APLVersion'
Z←'yes'≡⊃⎕CMD'test -d ',Argument,' && echo yes || echo no'
:Else
'gfa'⎕NA'I kernel32|GetFileAttributes* <0t'
:If Z←¯1≠attr←gfa⊂Argument ⍝ If file exists
Z←⊃2 16⊤attr ⍝ Return bit 4
:EndIf
:EndIf
∇
∇ Z←test_files_in_dir Argument
:If 'Mac' 'Linux'∨.≡3 5↑¨⊂⊃'.'⎕WG'APLVersion'
Z←⎕SH'find ',Argument,' -name \*_tests.dyalog'
:Else
#.⎕CY'files'
Z←#.Files.Dir Argument,'\*_tests.dyalog'
Z←(Argument,'\')∘,¨Z
#.⎕EX¨'CompFiles' 'Files' 'TestFiles'
:EndIf
∇
∇ Z←run_ut ut_data;returned;crashed;pass;crash;fail;message
(returned crashed time)←execute_function ut_data
(pass crash fail)←determine_pass_crash_or_fail returned crashed
message←determine_message pass fail crashed(2⊃ut_data)returned time
(pass∧~crashed)print_message_to_screen message
Z←(pass crash fail)
∇
∇ Z←execute_function ut_data;function;t
reset_UT_globals
function←(⍕(⊃ut_data[1])),'.',⊃ut_data[2]
:Trap sac
:If 3.2 3.3∨.=⎕NC⊂function
t←⎕TS
Z←(⍎function,' ⍬')0
t←⎕TS-t
:Else
t←⎕TS
Z←(⍎function)0
t←⎕TS-t
:EndIf
:Else
Z←(↑⎕DM)1
:If exception≢⍬
expect←exception
Z[2]←0
t←⎕TS-t
:EndIf
:EndTrap
Z,←⊂t
∇
∇ reset_UT_globals
expect_orig ← expect← ⎕NS⍬
exception←⍬
nexpect_orig ← nexpect← ⎕NS⍬
∇
∇ Z←is_test FunctionName;wsIndex
wsIndex←FunctionName⍳' '
FunctionName←(wsIndex-1)↑FunctionName
Z←'_TEST'≡¯5↑FunctionName
∇
∇ Z←format_time ts;m;s;ms
(m s ms)←¯3↑0 24 60 60 1000{⍺⊤⍺⊥⍵}¯5↑ts
Z←(⍕m),' m ',(⍕s),' s ',(⍕ms),' ms'
∇
∇ Heading print_passed_crashed_failed(ArrayRes time)
⎕←'-----------------------------------------'
⎕←Heading
⎕←' ⍋ Passed: ',⍕+/{1⊃⍵}¨ArrayRes
⎕←' ⍟ Crashed: ',⍕+/{2⊃⍵}¨ArrayRes
⎕←' ⍒ Failed: ',⍕+/{3⊃⍵}¨ArrayRes
⎕←' ○ Runtime: ',format_time time
∇
determine_pass_crash_or_fail←{
r c←⍵ ⋄ 0≠c:0 1 0 ⋄ z←(0 0 1)(1 0 0)
expect_orig≢expect:(⎕IO+expect≡r)⊃z ⋄ (⎕IO+nexpect≢r)⊃z
}
∇ Z←determine_message(pass fail crashed name returned time)
:If crashed
Z←'CRASHED: 'failure_message name returned
:ElseIf pass
Z←name,' Passed ',format_time time
:Else
Z←'FAILED: 'failure_message name returned
:EndIf
∇
∇ passed print_message_to_screen message
:If (~passed)∨passed∧print_passed
⎕←message
:EndIf
∇
∇ Z←term_to_text Term;Text;Rows
Text←#.DISPLAY Term
Rows←1⊃⍴Text
Z←(Rows 4⍴''),Text
∇
∇ Z←Cause failure_message(name returned);hdr;exp;expterm;got;gotterm
hdr←Cause,name
exp←'Expected'
expterm←term_to_text #.UT.expect
got←'Got'
gotterm←term_to_text returned
Z←align_and_join_message_parts hdr exp expterm got gotterm
∇
∇ Z←align_and_join_message_parts Parts;hdr;exp;expterm;got;gotterm;R1;C1;R2;C2;W
(hdr exp expterm got gotterm)←Parts
(R1 C1)←⍴expterm
(R2 C2)←⍴gotterm
W←⊃⊃⌈/C1 C2(⍴hdr)(⍴exp)(⍴got)
Z←(W↑hdr),[0.5](W↑exp)
Z←Z⍪(R1 W↑expterm)
Z←Z⍪(W↑got)
Z←Z⍪(R2 W↑gotterm)
∇
∇ Z←confparam in config
Z←1↓⊃({confparam≡⊃⍵}¨config)/config
∇
∇ Z←config has confparam
Z←∨/{confparam≡⊃⍵}¨config
∇
:EndNameSpace