-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_commands.py
More file actions
277 lines (215 loc) · 8.84 KB
/
test_commands.py
File metadata and controls
277 lines (215 loc) · 8.84 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"""
Tests for the Interlocutor command system.
Run with: python -m pytest test_commands.py -v
"""
import pytest
from interlocutor_commands.dispatcher import CommandDispatcher, CommandResult
from interlocutor_commands.dice import (
DiceCommand,
DiceExpression,
DiceResult,
parse_dice,
roll_dice,
)
from interlocutor_commands import dispatcher as default_dispatcher
# ============================================================
# DiceExpression parsing
# ============================================================
class TestParseDice:
"""Tests for the dice notation parser."""
def test_simple_d20(self):
expr = parse_dice("d20")
assert expr == DiceExpression(count=1, sides=20, modifier=0)
def test_multiple_dice(self):
expr = parse_dice("4d6")
assert expr == DiceExpression(count=4, sides=6, modifier=0)
def test_positive_modifier(self):
expr = parse_dice("2d8+5")
assert expr == DiceExpression(count=2, sides=8, modifier=5)
def test_negative_modifier(self):
expr = parse_dice("3d6-2")
assert expr == DiceExpression(count=3, sides=6, modifier=-2)
def test_whitespace_around_modifier(self):
expr = parse_dice("d10 + 7")
assert expr == DiceExpression(count=1, sides=10, modifier=7)
def test_whitespace_negative_modifier(self):
expr = parse_dice("2d6 - 3")
assert expr == DiceExpression(count=2, sides=6, modifier=-3)
def test_percentile_die(self):
expr = parse_dice("d100")
assert expr == DiceExpression(count=1, sides=100, modifier=0)
def test_case_insensitive(self):
expr = parse_dice("D20")
assert expr == DiceExpression(count=1, sides=20, modifier=0)
def test_leading_trailing_whitespace(self):
expr = parse_dice(" 2d6+1 ")
assert expr == DiceExpression(count=2, sides=6, modifier=1)
def test_invalid_notation_returns_none(self):
assert parse_dice("hello") is None
assert parse_dice("d") is None
assert parse_dice("20") is None
assert parse_dice("roll d20") is None # no slash, but also no dice-only
assert parse_dice("") is None
def test_too_many_dice_raises(self):
with pytest.raises(ValueError, match="Dice count"):
parse_dice("200d6")
def test_zero_dice_raises(self):
with pytest.raises(ValueError, match="Dice count"):
parse_dice("0d6")
def test_d1_raises(self):
with pytest.raises(ValueError, match="Die sides"):
parse_dice("d1")
def test_excessive_sides_raises(self):
with pytest.raises(ValueError, match="Die sides"):
parse_dice("d9999")
def test_excessive_modifier_raises(self):
with pytest.raises(ValueError, match="Modifier"):
parse_dice("d20+99999")
class TestDiceExpressionStr:
"""Tests for canonical string reconstruction."""
def test_single_die(self):
assert str(DiceExpression(1, 20, 0)) == "d20"
def test_multiple_dice(self):
assert str(DiceExpression(4, 6, 0)) == "4d6"
def test_positive_modifier(self):
assert str(DiceExpression(2, 8, 5)) == "2d8+5"
def test_negative_modifier(self):
assert str(DiceExpression(1, 10, -3)) == "d10-3"
# ============================================================
# Rolling
# ============================================================
class TestRollDice:
"""Tests for the dice roller."""
def test_roll_produces_correct_count(self):
expr = DiceExpression(count=5, sides=6, modifier=0)
result = roll_dice(expr)
assert len(result.rolls) == 5
def test_all_rolls_within_range(self):
expr = DiceExpression(count=50, sides=20, modifier=0)
result = roll_dice(expr)
assert all(1 <= r <= 20 for r in result.rolls)
def test_modifier_applied_to_total(self):
expr = DiceExpression(count=1, sides=6, modifier=10)
result = roll_dice(expr)
assert result.total == result.rolls[0] + 10
def test_negative_modifier(self):
expr = DiceExpression(count=1, sides=6, modifier=-3)
result = roll_dice(expr)
assert result.total == result.rolls[0] - 3
def test_subtotal_is_sum_of_rolls(self):
expr = DiceExpression(count=4, sides=6, modifier=5)
result = roll_dice(expr)
assert result.subtotal == sum(result.rolls)
assert result.total == result.subtotal + 5
def test_result_to_dict(self):
expr = DiceExpression(count=2, sides=8, modifier=3)
result = roll_dice(expr)
d = result.to_dict()
assert d["count"] == 2
assert d["sides"] == 8
assert d["modifier"] == 3
assert len(d["rolls"]) == 2
assert d["total"] == sum(d["rolls"]) + 3
class TestDiceResultFormat:
"""Tests for human-readable output formatting."""
def test_no_modifier_format(self):
result = DiceResult(
expression=DiceExpression(1, 20, 0),
rolls=(14,),
total=14,
)
summary = result.format_summary()
assert "d20" in summary
assert "[14]" in summary
assert "= 14" in summary
def test_positive_modifier_format(self):
result = DiceResult(
expression=DiceExpression(2, 6, 3),
rolls=(4, 5),
total=12,
)
summary = result.format_summary()
assert "2d6+3" in summary
assert "[4, 5]" in summary
assert "+ 3" in summary
assert "= 12" in summary
def test_negative_modifier_format(self):
result = DiceResult(
expression=DiceExpression(1, 8, -2),
rolls=(6,),
total=4,
)
summary = result.format_summary()
assert "d8-2" in summary
assert "- 2" in summary
assert "= 4" in summary
# ============================================================
# Command dispatch
# ============================================================
class TestDispatcher:
"""Tests for the command routing layer."""
def test_non_command_returns_none(self):
result = default_dispatcher.dispatch("hello everyone")
assert result is None
def test_slash_in_middle_returns_none(self):
result = default_dispatcher.dispatch("the signal/noise ratio is good")
assert result is None
def test_unknown_command_returns_none(self):
result = default_dispatcher.dispatch("/unknown 42")
assert result is None
def test_roll_command_works(self):
result = default_dispatcher.dispatch("/roll d20")
assert result is not None
assert result.command == "roll"
assert not result.is_error
def test_roll_alias_works(self):
result = default_dispatcher.dispatch("/r d20")
assert result is not None
assert result.command == "roll"
def test_case_insensitive_command(self):
result = default_dispatcher.dispatch("/Roll 2d6")
assert result is not None
assert not result.is_error
def test_roll_with_full_notation(self):
result = default_dispatcher.dispatch("/roll 4d6+2")
assert result is not None
assert result.details["count"] == 4
assert result.details["sides"] == 6
assert result.details["modifier"] == 2
def test_roll_no_args_gives_error(self):
result = default_dispatcher.dispatch("/roll")
assert result is not None
assert result.is_error
assert "Usage" in result.error
def test_roll_bad_notation_gives_error(self):
result = default_dispatcher.dispatch("/roll banana")
assert result is not None
assert result.is_error
def test_list_commands(self):
cmds = default_dispatcher.list_commands()
names = [name for name, _ in cmds]
assert "roll" in names
def test_register_collision_raises(self):
d = CommandDispatcher()
d.register(DiceCommand())
with pytest.raises(ValueError, match="collision"):
d.register(DiceCommand()) # same name again
# ============================================================
# Integration: edge cases that cross layers
# ============================================================
class TestIntegration:
"""End-to-end tests through the full dispatch pipeline."""
def test_d100_percentile(self):
result = default_dispatcher.dispatch("/roll d100")
assert 1 <= result.details["total"] <= 100
def test_max_dice(self):
result = default_dispatcher.dispatch("/roll 100d6")
assert len(result.details["rolls"]) == 100
def test_over_max_dice_error(self):
result = default_dispatcher.dispatch("/roll 101d6")
assert result.is_error
def test_whitespace_tolerance(self):
result = default_dispatcher.dispatch("/roll d10 + 5 ")
assert result is not None
assert not result.is_error
assert result.details["modifier"] == 5