Skip to content

Commit fdb6a0e

Browse files
committed
Added instruction append on error handling and edited JinJa2 template and example.py. Regenerated test file.
1 parent 987f5fd commit fdb6a0e

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Instructions append
2+
3+
## Exception messages
4+
5+
Sometimes it is necessary to [raise an exception](https://docs.python.org/3/tutorial/errors.html#raising-exceptions). When you do this, you should always include a **meaningful error message** to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. For situations where you know that the error source will be a certain type, you can choose to raise one of the [built in error types](https://docs.python.org/3/library/exceptions.html#base-classes), but should still include a meaningful message.
6+
7+
This particular exercise requires that you use the [raise statement](https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement) to "throw" a `ValueError` when given invalid coordinates. The tests will only pass if you both `raise` the `exception` and include a message with it.
8+
9+
To raise a `ValueError` with a message, write the message as an argument to the `exception` type:
10+
11+
```python
12+
# when the coordinates for the piece are invalid
13+
raise ValueError('Invalid coordinate')
14+
```

exercises/practice/go-counting/.meta/example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def walk(self, x, y,
3737

3838
def territory(self, x, y):
3939
if not self.valid(x, y):
40-
raise ValueError('invalid coordinate')
40+
raise ValueError('Invalid coordinate')
4141
if self.board[y][x] in STONES:
4242
return (NONE, set())
4343

exercises/practice/go-counting/.meta/template.j2

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515

1616

1717
{% macro test_case(case) -%}
18+
{%- set expected = case["expected"] %}
19+
{%- set exp_error = expected["error"] %}
1820
def test_{{ case["description"] | to_snake }}(self):
1921
board = Board({{ case["input"]["board"] }})
2022
{%- if case is error_case %}
21-
with self.assertRaisesWithMessage(ValueError):
23+
with self.assertRaises(ValueError) as err:
2224
board.territory(x={{ case["input"]["x"] }}, y={{ case["input"]["y"] }})
25+
self.assertEqual(type(err.exception), ValueError)
26+
self.assertEqual(err.exception.args[0], "{{ exp_error }}")
2327
{%- else %}
2428
{%- if "owner" in case["expected"] %}
2529
stone, territory = board.territory(x={{ case["input"]["x"] }}, y={{ case["input"]["y"] }})

exercises/practice/go-counting/go_counting_test.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,31 @@ def test_a_stone_and_not_a_territory_on_5x5_board(self):
3737

3838
def test_invalid_because_x_is_too_low_for_5x5_board(self):
3939
board = Board([" B ", " B B ", "B W B", " W W ", " W "])
40-
with self.assertRaisesWithMessage(ValueError):
40+
with self.assertRaises(ValueError) as err:
4141
board.territory(x=-1, y=1)
42+
self.assertEqual(type(err.exception), ValueError)
43+
self.assertEqual(err.exception.args[0], "Invalid coordinate")
4244

4345
def test_invalid_because_x_is_too_high_for_5x5_board(self):
4446
board = Board([" B ", " B B ", "B W B", " W W ", " W "])
45-
with self.assertRaisesWithMessage(ValueError):
47+
with self.assertRaises(ValueError) as err:
4648
board.territory(x=5, y=1)
49+
self.assertEqual(type(err.exception), ValueError)
50+
self.assertEqual(err.exception.args[0], "Invalid coordinate")
4751

4852
def test_invalid_because_y_is_too_low_for_5x5_board(self):
4953
board = Board([" B ", " B B ", "B W B", " W W ", " W "])
50-
with self.assertRaisesWithMessage(ValueError):
54+
with self.assertRaises(ValueError) as err:
5155
board.territory(x=1, y=-1)
56+
self.assertEqual(type(err.exception), ValueError)
57+
self.assertEqual(err.exception.args[0], "Invalid coordinate")
5258

5359
def test_invalid_because_y_is_too_high_for_5x5_board(self):
5460
board = Board([" B ", " B B ", "B W B", " W W ", " W "])
55-
with self.assertRaisesWithMessage(ValueError):
61+
with self.assertRaises(ValueError) as err:
5662
board.territory(x=1, y=5)
63+
self.assertEqual(type(err.exception), ValueError)
64+
self.assertEqual(err.exception.args[0], "Invalid coordinate")
5765

5866
def test_one_territory_is_the_whole_board(self):
5967
board = Board([" "])
@@ -75,11 +83,3 @@ def test_two_region_rectangular_board(self):
7583
self.assertSetEqual(territories[BLACK], {(0, 0), (2, 0)})
7684
self.assertSetEqual(territories[WHITE], set())
7785
self.assertSetEqual(territories[NONE], set())
78-
79-
# Utility functions
80-
def assertRaisesWithMessage(self, exception):
81-
return self.assertRaisesRegex(exception, r".+")
82-
83-
84-
if __name__ == "__main__":
85-
unittest.main()

0 commit comments

Comments
 (0)