-
Notifications
You must be signed in to change notification settings - Fork 0
/
languages.py
208 lines (197 loc) · 8.56 KB
/
languages.py
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
from utils import *
import subprocess
import types
Language.PYTHON = Language("python", "", ">>>", "py")
doctest = re.compile(r" *# Q.* - .*")
# https://stackoverflow.com/questions/940822/regular-expression-syntax-for-match-nothing
expect = re.compile(r"$^")
test = re.compile(r" *>>> .*\n")
no_tests = re.compile(r"$^")
buf = re.compile(r' *"""')
offset = 2
Language.PYTHON.formatting = Formatting(doctest, expect, test, no_tests, buf, offset)
def run(self, args, python, **kwargs):
tests = self.tests
src = self.file
result = ""
prev = None
total = correct = 0
wrong = False
keys = iter(list(tests.keys()))
inputs = ""
outputs = ""
for t in keys:
if not wrong and tests[t][0].language == self:
while True:
interpreter = subprocess.Popen([python, "-B", "-i", src], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, bufsize=0)
if not prev:
count = 1
if args.v:
result += "---------------------------------------------------------------------\nDoctests for {!s}\n\n".format(t[t.index('-') + 2:])
prev = t
curr = tests[t][0]
inputs += curr.test + "\n"
curr.output = curr.output.strip()
raw_out = interpreter.communicate(input=inputs)
interpreter.stdin.close()
out = ""
for o in raw_out:
out += o
try:
if not outputs:
test_out = curr.run(raw_out[0])
if raw_out[0]:
outputs += raw_out[0].strip() + "\n"
else:
test_out = curr.run(raw_out[0][raw_out[0].index(outputs) + len(outputs):])
outputs = raw_out[0]
except Exception as e:
import sys
sys.exit("An unexpected error has occurred")
total += 1
if args.v:
result += "Case {!s}:\n".format(count)
result += "{!s}\n".format(test_out[0])
elif not test_out[1]:
result += "---------------------------------------------------------------------\nDoctests for {!s}\n\n".format(t[t.index('-') + 2:])
result += "Case {!s}:\n".format(count)
result += "{!s}\n".format(test_out[0])
wrong = True
break
correct += int(test_out[1])
tests[t].remove(curr)
count += 1
if not tests[t]:
tests.pop(t)
prev = None
break
else:
break
return result, correct, total
Language.PYTHON.run = run.__get__(Language.PYTHON)
Language.SCHEME = Language("scheme", "scheme", "scm>", "scm")
doctest = re.compile(r"; Q.* - .*")
expect = re.compile(r"; expect .*")
test = re.compile(r"; .*\n")
no_tests = re.compile(r";;; No Tests\n")
buf = re.compile(r";;; Tests\n")
offset = 0
Language.SCHEME.formatting = Formatting(doctest, expect, test, no_tests, buf, offset)
def run(self, args, python, windows):
tests = self.tests
src = self.file
result = ""
prev = None
total = correct = 0
wrong = False
check = '(load-all ".")'
scheme = subprocess.Popen([python, "scheme", "-i", src], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, bufsize=0)
parens = scheme.communicate(input=check)
for line in parens:
if "SyntaxError" in line:
import sys
sys.exit("---------------------------------------------------------------------\n\n{!s} {!s}\n# Error: unexpected end of file\n\n---------------------------------------------------------------------\nTest summary\n 0 test case(s) passed before encountering first failed test case\n".format(self.prompt, check))
scheme.stdin.close()
keys = iter(list(tests.keys()))
inputs = ""
outputs = 0
for t in keys:
if not wrong and tests[t][0].language == self:
while True:
interpreter = subprocess.Popen([python, "scheme", "-i", src], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, bufsize=0)
for line in interpreter.stdout:
break
if not prev:
count = 1
if args.v:
result += "---------------------------------------------------------------------\nDoctests for {!s}\n\n".format(t[t.index('-') + 2:])
prev = t
curr = tests[t][0]
inputs += curr.test + "\n"
curr.output = curr.output.strip()
raw_out = interpreter.communicate(input=inputs)[0]
if windows:
raw_out = raw_out[1 + len(self.prompt) + 1:]
interpreter.stdin.close()
output_count = 0
while output_count < outputs:
raw_out = raw_out[raw_out.index("\nscm>") + 6:]
output_count += 1
outputs += 1
test_out = curr.run(raw_out[:raw_out.index("\n{!s}".format(self.prompt))])
total += 1
if args.v:
result += "Case {!s}:\n".format(count)
result += "{!s}\n".format(test_out[0])
elif not test_out[1]:
result += "---------------------------------------------------------------------\nDoctests for {!s}\n\n".format(t[t.index('-') + 2:])
result += "Case {!s}:\n".format(count)
result += "{!s}\n".format(test_out[0])
wrong = True
break
correct += int(test_out[1])
tests[t].remove(curr)
count += 1
if not tests[t]:
tests.pop(t)
prev = None
break
else:
break
return result, correct, total
Language.SCHEME.run = run.__get__(Language.SCHEME)
Language.SQL = Language("sql", "sqlite_shell.py", "sqlite3>", "sql")
doctest = re.compile(r"-- Q.* - .*")
expect = re.compile(r"-- expect .*")
test = re.compile(r"-- .*\n")
no_tests = re.compile(r"-- No Tests\n")
buf = re.compile(r"-- Tests\n")
offset = 1
Language.SQL.formatting = Formatting(doctest, expect, test, no_tests, buf, offset)
def run(self, args, python, **kwargs):
tests = self.tests
src = self.file
result = ""
prev = None
total = correct = 0
wrong = False
keys = iter(list(tests.keys()))
for t in keys:
if not wrong and tests[t][0].language == self:
while True:
interpreter = subprocess.Popen([python, "sqlite_shell.py", "-init", src], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True, bufsize=0)
if not prev:
count = 1
if args.v:
result += "---------------------------------------------------------------------\nDoctests for {!s}\n\n".format(t[t.index('-') + 2:])
prev = t
curr = tests[t][0]
curr.output = curr.output.strip()
raw_out = interpreter.communicate(input=curr.test)
interpreter.stdin.close()
if raw_out[0] == "":
raw_out = raw_out[1]
elif raw_out[1] == "":
raw_out = raw_out[0]
test_out = curr.run(raw_out[:])
total += 1
if args.v:
result += "Case {!s}:\n".format(count)
result += "{!s}\n".format(test_out[0])
elif not test_out[1]:
result += "---------------------------------------------------------------------\nDoctests for {!s}\n\n".format(t[t.index('-') + 2:])
result += "Case {!s}:\n".format(count)
result += "{!s}\n".format(test_out[0])
wrong = True
break
correct += int(test_out[1])
tests[t].remove(curr)
count += 1
if not tests[t]:
tests.pop(t)
prev = None
break
else:
break
return result, correct, total
Language.SQL.run = run.__get__(Language.SQL)