-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen.sq
More file actions
351 lines (319 loc) · 8.74 KB
/
codegen.sq
File metadata and controls
351 lines (319 loc) · 8.74 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
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
declare function local:showStepNumber($x as document-node())
{
for $y at $pos in $x//Program/MainClass/SetOfSteps/Step
where $y/StepLine/StepNumber/Integer = 1
return $y
};
declare function local:firstStepFunction($setOfSteps as node(),$functionname as xs:string)
as xs:string
{
let $firstStep := ($setOfSteps/Step)[1]
return "def "
||$functionname
|| "step(browser, loop = False):"
||" "
||" return "
||$functionname
||"step"
||$firstStep/StepLine/StepNumber/Integer
||"(browser, loop)"
};
declare function local:getNextStep($stepnumber,$stepcount)
as xs:string
{
if ($stepnumber=$stepcount) then
""
else
"step"||fn:sum((1,$stepnumber))||"(browser,loop)"
};
declare function local:objectExprFunction($objectExpr)
as xs:string
{
let $variant := $objectExpr/@variant
return switch($variant)
case "1" return "browser.html"
case "2" return 'browser.find_by_id("' || $objectExpr/String_literal || '").first'
case "3" return 'browser.find_by_id("' || $objectExpr/String_literal || '")'
case "5" return "browser.url"
default return ""
};
declare function local:ifStmtFunction($ifStmt as node(),$stepnumber)
as xs:string
{
" if len("
||local:objectExprFunction($ifStmt/ObjectExpr)
||")>0:"|| " "
||" "
||local:stmtFunction($ifStmt/Stmt,$stepnumber)|| " "
||" else:"|| " "
||" "
||local:stmtFunction($ifStmt/OtherwiseStmt/Stmt,$stepnumber)|| " "
};
declare function local:shouldHaveValue($result, $stepnumber)
as xs:string
{
".value!="
||'"'||$result/ShouldPhrase/String_literal||'":'|| " "
||' return "'
||$stepnumber
||'"'
};
declare function local:shouldContain($result, $stepnumber)
as xs:string
{
'.find("'
||$result/ShouldPhrase/String_literal
||'") == -1:' || " "
||' return "'
||$stepnumber
||'"'
};
declare function local:shouldBeURL($result, $stepnumber)
as xs:string
{
' != "'
|| $result/ShouldPhrase/URL ||'":'||'" '
||' return "'
||$stepnumber
||'"'
};
declare function local:shouldPhraseFunction($result, $stepnumber)
as xs:string
{
let $variant := $result/ShouldPhrase/@variant
return switch($variant)
case "0" return local:shouldHaveValue($result, $stepnumber)
case "1" return local:shouldContain($result, $stepnumber)
case "2" return local:shouldBeURL($result, $stepnumber)
default return ""
};
declare function local:goToUrl($stmt as node())
as xs:string
{
' url = "' || $stmt/URL || '"' || " "
||" if not (url.endswith(SLASH)):" || " "
||" url += SLASH" || " "
||" browser.visit(url)"|| " "
};
declare function local:stringExpr($stmt as node())
as xs:string
{
let $variant := $stmt/StringExpr/@variant
return switch($variant)
case "0" return '"'||$stmt/StringExpr/String_literal||'"'
case "2" return
fold-right($stmt/StringExpr/ObjectExpr,'""',function($objectexpr,$acc){local:objectExprFunction($objectexpr)||".value+" || $acc})
default return '"'||$stmt/String_literal||'"'
};
declare function local:enterString($stmt as node())
as xs:string
{
" "
||local:objectExprFunction($stmt/ObjectExpr)
||'.type('
||local:stringExpr($stmt)
||')'
};
declare function local:integerExpr($stmt as node())
as xs:string
{
let $variant := $stmt/IntegerExpr/@variant
return switch($variant)
case "2" return
fold-right($stmt/IntegerExpr/ObjectExpr,'0',function($objectexpr,$acc){"float(" ||local:objectExprFunction($objectexpr)||".value)+" || $acc})
default return $stmt/Integer
};
declare function local:enterInteger($stmt as node())
as xs:string
{
" "
||local:objectExprFunction($stmt/ObjectExpr)
||'.type(str('
||local:integerExpr($stmt)
||'))'
};
declare function local:goToStep($stmt as node())
as xs:string
{
"return step"
||$stmt/StepNumber/Integer
||"(browser,loop)"
};
declare function local:clickButton($stmt as node())
as xs:string
{
" oldURL=browser.url"|| " "
||" "
||local:objectExprFunction($stmt/ObjectExpr)
||".first.click()"|| " "
||" sleep(1)"|| " "
||" d = 0"||" "
||" newURL = browser.url"|| " "
||' while oldURL == newURL or browser.evaluate_script("document.readyState")!="complete":'|| " "
||" sleep(0.1)"|| " "
||" d+=1"|| " "
||" if d>10000:"|| " "
||' return "5"'|| " "
};
declare function local:loopStep($stmt as node(),$functionname,$stepnumber)
as xs:string
{
" c=0"|| " "
||" "
||"for c in range (0,"
||$stmt/IntegerTime/Integer
||"):"|| " "
||" "
||"err="
||$functionname
||"step"
||$stmt/StepNumber/Integer
||"(browser, loop=True)"|| " "
||" if not (err == None) and not (err.isspace()) and not (len(err) == 0):" || " "
||' return "'
||$stepnumber
||'"'|| " "
};
declare function local:doFunction($stmt as node(),$stepnumber)
as xs:string
{
let $variant := $stmt/IntegerTime/@variant
return switch($variant)
case "0" return local:loopStep($stmt,$stmt/Id||"_",$stepnumber)
case "1" return " err="||$stmt/ID||"_step(browser,loop=True)"
default return ""
};
declare function local:stmtFunction($stmt, $stepnumber)
as xs:string
{
let $variant := $stmt/@variant
return switch($variant)
case "0" return local:loopStep($stmt,"",$stepnumber)
case "1" return local:doFunction($stmt,$stepnumber)
case "2" return local:goToUrl($stmt)
case "3" return local:goToStep($stmt)
case "4" return local:enterString($stmt)
case "5" return local:enterInteger($stmt)
case "6" return local:clickButton($stmt)
case "7" return " browser.reload()"
default return "SYNTAX ERROR"
};
declare function local:resultFunction($result as node(),$stepnumber)
as xs:string
{
let $variant := $result/@variant
return switch($variant)
case "0" return " if " || local:objectExprFunction($result/ObjectExpr) || local:shouldPhraseFunction($result, $stepnumber)
default return ""
};
declare function local:functionFunction($function)
as xs:string
{
local:firstStepFunction($function/SetOfSteps, $function/ID||"_")
||" "
||" "
||fold-right($function/SetOfSteps/Step,"",function($step,$acc){local:stepFunction($step, $function/ID||"_",count($function/SetOfSteps/Step)) || $acc})
};
declare function local:stepFunction($step, $fuctionname,$stepcount)
as xs:string
{
if ($step/StepLine/Stmt) then
"def "
||$fuctionname
||"step"
||$step/StepLine/StepNumber/Integer
||"(browser, loop=False):"
||" "
||" "
||"#Step "
||$step/StepLine/StepNumber/Integer
||" "
||" "
||local:stmtFunction($step/StepLine/Stmt,$step/StepLine/StepNumber/Integer)
||" "
||" #Checking Step" || $step/StepLine/StepNumber/Integer ||" "
||local:resultFunction($step/StepLine/Result,$step/StepLine/StepNumber/Integer)
||" "
||" if loop:"
||" "
||" return"||" "
||" return "
||local:getNextStep($step/StepLine/StepNumber/Integer,$stepcount)
||" "
else if ($step/StepLine/IfStmt) then
"def "
||$fuctionname
||"step"
||$step/StepLine/StepNumber/Integer
||"(browser, loop=False):"
||" "
||" "
||"#Step "
||$step/StepLine/StepNumber/Integer
||" "
||" "
||local:ifStmtFunction($step/StepLine/IfStmt,$step/StepLine/StepNumber/Integer)
||" "
||" #Checking Step" || $step/StepLine/StepNumber/Integer ||" "
||local:resultFunction($step/StepLine/Result,$step/StepLine/StepNumber/Integer)
||" "
||" if loop:"
||" "
||" return"||" "
||" return "
||local:getNextStep($step/StepLine/StepNumber/Integer,$stepcount)
||" "
else if ($step/StepLine/Exit) then
"def "
||$fuctionname
||"step"
||$step/StepLine/StepNumber/Integer
||"(browser, loop=False):"
||" "
||" "
||"#Step "
||$step/StepLine/StepNumber/Integer
||" "
||" "
||" return"
||" "
||" #Checking Step" || $step/StepLine/StepNumber/Integer ||" " ||" if loop:"
||" "
||" return"||" "
else
""
};
let $setOfSteps := doc('$INPUTFILE')//Program/MainClass/SetOfSteps
let $setOfFunction := doc('$INPUTFILE')//Program/SetOfFunction
return "from splinter import Browser" || " "
||"from time import sleep" || " "
|| " "
|| " "
|| 'SLASH = "/"'
|| " "
|| " "
|| local:firstStepFunction($setOfSteps, "")
|| " "
|| " "
|| fold-right($setOfFunction/Function,"",function($function,$acc){local:functionFunction($function) || $acc})
|| " "
|| " "
|| fold-right($setOfSteps/Step,"",function($step,$acc){local:stepFunction($step,"",count($setOfSteps/Step)) || $acc})
|| " "
|| " "
|| "def checkSteps():" || " "
|| " browser=Browser('chrome')"|| " "
|| " err = step(browser)" || " "
|| " "
|| " #program is done" || " "
|| " browser.quit()" || " "
|| " "
|| " #return the failed step" || " "
|| " if not (err == None) and not (err.isspace()) and not (len(err) == 0):" || " "
|| ' return "Program failed on step: " + err + "\n"' || " "
|| " "
|| ' return "The testcase passed"' || " "
|| " "
|| " "
|| "if __name__ == '__main__':" || " "
|| " print(checkSteps())" || " "