Skip to content

Commit

Permalink
move some debugging exercises to document&test examples
Browse files Browse the repository at this point in the history
  • Loading branch information
colevandersWands committed Dec 8, 2024
1 parent 106cb00 commit 36d96ba
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 32 deletions.
1 change: 1 addition & 0 deletions 3_documenting_and_testing/code_review_checklist.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ the details become a habit.
- [ ] return value description
- [ ] include any assumptions
- [ ] include 3 or more (passing!) doctests
- [ ] assertions are documented (if there are any)
- [ ] include 1-2 use cases (if necessary)

## Function Implementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# -*- coding: utf-8 -*-
"""
A module for list manipulation focusing on alternating elements.
This is part of the debugging exercise series focusing on buggy tests.
Module contents:
- alternate_elements: Creates a new list with every other element
Expand Down Expand Up @@ -34,4 +33,5 @@ def alternate_elements(items: list) -> list:
[]
"""
assert isinstance(items, list), "input must be a list"

return items[::2]
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
# -*- coding: utf-8 -*-
"""
A module for counting vowels in a string.
This is part of the debugging exercise series focusing on buggy tests.
Module contents:
- count_vowels: Counts how many vowels are in a string
- count_vowels: counts the number of vowels in a string.
Created on 2024-12-06
Author: Claude AI
Created on XX XX XX
@author: Evan Cole + Claude AI
"""

def count_vowels(text: str) -> int:
Expand All @@ -18,6 +17,9 @@ def count_vowels(text: str) -> int:
text: str, the input string to check
Returns -> int: number of vowels in the text
Raises:
AssertionError: if the argument is not a string
>>> count_vowels("hello")
2
Expand Down
10 changes: 9 additions & 1 deletion 3_documenting_and_testing/examples/fibonacci_list.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on XX XX XX
A module for generating lists of Fibonacci numbers
Module contents:
- fibonacci_list: generates a list of n fibonacci numbers.
Created on XX XX XX
@author: Evan Cole
"""

Expand Down Expand Up @@ -34,6 +38,10 @@ def fibonacci_list(sequence_length: int) -> list[int]:
Returns -> list[int] with the first n numbers of the Fibonacci sequence
Raises:
AssertionError: if the argument is not an integer
AssertionError: if the argument is less than 0
>>> fibonacci_list(0)
[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"""

import unittest

from ..alternate_elements import alternate_elements

class TestAlternateElements(unittest.TestCase):
Expand All @@ -23,15 +22,15 @@ class TestAlternateElements(unittest.TestCase):
# Standard test cases
def test_five_numbers(self):
"""It should return every other number from a list of five"""
self.assertEqual(alternate_elements([1, 2, 3, 4, 5]), [2, 4])
self.assertEqual(alternate_elements([1, 2, 3, 4, 5]), [1, 3, 5])

def test_four_numbers(self):
"""It should return every other number from a list of four"""
self.assertEqual(alternate_elements([1, 2, 3, 4]), [1, 3])

def test_strings(self):
"""It should work with strings"""
self.assertEqual(alternate_elements(['a', 'b', 'c', 'd']), 'ac')
self.assertEqual(alternate_elements(['a', 'b', 'c', 'd']), ['a', 'c'])

# Edge cases
def test_empty_list(self):
Expand All @@ -40,7 +39,7 @@ def test_empty_list(self):

def test_single_element(self):
"""It should return single element list for single element input"""
self.assertEqual(alternate_elements([1]), [])
self.assertEqual(alternate_elements([1]), [1])

def test_two_elements(self):
"""It should return first element for two element input"""
Expand All @@ -51,10 +50,6 @@ def test_none_input(self):
"""It should raise AssertionError for None input"""
with self.assertRaises(AssertionError):
alternate_elements(None)

def test_string_input(self):
"""It should raise AssertionError for string input"""
self.assertEqual(alternate_elements("hello"), ["h", "l", "o"])

if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Test module for count_vowels function.
Contains intentionally buggy tests for debugging practice.
Test categories:
- Standard cases: typical number lists and ranges
- Edge cases: empty lists, equal bounds, non-integer numbers
- Defensive tests: invalid inputs, assertions
Created on 2024-12-06
Author: Claude AI
"""

import unittest

from ..count_vowels import count_vowels

class TestCountVowels(unittest.TestCase):
"""Test the count_vowels function - some tests are buggy!"""

# TODO: as an exercise, write more tests!

def test_empty_string(self):
"""It should return 0 for an empty string"""
self.assertEqual(count_vowels(""), 0)

def test_no_vowels(self):
"""It should return 0 for strings without vowels"""
self.assertEqual(count_vowels("cry"), 1)
self.assertEqual(count_vowels("cry"), 0)

def test_all_vowels(self):
"""It should count all vowels in a string"""
self.assertEqual(count_vowels("AUDIO"), 0)
self.assertEqual(count_vowels("AUIO"), 4)

def test_mixed_case(self):
"""It should handle mixed case strings"""
Expand Down
File renamed without changes
2 changes: 1 addition & 1 deletion 4_debugging/lesson_plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class: middle, center

### Fixing Bugs: _Rubber Ducky_

[![rubber ducky](./.assets/rubber-ducky.png)](https://rubberduckdebugging.com)
[![rubber ducky](./.assets/rubber_ducky.png)](https://rubberduckdebugging.com)

---

Expand Down

0 comments on commit 36d96ba

Please sign in to comment.