From ed5c6e929f9340114e920980c30db77dcd91e908 Mon Sep 17 00:00:00 2001 From: Abdur-Rahmaan Janhangeer Date: Tue, 10 Oct 2023 11:07:25 +0400 Subject: [PATCH] Revert "Refactoring and Enhanced capture_print Function " --- src/greenberry/{helpers.py => utils.py} | 17 ++++++++++---- tests/lang_test.py | 30 ++++++++++++++++-------- tests/{helpers_test.py => utils_test.py} | 13 ++++++++-- 3 files changed, 44 insertions(+), 16 deletions(-) rename src/greenberry/{helpers.py => utils.py} (87%) rename tests/{helpers_test.py => utils_test.py} (59%) diff --git a/src/greenberry/helpers.py b/src/greenberry/utils.py similarity index 87% rename from src/greenberry/helpers.py rename to src/greenberry/utils.py index 85a1ed0..52c433e 100644 --- a/src/greenberry/helpers.py +++ b/src/greenberry/utils.py @@ -98,10 +98,19 @@ def maths_eval(string): # previous InfixCon eval(res) -def capture_print(func, *args, **kwargs): +def capture_eval_print(code): temp_stdout = StringIO() - # Redirect stdout to catch the print statements from the provided function + # redirect stdout to catch print statement from eval function with contextlib.redirect_stdout(temp_stdout): - func(*args, **kwargs) + greenberry_eval(code) output = temp_stdout.getvalue().strip() - return output \ No newline at end of file + return output + + +def capture_maths_eval_print(code): + temp_stdout = StringIO() + # redirect stdout to catch print statement from eval function + with contextlib.redirect_stdout(temp_stdout): + maths_eval(code) + output = temp_stdout.getvalue().strip() + return output diff --git a/tests/lang_test.py b/tests/lang_test.py index 85b8b68..cff9a22 100644 --- a/tests/lang_test.py +++ b/tests/lang_test.py @@ -1,6 +1,16 @@ +# +# language tests +# + +import sys + +sys.path.append("..") + import unittest -from greenberry.helpers import capture_print -from greenberry.gb import greenberry_eval + +from utils import capture_eval_print + +from greenberry import greenberry_eval class GBLangTests(unittest.TestCase): @@ -9,7 +19,7 @@ def test_printd(self): def test_null(self): # check null input - x = None + x = "" try: greenberry_eval(x) except: @@ -18,32 +28,32 @@ def test_null(self): def test_eval_add(self): x = "print eval 1+2" - output = capture_print(greenberry_eval, x) + output = capture.eval_print(x) self.assertEqual(output, "3") def test_eval_minus(self): x = "print eval 3-2" - output = capture_print(greenberry_eval, x) + output = capture.eval_print(x) self.assertEqual(output, "1") def test_eval_times(self): x = "print eval 3*2" - output = capture_print(greenberry_eval, x) + output = capture.eval_print(x) self.assertEqual(output, "6") def test_eval_divide(self): x = "print eval 10/2" - output = capture_print(greenberry_eval, x) + output = capture.eval_print(x) self.assertEqual(output, "5.0") def test_eval_mixed_ops(self): x = "print eval 1*3+10+3-2/2" - output = capture_print(greenberry_eval, x) + output = capture.eval_print(x) self.assertEqual(output, "15.0") - def test_eval_mixed_ops_with_parentheses(self): + def test_eval_mixed_ops(self): x = "print eval 1*3+10+3-2/2+(3+2)" - output = capture_print(greenberry_eval, x) + output = capture.eval_print(x) self.assertEqual(output, "20.0") diff --git a/tests/helpers_test.py b/tests/utils_test.py similarity index 59% rename from tests/helpers_test.py rename to tests/utils_test.py index 04d8325..ef5eddd 100644 --- a/tests/helpers_test.py +++ b/tests/utils_test.py @@ -1,11 +1,20 @@ +# +# utils test +# + +import sys + +sys.path.append("..") + import unittest -from greenberry.helpers import capture_print, maths_eval +from utils import capture_maths_eval_print + class GBUtilsTests(unittest.TestCase): def test_maths_eval_add(self): x = "3+2-1" - output = capture_print(maths_eval, x) + output = capture_maths_eval_print(x) self.assertEqual(output, "4")