added tests for common.calculate_math_string

This commit is contained in:
Michael Pöhn 2018-05-22 13:24:37 +02:00
parent 24b20d7668
commit 6b1f242d25
2 changed files with 15 additions and 0 deletions

View File

@ -3224,6 +3224,8 @@ def calculate_math_string(expr):
raise SyntaxError(node)
try:
if '#' in expr:
raise SyntaxError('no comments allowed')
return execute_ast(ast.parse(expr, mode='eval').body)
except SyntaxError as e:
raise SyntaxError("could not parse expression '{expr}', "

View File

@ -779,6 +779,19 @@ class CommonTest(unittest.TestCase):
self.assertEqual(('1.0-free', '1', 'com.kunzisoft.fdroidtest.applicationidsuffix'),
fdroidserver.common.parse_androidmanifests(paths, app))
def test_calculate_math_string(self):
self.assertEqual(1234, fdroidserver.common.calculate_math_string('1234'))
self.assertEqual(4, fdroidserver.common.calculate_math_string('(1+1)*2'))
self.assertEqual(2, fdroidserver.common.calculate_math_string('(1-1)*2+3*1-1'))
with self.assertRaises(SyntaxError):
fdroidserver.common.calculate_math_string('__import__("urllib")')
with self.assertRaises(SyntaxError):
fdroidserver.common.calculate_math_string('self')
with self.assertRaises(SyntaxError):
fdroidserver.common.calculate_math_string('1+1; print(1)')
with self.assertRaises(SyntaxError):
fdroidserver.common.calculate_math_string('1-1 # no comment')
if __name__ == "__main__":
parser = optparse.OptionParser()