Skip to content

Commit f53e2ef

Browse files
Nathan Parsonscmccandless
authored andcommitted
Implement checks for raising messages with exceptions (exercism#1113)
* Implement checks for messages being raised with exceptions (Fixes exercism#1080) * Add self.assertRaisesWithMessage method to relevant exercise tests - Uses self.assertRaisesRegex - Checks only for the presence of a message, not content * Add meaningful messages to failing examples * octal: Switch to using a context manager for exception tests * Add note regarding error messages to the insert * simple-linked-list: Move hints.md to correct location * simple-cipher: Remove extra whitespace from lines * collatz-conjecture: Update hints.md * Regenerate README to include exceptions section
1 parent 2f93a62 commit f53e2ef

152 files changed

Lines changed: 2252 additions & 230 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/EXERCISE_README_INSERT.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
## Exception messages
2+
3+
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
4+
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
5+
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
6+
a message.
7+
8+
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
9+
`raise Exception`, you shold write:
10+
11+
```python
12+
raise Exception("Meaningful message indicating the source of the error")
13+
```
14+
15+
116
## Submitting Exercises
217

318
Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.

exercises/accumulate/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ Keep your hands off that collect/map/fmap/whatchamacallit functionality
2525
provided by your standard library!
2626
Solve this one yourself using other basic tools instead.
2727

28+
## Exception messages
29+
30+
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
31+
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
32+
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
33+
a message.
34+
35+
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
36+
`raise Exception`, you shold write:
37+
38+
```python
39+
raise Exception("Meaningful message indicating the source of the error")
40+
```
41+
42+
2843
## Submitting Exercises
2944

3045
Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.

exercises/acronym/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@ Techies love their TLA (Three Letter Acronyms)!
77
Help generate some jargon by writing a program that converts a long name
88
like Portable Network Graphics to its acronym (PNG).
99

10+
## Exception messages
11+
12+
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
13+
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
14+
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
15+
a message.
16+
17+
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
18+
`raise Exception`, you shold write:
19+
20+
```python
21+
raise Exception("Meaningful message indicating the source of the error")
22+
```
23+
24+
1025
## Submitting Exercises
1126

1227
Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.

exercises/all-your-base/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ I think you got the idea!
3131

3232
*Yes. Those three numbers above are exactly the same. Congratulations!*
3333

34+
## Exception messages
35+
36+
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
37+
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
38+
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
39+
a message.
40+
41+
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
42+
`raise Exception`, you shold write:
43+
44+
```python
45+
raise Exception("Meaningful message indicating the source of the error")
46+
```
47+
48+
3449
## Submitting Exercises
3550

3651
Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.

exercises/all-your-base/all_your_base_test.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,41 +44,51 @@ def test_leading_zeros(self):
4444
self.assertEqual(rebase(7, [0, 6, 0], 10), [4, 2])
4545

4646
def test_first_base_is_one(self):
47-
with self.assertRaises(ValueError):
47+
with self.assertRaisesWithMessage(ValueError):
4848
rebase(1, [], 10)
4949

5050
def test_first_base_is_zero(self):
51-
with self.assertRaises(ValueError):
51+
with self.assertRaisesWithMessage(ValueError):
5252
rebase(0, [], 10)
5353

5454
def test_first_base_is_negative(self):
55-
with self.assertRaises(ValueError):
55+
with self.assertRaisesWithMessage(ValueError):
5656
rebase(-2, [1], 10)
5757

5858
def test_negative_digit(self):
59-
with self.assertRaises(ValueError):
59+
with self.assertRaisesWithMessage(ValueError):
6060
rebase(2, [1, -1, 1, 0, 1, 0], 10)
6161

6262
def test_invalid_positive_digit(self):
63-
with self.assertRaises(ValueError):
63+
with self.assertRaisesWithMessage(ValueError):
6464
rebase(2, [1, 2, 1, 0, 1, 0], 10)
6565

6666
def test_second_base_is_one(self):
67-
with self.assertRaises(ValueError):
67+
with self.assertRaisesWithMessage(ValueError):
6868
rebase(2, [1, 0, 1, 0, 1, 0], 1)
6969

7070
def test_second_base_is_zero(self):
71-
with self.assertRaises(ValueError):
71+
with self.assertRaisesWithMessage(ValueError):
7272
rebase(10, [7], 0)
7373

7474
def test_second_base_is_negative(self):
75-
with self.assertRaises(ValueError):
75+
with self.assertRaisesWithMessage(ValueError):
7676
rebase(2, [1], -7)
7777

7878
def test_both_bases_are_negative(self):
79-
with self.assertRaises(ValueError):
79+
with self.assertRaisesWithMessage(ValueError):
8080
rebase(-2, [1], -7)
8181

82+
# Utility functions
83+
def setUp(self):
84+
try:
85+
self.assertRaisesRegex = self.assertRaisesRegexp
86+
except AttributeError:
87+
pass
88+
89+
def assertRaisesWithMessage(self, exception):
90+
return self.assertRaisesRegex(exception, r".+")
91+
8292

8393
if __name__ == '__main__':
8494
unittest.main()

exercises/allergies/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ allergens that score 256, 512, 1024, etc.). Your program should
2929
ignore those components of the score. For example, if the allergy
3030
score is 257, your program should only report the eggs (1) allergy.
3131

32+
## Exception messages
33+
34+
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
35+
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
36+
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
37+
a message.
38+
39+
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
40+
`raise Exception`, you shold write:
41+
42+
```python
43+
raise Exception("Meaningful message indicating the source of the error")
44+
```
45+
46+
3247
## Submitting Exercises
3348

3449
Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.

exercises/alphametics/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ a multi-digit number must not be zero.
3131

3232
Write a function to solve alphametics puzzles.
3333

34+
## Exception messages
35+
36+
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
37+
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
38+
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
39+
a message.
40+
41+
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
42+
`raise Exception`, you shold write:
43+
44+
```python
45+
raise Exception("Meaningful message indicating the source of the error")
46+
```
47+
48+
3449
## Submitting Exercises
3550

3651
Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.

exercises/anagram/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ Given `"listen"` and a list of candidates like `"enlists" "google"
66
"inlets" "banana"` the program should return a list containing
77
`"inlets"`.
88

9+
## Exception messages
10+
11+
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
12+
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
13+
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
14+
a message.
15+
16+
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
17+
`raise Exception`, you shold write:
18+
19+
```python
20+
raise Exception("Meaningful message indicating the source of the error")
21+
```
22+
23+
924
## Submitting Exercises
1025

1126
Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.

exercises/atbash-cipher/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ things based on word boundaries.
2828
- Decoding `gvhg` gives `test`
2929
- Decoding `gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt` gives `thequickbrownfoxjumpsoverthelazydog`
3030

31+
## Exception messages
32+
33+
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
34+
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
35+
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
36+
a message.
37+
38+
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
39+
`raise Exception`, you shold write:
40+
41+
```python
42+
raise Exception("Meaningful message indicating the source of the error")
43+
```
44+
45+
3146
## Submitting Exercises
3247

3348
Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.

exercises/beer-song/README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Beer Song
22

3-
Produce the lyrics to that beloved classic, that field-trip favorite: 99 Bottles of Beer on the Wall.
3+
Recite the lyrics to that beloved classic, that field-trip favorite: 99 Bottles of Beer on the Wall.
44

55
Note that not all verses are identical.
66

@@ -320,6 +320,21 @@ are some additional things you could try:
320320
Then please share your thoughts in a comment on the submission. Did this
321321
experiment make the code better? Worse? Did you learn anything from it?
322322

323+
## Exception messages
324+
325+
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
326+
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
327+
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
328+
a message.
329+
330+
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
331+
`raise Exception`, you shold write:
332+
333+
```python
334+
raise Exception("Meaningful message indicating the source of the error")
335+
```
336+
337+
323338
## Submitting Exercises
324339

325340
Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.

0 commit comments

Comments
 (0)