Add support for per test suite helper functions

This commit is contained in:
Mohammad Azim Khan 2018-02-06 13:08:01 +00:00
parent 05d83fa406
commit b522929666
3 changed files with 53 additions and 21 deletions

3
.gitignore vendored
View File

@ -21,5 +21,8 @@ massif-*
*.ilk *.ilk
*.lib *.lib
# Python build artifacts:
*.pyc
# CMake generates *.dir/ folders for in-tree builds (used by MSVC projects), ignore all of those: # CMake generates *.dir/ folders for in-tree builds (used by MSVC projects), ignore all of those:
*.dir/ *.dir/

View File

@ -42,6 +42,9 @@ import shutil
BEGIN_HEADER_REGEX = '/\*\s*BEGIN_HEADER\s*\*/' BEGIN_HEADER_REGEX = '/\*\s*BEGIN_HEADER\s*\*/'
END_HEADER_REGEX = '/\*\s*END_HEADER\s*\*/' END_HEADER_REGEX = '/\*\s*END_HEADER\s*\*/'
BEGIN_SUITE_HELPERS_REGEX = '/\*\s*BEGIN_SUITE_HELPERS\s*\*/'
END_SUITE_HELPERS_REGEX = '/\*\s*END_SUITE_HELPERS\s*\*/'
BEGIN_DEP_REGEX = 'BEGIN_DEPENDENCIES' BEGIN_DEP_REGEX = 'BEGIN_DEPENDENCIES'
END_DEP_REGEX = 'END_DEPENDENCIES' END_DEP_REGEX = 'END_DEPENDENCIES'
@ -172,20 +175,21 @@ def gen_dispatch(name, deps):
return dispatch_code return dispatch_code
def parse_suite_headers(funcs_f): def parse_until_pattern(funcs_f, end_regex):
""" """
Parses function headers. Parses function headers or helper code until end pattern.
:param funcs_f: file object for .functions file :param funcs_f: file object for .functions file
:param end_regex: Pattern to stop parsing
:return: Test suite headers code :return: Test suite headers code
""" """
headers = '#line %d "%s"\n' % (funcs_f.line_no + 1, funcs_f.name) headers = '#line %d "%s"\n' % (funcs_f.line_no + 1, funcs_f.name)
for line in funcs_f: for line in funcs_f:
if re.search(END_HEADER_REGEX, line): if re.search(end_regex, line):
break break
headers += line headers += line
else: else:
raise InvalidFileFormat("file: %s - end header pattern [%s] not found!" % (funcs_f.name, END_HEADER_REGEX)) raise InvalidFileFormat("file: %s - end pattern [%s] not found!" % (funcs_f.name, end_regex))
return headers return headers
@ -325,6 +329,7 @@ def parse_functions(funcs_f):
a dict with function identifiers and arguments info. a dict with function identifiers and arguments info.
""" """
suite_headers = '' suite_headers = ''
suite_helpers = ''
suite_deps = [] suite_deps = []
suite_functions = '' suite_functions = ''
func_info = {} func_info = {}
@ -332,8 +337,11 @@ def parse_functions(funcs_f):
dispatch_code = '' dispatch_code = ''
for line in funcs_f: for line in funcs_f:
if re.search(BEGIN_HEADER_REGEX, line): if re.search(BEGIN_HEADER_REGEX, line):
headers = parse_suite_headers(funcs_f) headers = parse_until_pattern(funcs_f, END_HEADER_REGEX)
suite_headers += headers suite_headers += headers
elif re.search(BEGIN_SUITE_HELPERS_REGEX, line):
helpers = parse_until_pattern(funcs_f, END_SUITE_HELPERS_REGEX)
suite_helpers += helpers
elif re.search(BEGIN_DEP_REGEX, line): elif re.search(BEGIN_DEP_REGEX, line):
deps = parse_suite_deps(funcs_f) deps = parse_suite_deps(funcs_f)
suite_deps += deps suite_deps += deps
@ -350,7 +358,7 @@ def parse_functions(funcs_f):
function_idx += 1 function_idx += 1
ifdef, endif = gen_deps(suite_deps) ifdef, endif = gen_deps(suite_deps)
func_code = ifdef + suite_headers + suite_functions + endif func_code = ifdef + suite_headers + suite_helpers + suite_functions + endif
return suite_deps, dispatch_code, func_code, func_info return suite_deps, dispatch_code, func_code, func_info

View File

