From d9cd81067fe7e3bbe37a804a56d8aa7d2a02f61e Mon Sep 17 00:00:00 2001 From: Prikshit7766 Date: Sun, 1 Oct 2023 16:02:13 +0530 Subject: [PATCH 1/5] Restructure File Names to Avoid Naming Conflict --- src/greenberry/{utils.py => helpers.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/greenberry/{utils.py => helpers.py} (100%) diff --git a/src/greenberry/utils.py b/src/greenberry/helpers.py similarity index 100% rename from src/greenberry/utils.py rename to src/greenberry/helpers.py From 3e29b95ae738d42873896ad8e0c92a132b2fa34c Mon Sep 17 00:00:00 2001 From: Prikshit7766 Date: Sun, 1 Oct 2023 16:03:06 +0530 Subject: [PATCH 2/5] helper.py: added capture_print --- src/greenberry/helpers.py | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/greenberry/helpers.py b/src/greenberry/helpers.py index 52c433e..85a1ed0 100644 --- a/src/greenberry/helpers.py +++ b/src/greenberry/helpers.py @@ -98,19 +98,10 @@ def maths_eval(string): # previous InfixCon eval(res) -def capture_eval_print(code): +def capture_print(func, *args, **kwargs): temp_stdout = StringIO() - # redirect stdout to catch print statement from eval function + # Redirect stdout to catch the print statements from the provided function with contextlib.redirect_stdout(temp_stdout): - greenberry_eval(code) + func(*args, **kwargs) output = temp_stdout.getvalue().strip() - 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 + return output \ No newline at end of file From 7ca41f5d8bedc2585c9b78f26db673d977c049a2 Mon Sep 17 00:00:00 2001 From: Prikshit7766 Date: Sun, 1 Oct 2023 16:04:38 +0530 Subject: [PATCH 3/5] updated lang_test --- tests/lang_test.py | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/tests/lang_test.py b/tests/lang_test.py index cff9a22..85b8b68 100644 --- a/tests/lang_test.py +++ b/tests/lang_test.py @@ -1,16 +1,6 @@ -# -# language tests -# - -import sys - -sys.path.append("..") - import unittest - -from utils import capture_eval_print - -from greenberry import greenberry_eval +from greenberry.helpers import capture_print +from greenberry.gb import greenberry_eval class GBLangTests(unittest.TestCase): @@ -19,7 +9,7 @@ def test_printd(self): def test_null(self): # check null input - x = "" + x = None try: greenberry_eval(x) except: @@ -28,32 +18,32 @@ def test_null(self): def test_eval_add(self): x = "print eval 1+2" - output = capture.eval_print(x) + output = capture_print(greenberry_eval, x) self.assertEqual(output, "3") def test_eval_minus(self): x = "print eval 3-2" - output = capture.eval_print(x) + output = capture_print(greenberry_eval, x) self.assertEqual(output, "1") def test_eval_times(self): x = "print eval 3*2" - output = capture.eval_print(x) + output = capture_print(greenberry_eval, x) self.assertEqual(output, "6") def test_eval_divide(self): x = "print eval 10/2" - output = capture.eval_print(x) + output = capture_print(greenberry_eval, x) self.assertEqual(output, "5.0") def test_eval_mixed_ops(self): x = "print eval 1*3+10+3-2/2" - output = capture.eval_print(x) + output = capture_print(greenberry_eval, x) self.assertEqual(output, "15.0") - def test_eval_mixed_ops(self): + def test_eval_mixed_ops_with_parentheses(self): x = "print eval 1*3+10+3-2/2+(3+2)" - output = capture.eval_print(x) + output = capture_print(greenberry_eval, x) self.assertEqual(output, "20.0") From 555b1fe9efb3ee212b20b251366f8ae0c1589725 Mon Sep 17 00:00:00 2001 From: Prikshit7766 Date: Sun, 1 Oct 2023 16:09:30 +0530 Subject: [PATCH 4/5] Rename utils_test.py to helpers_test.py and update test --- tests/{utils_test.py => helpers_test.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/{utils_test.py => helpers_test.py} (100%) diff --git a/tests/utils_test.py b/tests/helpers_test.py similarity index 100% rename from tests/utils_test.py rename to tests/helpers_test.py From d8ea37791e7c58a2e62011723c80bc77cf5476f4 Mon Sep 17 00:00:00 2001 From: Prikshit7766 Date: Sun, 1 Oct 2023 16:25:16 +0530 Subject: [PATCH 5/5] updated helpers_test --- tests/helpers_test.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/tests/helpers_test.py b/tests/helpers_test.py index ef5eddd..04d8325 100644 --- a/tests/helpers_test.py +++ b/tests/helpers_test.py @@ -1,20 +1,11 @@ -# -# utils test -# - -import sys - -sys.path.append("..") - import unittest -from utils import capture_maths_eval_print - +from greenberry.helpers import capture_print, maths_eval class GBUtilsTests(unittest.TestCase): def test_maths_eval_add(self): x = "3+2-1" - output = capture_maths_eval_print(x) + output = capture_print(maths_eval, x) self.assertEqual(output, "4")