Added error handling instruction append, added additional test cases, corrected JinJa2 template, and regenerated test cases.

This commit is contained in:
BethanyG
2021-11-01 11:39:06 -07:00
committed by BethanyG
parent 96699bc539
commit bf83d7689a
5 changed files with 32 additions and 21 deletions

View File

@@ -1,4 +1,14 @@
# Implementation Notes # Instructions append
The board function must validate its input and raise a
ValueError with a *meaningful* error message if the ## Exception messages
input turns out to be malformed.
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.
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.
To raise a `ValueError` with a message, write the message as an argument to the `exception` type:
```python
# when the board receives malformed input
raise ValueError("The board is invalid with current input.")
```

View File

@@ -38,7 +38,7 @@
" " " "
] ]
}, },
"expected": {"error": "The minefield must have a consistent length"} "expected": {"error": "The board is invalid with current input."}
}, },
{ {
"description": "invalid char", "description": "invalid char",
@@ -46,7 +46,7 @@
"input": { "input": {
"minefield": ["X * "] "minefield": ["X * "]
}, },
"expected": {"error": "The minefield contains an invalid character"} "expected": {"error": "The board is invalid with current input."}
} }
] ]

View File

@@ -1,5 +1,5 @@
def annotate(minefield): def annotate(minefield):
if(minefield == []): if not minefield:
return [] return []
verify_board(minefield) verify_board(minefield)
row_len = len(minefield[0]) row_len = len(minefield[0])
@@ -30,11 +30,11 @@ def verify_board(minefield):
# Rows with different lengths # Rows with different lengths
row_len = len(minefield[0]) row_len = len(minefield[0])
if not all(len(row) == row_len for row in minefield): if not all(len(row) == row_len for row in minefield):
raise ValueError("Invalid board") raise ValueError("The board is invalid with current input.")
# Unknown character in board # Unknown character in board
character_set = set() character_set = set()
for row in minefield: for row in minefield:
character_set.update(row) character_set.update(row)
if character_set - set(' *'): if character_set - set(' *'):
raise ValueError("Invalid board") raise ValueError("The board is invalid with current input.")

View File

@@ -14,10 +14,11 @@ class {{ exercise | camel_case }}Test(unittest.TestCase):
{% for case in additional_cases -%} {% for case in additional_cases -%}
def test_{{ case["description"] | to_snake }}(self): def test_{{ case["description"] | to_snake }}(self):
{%- if case is error_case %} {%- if case is error_case %}
with self.assertRaisesWithMessage(ValueError): with self.assertRaises(ValueError) as err:
{{ test_call(case) }} {{ test_call(case) }}
self.assertEqual(type(err.exception), ValueError)
self.assertEqual(err.exception.args[0], "{{ case["expected"]["error"] }}")
{%- else %} {%- else %}
self.assertEqual({{- test_call(case) }}, {{ case["expected"] }}) self.assertEqual({{- test_call(case) }}, {{ case["expected"] }})
{%- endif %} {%- endif %}
{% endfor %} {% endfor %}
{{ macros.footer(True) }}

View File

@@ -58,17 +58,17 @@ class MinesweeperTest(unittest.TestCase):
) )
def test_different_len(self): def test_different_len(self):
with self.assertRaisesWithMessage(ValueError): with self.assertRaises(ValueError) as err:
annotate([" ", "* ", " "]) annotate([" ", "* ", " "])
self.assertEqual(type(err.exception), ValueError)
self.assertEqual(
err.exception.args[0], "The board is invalid with current input."
)
def test_invalid_char(self): def test_invalid_char(self):
with self.assertRaisesWithMessage(ValueError): with self.assertRaises(ValueError) as err:
annotate(["X * "]) annotate(["X * "])
self.assertEqual(type(err.exception), ValueError)
# Utility functions self.assertEqual(
def assertRaisesWithMessage(self, exception): err.exception.args[0], "The board is invalid with current input."
return self.assertRaisesRegex(exception, r".+") )
if __name__ == "__main__":
unittest.main()