From 36d96bac5f122fb14e0d03bb0e4e045550a6dccd Mon Sep 17 00:00:00 2001 From: colevandersWands <18554853+colevandersWands@users.noreply.github.com> Date: Sat, 7 Dec 2024 21:03:03 -0500 Subject: [PATCH] move some debugging exercises to document&test examples --- .../code_review_checklist.md | 1 + .../examples}/alternate_elements.py | 2 +- .../examples}/count_vowels.py | 10 +++++---- .../examples/fibonacci_list.py | 10 ++++++++- .../tests/test_alternate_elements.py | 11 +++------ .../examples}/tests/test_count_vowels.py | 21 ++++-------------- .../{rubber-ducky.png => rubber_ducky.png} | Bin 4_debugging/lesson_plan.md | 2 +- 8 files changed, 25 insertions(+), 32 deletions(-) rename {4_debugging/exercises/1_buggy_tests => 3_documenting_and_testing/examples}/alternate_elements.py (93%) rename {4_debugging/exercises/1_buggy_tests => 3_documenting_and_testing/examples}/count_vowels.py (78%) rename {4_debugging/exercises/1_buggy_tests => 3_documenting_and_testing/examples}/tests/test_alternate_elements.py (84%) rename {4_debugging/exercises/1_buggy_tests => 3_documenting_and_testing/examples}/tests/test_count_vowels.py (63%) rename 4_debugging/.assets/{rubber-ducky.png => rubber_ducky.png} (100%) diff --git a/3_documenting_and_testing/code_review_checklist.md b/3_documenting_and_testing/code_review_checklist.md index 32423fc..0d33a9b 100644 --- a/3_documenting_and_testing/code_review_checklist.md +++ b/3_documenting_and_testing/code_review_checklist.md @@ -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 diff --git a/4_debugging/exercises/1_buggy_tests/alternate_elements.py b/3_documenting_and_testing/examples/alternate_elements.py similarity index 93% rename from 4_debugging/exercises/1_buggy_tests/alternate_elements.py rename to 3_documenting_and_testing/examples/alternate_elements.py index 4643527..cfcee1e 100644 --- a/4_debugging/exercises/1_buggy_tests/alternate_elements.py +++ b/3_documenting_and_testing/examples/alternate_elements.py @@ -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 @@ -34,4 +33,5 @@ def alternate_elements(items: list) -> list: [] """ assert isinstance(items, list), "input must be a list" + return items[::2] diff --git a/4_debugging/exercises/1_buggy_tests/count_vowels.py b/3_documenting_and_testing/examples/count_vowels.py similarity index 78% rename from 4_debugging/exercises/1_buggy_tests/count_vowels.py rename to 3_documenting_and_testing/examples/count_vowels.py index 99db9be..b416b68 100644 --- a/4_debugging/exercises/1_buggy_tests/count_vowels.py +++ b/3_documenting_and_testing/examples/count_vowels.py @@ -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: @@ -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 diff --git a/3_documenting_and_testing/examples/fibonacci_list.py b/3_documenting_and_testing/examples/fibonacci_list.py index f655743..cbc1aa3 100644 --- a/3_documenting_and_testing/examples/fibonacci_list.py +++ b/3_documenting_and_testing/examples/fibonacci_list.py @@ -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 """ @@ -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) [] diff --git a/4_debugging/exercises/1_buggy_tests/tests/test_alternate_elements.py b/3_documenting_and_testing/examples/tests/test_alternate_elements.py similarity index 84% rename from 4_debugging/exercises/1_buggy_tests/tests/test_alternate_elements.py rename to 3_documenting_and_testing/examples/tests/test_alternate_elements.py index b66444f..95b1c43 100644 --- a/4_debugging/exercises/1_buggy_tests/tests/test_alternate_elements.py +++ b/3_documenting_and_testing/examples/tests/test_alternate_elements.py @@ -14,7 +14,6 @@ """ import unittest - from ..alternate_elements import alternate_elements class TestAlternateElements(unittest.TestCase): @@ -23,7 +22,7 @@ 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""" @@ -31,7 +30,7 @@ def test_four_numbers(self): 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): @@ -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""" @@ -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() diff --git a/4_debugging/exercises/1_buggy_tests/tests/test_count_vowels.py b/3_documenting_and_testing/examples/tests/test_count_vowels.py similarity index 63% rename from 4_debugging/exercises/1_buggy_tests/tests/test_count_vowels.py rename to 3_documenting_and_testing/examples/tests/test_count_vowels.py index 8a93f29..738140c 100644 --- a/4_debugging/exercises/1_buggy_tests/tests/test_count_vowels.py +++ b/3_documenting_and_testing/examples/tests/test_count_vowels.py @@ -1,24 +1,11 @@ -#!/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""" @@ -26,11 +13,11 @@ def test_empty_string(self): 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""" diff --git a/4_debugging/.assets/rubber-ducky.png b/4_debugging/.assets/rubber_ducky.png similarity index 100% rename from 4_debugging/.assets/rubber-ducky.png rename to 4_debugging/.assets/rubber_ducky.png diff --git a/4_debugging/lesson_plan.md b/4_debugging/lesson_plan.md index cb5ab03..8f50406 100644 --- a/4_debugging/lesson_plan.md +++ b/4_debugging/lesson_plan.md @@ -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) ---