-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot_compiler_test.py
87 lines (75 loc) · 2.03 KB
/
bot_compiler_test.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
import bot_compiler
import subprocess
import unittest
import os
player_command_file = 'ololo'
HelloWorld = [
['cpp', '''
#include <iostream>
int main()
{
std::cout << "Hello, world!";
return 0;
}
'''], ['pas', '''
program MyProgram;
begin
Write ('Hello, world!');
end.
'''], ['py', '''
print('Hello, world!', end='')
''']]
config = [
"John Smith" "HelloC" "hello1",
"John Smith" "HelloPas" "hello2",
"John Smith" "HelloPython" "hello3"
]
players = [
["John Smith", "HelloC", "hello1.cpp"],
["John Smith", "HelloPas", "hello2.pas"],
["John Smith", "HelloPython", "hello3.py"]
]
class BotCompilerTest(unittest.TestCase):
def setUp(self):
''' Creates source code files to compile '''
j = 1
for extension, program in HelloWorld:
f = open('hello{}.{}'.format(str(j), extension), 'w')
for line in program:
f.writelines([line])
f.close()
j = j + 1
def tearDown(self):
''' Removes source code files from disk '''
os.remove('hello1.cpp')
if os.name == 'nt':
os.remove('hello1.exe')
else:
os.remove('hello1')
os.remove('hello2.pas')
os.remove('hello2.o')
if os.name == 'nt':
os.remove('hello2.exe')
else:
os.remove('hello2')
os.remove('hello3.py')
os.remove('ololo')
def test_compile(self):
bot_c = bot_compiler.BotCompiler()
bot_c.compile(players, player_command_file)
f = open(player_command_file, 'r')
bots = f.readlines()
f.close()
for bot in bots:
command_line = bot.split("\'")[5]
process = subprocess.Popen(
command_line,
shell=True,
cwd=os.path.abspath(os.curdir),
stdout=subprocess.PIPE
)
out = process.stdout.read()
process.kill()
self.assertEqual(out, b'Hello, world!')
if __name__ == '__main__':
unittest.main()