forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_testerlib.livecodescript
More file actions
242 lines (198 loc) · 6.72 KB
/
_testerlib.livecodescript
File metadata and controls
242 lines (198 loc) · 6.72 KB
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
script "TesterLib"
/*
Copyright (C) 2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
/*
This script is a library of commands and functions that can be used by
test runner implementations.
*/
on revLoadLibrary
insert the script of me into back
end revLoadLibrary
----------------------------------------------------------------
-- Script-only stack handling
----------------------------------------------------------------
-- Get all livecode script files beneath the CWD, apart from
-- filenames starting with "." or "_"
function TesterGetTestFileNames pBaseFolder
local tFiles, tCount
put empty into tFiles
put 0 into tCount
if pBaseFolder is empty then
put the defaultfolder into pBaseFolder
end if
TesterGetTestFileNames_Recursive pBaseFolder, empty, tFiles, tCount
return tFiles
end TesterGetTestFileNames
-- Helper command used by runGetTestFileNames
private command TesterGetTestFileNames_Recursive pPath, pRelPath, @xFiles, @xCount
-- Save the CWD
local tSaveFolder
put the defaultfolder into tSaveFolder
set the defaultfolder to pPath
-- Process files in the current directory
local tFile
repeat for each line tFile in the files
if tFile ends with ".livecodescript" and \
not (tFile begins with "." or tFile begins with "_") then
if pRelPath is not empty then
put pRelPath & slash before tFile
end if
add 1 to xCount
put tFile into xFiles[xCount]
end if
end repeat
-- Process subdirectories
local tFolder, tFolderPath
repeat for each line tFolder in the folders
if tFolder begins with "." then
next repeat
end if
put pPath & slash & tFolder into tFolderPath
if pRelPath is not empty then
put pRelPath & slash before tFolder
end if
TesterGetTestFileNames_Recursive tFolderPath, tFolder, xFiles, xCount
end repeat
-- Restore the CWD
set the defaultfolder to tSaveFolder
end TesterGetTestFileNames_Recursive
-- Get a number-indexed array contain the names of all "test"
-- commands in pFilename.
function TesterParseTestCommandNames pFilename
local tScript
-- Get the contents of the file
open file pFilename for "UTF-8" text read
if the result is not empty then
throw the result
end if
read from file pFilename until end
put it into tScript
close file pFilename
-- Scan the file for "on Test*" definitions
local tCommandNames, tCount, tLine, tName
repeat for each line tLine in tScript
if token 1 of tLine is not "on" then
next repeat
end if
put token 2 of tLine into tName
if not (tName begins with "Test") then
next repeat
end if
-- Exclude the test setup message
if tName is "TestSetup" or tName is "TestTearDown" then
next repeat
end if
add 1 to tCount
put tName into tCommandNames[tCount]
end repeat
return tCommandNames
end TesterParseTestCommandNames
-- Prettify a test name by removing a ".livecodescript" suffix
function TesterGetPrettyTestName pFilename
if pFilename ends with ".livecodescript" then
set the itemDelimiter to "."
return item 1 to -2 of pFileName
end if
end TesterGetPrettyTestName
----------------------------------------------------------------
-- TAP support
----------------------------------------------------------------
-- Scan TAP output and extract summary of test results
function TesterTapAnalyse pTapData
local tStats
put 0 into tStats["pass"]
put 0 into tStats["xpass"]
put 0 into tStats["xfail"]
put 0 into tStats["fail"]
put 0 into tStats["skip"]
put pTapData into tStats["log"]
local tLine, tIsOkay, tIsTodo, tIsSkip
local tTail, tDirective
repeat for each line tLine in pTapData
-- Check if the line is a test result and, if so, whether
-- the test was successful
if token 1 of tLine is "ok" then
put true into tIsOkay
else if token 1 of tLine is "not" and token 2 of tLine is "ok" then
put false into tIsOkay
else
-- No test result on this line
next repeat
end if
-- Check if the test on this line was skipped or was expected to fail
put false into tIsTodo
put false into tIsSkip
put char (offset("#", tLine) + 1) to -1 of tLine into tTail
switch token 1 of tTail
case "TODO"
put true into tIsTodo
break
case "SKIP"
put true into tIsSkip
break
end switch
if tIsOkay then
if tIsTodo then
add 1 to tStats["xpass"]
else if tIsSkip then
add 1 to tStats["skip"]
else
add 1 to tStats["pass"]
end if
else
if tIsTodo then
add 1 to tStats["xfail"]
else
add 1 to tStats["fail"]
end if
end if
end repeat
return tStats
end TesterTapAnalyse
-- Examine the statistics generated by tapAnalyse() and return a string
-- describing the test log's worst result
function TesterTapGetWorstResult pTapAnalysis
local tResult
repeat for each item tResult in "fail,xpass,xfail,pass,skip"
if pTapAnalysis[tResult] > 0 then
return tResult
end if
end repeat
return "pass"
end TesterTapGetWorstResult
-- Get the total number of tests from the statistics generated by
-- tapAnalyse()
function TesterTapGetTestCount pAnalysis
return pAnalysis["pass"] + pAnalysis["xpass"] + pAnalysis["xfail"] + \
pAnalysis["fail"] + pAnalysis["skip"]
end TesterTapGetTestCount
-- Combine two sets of TAP results
function TesterTapCombine pAnalysisA, pAnalysisB
local tCombined, tKey
if pAnalysisA is an array and pAnalysisB is an array then
repeat for each key tKey in pAnalysisB
if tKey is "log" then
put pAnalysisA[tKey] & pAnalysisB[tKey] into tCombined[tKey]
else
put pAnalysisA[tKey] + pAnalysisB[tKey] into tCombined[tKey]
end if
end repeat
else if pAnalysisA is an array then
put pAnalysisA into tCombined
else if pAnalysisB is an array then
put pAnalysisB into tCombined
else
put empty into tCombined
end if
return tCombined
end TesterTapCombine