mirror of
				https://github.com/cuberite/polarssl.git
				synced 2025-11-03 20:22:59 -05: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
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class GeneratorInputError(Exception):
 | 
			
		||||
    """
 | 
			
		||||
    Exception to indicate error in the input to the generator.
 | 
			
		||||
    """
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class FileWrapper(io.FileIO):
 | 
			
		||||
    """
 | 
			
		||||
    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)
 | 
			
		||||
            suite_functions += func_code
 | 
			
		||||
            # Generate dispatch code and enumeration info
 | 
			
		||||
            assert func_name not in func_info, "file: %s - function %s re-declared at line %d" % \
 | 
			
		||||
                                               (funcs_f.name, func_name, funcs_f.line_no)
 | 
			
		||||
            if func_name in func_info:
 | 
			
		||||
                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)
 | 
			
		||||
            dispatch_code += '/* Function Id: %d */\n' % function_idx
 | 
			
		||||
            dispatch_code += func_dispatch
 | 
			
		||||
@ -411,8 +420,9 @@ def parse_test_data(data_f, debug=False):
 | 
			
		||||
 | 
			
		||||
        # Blank line indicates end of test
 | 
			
		||||
        if len(line) == 0:
 | 
			
		||||
            assert state != STATE_READ_ARGS, "Newline before arguments. " \
 | 
			
		||||
                                                 "Test function and arguments missing for %s" % name
 | 
			
		||||
            if state == STATE_READ_ARGS:
 | 
			
		||||
                raise GeneratorInputError("Newline before arguments. " \
 | 
			
		||||
                        "Test function and arguments missing for %s" % name)
 | 
			
		||||
            continue
 | 
			
		||||
 | 
			
		||||
        if state == STATE_READ_NAME:
 | 
			
		||||
@ -432,8 +442,9 @@ def parse_test_data(data_f, debug=False):
 | 
			
		||||
                yield name, function, deps, args
 | 
			
		||||
                deps = []
 | 
			
		||||
                state = STATE_READ_NAME
 | 
			
		||||
    assert state != STATE_READ_ARGS, "Newline before arguments. " \
 | 
			
		||||
                                     "Test function and arguments missing for %s" % name
 | 
			
		||||
    if state == STATE_READ_ARGS:
 | 
			
		||||
        raise GeneratorInputError("Newline before arguments. " \
 | 
			
		||||
                "Test function and arguments missing for %s" % name)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def gen_dep_check(dep_id, dep):
 | 
			
		||||
@ -444,9 +455,11 @@ def gen_dep_check(dep_id, dep):
 | 
			
		||||
    :param dep: Dependency macro
 | 
			
		||||
    :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)
 | 
			
		||||
    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 = '''
 | 
			
		||||
        case {id}:
 | 
			
		||||
            {{
 | 
			
		||||
@ -468,8 +481,10 @@ def gen_expression_check(exp_id, exp):
 | 
			
		||||
    :param exp: Expression/Macro
 | 
			
		||||
    :return: Expression check code
 | 
			
		||||
    """
 | 
			
		||||
    assert exp_id > -1, "Expression Id should be a positive integer."
 | 
			
		||||
    assert len(exp) > 0, "Expression should not be an empty string."
 | 
			
		||||
    if exp_id < 0:
 | 
			
		||||
        raise GeneratorInputError("Expression Id should be a positive integer.")
 | 
			
		||||
    if len(exp) == 0:
 | 
			
		||||
        raise GeneratorInputError("Expression should not be an empty string.")
 | 
			
		||||
    exp_code = '''
 | 
			
		||||
        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
 | 
			
		||||
        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]
 | 
			
		||||
        out_data_f.write(str(func_id))
 | 
			
		||||
 | 
			
		||||
        # Write parameters
 | 
			
		||||
        assert len(test_args) == len(func_args), \
 | 
			
		||||
            "Invalid number of arguments in test %s. See function %s signature." % (test_name, function_name)
 | 
			
		||||
        if len(test_args) != len(func_args):
 | 
			
		||||
            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)
 | 
			
		||||
 | 
			
		||||
        # Write a newline as test case separator
 | 
			
		||||
@ -726,4 +743,8 @@ def check_cmd():
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
#
 | 
			
		||||
# Copyright (C) 2018, ARM Limited, All Rights Reserved
 | 
			
		||||
@ -857,7 +858,7 @@ void func()
 | 
			
		||||
/* END_CASE */
 | 
			
		||||
'''
 | 
			
		||||
        s = StringIOWrapper('test_suite_ut.function', data)
 | 
			
		||||
        self.assertRaises(AssertionError, parse_functions, s)
 | 
			
		||||
        self.assertRaises(GeneratorInputError, parse_functions, s)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ExcapedSplit(TestCase):
 | 
			
		||||
