Skip to content

Commit

Permalink
Merge pull request #93 from Prikshit7766/capture-print
Browse files Browse the repository at this point in the history
Refactoring and Enhanced capture_print Function
  • Loading branch information
Abdur-rahmaanJ committed Oct 10, 2023
2 parents 14fa3b1 + d8ea377 commit a182443
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 44 deletions.
17 changes: 4 additions & 13 deletions src/greenberry/utils.py → src/greenberry/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 2 additions & 11 deletions tests/utils_test.py → tests/helpers_test.py
Original file line number Diff line number Diff line change
@@ -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")


Expand Down
30 changes: 10 additions & 20 deletions tests/lang_test.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -19,7 +9,7 @@ def test_printd(self):

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


Expand Down

0 comments on commit a182443

Please sign in to comment.