Skip to content

Commit c89dfd4

Browse files
author
Paul Paterson
committed
Updates for Python24
--HG-- extra : convert_revision : svn%3A33e6b92d-8b28-0410-bad6-e5809550d60d/Trunk%4013
1 parent c9b8631 commit c89dfd4

File tree

1 file changed

+169
-173
lines changed

1 file changed

+169
-173
lines changed

vb2pyautotest/autotest.py

+169-173
Original file line numberDiff line numberDiff line change
@@ -1,173 +1,169 @@
1-
# Created by Leo from: C:\Development\Python22\Lib\site-packages\vb2py\vb2py.leo
2-
3-
# @color
4-
5-
"""An automatic testing framework to compare VB and Python versions of a function"""
6-
7-
import os
8-
import pprint
9-
from vb2py import converter, vbparser, config
10-
Config = config.VB2PYConfigObject("autotest.ini", "../vb2pyautotest")
11-
12-
# << AutoTest Functions >> (1 of 3)
13-
class TestMaker:
14-
"""A Base Class to help in making unit tests"""
15-
16-
default_filename = "testscript.txt"
17-
18-
# << TestMaker methods >> (1 of 8)
19-
def __init__(self, filename):
20-
"""Initialize the test maker"""
21-
self.filename = filename
22-
# << TestMaker methods >> (2 of 8)
23-
def parseVB(self):
24-
"""Parse the VB code"""
25-
self.parser = converter.VBConverter(converter.importTarget("PythonCard"), converter.FileParser)
26-
self.parser.doConversion(self.filename)
27-
# << TestMaker methods >> (3 of 8)
28-
def createTests(self):
29-
"""Create the test signatures"""
30-
self.tests = self.extractSignatures(self.parser)
31-
# << TestMaker methods >> (4 of 8)
32-
def makeTestForFunction(self, testid, modulename, fnname, paramlist):
33-
"""Make a test script for a function
34-
35-
The function resides in a module and takes a list of parameters which
36-
are specified in paramlist. Paramlist also includes a list of values
37-
to pass as that particular parameter.
38-
39-
The results of the test are written to a file testid
40-
41-
"""
42-
raise NotImplementedError
43-
# << TestMaker methods >> (5 of 8)
44-
def extractSignatures(self, project):
45-
"""Extract function test signatures from a project"""
46-
fns = []
47-
for module in project.modules:
48-
for defn in module.code_structure.locals:
49-
if isinstance(defn, vbparser.VBFunction):
50-
test = ["test_%s_%s" % (module.name, defn.identifier),
51-
module.name,
52-
defn.identifier]
53-
ranges = []
54-
for param in defn.parameters:
55-
try:
56-
thisrange = Config["DefaultRanges", param.type]
57-
except config.ConfigParser.NoOptionError:
58-
thisrange = []
59-
ranges.append((param.identifier, eval(thisrange)))
60-
test.append(ranges)
61-
fns.append(test)
62-
return fns
63-
# << TestMaker methods >> (6 of 8)
64-
def createTestScript(self):
65-
"""Create a script containing the tests"""
66-
ret = []
67-
for test in self.tests:
68-
ret.append(self.makeTestForFunction(*test))
69-
return "\n".join(ret)
70-
# << TestMaker methods >> (7 of 8)
71-
def writeTestsToFile(self, filename=None):
72-
"""Write the test script to a file"""
73-
script = self.createTestScript()
74-
if filename is None:
75-
filename = self.default_filename
76-
f = open(filename, "w")
77-
f.write(script)
78-
f.close()
79-
# << TestMaker methods >> (8 of 8)
80-
def makeTestFile(self, filename=None):
81-
"""Translate VB and make tests"""
82-
self.parseVB()
83-
self.createTests()
84-
self.writeTestsToFile(filename)
85-
# -- end -- << TestMaker methods >>
86-
# << AutoTest Functions >> (2 of 3)
87-
class PythonTestMaker(TestMaker):
88-
"""A Class to help in making Python unit tests"""
89-
90-
default_filename = "testscript.py"
91-
92-
# << PythonTestMaker methods >>
93-
def makeTestForFunction(self, testid, modulename, fnname, paramlist):
94-
"""Make a Python test script for a function
95-
96-
The function resides in a module and takes a list of parameters which
97-
are specified in paramlist. Paramlist also includes a list of values
98-
to pass as that particular parameter.
99-
100-
The results of the test are written to a file testid
101-
102-
"""
103-
ret = []
104-
ret.append("from %s import %s" % (modulename, fnname))
105-
ret.append("results = []")
106-
tabs = ""
107-
#
108-
for param, values in paramlist:
109-
ret.append("%sfor %s in %s:" % (tabs, param, values))
110-
tabs += "\t"
111-
#
112-
arg_list = ",".join([param[0] for param in paramlist])
113-
ret.append("%sresults.append((%s(%s), %s))" % (tabs, fnname, arg_list, arg_list))
114-
ret.extend(("f = open('%s_py.txt', 'w')" % testid,
115-
r"f.write('# vb2Py Autotest results\n')",
116-
r"f.write('\n'.join([', '.join(map(str, x)) for x in results]))",
117-
r"f.close()"))
118-
#
119-
return "\n".join(ret)
120-
# -- end -- << PythonTestMaker methods >>
121-
# << AutoTest Functions >> (3 of 3)
122-
class VBTestMaker(TestMaker):
123-
"""A Class to help in making VB unit tests"""
124-
125-
default_filename = "testscript.bas"
126-
127-
# << VBTestMaker methods >>
128-
def makeTestForFunction(self, testid, modulename, fnname, paramlist):
129-
"""Make a VB test script for a function
130-
131-
The function resides in a module and takes a list of parameters which
132-
are specified in paramlist. Paramlist also includes a list of values
133-
to pass as that particular parameter.
134-
135-
The results of the test are written to a file testid
136-
137-
"""
138-
ret = []
139-
ret.append("Dim Results()")
140-
ret.append("ReDim Results(0)")
141-
tabs = ""
142-
#
143-
for param, values in paramlist:
144-
ret.append("%sfor each %s in Array%s" % (tabs, param, tuple(values)))
145-
tabs += "\t"
146-
#
147-
arg_list = ",".join([param[0] for param in paramlist])
148-
ret.append("%sRedim Preserve Results(UBound(Results)+1)" % tabs)
149-
ret.append("%sAnswer = %s(%s)" % (tabs, fnname, arg_list))
150-
#
151-
result_list = ' & "," & '.join(["Str(%s)" % x for x in ["Answer"] + [param[0] for param in paramlist]])
152-
ret.append("%sResults(Ubound(Results)) = %s" % (tabs, result_list))
153-
#
154-
for param, values in paramlist:
155-
tabs = tabs[:-1]
156-
ret.append("Next %s" % param)
157-
#
158-
ret.extend(("Chn = NextFile",
159-
"Open '%s_vb.txt' For Output As #Chn" % testid,
160-
'Print #Chn, "# vb2Py Autotest results"',
161-
"For Each X In Results",
162-
" Print #Chn, X",
163-
"Next X",
164-
"Close #Chn"))
165-
#
166-
return "\n".join(ret)
167-
# -- end -- << VBTestMaker methods >>
168-
# -- end -- << AutoTest Functions >>
169-
170-
if __name__ == "__main__":
171-
filename = 'c:\\development\\python22\\lib\\site-packages\\vb2py\\vb\\test3\\Globals.bas'
172-
p = PythonTestMaker(filename)
173-
v = VBTestMaker(filename)
1+
"""An automatic testing framework to compare VB and Python versions of a function"""
2+
3+
import os
4+
import pprint
5+
from vb2py import converter, vbparser, config
6+
Config = config.VB2PYConfigObject("autotest.ini", "../vb2pyautotest")
7+
8+
# << AutoTest Functions >> (1 of 3)
9+
class TestMaker:
10+
"""A Base Class to help in making unit tests"""
11+
12+
default_filename = "testscript.txt"
13+
14+
# << TestMaker methods >> (1 of 8)
15+
def __init__(self, filename):
16+
"""Initialize the test maker"""
17+
self.filename = filename
18+
# << TestMaker methods >> (2 of 8)
19+
def parseVB(self):
20+
"""Parse the VB code"""
21+
self.parser = converter.VBConverter(converter.importTarget("PythonCard"), converter.FileParser)
22+
self.parser.doConversion(self.filename)
23+
# << TestMaker methods >> (3 of 8)
24+
def createTests(self):
25+
"""Create the test signatures"""
26+
self.tests = self.extractSignatures(self.parser)
27+
# << TestMaker methods >> (4 of 8)
28+
def makeTestForFunction(self, testid, modulename, fnname, paramlist):
29+
"""Make a test script for a function
30+
31+
The function resides in a module and takes a list of parameters which
32+
are specified in paramlist. Paramlist also includes a list of values
33+
to pass as that particular parameter.
34+
35+
The results of the test are written to a file testid
36+
37+
"""
38+
raise NotImplementedError
39+
# << TestMaker methods >> (5 of 8)
40+
def extractSignatures(self, project):
41+
"""Extract function test signatures from a project"""
42+
fns = []
43+
for module in project.modules:
44+
for defn in module.code_structure.locals:
45+
if isinstance(defn, vbparser.VBFunction):
46+
test = ["test_%s_%s" % (module.name, defn.identifier),
47+
module.name,
48+
defn.identifier]
49+
ranges = []
50+
for param in defn.parameters:
51+
try:
52+
thisrange = Config["DefaultRanges", param.type]
53+
except config.ConfigParser.NoOptionError:
54+
thisrange = []
55+
ranges.append((param.identifier, eval(thisrange)))
56+
test.append(ranges)
57+
fns.append(test)
58+
return fns
59+
# << TestMaker methods >> (6 of 8)
60+
def createTestScript(self):
61+
"""Create a script containing the tests"""
62+
ret = []
63+
for test in self.tests:
64+
ret.append(self.makeTestForFunction(*test))
65+
return "\n".join(ret)
66+
# << TestMaker methods >> (7 of 8)
67+
def writeTestsToFile(self, filename=None):
68+
"""Write the test script to a file"""
69+
script = self.createTestScript()
70+
if filename is None:
71+
filename = self.default_filename
72+
f = open(filename, "w")
73+
f.write(script)
74+
f.close()
75+
# << TestMaker methods >> (8 of 8)
76+
def makeTestFile(self, filename=None):
77+
"""Translate VB and make tests"""
78+
self.parseVB()
79+
self.createTests()
80+
self.writeTestsToFile(filename)
81+
# -- end -- << TestMaker methods >>
82+
# << AutoTest Functions >> (2 of 3)
83+
class PythonTestMaker(TestMaker):
84+
"""A Class to help in making Python unit tests"""
85+
86+
default_filename = "testscript.py"
87+
88+
# << PythonTestMaker methods >>
89+
def makeTestForFunction(self, testid, modulename, fnname, paramlist):
90+
"""Make a Python test script for a function
91+
92+
The function resides in a module and takes a list of parameters which
93+
are specified in paramlist. Paramlist also includes a list of values
94+
to pass as that particular parameter.
95+
96+
The results of the test are written to a file testid
97+
98+
"""
99+
ret = []
100+
ret.append("from %s import %s" % (modulename, fnname))
101+
ret.append("results = []")
102+
tabs = ""
103+
#
104+
for param, values in paramlist:
105+
ret.append("%sfor %s in %s:" % (tabs, param, values))
106+
tabs += "\t"
107+
#
108+
arg_list = ",".join([param[0] for param in paramlist])
109+
ret.append("%sresults.append((%s(%s), %s))" % (tabs, fnname, arg_list, arg_list))
110+
ret.extend(("f = open('%s_py.txt', 'w')" % testid,
111+
r"f.write('# vb2Py Autotest results\n')",
112+
r"f.write('\n'.join([', '.join(map(str, x)) for x in results]))",
113+
r"f.close()"))
114+
#
115+
return "\n".join(ret)
116+
# -- end -- << PythonTestMaker methods >>
117+
# << AutoTest Functions >> (3 of 3)
118+
class VBTestMaker(TestMaker):
119+
"""A Class to help in making VB unit tests"""
120+
121+
default_filename = "testscript.bas"
122+
123+
# << VBTestMaker methods >>
124+
def makeTestForFunction(self, testid, modulename, fnname, paramlist):
125+
"""Make a VB test script for a function
126+
127+
The function resides in a module and takes a list of parameters which
128+
are specified in paramlist. Paramlist also includes a list of values
129+
to pass as that particular parameter.
130+
131+
The results of the test are written to a file testid
132+
133+
"""
134+
ret = []
135+
ret.append("Dim Results()")
136+
ret.append("ReDim Results(0)")
137+
tabs = ""
138+
#
139+
for param, values in paramlist:
140+
ret.append("%sfor each %s in Array%s" % (tabs, param, tuple(values)))
141+
tabs += "\t"
142+
#
143+
arg_list = ",".join([param[0] for param in paramlist])
144+
ret.append("%sRedim Preserve Results(UBound(Results)+1)" % tabs)
145+
ret.append("%sAnswer = %s(%s)" % (tabs, fnname, arg_list))
146+
#
147+
result_list = ' & "," & '.join(["Str(%s)" % x for x in ["Answer"] + [param[0] for param in paramlist]])
148+
ret.append("%sResults(Ubound(Results)) = %s" % (tabs, result_list))
149+
#
150+
for param, values in paramlist:
151+
tabs = tabs[:-1]
152+
ret.append("Next %s" % param)
153+
#
154+
ret.extend(("Chn = NextFile",
155+
"Open '%s_vb.txt' For Output As #Chn" % testid,
156+
'Print #Chn, "# vb2Py Autotest results"',
157+
"For Each X In Results",
158+
" Print #Chn, X",
159+
"Next X",
160+
"Close #Chn"))
161+
#
162+
return "\n".join(ret)
163+
# -- end -- << VBTestMaker methods >>
164+
# -- end -- << AutoTest Functions >>
165+
166+
if __name__ == "__main__":
167+
filename = 'c:\\development\\python22\\lib\\site-packages\\vb2py\\vb\\test3\\Globals.bas'
168+
p = PythonTestMaker(filename)
169+
v = VBTestMaker(filename)

0 commit comments

Comments
 (0)