Browse Source

refactoring: changed function names

remotes/origin/feature/feature-read-math-functions
fdai7763 11 months ago
parent
commit
df3f7226b1
  1. 6
      src/main/py/read_math_function.py
  2. 12
      src/test/py/test_read_math_function.py

6
src/main/py/read_math_function.py

@ -1,9 +1,9 @@
def read_constant(type, value):
def simplify_constant(type, value):
if type != "c": if type != "c":
return False return False
if is_string(value): if is_string(value):
if is_allowed_string(type, value): if is_allowed_string(type, value):
return calculate_string(value)
return calculate_constant_string(value)
else: else:
return False return False
return value return value
@ -18,7 +18,7 @@ def is_allowed_string(type, value):
return all(character.isdigit() or character in allowed for character in value) return all(character.isdigit() or character in allowed for character in value)
def calculate_string(string):
def calculate_constant_string(string):
string = string.replace('^', '**') string = string.replace('^', '**')
try: try:
return eval(string) return eval(string)

12
src/test/py/test_read_math_function.py

@ -4,9 +4,9 @@ from src.main.py.read_math_function import *
class read_function(unittest.TestCase): class read_function(unittest.TestCase):
def test_constant_without_calculation(self): def test_constant_without_calculation(self):
self.assertEqual(read_constant("c", 42), 42)
self.assertEqual(simplify_constant("c", 42), 42)
def test_constant_no_c(self): def test_constant_no_c(self):
self.assertEqual(read_constant("e", "42*e^x"), False)
self.assertEqual(simplify_constant("e", "42*e^x"), False)
def test_constant_isstring_true(self): def test_constant_isstring_true(self):
self.assertEqual(is_string("42*e^x"), True) self.assertEqual(is_string("42*e^x"), True)
def test_constant_isstring_false(self): def test_constant_isstring_false(self):
@ -16,13 +16,13 @@ class read_function(unittest.TestCase):
def test_constant_isallowed_string_false(self): def test_constant_isallowed_string_false(self):
self.assertEqual(is_allowed_string("c", "42^(2)+1-3*4/3x"), False) self.assertEqual(is_allowed_string("c", "42^(2)+1-3*4/3x"), False)
def test_constant_calculate_without_pow(self): def test_constant_calculate_without_pow(self):
self.assertEqual(calculate_string("42+1-3*4/3"), 39)
self.assertEqual(calculate_constant_string("42+1-3*4/3"), 39)
def test_constant_calculate_with_pow(self): def test_constant_calculate_with_pow(self):
self.assertEqual(calculate_string("42^(2)+1-3*4/3"), 1761)
self.assertEqual(calculate_constant_string("42^(2)+1-3*4/3"), 1761)
def test_constant_calculate_string_decision(self): def test_constant_calculate_string_decision(self):
self.assertEqual(read_constant("c", "42^(2)+1-3*4/3"), 1761)
self.assertEqual(simplify_constant("c", "42^(2)+1-3*4/3"), 1761)
def test_constant_string_not_allowed_decision(self): def test_constant_string_not_allowed_decision(self):
self.assertEqual(read_constant("c", "42^(2)+1-3*4/3x"), False)
self.assertEqual(simplify_constant("c", "42^(2)+1-3*4/3x"), False)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Loading…
Cancel
Save