Skip to content

Commit

Permalink
Revert "Refactoring and Enhanced capture_print Function "
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdur-rahmaanJ committed Oct 10, 2023
1 parent a182443 commit ed5c6e9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
17 changes: 13 additions & 4 deletions src/greenberry/helpers.py → src/greenberry/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
30 changes: 20 additions & 10 deletions tests/lang_test.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -9,7 +19,7 @@ def test_printd(self):

def test_null(self):
# check null input
x = None
x = ""
try:
greenberry_eval(x)
except:
Expand All @@ -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")


Expand Down
13 changes: 11 additions & 2 deletions tests/helpers_test.py → tests/utils_test.py
Original file line number Diff line number Diff line change
@@ -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")


Expand Down

0 comments on commit ed5c6e9

Please sign in to comment.