Skip to content

Commit

Permalink
Implement checks for raising messages with exceptions (#1113)
Browse files Browse the repository at this point in the history
* Implement checks for messages being raised with exceptions
(Fixes #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
  • Loading branch information
Nathan Parsons authored and cmccandless committed Dec 12, 2017
1 parent 2f93a62 commit f53e2ef
Show file tree
Hide file tree
Showing 152 changed files with 2,252 additions and 230 deletions.
15 changes: 15 additions & 0 deletions docs/EXERCISE_README_INSERT.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
## Exception messages

Sometimes it is necessary to raise an exception. When you do this, you should 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. Not
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
a message.

To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
`raise Exception`, you shold write:

```python
raise Exception("Meaningful message indicating the source of the error")
```


## Submitting Exercises

Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.
Expand Down
15 changes: 15 additions & 0 deletions exercises/accumulate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ Keep your hands off that collect/map/fmap/whatchamacallit functionality
provided by your standard library!
Solve this one yourself using other basic tools instead.

## Exception messages

Sometimes it is necessary to raise an exception. When you do this, you should 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. Not
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
a message.

To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
`raise Exception`, you shold write:

```python
raise Exception("Meaningful message indicating the source of the error")
```


## Submitting Exercises

Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.
Expand Down
15 changes: 15 additions & 0 deletions exercises/acronym/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ Techies love their TLA (Three Letter Acronyms)!
Help generate some jargon by writing a program that converts a long name
like Portable Network Graphics to its acronym (PNG).

## Exception messages

Sometimes it is necessary to raise an exception. When you do this, you should 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. Not
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
a message.

To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
`raise Exception`, you shold write:

```python
raise Exception("Meaningful message indicating the source of the error")
```


## Submitting Exercises

Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.
Expand Down
15 changes: 15 additions & 0 deletions exercises/all-your-base/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ I think you got the idea!

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

## Exception messages

Sometimes it is necessary to raise an exception. When you do this, you should 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. Not
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
a message.

To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
`raise Exception`, you shold write:

```python
raise Exception("Meaningful message indicating the source of the error")
```


## Submitting Exercises

Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.
Expand Down
28 changes: 19 additions & 9 deletions exercises/all-your-base/all_your_base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,41 +44,51 @@ def test_leading_zeros(self):
self.assertEqual(rebase(7, [0, 6, 0], 10), [4, 2])

def test_first_base_is_one(self):
with self.assertRaises(ValueError):
with self.assertRaisesWithMessage(ValueError):
rebase(1, [], 10)

def test_first_base_is_zero(self):
with self.assertRaises(ValueError):
with self.assertRaisesWithMessage(ValueError):
rebase(0, [], 10)

def test_first_base_is_negative(self):
with self.assertRaises(ValueError):
with self.assertRaisesWithMessage(ValueError):
rebase(-2, [1], 10)

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

def test_invalid_positive_digit(self):
with self.assertRaises(ValueError):
with self.assertRaisesWithMessage(ValueError):
rebase(2, [1, 2, 1, 0, 1, 0], 10)

def test_second_base_is_one(self):
with self.assertRaises(ValueError):
with self.assertRaisesWithMessage(ValueError):
rebase(2, [1, 0, 1, 0, 1, 0], 1)

def test_second_base_is_zero(self):
with self.assertRaises(ValueError):
with self.assertRaisesWithMessage(ValueError):
rebase(10, [7], 0)

def test_second_base_is_negative(self):
with self.assertRaises(ValueError):
with self.assertRaisesWithMessage(ValueError):
rebase(2, [1], -7)

def test_both_bases_are_negative(self):
with self.assertRaises(ValueError):
with self.assertRaisesWithMessage(ValueError):
rebase(-2, [1], -7)

# Utility functions
def setUp(self):
try:
self.assertRaisesRegex = self.assertRaisesRegexp
except AttributeError:
pass

def assertRaisesWithMessage(self, exception):
return self.assertRaisesRegex(exception, r".+")


if __name__ == '__main__':
unittest.main()
15 changes: 15 additions & 0 deletions exercises/allergies/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ allergens that score 256, 512, 1024, etc.). Your program should
ignore those components of the score. For example, if the allergy
score is 257, your program should only report the eggs (1) allergy.

## Exception messages

Sometimes it is necessary to raise an exception. When you do this, you should 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. Not
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
a message.

To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
`raise Exception`, you shold write:

```python
raise Exception("Meaningful message indicating the source of the error")
```


## Submitting Exercises

Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.
Expand Down
15 changes: 15 additions & 0 deletions exercises/alphametics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ a multi-digit number must not be zero.

Write a function to solve alphametics puzzles.

## Exception messages

Sometimes it is necessary to raise an exception. When you do this, you should 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. Not
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
a message.

To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
`raise Exception`, you shold write:

```python
raise Exception("Meaningful message indicating the source of the error")
```


## Submitting Exercises

Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.
Expand Down
15 changes: 15 additions & 0 deletions exercises/anagram/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ Given `"listen"` and a list of candidates like `"enlists" "google"
"inlets" "banana"` the program should return a list containing
`"inlets"`.

## Exception messages

Sometimes it is necessary to raise an exception. When you do this, you should 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. Not
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
a message.

To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
`raise Exception`, you shold write:

```python
raise Exception("Meaningful message indicating the source of the error")
```


## Submitting Exercises

Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.
Expand Down
15 changes: 15 additions & 0 deletions exercises/atbash-cipher/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ things based on word boundaries.
- Decoding `gvhg` gives `test`
- Decoding `gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt` gives `thequickbrownfoxjumpsoverthelazydog`

## Exception messages

Sometimes it is necessary to raise an exception. When you do this, you should 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. Not
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
a message.

To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
`raise Exception`, you shold write:

```python
raise Exception("Meaningful message indicating the source of the error")
```


## Submitting Exercises

Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.
Expand Down
17 changes: 16 additions & 1 deletion exercises/beer-song/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Beer Song

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

Note that not all verses are identical.

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

## Exception messages

Sometimes it is necessary to raise an exception. When you do this, you should 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. Not
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
a message.

To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
`raise Exception`, you shold write:

```python
raise Exception("Meaningful message indicating the source of the error")
```


## Submitting Exercises

Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.
Expand Down
18 changes: 16 additions & 2 deletions exercises/binary-search-tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ And if we then added 1, 5, and 7, it would look like this
/ \ / \
1 3 5 7

## Exception messages

Sometimes it is necessary to raise an exception. When you do this, you should 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. Not
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
a message.

To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
`raise Exception`, you shold write:

```python
raise Exception("Meaningful message indicating the source of the error")
```


## Submitting Exercises

Expand All @@ -65,7 +79,7 @@ please see the [help page](http://exercism.io/languages/python).

## Source

Wikipedia [https://en.wikipedia.org/wiki/Binary_search_tree](https://en.wikipedia.org/wiki/Binary_search_tree)
Josh Cheek [https://twitter.com/josh_cheek](https://twitter.com/josh_cheek)

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
15 changes: 15 additions & 0 deletions exercises/binary-search/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ A binary search halves the number of items to check with each iteration,
so locating an item (or determining its absence) takes logarithmic time.
A binary search is a dichotomic divide and conquer search algorithm.

## Exception messages

Sometimes it is necessary to raise an exception. When you do this, you should 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. Not
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
a message.

To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
`raise Exception`, you shold write:

```python
raise Exception("Meaningful message indicating the source of the error")
```


## Submitting Exercises

Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory.
Expand Down
18 changes: 14 additions & 4 deletions exercises/binary-search/binary_search_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,31 @@ def test_finds_value_in_array_of_even_length(self):
5)

def test_identifies_value_missing(self):
with self.assertRaises(ValueError):
with self.assertRaisesWithMessage(ValueError):
binary_search([1, 3, 4, 6, 8, 9, 11], 7)

def test_value_smaller_than_arrays_minimum(self):
with self.assertRaises(ValueError):
with self.assertRaisesWithMessage(ValueError):
binary_search([1, 3, 4, 6, 8, 9, 11], 0)

def test_value_larger_than_arrays_maximum(self):
with self.assertRaises(ValueError):
with self.assertRaisesWithMessage(ValueError):
binary_search([1, 3, 4, 6, 8, 9, 11], 13)

def test_empty_array(self):
with self.assertRaises(ValueError):
with self.assertRaisesWithMessage(ValueError):
binary_search([], 1)

# Utility functions
def setUp(self):
try:
self.assertRaisesRegex = self.assertRaisesRegexp
except AttributeError:
pass

def assertRaisesWithMessage(self, exception):
return self.assertRaisesRegex(exception, r".+")


if __name__ == '__main__':
unittest.main()
15 changes: 15 additions & 0 deletions exercises/binary/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ Binary is similar, but uses powers of 2 rather than powers of 10.

So: `101 => 1*2^2 + 0*2^1 + 1*2^0 => 1*4 + 0*2 + 1*1 => 4 + 1 => 5 base 10`.

## Exception messages

Sometimes it is necessary to raise an exception. When you do this, you should 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. Not
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
a message.

To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
`raise Exception`, you shold write:

```python
raise Exception("Meaningful message indicating the source of the error")
```


## Submitting Exercises

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

0 comments on commit f53e2ef

Please sign in to comment.