From aab83e5d1f5739ffcc56136cbdb1cea9778c8e0f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 23 Mar 2026 13:43:43 +0000 Subject: [PATCH] test: add unit tests for add function in calcfinalq2.py Refactored calcfinalq2.py to wrap the main execution loop in an `if __name__ == '__main__':` block to prevent blocking infinite loops when importing the file for tests. Created test_calcfinalq2.py to test the `add` function with positive, negative, mixed, and zero numeric values. Also added a .gitignore to exclude __pycache__ binary files from tracking. Co-authored-by: ManupaKDU <95234271+ManupaKDU@users.noreply.github.com> --- .gitignore | 7 ++++ Learning/Part-1/Python/calcfinalq2.py | 40 ++++++++++++---------- Learning/Part-1/Python/test_calcfinalq2.py | 24 +++++++++++++ 3 files changed, 52 insertions(+), 19 deletions(-) create mode 100644 .gitignore create mode 100644 Learning/Part-1/Python/test_calcfinalq2.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e96d33d --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +__pycache__/ +*.py[cod] +.pytest_cache/ +.coverage +htmlcov/ +*.exe +caeser_test diff --git a/Learning/Part-1/Python/calcfinalq2.py b/Learning/Part-1/Python/calcfinalq2.py index 6358120..5ee5505 100644 --- a/Learning/Part-1/Python/calcfinalq2.py +++ b/Learning/Part-1/Python/calcfinalq2.py @@ -91,22 +91,24 @@ def select_op(choice): print("Unrecognized operation") c1 = 0 hist = [] -while True: - print("Select operation.") - print("1.Add : + ") - print("2.Subtract : - ") - print("3.Multiply : * ") - print("4.Divide : / ") - print("5.Power : ^ ") - print("6.Remainder: % ") - print("7.Terminate: # ") - print("8.Reset : $ ") - print("8.History : ? ") - # take input from the user - choice = input("Enter choice(+,-,*,/,^,%,#,$,?): ") - print(choice) - c1 +=1 - if(select_op(choice) == -1): - #program ends here - print("Done. Terminating") - exit() \ No newline at end of file + +if __name__ == "__main__": + while True: + print("Select operation.") + print("1.Add : + ") + print("2.Subtract : - ") + print("3.Multiply : * ") + print("4.Divide : / ") + print("5.Power : ^ ") + print("6.Remainder: % ") + print("7.Terminate: # ") + print("8.Reset : $ ") + print("8.History : ? ") + # take input from the user + choice = input("Enter choice(+,-,*,/,^,%,#,$,?): ") + print(choice) + c1 +=1 + if(select_op(choice) == -1): + #program ends here + print("Done. Terminating") + exit() \ No newline at end of file diff --git a/Learning/Part-1/Python/test_calcfinalq2.py b/Learning/Part-1/Python/test_calcfinalq2.py new file mode 100644 index 0000000..68e5c47 --- /dev/null +++ b/Learning/Part-1/Python/test_calcfinalq2.py @@ -0,0 +1,24 @@ +import unittest +from calcfinalq2 import add + +class TestCalcAdd(unittest.TestCase): + def test_add_positive_numbers(self): + self.assertEqual(add(5, 3), 8) + self.assertEqual(add(10.5, 2.5), 13.0) + + def test_add_negative_numbers(self): + self.assertEqual(add(-5, -3), -8) + self.assertEqual(add(-10.5, -2.5), -13.0) + + def test_add_mixed_numbers(self): + self.assertEqual(add(5, -3), 2) + self.assertEqual(add(-10, 5), -5) + self.assertEqual(add(10.5, -2.5), 8.0) + + def test_add_zero(self): + self.assertEqual(add(5, 0), 5) + self.assertEqual(add(0, -3), -3) + self.assertEqual(add(0, 0), 0) + +if __name__ == '__main__': + unittest.main()