Debian Codegraph @

tests/build_tests.py

raw
1 #!/usr/bin/python3
2 f = open('run_tests.py', 'w')
3 f.write('''#!/usr/bin/env python3
4
5 #####################################################
6 ### run_tests.py IS A GENERATED FILE, DO NOT EDIT ###
7 #####################################################
8
9 import subprocess, unittest
10
11 class CompilerTest:
12 rules_file = ''
13 bin_file = ''
14 def test_compiles(self):
15 with self.assertRaises(subprocess.CalledProcessError):
16 subprocess.check_output(['../src/rtx-comp', self.rules_file, self.bin_file],
17 stderr=subprocess.STDOUT, universal_newlines=True)
18
19 class InterpreterTest:
20 rules_file = ''
21 bin_file = ''
22 input = ''
23 output = ''
24 lex_file = ''
25 def setUp(self):
26 args = ['../src/rtx-comp']
27 if len(self.lex_file) > 0:
28 args += ['-l', self.lex_file]
29 args += [self.rules_file, self.bin_file]
30 subprocess.check_output(args, stderr=subprocess.STDOUT, universal_newlines=True)
31 def test_output(self):
32 args = ['../src/rtx-proc', '-a', self.bin_file]
33 actual = subprocess.check_output(args, input=self.input, universal_newlines=True)
34 self.maxDiff = None
35 self.assertEqual(self.output, actual)
36
37
38 ''')
39
40 err = '''
41 class {0}(CompilerTest, unittest.TestCase):
42 rules_file = '{0}.rtx'
43 bin_file = '{0}.bin'
44 '''
45
46 run = '''
47 class {0}(InterpreterTest, unittest.TestCase):
48 rules_file = '{0}.rtx'
49 bin_file = '{0}.bin'
50 input = """{1}"""
51 output = """{2}"""
52 '''
53
54 run_xml = '''
55 class {0}XML(InterpreterTest, unittest.TestCase):
56 rules_file = '{0}.trx'
57 bin_file = '{0}.bin'
58 input = """{1}"""
59 output = """{2}"""
60 '''
61
62 cook = '''
63 class Cookbook{0}(InterpreterTest, unittest.TestCase):
64 rules_file = 'cookbook/{0}.rtx'
65 bin_file = 'cookbook/{0}.bin'
66 input = """{1}"""
67 output = """{2}"""
68 '''
69
70 from os import listdir
71 from os.path import basename
72 ls = listdir('.')
73 for fname in ls:
74 if fname.endswith('.rtx') or fname.endswith('.trx'):
75 base, ext = fname.split('.')
76 if (base + '.input') in ls:
77 fi = open(base + '.input')
78 i = fi.read()
79 fi.close()
80 fo = open(base + '.output')
81 o = fo.read()
82 fo.close()
83 if ext == 'trx':
84 f.write(run_xml.format(base, i, o))
85 else:
86 f.write(run.format(base, i, o))
87 if (base + '.lex') in ls:
88 f.write(" lex_file = '%s.lex'\n" % base)
89 else:
90 f.write(err.format(base))
91 for fname in listdir('./cookbook'):
92 if fname.endswith('.rtx'):
93 base = fname.split('.')[0]
94 rf = open('cookbook/' + fname)
95 i = ''
96 o = ''
97 for l in rf.readlines():
98 if l.startswith('! < '): i += l[4:]
99 elif l.startswith('! > '): o += l[4:]
100 f.write(cook.format(base, i, o))
101 f.write('''
102 if __name__ == '__main__':
103 unittest.main(buffer=True, verbosity=2)
104 ''')