mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2024-11-23 04:25:44 +01:00
Replace asserts with exceptions in generate_test_code.py
This commit is contained in:
parent
c3521dfdd6
commit
3b06f226e9
@ -62,6 +62,13 @@ class InvalidFileFormat(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class GeneratorInputError(Exception):
|
||||||
|
"""
|
||||||
|
Exception to indicate error in the input to the generator.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class FileWrapper(io.FileIO):
|
class FileWrapper(io.FileIO):
|
||||||
"""
|
"""
|
||||||
File wrapper class. Provides reading with line no. tracking.
|
File wrapper class. Provides reading with line no. tracking.
|
||||||
@ -353,8 +360,10 @@ def parse_functions(funcs_f):
|
|||||||
func_name, args, func_code, func_dispatch = parse_function_code(funcs_f, deps, suite_deps)
|
func_name, args, func_code, func_dispatch = parse_function_code(funcs_f, deps, suite_deps)
|
||||||
suite_functions += func_code
|
suite_functions += func_code
|
||||||
# Generate dispatch code and enumeration info
|
# Generate dispatch code and enumeration info
|
||||||
assert func_name not in func_info, "file: %s - function %s re-declared at line %d" % \
|
if func_name in func_info:
|
||||||
(funcs_f.name, func_name, funcs_f.line_no)
|
raise GeneratorInputError(
|
||||||
|
"file: %s - function %s re-declared at line %d" % \
|
||||||
|
(funcs_f.name, func_name, funcs_f.line_no))
|
||||||
func_info[func_name] = (function_idx, args)
|
func_info[func_name] = (function_idx, args)
|
||||||
dispatch_code += '/* Function Id: %d */\n' % function_idx
|
dispatch_code += '/* Function Id: %d */\n' % function_idx
|
||||||
dispatch_code += func_dispatch
|
dispatch_code += func_dispatch
|
||||||
@ -411,8 +420,9 @@ def parse_test_data(data_f, debug=False):
|
|||||||
|
|
||||||
# Blank line indicates end of test
|
# Blank line indicates end of test
|
||||||
if len(line) == 0:
|
if len(line) == 0:
|
||||||
assert state != STATE_READ_ARGS, "Newline before arguments. " \
|
if state == STATE_READ_ARGS:
|
||||||
"Test function and arguments missing for %s" % name
|
raise GeneratorInputError("Newline before arguments. " \
|
||||||
|
"Test function and arguments missing for %s" % name)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if state == STATE_READ_NAME:
|
if state == STATE_READ_NAME:
|
||||||
@ -432,8 +442,9 @@ def parse_test_data(data_f, debug=False):
|
|||||||
yield name, function, deps, args
|
yield name, function, deps, args
|
||||||
deps = []
|
deps = []
|
||||||
state = STATE_READ_NAME
|
state = STATE_READ_NAME
|
||||||
assert state != STATE_READ_ARGS, "Newline before arguments. " \
|
if state == STATE_READ_ARGS:
|
||||||
"Test function and arguments missing for %s" % name
|
raise GeneratorInputError("Newline before arguments. " \
|
||||||
|
"Test function and arguments missing for %s" % name)
|
||||||
|
|
||||||
|
|
||||||
def gen_dep_check(dep_id, dep):
|
def gen_dep_check(dep_id, dep):
|
||||||
@ -444,9 +455,11 @@ def gen_dep_check(dep_id, dep):
|
|||||||
:param dep: Dependency macro
|
:param dep: Dependency macro
|
||||||
:return: Dependency check code
|
:return: Dependency check code
|
||||||
"""
|
"""
|
||||||
assert dep_id > -1, "Dependency Id should be a positive integer."
|
if dep_id < 0:
|
||||||
|
raise GeneratorInputError("Dependency Id should be a positive integer.")
|
||||||
noT, dep = ('!', dep[1:]) if dep[0] == '!' else ('', dep)
|
noT, dep = ('!', dep[1:]) if dep[0] == '!' else ('', dep)
|
||||||
assert len(dep) > 0, "Dependency should not be an empty string."
|
if len(dep) == 0:
|
||||||
|
raise GeneratorInputError("Dependency should not be an empty string.")
|
||||||
dep_check = '''
|
dep_check = '''
|
||||||
case {id}:
|
case {id}:
|
||||||
{{
|
{{
|
||||||
@ -468,8 +481,10 @@ def gen_expression_check(exp_id, exp):
|
|||||||
:param exp: Expression/Macro
|
:param exp: Expression/Macro
|
||||||
:return: Expression check code
|
:return: Expression check code
|
||||||
"""
|
"""
|
||||||
assert exp_id > -1, "Expression Id should be a positive integer."
|
if exp_id < 0:
|
||||||
assert len(exp) > 0, "Expression should not be an empty string."
|
raise GeneratorInputError("Expression Id should be a positive integer.")
|
||||||
|
if len(exp) == 0:
|
||||||
|
raise GeneratorInputError("Expression should not be an empty string.")
|
||||||
exp_code = '''
|
exp_code = '''
|
||||||
case {exp_id}:
|
case {exp_id}:
|
||||||
{{
|
{{
|
||||||
@ -583,13 +598,15 @@ def gen_from_test_data(data_f, out_data_f, func_info, suite_deps):
|
|||||||
|
|
||||||
# Write test function name
|
# Write test function name
|
||||||
test_function_name = 'test_' + function_name
|
test_function_name = 'test_' + function_name
|
||||||
assert test_function_name in func_info, "Function %s not found!" % test_function_name
|
if test_function_name not in func_info:
|
||||||
|
raise GeneratorInputError("Function %s not found!" % test_function_name)
|
||||||
func_id, func_args = func_info[test_function_name]
|
func_id, func_args = func_info[test_function_name]
|
||||||
out_data_f.write(str(func_id))
|
out_data_f.write(str(func_id))
|
||||||
|
|
||||||
# Write parameters
|
# Write parameters
|
||||||
assert len(test_args) == len(func_args), \
|
if len(test_args) != len(func_args):
|
||||||
"Invalid number of arguments in test %s. See function %s signature." % (test_name, function_name)
|
raise GeneratorInputError("Invalid number of arguments in test %s. See function %s signature." % (test_name,
|
||||||
|
function_name))
|
||||||
expression_code += write_parameters(out_data_f, test_args, func_args, unique_expressions)
|
expression_code += write_parameters(out_data_f, test_args, func_args, unique_expressions)
|
||||||
|
|
||||||
# Write a newline as test case separator
|
# Write a newline as test case separator
|
||||||
@ -726,4 +743,8 @@ def check_cmd():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
check_cmd()
|
try:
|
||||||
|
check_cmd()
|
||||||
|
except GeneratorInputError as e:
|
||||||
|
script_name = os.path.basename(sys.argv[0])
|
||||||
|
print("%s: input error: %s" % (script_name, str(e)))
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
# Unit test for generate_test_code.py
|
# Unit test for generate_test_code.py
|
||||||
#
|
#
|
||||||
# Copyright (C) 2018, ARM Limited, All Rights Reserved
|
# Copyright (C) 2018, ARM Limited, All Rights Reserved
|
||||||
@ -857,7 +858,7 @@ void func()
|
|||||||
/* END_CASE */
|
/* END_CASE */
|
||||||
'''
|
'''
|
||||||
s = StringIOWrapper('test_suite_ut.function', data)
|
s = StringIOWrapper('test_suite_ut.function', data)
|
||||||
self.assertRaises(AssertionError, parse_functions, s)
|
self.assertRaises(GeneratorInputError, parse_functions, s)
|
||||||
|
|
||||||
|
|
||||||
class ExcapedSplit(TestCase):
|
class ExcapedSplit(TestCase):
|
||||||
@ -994,7 +995,7 @@ dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
|
|||||||
|
|
||||||
def test_no_args(self):
|
def test_no_args(self):
|
||||||
"""
|
"""
|
||||||
Test AssertionError is raised when test function name and args line is missing.
|
Test GeneratorInputError is raised when test function name and args line is missing.
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
data = """
|
data = """
|
||||||
@ -1011,13 +1012,13 @@ dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
|
|||||||
try:
|
try:
|
||||||
for x, y, z, a in parse_test_data(s):
|
for x, y, z, a in parse_test_data(s):
|
||||||
pass
|
pass
|
||||||
except AssertionError, e:
|
except GeneratorInputError as e:
|
||||||
pass
|
pass
|
||||||
self.assertEqual(type(e), AssertionError)
|
self.assertEqual(type(e), GeneratorInputError)
|
||||||
|
|
||||||
def test_incomplete_data(self):
|
def test_incomplete_data(self):
|
||||||
"""
|
"""
|
||||||
Test AssertionError is raised when test function name and args line is missing.
|
Test GeneratorInputError is raised when test function name and args line is missing.
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
data = """
|
data = """
|
||||||
@ -1029,9 +1030,9 @@ depends_on:YAHOO
|
|||||||
try:
|
try:
|
||||||
for x, y, z, a in parse_test_data(s):
|
for x, y, z, a in parse_test_data(s):
|
||||||
pass
|
pass
|
||||||
except AssertionError, e:
|
except GeneratorInputError as e:
|
||||||
pass
|
pass
|
||||||
self.assertEqual(type(e), AssertionError)
|
self.assertEqual(type(e), GeneratorInputError)
|
||||||
|
|
||||||
|
|
||||||
class GenDepCheck(TestCase):
|
class GenDepCheck(TestCase):
|
||||||
@ -1080,14 +1081,14 @@ class GenDepCheck(TestCase):
|
|||||||
Test invalid dependency input.
|
Test invalid dependency input.
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
self.assertRaises(AssertionError, gen_dep_check, 5, '!')
|
self.assertRaises(GeneratorInputError, gen_dep_check, 5, '!')
|
||||||
|
|
||||||
def test_negative_dep_id(self):
|
def test_negative_dep_id(self):
|
||||||
"""
|
"""
|
||||||
Test invalid dependency input.
|
Test invalid dependency input.
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
self.assertRaises(AssertionError, gen_dep_check, -1, 'YAHOO')
|
self.assertRaises(GeneratorInputError, gen_dep_check, -1, 'YAHOO')
|
||||||
|
|
||||||
|
|
||||||
class GenExpCheck(TestCase):
|
class GenExpCheck(TestCase):
|
||||||
@ -1114,14 +1115,14 @@ class GenExpCheck(TestCase):
|
|||||||
Test invalid expression input.
|
Test invalid expression input.
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
self.assertRaises(AssertionError, gen_expression_check, 5, '')
|
self.assertRaises(GeneratorInputError, gen_expression_check, 5, '')
|
||||||
|
|
||||||
def test_negative_exp_id(self):
|
def test_negative_exp_id(self):
|
||||||
"""
|
"""
|
||||||
Test invalid expression id.
|
Test invalid expression id.
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
self.assertRaises(AssertionError, gen_expression_check, -1, 'YAHOO')
|
self.assertRaises(GeneratorInputError, gen_expression_check, -1, 'YAHOO')
|
||||||
|
|
||||||
|
|
||||||
class WriteDeps(TestCase):
|
class WriteDeps(TestCase):
|
||||||
@ -1437,7 +1438,7 @@ func1:0
|
|||||||
out_data_f = StringIOWrapper('test_suite_ut.datax', '')
|
out_data_f = StringIOWrapper('test_suite_ut.datax', '')
|
||||||
func_info = {'test_func2': (1, ('int',))}
|
func_info = {'test_func2': (1, ('int',))}
|
||||||
suite_deps = []
|
suite_deps = []
|
||||||
self.assertRaises(AssertionError, gen_from_test_data, data_f, out_data_f, func_info, suite_deps)
|
self.assertRaises(GeneratorInputError, gen_from_test_data, data_f, out_data_f, func_info, suite_deps)
|
||||||
|
|
||||||
def test_different_func_args(self):
|
def test_different_func_args(self):
|
||||||
"""
|
"""
|
||||||
@ -1453,7 +1454,7 @@ func1:0
|
|||||||
out_data_f = StringIOWrapper('test_suite_ut.datax', '')
|
out_data_f = StringIOWrapper('test_suite_ut.datax', '')
|
||||||
func_info = {'test_func2': (1, ('int','hex'))}
|
func_info = {'test_func2': (1, ('int','hex'))}
|
||||||
suite_deps = []
|
suite_deps = []
|
||||||
self.assertRaises(AssertionError, gen_from_test_data, data_f, out_data_f, func_info, suite_deps)
|
self.assertRaises(GeneratorInputError, gen_from_test_data, data_f, out_data_f, func_info, suite_deps)
|
||||||
|
|
||||||
def test_output(self):
|
def test_output(self):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user