-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
executable file
·104 lines (86 loc) · 2.39 KB
/
tests.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
#!/usr/bin/env python3
import logic
import unittest
TEST_USERNAME = "LOL"
TEST_PASSWORD = "YOLO"
# Disgusting bit of python dark magic that badly emulates parameterized tests.
# Note that the output is terrible, and all output is always seen.
def with_battery(test_battery):
def decorator(fn):
def spoof_test_fn(testcase_self):
for i, entry in enumerate(test_battery):
with testcase_self.subTest(i=i, entry=entry):
fn(testcase_self, entry)
return spoof_test_fn
return decorator
class MockInterface(logic.CallbackInterface):
def __init__(self):
self.actions = []
def send(self, *args):
self.actions.append(("send", *args))
def log(self, *args):
pass # Don't want to test logs
def die(self):
self.actions.append(("die"))
BEHAVIOR_TESTS = [
# Does this even work?
[],
# Does starting even work?
[
[
["motd", "ohai"],
[
("send", "join", TEST_USERNAME, TEST_PASSWORD),
("send", "chat", "ohai"),
],
],
],
# Do we die after an error?
[
[
["error", "505 or something"],
[
("die"),
],
],
],
# Do we crash after a chat message?
[
[
["chat", "42", "henlo"],
[
],
],
],
[
[
["motd", "ohai"],
[
("send", "join", TEST_USERNAME, TEST_PASSWORD),
("send", "chat", "ohai"),
],
],
[
["chat", "42", "henlo"],
[
],
],
],
]
class BehaviorTests(unittest.TestCase):
def test_import_secret(self):
import secret
# Just to make sure it's properly formatted
def test_import_bwbot(self):
import bwbot
# Just to make sure it's properly formatted
@with_battery(BEHAVIOR_TESTS)
def test_behavior(self, steps):
mock_interface = MockInterface()
game = logic.Logic(TEST_USERNAME, TEST_PASSWORD, mock_interface)
for step_input, step_expected_outputs in steps:
game.digest(step_input[0], step_input[1:])
self.assertEqual(mock_interface.actions, step_expected_outputs)
mock_interface.actions = []
if __name__ == "__main__":
unittest.main()