Skip to content

Commit e441599

Browse files
committed
Added instructions append for error handling, updated JinJa2 template and example, and regenerated test files.
1 parent fdb6a0e commit e441599

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
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 the square input is out of range. 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 square value is not in the acceptable range
13+
raise ValueError("square must be between 1 and 64")
14+
```

exercises/practice/grains/.meta/example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
def square(number):
22
if number == 0:
3-
raise ValueError("Square input of zero is invalid.")
3+
raise ValueError("square must be between 1 and 64")
44
elif number < 0:
5-
raise ValueError("Negative square input is invalid.")
5+
raise ValueError("square must be between 1 and 64")
66
elif number > 64:
7-
raise ValueError("Square input greater than 64 is invalid.")
7+
raise ValueError("square must be between 1 and 64")
88

99
return 2 ** (number - 1)
1010

exercises/practice/grains/.meta/template.j2

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
{% macro test_case(case) %}
44
{%- set input = case["input"] %}
5+
{%- set expected = case["expected"] %}
6+
{%- set exp_error = expected["error"] %}
57
def test_{{ case["description"] | to_snake }}(self):
68
{%- if case is error_case %}
7-
with self.assertRaisesWithMessage(ValueError):
8-
{{ case["property"] | to_snake }}({{ input["square"] }})
9+
with self.assertRaises(ValueError) as err:
10+
{{ case["property"] | to_snake }}({{ input["square"] }})
11+
self.assertEqual(type(err.exception), ValueError)
12+
self.assertEqual(err.exception.args[0], "{{ exp_error }}")
913
{%- else%}
1014
self.assertEqual({{ case["property"] | to_snake }}(
1115
{{ input["square"] }}), {{ case["expected"] }})
@@ -23,6 +27,4 @@ class {{ exercise | camel_case }}Test(unittest.TestCase):
2327
{%-else%}
2428
{{test_case(case)}}
2529
{%-endif%}
26-
{% endfor %}
27-
28-
{{ macros.footer(has_error_case) }}
30+
{% endfor %}

exercises/practice/grains/grains_test.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,22 @@ def test_grains_on_square_64(self):
3131
self.assertEqual(square(64), 9223372036854775808)
3232

3333
def test_square_0_raises_an_exception(self):
34-
with self.assertRaisesWithMessage(ValueError):
34+
with self.assertRaises(ValueError) as err:
3535
square(0)
36+
self.assertEqual(type(err.exception), ValueError)
37+
self.assertEqual(err.exception.args[0], "square must be between 1 and 64")
3638

3739
def test_negative_square_raises_an_exception(self):
38-
with self.assertRaisesWithMessage(ValueError):
40+
with self.assertRaises(ValueError) as err:
3941
square(-1)
42+
self.assertEqual(type(err.exception), ValueError)
43+
self.assertEqual(err.exception.args[0], "square must be between 1 and 64")
4044

4145
def test_square_greater_than_64_raises_an_exception(self):
42-
with self.assertRaisesWithMessage(ValueError):
46+
with self.assertRaises(ValueError) as err:
4347
square(65)
48+
self.assertEqual(type(err.exception), ValueError)
49+
self.assertEqual(err.exception.args[0], "square must be between 1 and 64")
4450

4551
def test_returns_the_total_number_of_grains_on_the_board(self):
4652
self.assertEqual(total(), 18446744073709551615)
47-
48-
# Utility functions
49-
def assertRaisesWithMessage(self, exception):
50-
return self.assertRaisesRegex(exception, r".+")
51-
52-
53-
if __name__ == "__main__":
54-
unittest.main()

0 commit comments

Comments
 (0)