@ -994,7 +995,7 @@ dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
 | 
			
		||||
 | 
			
		||||
    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:
 | 
			
		||||
        """
 | 
			
		||||
        data = """
 | 
			
		||||
@ -1011,13 +1012,13 @@ dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622"
 | 
			
		||||
        try:
 | 
			
		||||
            for x, y, z, a in parse_test_data(s):
 | 
			
		||||
                pass
 | 
			
		||||
        except AssertionError, e:
 | 
			
		||||
        except GeneratorInputError as e:
 | 
			
		||||
            pass
 | 
			
		||||
        self.assertEqual(type(e), AssertionError)
 | 
			
		||||
        self.assertEqual(type(e), GeneratorInputError)
 | 
			
		||||
 | 
			
		||||
    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:
 | 
			
		||||
        """
 | 
			
		||||
        data = """
 | 
			
		||||
@ -1029,9 +1030,9 @@ depends_on:YAHOO
 | 
			
		||||
        try:
 | 
			
		||||
            for x, y, z, a in parse_test_data(s):
 | 
			
		||||
                pass
 | 
			
		||||
        except AssertionError, e:
 | 
			
		||||
        except GeneratorInputError as e:
 | 
			
		||||
            pass
 | 
			
		||||
        self.assertEqual(type(e), AssertionError)
 | 
			
		||||
        self.assertEqual(type(e), GeneratorInputError)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class GenDepCheck(TestCase):
 | 
			
		||||
@ -1080,14 +1081,14 @@ class GenDepCheck(TestCase):
 | 
			
		||||
        Test invalid dependency input.
 | 
			
		||||
        :return:
 | 
			
		||||
        """
 | 
			
		||||
        self.assertRaises(AssertionError, gen_dep_check, 5, '!')
 | 
			
		||||
        self.assertRaises(GeneratorInputError, gen_dep_check, 5, '!')
 | 
			
		||||
 | 
			
		||||
    def test_negative_dep_id(self):
 | 
			
		||||
        """
 | 
			
		||||
        Test invalid dependency input.
 | 
			
		||||
        :return:
 | 
			
		||||
        """
 | 
			
		||||
        self.assertRaises(AssertionError, gen_dep_check, -1, 'YAHOO')
 | 
			
		||||
        self.assertRaises(GeneratorInputError, gen_dep_check, -1, 'YAHOO')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class GenExpCheck(TestCase):
 | 
			
		||||
@ -1114,14 +1115,14 @@ class GenExpCheck(TestCase):
 | 
			
		||||
        Test invalid expression input.
 | 
			
		||||
        :return:
 | 
			
		||||
        """
 | 
			
		||||
        self.assertRaises(AssertionError, gen_expression_check, 5, '')
 | 
			
		||||
        self.assertRaises(GeneratorInputError, gen_expression_check, 5, '')
 | 
			
		||||
 | 
			
		||||
    def test_negative_exp_id(self):
 | 
			
		||||
        """
 | 
			
		||||
        Test invalid expression id.
 | 
			
		||||
        :return:
 | 
			
		||||
        """
 | 
			
		||||
        self.assertRaises(AssertionError, gen_expression_check, -1, 'YAHOO')
 | 
			
		||||
        self.assertRaises(GeneratorInputError, gen_expression_check, -1, 'YAHOO')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class WriteDeps(TestCase):
 | 
			
		||||
@ -1437,7 +1438,7 @@ func1:0
 | 
			
		||||
        out_data_f = StringIOWrapper('test_suite_ut.datax', '')
 | 
			
		||||
        func_info = {'test_func2': (1, ('int',))}
 | 
			
		||||
        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):
 | 
			
		||||
        """
 | 
			
		||||
@ -1453,7 +1454,7 @@ func1:0
 | 
			
		||||
        out_data_f = StringIOWrapper('test_suite_ut.datax', '')
 | 
			
		||||
        func_info = {'test_func2': (1, ('int','hex'))}
 | 
			
		||||
        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):
 | 
			
		||||
        """
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user