/
test_file.py
109 lines (92 loc) · 3.5 KB
/
test_file.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
import unittest
import pygambit
class TestGambitEfgFile(unittest.TestCase):
def setUp(self):
with open("contrib/games/e02.efg") as f:
self.file_text = f.read()
def tearDown(self):
pass
def test_parse_string_empty(self):
with self.assertRaises(ValueError) as e:
pygambit.Game.parse_game("")
self.assertEqual(
str(e.exception),
"Parse error in game file: line 1:2: Expecting file type"
)
def test_parse_string_wrong_magic(self):
ft = self.file_text.replace("EFG", "")
with self.assertRaises(ValueError) as e:
pygambit.Game.parse_game(ft)
self.assertEqual(
str(e.exception),
"Parse error in game file: line 1:3: Expecting file type"
)
def test_parse_string_wrong_version(self):
ft = self.file_text.replace("EFG 2", "EFG 1")
with self.assertRaises(ValueError) as e:
pygambit.Game.parse_game(ft)
self.assertEqual(
str(e.exception),
"Parse error in game file: line 1:6: Accepting only EFG version 2"
)
def test_parse_string_wrong_precision(self):
ft = self.file_text.replace("EFG 2 R", "EFG 2 X")
with self.assertRaises(ValueError) as e:
pygambit.Game.parse_game(ft)
self.assertEqual(
str(e.exception),
"Parse error in game file: line 1:9: "
"Accepting only EFG R or D data type"
)
def test_parse_string_node_type(self):
ft = self.file_text.replace('p "" 1 1', 'x "" 1 1')
with self.assertRaises(ValueError) as e:
pygambit.Game.parse_game(ft)
self.assertEqual(
str(e.exception),
"Parse error in game file: line 4:3: Invalid type of node"
)
def test_parse_string_removed_player(self):
ft = self.file_text.replace('"Player 2"', "")
with self.assertRaises(ValueError) as e:
pygambit.Game.parse_game(ft)
self.assertEqual(
str(e.exception),
"Parse error in game file: line 5:26: Expecting '}' after outcome"
)
def test_parse_string_extra_payoff(self):
ft = self.file_text.replace("1, 1", "1, 2, 3")
with self.assertRaises(ValueError) as e:
pygambit.Game.parse_game(ft)
self.assertEqual(
str(e.exception),
"Parse error in game file: line 5:29: Expecting '}' after outcome"
)
def test_write_game_gte_sanity(self):
g = pygambit.Game.parse_game(self.file_text)
g.write("gte")
class TestGambitNfgFile(unittest.TestCase):
def setUp(self):
with open("contrib/games/e02.nfg") as f:
self.file_text = f.read()
def tearDown(self):
pass
def test_parse_string_removed_title(self):
ft = self.file_text.replace(
'"Selten (IJGT, 75), Figure 2, normal form"', ""
)
with self.assertRaises(ValueError) as e:
pygambit.Game.parse_game(ft)
self.assertEqual(
str(e.exception),
"Parse error in game file: line 1:11: Game title missing"
)
def test_parse_string_removed_player(self):
ft = self.file_text.replace('"Player 2"', "")
with self.assertRaises(ValueError) as e:
pygambit.Game.parse_game(ft)
self.assertEqual(
str(e.exception),
"Parse error in game file: line 1:73: "
"Not enough players for number of strategy entries"
)