@ -280,9 +280,9 @@ class StringIOWrapper(StringIO, object):
return line return line
class ParseSuiteHeaders(TestCase): class ParseUntilPattern(TestCase):
""" """
Test Suite for testing parse_suite_headers(). Test Suite for testing parse_until_pattern().
""" """
def test_suite_headers(self): def test_suite_headers(self):
@ -302,7 +302,7 @@ class ParseSuiteHeaders(TestCase):
#define ECP_PF_UNKNOWN -1 #define ECP_PF_UNKNOWN -1
''' '''
s = StringIOWrapper('test_suite_ut.function', data, line_no=0) s = StringIOWrapper('test_suite_ut.function', data, line_no=0)
headers = parse_suite_headers(s) headers = parse_until_pattern(s, END_HEADER_REGEX)
self.assertEqual(headers, expected) self.assertEqual(headers, expected)
def test_line_no(self): def test_line_no(self):
@ -323,7 +323,7 @@ class ParseSuiteHeaders(TestCase):
#define ECP_PF_UNKNOWN -1 #define ECP_PF_UNKNOWN -1
''' % (offset_line_no + 1) ''' % (offset_line_no + 1)
s = StringIOWrapper('test_suite_ut.function', data, offset_line_no) s = StringIOWrapper('test_suite_ut.function', data, offset_line_no)
headers = parse_suite_headers(s) headers = parse_until_pattern(s, END_HEADER_REGEX)
self.assertEqual(headers, expected) self.assertEqual(headers, expected)
def test_no_end_header_comment(self): def test_no_end_header_comment(self):
@ -337,7 +337,7 @@ class ParseSuiteHeaders(TestCase):
''' '''
s = StringIOWrapper('test_suite_ut.function', data) s = StringIOWrapper('test_suite_ut.function', data)
self.assertRaises(InvalidFileFormat, parse_suite_headers, s) self.assertRaises(InvalidFileFormat, parse_until_pattern, s, END_HEADER_REGEX)
class ParseSuiteDeps(TestCase): class ParseSuiteDeps(TestCase):
@ -620,15 +620,15 @@ class ParseFunction(TestCase):
Test Suite for testing parse_functions() Test Suite for testing parse_functions()
""" """
@patch("generate_code.parse_suite_headers") @patch("generate_code.parse_until_pattern")
def test_begin_header(self, parse_suite_headers_mock): def test_begin_header(self, parse_until_pattern_mock):
""" """
Test that begin header is checked and parse_suite_headers() is called. Test that begin header is checked and parse_until_pattern() is called.
:return: :return:
""" """
def stop(this): def stop(this):
raise Exception raise Exception
parse_suite_headers_mock.side_effect = stop parse_until_pattern_mock.side_effect = stop
data = '''/* BEGIN_HEADER */ data = '''/* BEGIN_HEADER */
#include "mbedtls/ecp.h" #include "mbedtls/ecp.h"
@ -637,13 +637,34 @@ class ParseFunction(TestCase):
''' '''
s = StringIOWrapper('test_suite_ut.function', data) s = StringIOWrapper('test_suite_ut.function', data)
self.assertRaises(Exception, parse_functions, s) self.assertRaises(Exception, parse_functions, s)
parse_suite_headers_mock.assert_called_with(s) parse_until_pattern_mock.assert_called_with(s, END_HEADER_REGEX)
self.assertEqual(s.line_no, 2)
@patch("generate_code.parse_until_pattern")
def test_begin_helper(self, parse_until_pattern_mock):
"""
Test that begin helper is checked and parse_until_pattern() is called.
:return:
"""
def stop(this):
raise Exception
parse_until_pattern_mock.side_effect = stop
data = '''/* BEGIN_SUITE_HELPERS */
void print_helloworld()
{
printf ("Hello World!\n");
}
/* END_SUITE_HELPERS */
'''
s = StringIOWrapper('test_suite_ut.function', data)
self.assertRaises(Exception, parse_functions, s)
parse_until_pattern_mock.assert_called_with(s, END_SUITE_HELPERS_REGEX)
self.assertEqual(s.line_no, 2) self.assertEqual(s.line_no, 2)
@patch("generate_code.parse_suite_deps") @patch("generate_code.parse_suite_deps")
def test_begin_dep(self, parse_suite_deps_mock): def test_begin_dep(self, parse_suite_deps_mock):
""" """
Test that begin header is checked and parse_suite_headers() is called. Test that begin dep is checked and parse_suite_deps() is called.
:return: :return:
""" """
def stop(this): def stop(this):
@ -662,7 +683,7 @@ class ParseFunction(TestCase):
@patch("generate_code.parse_function_deps") @patch("generate_code.parse_function_deps")
def test_begin_function_dep(self, parse_function_deps_mock): def test_begin_function_dep(self, parse_function_deps_mock):
""" """
Test that begin header is checked and parse_suite_headers() is called. Test that begin dep is checked and parse_function_deps() is called.
:return: :return:
""" """
def stop(this): def stop(this):
@ -683,7 +704,7 @@ class ParseFunction(TestCase):
@patch("generate_code.parse_function_deps") @patch("generate_code.parse_function_deps")
def test_return(self, parse_function_deps_mock, parse_function_code_mock): def test_return(self, parse_function_deps_mock, parse_function_code_mock):
""" """
Test that begin header is checked and parse_suite_headers() is called. Test that begin case is checked and parse_function_code() is called.
:return: :return:
""" """
def stop(this): def stop(this):
@ -718,7 +739,7 @@ class ParseFunction(TestCase):
def test_parsing(self): def test_parsing(self):
""" """
Test that begin header is checked and parse_suite_headers() is called. Test case parsing.
:return: :return:
""" """
data = '''/* BEGIN_HEADER */ data = '''/* BEGIN_HEADER */
@ -811,7 +832,7 @@ void test_func2_wrapper( void ** params )
def test_same_function_name(self): def test_same_function_name(self):
""" """
Test that begin header is checked and parse_suite_headers() is called. Test name conflict.
:return: :return:
""" """
data = '''/* BEGIN_HEADER */ data = '''/* BEGIN_HEADER */