Skip to content

Commit bf83d76

Browse files
committed
Added error handling instruction append, added additional test cases, corrected JinJa2 template, and regenerated test cases.
1 parent 96699bc commit bf83d76

File tree

5 files changed

+32
-21
lines changed

5 files changed

+32
-21
lines changed
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
# Implementation Notes
2-
The board function must validate its input and raise a
3-
ValueError with a *meaningful* error message if the
4-
input turns out to be malformed.
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 the `board()` function receives malformed input. 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 board receives malformed input
13+
raise ValueError("The board is invalid with current input.")
14+
```

exercises/practice/minesweeper/.meta/additional_tests.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@
3838
" "
3939
]
4040
},
41-
"expected": {"error": "The minefield must have a consistent length"}
41+
"expected": {"error": "The board is invalid with current input."}
4242
},
4343
{
4444
"description": "invalid char",
4545
"property": "annotate",
4646
"input": {
4747
"minefield": ["X * "]
4848
},
49-
"expected": {"error": "The minefield contains an invalid character"}
49+
"expected": {"error": "The board is invalid with current input."}
5050

5151
}
5252
]

exercises/practice/minesweeper/.meta/example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
def annotate(minefield):
2-
if(minefield == []):
2+
if not minefield:
33
return []
44
verify_board(minefield)
55
row_len = len(minefield[0])
@@ -30,11 +30,11 @@ def verify_board(minefield):
3030
# Rows with different lengths
3131
row_len = len(minefield[0])
3232
if not all(len(row) == row_len for row in minefield):
33-
raise ValueError("Invalid board")
33+
raise ValueError("The board is invalid with current input.")
3434

3535
# Unknown character in board
3636
character_set = set()
3737
for row in minefield:
3838
character_set.update(row)
3939
if character_set - set(' *'):
40-
raise ValueError("Invalid board")
40+
raise ValueError("The board is invalid with current input.")

exercises/practice/minesweeper/.meta/template.j2

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ class {{ exercise | camel_case }}Test(unittest.TestCase):
1414
{% for case in additional_cases -%}
1515
def test_{{ case["description"] | to_snake }}(self):
1616
{%- if case is error_case %}
17-
with self.assertRaisesWithMessage(ValueError):
17+
with self.assertRaises(ValueError) as err:
1818
{{ test_call(case) }}
19+
self.assertEqual(type(err.exception), ValueError)
20+
self.assertEqual(err.exception.args[0], "{{ case["expected"]["error"] }}")
1921
{%- else %}
2022
self.assertEqual({{- test_call(case) }}, {{ case["expected"] }})
2123
{%- endif %}
2224
{% endfor %}
23-
{{ macros.footer(True) }}

exercises/practice/minesweeper/minesweeper_test.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,17 @@ def test_annotate_9(self):
5858
)
5959

6060
def test_different_len(self):
61-
with self.assertRaisesWithMessage(ValueError):
61+
with self.assertRaises(ValueError) as err:
6262
annotate([" ", "* ", " "])
63+
self.assertEqual(type(err.exception), ValueError)
64+
self.assertEqual(
65+
err.exception.args[0], "The board is invalid with current input."
66+
)
6367

6468
def test_invalid_char(self):
65-
with self.assertRaisesWithMessage(ValueError):
69+
with self.assertRaises(ValueError) as err:
6670
annotate(["X * "])
67-
68-
# Utility functions
69-
def assertRaisesWithMessage(self, exception):
70-
return self.assertRaisesRegex(exception, r".+")
71-
72-
73-
if __name__ == "__main__":
74-
unittest.main()
71+
self.assertEqual(type(err.exception), ValueError)
72+
self.assertEqual(
73+
err.exception.args[0], "The board is invalid with current input."
74+
)

0 commit comments

Comments
 (0)