From f1b89449df6e574c31af2bb6dbd10d9ae1f8f5ef Mon Sep 17 00:00:00 2001 From: linopino <lasse.pikkemaat@web.de> Date: Wed, 26 Mar 2025 23:41:34 +0100 Subject: [PATCH 1/2] Erstellung der Testszenarien von Feature3 --- tests/test_stringclaculator.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_stringclaculator.py b/tests/test_stringclaculator.py index dbafdfe..88f079a 100644 --- a/tests/test_stringclaculator.py +++ b/tests/test_stringclaculator.py @@ -9,6 +9,9 @@ #Feature2 Neues Trennzeichen neben dem Komma # Bei Eingabe von 1\n2,3 soll 6 ausgegeben werden # Bei Eingabe von 10\n20\n30 soll 60 ausgegeben werden +#Feature3 Negative Zahlen ausschließen +# Bei Eingabe von 1, -2, 3 soll ein Error erscheinen "Negative nicht erlaubt: [-2] +# Bei Eingabe von -10\n -20, -30 soll ein Error erscheinen "Negative nicht erlaubt: [-10,-20,30] import unittest from src.interfaces import IStringCalculator -- GitLab From 759998e481039e48696fb49167156fcb2cb29b3e Mon Sep 17 00:00:00 2001 From: linopino <lasse.pikkemaat@web.de> Date: Thu, 27 Mar 2025 00:03:23 +0100 Subject: [PATCH 2/2] Umsetzung der Testszenarien von Feature3 --- src/stringcalculator.py | 17 ++++++++++++++--- tests/test_stringclaculator.py | 14 +++++++++++++- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/stringcalculator.py b/src/stringcalculator.py index 748f0de..9c4ba23 100644 --- a/src/stringcalculator.py +++ b/src/stringcalculator.py @@ -7,7 +7,18 @@ class StringCalculator(IStringCalculator): if not numbers: return 0 - # Erlaubt Kommas und Zeilenumbrüche als Trennzeichen - tokens = re.split(r"[,\n]", numbers) + # Prüfe auf das ungültige Format "1,\n" + if ",\n" in numbers: + raise ValueError("Ungültiges Zahlenformat: ',\\n' ist nicht erlaubt") - return sum(map(int, tokens)) \ No newline at end of file + # Trenne Zahlen anhand von Komma oder Zeilenumbruch + tokens = re.split(r",|\n", numbers) + + # Konvertiere zu Integern und finde negative Zahlen + numbers_list = list(map(int, tokens)) + negative_numbers = [num for num in numbers_list if num < 0] + + if negative_numbers: + raise ValueError(f"Negative nicht erlaubt: {negative_numbers}") + + return sum(numbers_list) \ No newline at end of file diff --git a/tests/test_stringclaculator.py b/tests/test_stringclaculator.py index 88f079a..42e9681 100644 --- a/tests/test_stringclaculator.py +++ b/tests/test_stringclaculator.py @@ -38,8 +38,20 @@ class TestStringCalculator(unittest.TestCase): self.assertEqual(self.calculator.add("1\n2,3"), 6) self.assertEqual(self.calculator.add("10\n20\n30"), 60) + def test_add_single_negative_number(self): + with self.assertRaises(ValueError) as context: + self.calculator.add("1,-2,3") + print(str(context.exception)) + self.assertEqual(str(context.exception), "Negative nicht erlaubt: [-2]") + + def test_add_multiple_negative_numbers(self): + with self.assertRaises(ValueError) as context: + self.calculator.add("-10\n-20,-30") + print(str(context.exception)) + self.assertEqual(str(context.exception), "Negative nicht erlaubt: [-10, -20, -30]") + if __name__ == "__main__": unittest.main() # python -m unittest tests.test_stringcalculator -# python -m unittest discover -s tests \ No newline at end of file +# python -m unittest discover -s tests -v \ No newline at end of file -- GitLab