Skip to content
Snippets Groups Projects
Commit 759998e4 authored by linopino's avatar linopino
Browse files

Umsetzung der Testszenarien von Feature3

parent f1b89449
No related branches found
No related tags found
2 merge requests!7Develop into main,!3Feature3
...@@ -7,7 +7,18 @@ class StringCalculator(IStringCalculator): ...@@ -7,7 +7,18 @@ class StringCalculator(IStringCalculator):
if not numbers: if not numbers:
return 0 return 0
# Erlaubt Kommas und Zeilenumbrüche als Trennzeichen # Prüfe auf das ungültige Format "1,\n"
tokens = re.split(r"[,\n]", numbers) if ",\n" in numbers:
raise ValueError("Ungültiges Zahlenformat: ',\\n' ist nicht erlaubt")
return sum(map(int, tokens)) # Trenne Zahlen anhand von Komma oder Zeilenumbruch
\ No newline at end of file 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
...@@ -38,8 +38,20 @@ class TestStringCalculator(unittest.TestCase): ...@@ -38,8 +38,20 @@ class TestStringCalculator(unittest.TestCase):
self.assertEqual(self.calculator.add("1\n2,3"), 6) self.assertEqual(self.calculator.add("1\n2,3"), 6)
self.assertEqual(self.calculator.add("10\n20\n30"), 60) 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__": if __name__ == "__main__":
unittest.main() unittest.main()
# python -m unittest tests.test_stringcalculator # python -m unittest tests.test_stringcalculator
# python -m unittest discover -s tests # python -m unittest discover -s tests -v
\ No newline at end of file \ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment