diff --git a/tests/scripts/generate_test_code.py b/tests/scripts/generate_test_code.py index a9ec566e6..a28a73669 100755 --- a/tests/scripts/generate_test_code.py +++ b/tests/scripts/generate_test_code.py @@ -16,7 +16,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # -# This file is part of mbed TLS (https://tls.mbed.org) +# This file is part of Mbed TLS (https://tls.mbed.org) """ This script is a key part of Mbed TLS test suites framework. For @@ -29,7 +29,7 @@ Mbed TLS test suites: Scope: ------ The test suites focus on unit testing the crypto primitives and also -include x509 parser tests. Tests can be added to test any MBED TLS +include x509 parser tests. Tests can be added to test any Mbed TLS module. However, the framework is not capable of testing SSL protocol, since that requires full stack execution and that is best tested as part of the system test. @@ -59,7 +59,8 @@ as an expression. Following is an example test definition: X509 CRL Unsupported critical extension (issuingDistributionPoint) depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_SHA256_C -mbedtls_x509_crl_parse:"data_files/crl-idp.pem":MBEDTLS_ERR_X509_INVALID_EXTENSIONS + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG +mbedtls_x509_crl_parse:"data_files/crl-idp.pem":\ + MBEDTLS_ERR_X509_INVALID_EXTENSIONS + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG Test functions: --------------- @@ -170,17 +171,17 @@ import sys import argparse -BEGIN_HEADER_REGEX = '/\*\s*BEGIN_HEADER\s*\*/' -END_HEADER_REGEX = '/\*\s*END_HEADER\s*\*/' +BEGIN_HEADER_REGEX = r'/\*\s*BEGIN_HEADER\s*\*/' +END_HEADER_REGEX = r'/\*\s*END_HEADER\s*\*/' -BEGIN_SUITE_HELPERS_REGEX = '/\*\s*BEGIN_SUITE_HELPERS\s*\*/' -END_SUITE_HELPERS_REGEX = '/\*\s*END_SUITE_HELPERS\s*\*/' +BEGIN_SUITE_HELPERS_REGEX = r'/\*\s*BEGIN_SUITE_HELPERS\s*\*/' +END_SUITE_HELPERS_REGEX = r'/\*\s*END_SUITE_HELPERS\s*\*/' -BEGIN_DEP_REGEX = 'BEGIN_DEPENDENCIES' -END_DEP_REGEX = 'END_DEPENDENCIES' +BEGIN_DEP_REGEX = r'BEGIN_DEPENDENCIES' +END_DEP_REGEX = r'END_DEPENDENCIES' -BEGIN_CASE_REGEX = '/\*\s*BEGIN_CASE\s*(.*?)\s*\*/' -END_CASE_REGEX = '/\*\s*END_CASE\s*\*/' +BEGIN_CASE_REGEX = r'/\*\s*BEGIN_CASE\s*(.*?)\s*\*/' +END_CASE_REGEX = r'/\*\s*END_CASE\s*\*/' class GeneratorInputError(Exception): @@ -192,7 +193,7 @@ class GeneratorInputError(Exception): pass -class FileWrapper(io.FileIO): +class FileWrapper(io.FileIO, object): """ This class extends built-in io.FileIO class with attribute line_no, that indicates line number for the line that is read. @@ -205,9 +206,9 @@ class FileWrapper(io.FileIO): :param file_name: File path to open. """ super(FileWrapper, self).__init__(file_name, 'r') - self.line_no = 0 + self._line_no = 0 - def __next__(self): + def next(self): """ Python 2 iterator method. This method overrides base class's next method and extends the next method to count the line @@ -220,23 +221,31 @@ class FileWrapper(io.FileIO): """ parent = super(FileWrapper, self) if hasattr(parent, '__next__'): - line = parent.__next__() # Python 3 + line = parent.__next__() # Python 3 else: - line = parent.next() # Python 2 - if line: - self.line_no += 1 + line = parent.next() # Python 2 + if line is not None: + self._line_no += 1 # Convert byte array to string with correct encoding and # strip any whitespaces added in the decoding process. return line.decode(sys.getdefaultencoding()).strip() + "\n" return None # Python 3 iterator method - next = __next__ + __next__ = next + + def get_line_no(self): + """ + Gives current line number. + """ + return self._line_no + + line_no = property(get_line_no) def split_dep(dep): """ - Split NOT character '!' from dependency. Used by gen_deps() + Split NOT character '!' from dependency. Used by gen_dependencies() :param dep: Dependency list :return: string tuple. Ex: ('!', MACRO) for !MACRO and ('', MACRO) for @@ -245,7 +254,7 @@ def split_dep(dep): return ('!', dep[1:]) if dep[0] == '!' else ('', dep) -def gen_deps(deps): +def gen_dependencies(dependencies): """ Test suite data and functions specifies compile time dependencies. This function generates C preprocessor code from the input @@ -256,36 +265,39 @@ def gen_deps(deps): function split_dep() and proper preprocessor check is generated accordingly. - :param deps: List of dependencies. + :param dependencies: List of dependencies. :return: if defined and endif code with macro annotations for readability. """ - dep_start = ''.join(['#if %sdefined(%s)\n' % split_dep(x) for x in deps]) - dep_end = ''.join(['#endif /* %s */\n' % x for x in reversed(deps)]) + dep_start = ''.join(['#if %sdefined(%s)\n' % (x, y) for x, y in + map(split_dep, dependencies)]) + dep_end = ''.join(['#endif /* %s */\n' % + x for x in reversed(dependencies)]) return dep_start, dep_end -def gen_deps_one_line(deps): +def gen_dependencies_one_line(dependencies): """ - Similar to gen_deps() but generates dependency checks in one line. + Similar to gen_dependencies() but generates dependency checks in one line. Useful for generating code with #else block. - :param deps: List of dependencies. - :return: ifdef code + :param dependencies: List of dependencies. + :return: Preprocessor check code """ - defines = '#if ' if len(deps) else '' - defines += ' && '.join(['%sdefined(%s)' % split_dep(x) for x in deps]) + defines = '#if ' if dependencies else '' + defines += ' && '.join(['%sdefined(%s)' % (x, y) for x, y in map( + split_dep, dependencies)]) return defines -def gen_function_wrapper(name, locals, args_dispatch): +def gen_function_wrapper(name, local_vars, args_dispatch): """ Creates test function wrapper code. A wrapper has the code to unpack parameters from parameters[] array. :param name: Test function name - :param locals: Local variables declaration code + :param local_vars: Local variables declaration code :param args_dispatch: List of dispatch arguments. Ex: ['(char *)params[0]', '*((int *)params[1])'] :return: Test function wrapper. @@ -300,11 +312,11 @@ void {name}_wrapper( void ** params ) '''.format(name=name, unused_params='' if args_dispatch else ' (void)params;\n', args=', '.join(args_dispatch), - locals=locals) + locals=local_vars) return wrapper -def gen_dispatch(name, deps): +def gen_dispatch(name, dependencies): """ Test suite code template main_test.function defines a C function array to contain test case functions. This function generates an @@ -314,18 +326,18 @@ def gen_dispatch(name, deps): dependencies are met, else NULL is assigned. :param name: Test function name - :param deps: List of dependencies + :param dependencies: List of dependencies :return: Dispatch code. """ - if len(deps): - ifdef = gen_deps_one_line(deps) + if dependencies: + preprocessor_check = gen_dependencies_one_line(dependencies) dispatch_code = ''' -{ifdef} +{preprocessor_check} {name}_wrapper, #else NULL, #endif -'''.format(ifdef=ifdef, name=name) +'''.format(preprocessor_check=preprocessor_check, name=name) else: dispatch_code = ''' {name}_wrapper, @@ -350,12 +362,12 @@ def parse_until_pattern(funcs_f, end_regex): headers += line else: raise GeneratorInputError("file: %s - end pattern [%s] not found!" % - (funcs_f.name, end_regex)) + (funcs_f.name, end_regex)) return headers -def parse_suite_deps(funcs_f): +def parse_suite_dependencies(funcs_f): """ Parses test suite dependencies specified at the top of a .function file, that starts with pattern BEGIN_DEPENDENCIES @@ -365,21 +377,22 @@ def parse_suite_deps(funcs_f): :param funcs_f: file object for .functions file :return: List of test suite dependencies. """ - deps = [] + dependencies = [] for line in funcs_f: - m = re.search('depends_on\:(.*)', line.strip()) - if m: - deps += [x.strip() for x in m.group(1).split(':')] + match = re.search('depends_on:(.*)', line.strip()) + if match: + dependencies += [x.strip() for x in match.group(1).split(':')] if re.search(END_DEP_REGEX, line): break else: raise GeneratorInputError("file: %s - end dependency pattern [%s]" - " not found!" % (funcs_f.name, END_DEP_REGEX)) + " not found!" % (funcs_f.name, + END_DEP_REGEX)) - return deps + return dependencies -def parse_function_deps(line): +def parse_function_dependencies(line): """ Parses function dependencies, that are in the same line as comment BEGIN_CASE. Dependencies are specified after pattern @@ -388,14 +401,15 @@ def parse_function_deps(line): :param line: Line from .functions file that has dependencies. :return: List of dependencies. """ - deps = [] - m = re.search(BEGIN_CASE_REGEX, line) - dep_str = m.group(1) - if len(dep_str): - m = re.search('depends_on:(.*)', dep_str) - if m: - deps = [x.strip() for x in m.group(1).strip().split(':')] - return deps + dependencies = [] + match = re.search(BEGIN_CASE_REGEX, line) + dep_str = match.group(1) + if dep_str: + match = re.search('depends_on:(.*)', dep_str) + if match: + dependencies = [x.strip() + for x in match.group(1).strip().split(':')] + return dependencies def parse_function_signature(line): @@ -410,31 +424,31 @@ def parse_function_signature(line): wrapper function and argument dispatch code. """ args = [] - locals = '' + local_vars = '' args_dispatch = [] # Check if the test function returns void. - m = re.search('\s*void\s+(\w+)\s*\(', line, re.I) - if not m: + match = re.search(r'\s*void\s+(\w+)\s*\(', line, re.I) + if not match: raise ValueError("Test function should return 'void'\n%s" % line) - name = m.group(1) - line = line[len(m.group(0)):] + name = match.group(1) + line = line[len(match.group(0)):] arg_idx = 0 for arg in line[:line.find(')')].split(','): arg = arg.strip() if arg == '': continue - if re.search('int\s+.*', arg.strip()): + if re.search(r'int\s+.*', arg.strip()): args.append('int') args_dispatch.append('*( (int *) params[%d] )' % arg_idx) - elif re.search('char\s*\*\s*.*', arg.strip()): + elif re.search(r'char\s*\*\s*.*', arg.strip()): args.append('char*') args_dispatch.append('(char *) params[%d]' % arg_idx) - elif re.search('data_t\s*\*\s*.*', arg.strip()): + elif re.search(r'data_t\s*\*\s*.*', arg.strip()): args.append('hex') # create a structure pointer_initializer = '(uint8_t *) params[%d]' % arg_idx len_initializer = '*( (uint32_t *) params[%d] )' % (arg_idx+1) - locals += """ data_t data%d = {%s, %s}; + local_vars += """ data_t data%d = {%s, %s}; """ % (arg_idx, pointer_initializer, len_initializer) args_dispatch.append('&data%d' % arg_idx) @@ -444,37 +458,38 @@ def parse_function_signature(line): "'char *' or 'data_t'\n%s" % line) arg_idx += 1 - return name, args, locals, args_dispatch + return name, args, local_vars, args_dispatch -def parse_function_code(funcs_f, deps, suite_deps): +def parse_function_code(funcs_f, dependencies, suite_dependencies): """ Parses out a function from function file object and generates function and dispatch code. :param funcs_f: file object of the functions file. - :param deps: List of dependencies - :param suite_deps: List of test suite dependencies + :param dependencies: List of dependencies + :param suite_dependencies: List of test suite dependencies :return: Function name, arguments, function code and dispatch code. """ code = '#line %d "%s"\n' % (funcs_f.line_no + 1, funcs_f.name) for line in funcs_f: # Check function signature - m = re.match('.*?\s+(\w+)\s*\(', line, re.I) - if m: + match = re.match(r'.*?\s+(\w+)\s*\(', line, re.I) + if match: # check if we have full signature i.e. split in more lines - if not re.match('.*\)', line): + if not re.match(r'.*\)', line): for lin in funcs_f: line += lin - if re.search('.*?\)', line): + if re.search(r'.*?\)', line): break - name, args, locals, args_dispatch = parse_function_signature(line) + name, args, local_vars, args_dispatch = parse_function_signature( + line) code += line.replace(name, 'test_' + name) name = 'test_' + name break else: raise GeneratorInputError("file: %s - Test functions not found!" % - funcs_f.name) + funcs_f.name) for line in funcs_f: if re.search(END_CASE_REGEX, line): @@ -482,20 +497,22 @@ def parse_function_code(funcs_f, deps, suite_deps): code += line else: raise GeneratorInputError("file: %s - end case pattern [%s] not " - "found!" % (funcs_f.name, END_CASE_REGEX)) + "found!" % (funcs_f.name, END_CASE_REGEX)) # Add exit label if not present if code.find('exit:') == -1: - s = code.rsplit('}', 1) - if len(s) == 2: + split_code = code.rsplit('}', 1) + if len(split_code) == 2: code = """exit: ;; -}""".join(s) +}""".join(split_code) - code += gen_function_wrapper(name, locals, args_dispatch) - ifdef, endif = gen_deps(deps) - dispatch_code = gen_dispatch(name, suite_deps + deps) - return name, args, ifdef + code + endif, dispatch_code + code += gen_function_wrapper(name, local_vars, args_dispatch) + preprocessor_check_start, preprocessor_check_end = \ + gen_dependencies(dependencies) + dispatch_code = gen_dispatch(name, suite_dependencies + dependencies) + return (name, args, preprocessor_check_start + code + + preprocessor_check_end, dispatch_code) def parse_functions(funcs_f): @@ -508,9 +525,8 @@ def parse_functions(funcs_f): code, function code and a dict with function identifiers and arguments info. """ - suite_headers = '' suite_helpers = '' - suite_deps = [] + suite_dependencies = [] suite_functions = '' func_info = {} function_idx = 0 @@ -518,62 +534,61 @@ def parse_functions(funcs_f): for line in funcs_f: if re.search(BEGIN_HEADER_REGEX, line): headers = parse_until_pattern(funcs_f, END_HEADER_REGEX) - suite_headers += headers + suite_helpers += 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): - deps = parse_suite_deps(funcs_f) - suite_deps += deps + suite_dependencies += parse_suite_dependencies(funcs_f) elif re.search(BEGIN_CASE_REGEX, line): - deps = parse_function_deps(line) + dependencies = parse_function_dependencies(line) func_name, args, func_code, func_dispatch =\ - parse_function_code(funcs_f, deps, suite_deps) + parse_function_code(funcs_f, dependencies, suite_dependencies) suite_functions += func_code # Generate dispatch code and enumeration info if func_name in func_info: raise GeneratorInputError( - "file: %s - function %s re-declared at line %d" % \ + "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 function_idx += 1 - ifdef, endif = gen_deps(suite_deps) - func_code = ifdef + suite_headers + suite_helpers + suite_functions + endif - return suite_deps, dispatch_code, func_code, func_info + func_code = (suite_helpers + + suite_functions).join(gen_dependencies(suite_dependencies)) + return suite_dependencies, dispatch_code, func_code, func_info -def escaped_split(str, ch): +def escaped_split(inp_str, split_char): """ - Split str on character ch but ignore escaped \{ch} + Split inp_str on character split_char but ignore if escaped. Since, return value is used to write back to the intermediate data file, any escape characters in the input are retained in the output. - :param str: String to split - :param ch: split character + :param inp_str: String to split + :param split_char: split character :return: List of splits """ - if len(ch) > 1: + if len(split_char) > 1: raise ValueError('Expected split character. Found string!') out = [] part = '' escape = False - for i in range(len(str)): - if not escape and str[i] == ch: + for character in inp_str: + if not escape and character == split_char: out.append(part) part = '' else: - part += str[i] - escape = not escape and str[i] == '\\' - if len(part): + part += character + escape = not escape and character == '\\' + if part: out.append(part) return out -def parse_test_data(data_f, debug=False): +def parse_test_data(data_f): """ Parses .data file for each test case name, test function name, test dependencies and test arguments. This information is @@ -587,44 +602,44 @@ def parse_test_data(data_f, debug=False): :return: Generator that yields test name, function name, dependency list and function argument list. """ - STATE_READ_NAME = 0 - STATE_READ_ARGS = 1 - state = STATE_READ_NAME - deps = [] + __state_read_name = 0 + __state_read_args = 1 + state = __state_read_name + dependencies = [] name = '' for line in data_f: line = line.strip() - if len(line) and line[0] == '#': # Skip comments + if line and line[0] == '#': # Skip comments continue # Blank line indicates end of test - if len(line) == 0: - if state == STATE_READ_ARGS: + if not line: + if state == __state_read_args: raise GeneratorInputError("[%s:%d] Newline before arguments. " "Test function and arguments " "missing for %s" % (data_f.name, data_f.line_no, name)) continue - if state == STATE_READ_NAME: + if state == __state_read_name: # Read test name name = line - state = STATE_READ_ARGS - elif state == STATE_READ_ARGS: + state = __state_read_args + elif state == __state_read_args: # Check dependencies - m = re.search('depends_on\:(.*)', line) - if m: - deps = [x.strip() for x in m.group(1).split(':') if len( - x.strip())] + match = re.search('depends_on:(.*)', line) + if match: + dependencies = [x.strip() for x in match.group(1).split(':') + if len(x.strip())] else: # Read test vectors parts = escaped_split(line, ':') - function = parts[0] + test_function = parts[0] args = parts[1:] - yield name, function, deps, args - deps = [] - state = STATE_READ_NAME - if state == STATE_READ_ARGS: + yield name, test_function, dependencies, args + dependencies = [] + state = __state_read_name + if state == __state_read_args: raise GeneratorInputError("[%s:%d] Newline before arguments. " "Test function and arguments missing for " "%s" % (data_f.name, data_f.line_no, name)) @@ -642,19 +657,19 @@ def gen_dep_check(dep_id, dep): if dep_id < 0: raise GeneratorInputError("Dependency Id should be a positive " "integer.") - noT, dep = ('!', dep[1:]) if dep[0] == '!' else ('', dep) - if len(dep) == 0: + _not, dep = ('!', dep[1:]) if dep[0] == '!' else ('', dep) + if not dep: raise GeneratorInputError("Dependency should not be an empty string.") dep_check = ''' case {id}: {{ -#if {noT}defined({macro}) +#if {_not}defined({macro}) ret = DEPENDENCY_SUPPORTED; #else ret = DEPENDENCY_NOT_SUPPORTED; #endif }} - break;'''.format(noT=noT, macro=dep, id=dep_id) + break;'''.format(_not=_not, macro=dep, id=dep_id) return dep_check @@ -670,7 +685,7 @@ def gen_expression_check(exp_id, exp): if exp_id < 0: raise GeneratorInputError("Expression Id should be a positive " "integer.") - if len(exp) == 0: + if not exp: raise GeneratorInputError("Expression should not be an empty string.") exp_code = ''' case {exp_id}: @@ -681,28 +696,28 @@ def gen_expression_check(exp_id, exp): return exp_code -def write_deps(out_data_f, test_deps, unique_deps): +def write_dependencies(out_data_f, test_dependencies, unique_dependencies): """ Write dependencies to intermediate test data file, replacing the string form with identifiers. Also, generates dependency check code. :param out_data_f: Output intermediate data file - :param test_deps: Dependencies - :param unique_deps: Mutable list to track unique dependencies + :param test_dependencies: Dependencies + :param unique_dependencies: Mutable list to track unique dependencies that are global to this re-entrant function. :return: returns dependency check code. """ dep_check_code = '' - if len(test_deps): + if test_dependencies: out_data_f.write('depends_on') - for dep in test_deps: - if dep not in unique_deps: - unique_deps.append(dep) - dep_id = unique_deps.index(dep) + for dep in test_dependencies: + if dep not in unique_dependencies: + unique_dependencies.append(dep) + dep_id = unique_dependencies.index(dep) dep_check_code += gen_dep_check(dep_id, dep) else: - dep_id = unique_deps.index(dep) + dep_id = unique_dependencies.index(dep) out_data_f.write(':' + str(dep_id)) out_data_f.write('\n') return dep_check_code @@ -722,12 +737,12 @@ def write_parameters(out_data_f, test_args, func_args, unique_expressions): :return: Returns expression check code. """ expression_code = '' - for i in range(len(test_args)): + for i, _ in enumerate(test_args): typ = func_args[i] val = test_args[i] # check if val is a non literal int val (i.e. an expression) - if typ == 'int' and not re.match('(\d+$)|((0x)?[0-9a-fA-F]+$)', val): + if typ == 'int' and not re.match(r'(\d+$)|((0x)?[0-9a-fA-F]+$)', val): typ = 'exp' if val not in unique_expressions: unique_expressions.append(val) @@ -744,33 +759,33 @@ def write_parameters(out_data_f, test_args, func_args, unique_expressions): return expression_code -def gen_suite_deps_checks(suite_deps, dep_check_code, expression_code): +def gen_suite_dep_checks(suite_dependencies, dep_check_code, expression_code): """ Generates preprocessor checks for test suite dependencies. - :param suite_deps: Test suite dependencies read from the + :param suite_dependencies: Test suite dependencies read from the .functions file. :param dep_check_code: Dependency check code :param expression_code: Expression check code :return: Dependency and expression code guarded by test suite dependencies. """ - if len(suite_deps): - ifdef = gen_deps_one_line(suite_deps) + if suite_dependencies: + preprocessor_check = gen_dependencies_one_line(suite_dependencies) dep_check_code = ''' -{ifdef} +{preprocessor_check} {code} #endif -'''.format(ifdef=ifdef, code=dep_check_code) +'''.format(preprocessor_check=preprocessor_check, code=dep_check_code) expression_code = ''' -{ifdef} +{preprocessor_check} {code} #endif -'''.format(ifdef=ifdef, code=expression_code) +'''.format(preprocessor_check=preprocessor_check, code=expression_code) return dep_check_code, expression_code -def gen_from_test_data(data_f, out_data_f, func_info, suite_deps): +def gen_from_test_data(data_f, out_data_f, func_info, suite_dependencies): """ This function reads test case name, dependencies and test vectors from the .data file. This information is correlated with the test @@ -785,19 +800,20 @@ def gen_from_test_data(data_f, out_data_f, func_info, suite_deps): :param out_data_f:Output intermediate data file :param func_info: Dict keyed by function and with function id and arguments info - :param suite_deps: Test suite deps + :param suite_dependencies: Test suite dependencies :return: Returns dependency and expression check code """ - unique_deps = [] + unique_dependencies = [] unique_expressions = [] dep_check_code = '' expression_code = '' - for test_name, function_name, test_deps, test_args in parse_test_data( - data_f): + for test_name, function_name, test_dependencies, test_args in \ + parse_test_data(data_f): out_data_f.write(test_name + '\n') - # Write deps - dep_check_code += write_deps(out_data_f, test_deps, unique_deps) + # Write dependencies + dep_check_code += write_dependencies(out_data_f, test_dependencies, + unique_dependencies) # Write test function name test_function_name = 'test_' + function_name @@ -810,35 +826,143 @@ def gen_from_test_data(data_f, out_data_f, func_info, suite_deps): # Write parameters if len(test_args) != len(func_args): raise GeneratorInputError("Invalid number of arguments in test " - "%s. See function %s signature." % ( - test_name, function_name)) + "%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 out_data_f.write('\n') - dep_check_code, expression_code = gen_suite_deps_checks( - suite_deps, dep_check_code, expression_code) + dep_check_code, expression_code = gen_suite_dep_checks( + suite_dependencies, dep_check_code, expression_code) return dep_check_code, expression_code -def generate_code(funcs_file, data_file, template_file, platform_file, - helpers_file, suites_dir, c_file, out_data_file): +def add_input_info(funcs_file, data_file, template_file, + c_file, snippets): """ - Generates C source code from test suite file, data file, common - helpers file and platform file. + Add generator input info in snippets. :param funcs_file: Functions file object :param data_file: Data file object :param template_file: Template file object - :param platform_file: Platform file object - :param helpers_file: Helper functions file object - :param suites_dir: Test suites dir :param c_file: Output C file object - :param out_data_file: Output intermediate data file object + :param snippets: Dictionary to contain code pieces to be + substituted in the template. :return: """ + snippets['test_file'] = c_file + snippets['test_main_file'] = template_file + snippets['test_case_file'] = funcs_file + snippets['test_case_data_file'] = data_file + + +def read_code_from_input_files(platform_file, helpers_file, + out_data_file, snippets): + """ + Read code from input files and create substitutions for replacement + strings in the template file. + + :param platform_file: Platform file object + :param helpers_file: Helper functions file object + :param out_data_file: Output intermediate data file object + :param snippets: Dictionary to contain code pieces to be + substituted in the template. + :return: + """ + # Read helpers + with open(helpers_file, 'r') as help_f, open(platform_file, 'r') as \ + platform_f: + snippets['test_common_helper_file'] = helpers_file + snippets['test_common_helpers'] = help_f.read() + snippets['test_platform_file'] = platform_file + snippets['platform_code'] = platform_f.read().replace( + 'DATA_FILE', out_data_file.replace('\\', '\\\\')) # escape '\' + + +def write_test_source_file(template_file, c_file, snippets): + """ + Write output source file with generated source code. + + :param template_file: Template file name + :param c_file: Output source file + :param snippets: Generated and code snippets + :return: + """ + with open(template_file, 'r') as template_f, open(c_file, 'w') as c_f: + line_no = 1 + for line in template_f.readlines(): + # Update line number. +1 as #line directive sets next line number + snippets['line_no'] = line_no + 1 + code = line.format(**snippets) + c_f.write(code) + line_no += 1 + + +def parse_function_file(funcs_file, snippets): + """ + Parse function file and generate function dispatch code. + + :param funcs_file: Functions file name + :param snippets: Dictionary to contain code pieces to be + substituted in the template. + :return: + """ + with FileWrapper(funcs_file) as funcs_f: + suite_dependencies, dispatch_code, func_code, func_info = \ + parse_functions(funcs_f) + snippets['functions_code'] = func_code + snippets['dispatch_code'] = dispatch_code + return suite_dependencies, func_info + + +def generate_intermediate_data_file(data_file, out_data_file, + suite_dependencies, func_info, snippets): + """ + Generates intermediate data file from input data file and + information read from functions file. + + :param data_file: Data file name + :param out_data_file: Output/Intermediate data file + :param suite_dependencies: List of suite dependencies. + :param func_info: Function info parsed from functions file. + :param snippets: Dictionary to contain code pieces to be + substituted in the template. + :return: + """ + with FileWrapper(data_file) as data_f, \ + open(out_data_file, 'w') as out_data_f: + dep_check_code, expression_code = gen_from_test_data( + data_f, out_data_f, func_info, suite_dependencies) + snippets['dep_check_code'] = dep_check_code + snippets['expression_code'] = expression_code + + +def generate_code(**input_info): + """ + Generates C source code from test suite file, data file, common + helpers file and platform file. + + input_info expands to following parameters: + funcs_file: Functions file object + data_file: Data file object + template_file: Template file object + platform_file: Platform file object + helpers_file: Helper functions file object + suites_dir: Test suites dir + c_file: Output C file object + out_data_file: Output intermediate data file object + :return: + """ + funcs_file = input_info['funcs_file'] + data_file = input_info['data_file'] + template_file = input_info['template_file'] + platform_file = input_info['platform_file'] + helpers_file = input_info['helpers_file'] + suites_dir = input_info['suites_dir'] + c_file = input_info['c_file'] + out_data_file = input_info['out_data_file'] for name, path in [('Functions file', funcs_file), ('Data file', data_file), ('Template file', template_file), @@ -848,44 +972,15 @@ def generate_code(funcs_file, data_file, template_file, platform_file, if not os.path.exists(path): raise IOError("ERROR: %s [%s] not found!" % (name, path)) - snippets = {'generator_script' : os.path.basename(__file__)} - - # Read helpers - with open(helpers_file, 'r') as help_f, open(platform_file, 'r') as \ - platform_f: - snippets['test_common_helper_file'] = helpers_file - snippets['test_common_helpers'] = help_f.read() - snippets['test_platform_file'] = platform_file - snippets['platform_code'] = platform_f.read().replace( - 'DATA_FILE', out_data_file.replace('\\', '\\\\')) # escape '\' - - # Function code - with FileWrapper(funcs_file) as funcs_f, FileWrapper(data_file) as \ - data_f, open(out_data_file, 'w') as out_data_f: - suite_deps, dispatch_code, func_code, func_info = parse_functions( - funcs_f) - snippets['functions_code'] = func_code - snippets['dispatch_code'] = dispatch_code - dep_check_code, expression_code = gen_from_test_data( - data_f, out_data_f, func_info, suite_deps) - snippets['dep_check_code'] = dep_check_code - snippets['expression_code'] = expression_code - - snippets['test_file'] = c_file - snippets['test_main_file'] = template_file - snippets['test_case_file'] = funcs_file - snippets['test_case_data_file'] = data_file - # Read Template - # Add functions - # - with open(template_file, 'r') as template_f, open(c_file, 'w') as c_f: - line_no = 1 - for line in template_f.readlines(): - # Update line number. +1 as #line directive sets next line number - snippets['line_no'] = line_no + 1 - code = line.format(**snippets) - c_f.write(code) - line_no += 1 + snippets = {'generator_script': os.path.basename(__file__)} + read_code_from_input_files(platform_file, helpers_file, + out_data_file, snippets) + add_input_info(funcs_file, data_file, template_file, + c_file, snippets) + suite_dependencies, func_info = parse_function_file(funcs_file, snippets) + generate_intermediate_data_file(data_file, out_data_file, + suite_dependencies, func_info, snippets) + write_test_source_file(template_file, c_file, snippets) def check_cmd(): @@ -949,18 +1044,20 @@ def check_cmd(): out_c_file_dir = os.path.dirname(out_c_file) out_data_file_dir = os.path.dirname(out_data_file) - for d in [out_c_file_dir, out_data_file_dir]: - if not os.path.exists(d): - os.makedirs(d) + for directory in [out_c_file_dir, out_data_file_dir]: + if not os.path.exists(directory): + os.makedirs(directory) - generate_code(args.funcs_file, args.data_file, args.template_file, - args.platform_file, args.helpers_file, args.suites_dir, - out_c_file, out_data_file) + generate_code(funcs_file=args.funcs_file, data_file=args.data_file, + template_file=args.template_file, + platform_file=args.platform_file, + helpers_file=args.helpers_file, suites_dir=args.suites_dir, + c_file=out_c_file, out_data_file=out_data_file) if __name__ == "__main__": try: check_cmd() - except GeneratorInputError as e: - script_name = os.path.basename(sys.argv[0]) - print("%s: input error: %s" % (script_name, str(e))) + except GeneratorInputError as err: + print("%s: input error: %s" % + (os.path.basename(sys.argv[0]), str(err))) diff --git a/tests/scripts/mbedtls_test.py b/tests/scripts/mbedtls_test.py index c3b1b7a3f..8fd72613e 100755 --- a/tests/scripts/mbedtls_test.py +++ b/tests/scripts/mbedtls_test.py @@ -15,18 +15,18 @@ # See the License for the specific language governing permissions and # limitations under the License. # -# This file is part of mbed TLS (https://tls.mbed.org) +# This file is part of Mbed TLS (https://tls.mbed.org) """ -Mbed TLS on-target test suite tests are implemented as mbed-os greentea +Mbed TLS on-target test suite tests are implemented as mbed-os Greentea tests. Greentea tests are implemented in two parts: target test and host test. Target test is a C application that is built for the target platform and executes on the target. Host test is a Python class derived from mbed_host_tests.BaseHostTest. Target communicates with the host over serial for the test data. -Python tool mbedgt (greentea) is responsible for flashing the test +Python tool mbedgt (Greentea) is responsible for flashing the test binary on to the target and dynamically loading the host test. This script contains the host test for handling target test's @@ -64,67 +64,69 @@ class TestDataParser(object): :param data_file: Data file path """ - with open(data_file, 'r') as f: - self.__parse(f) + with open(data_file, 'r') as data_f: + self.__parse(data_f) @staticmethod - def __escaped_split(str, ch): + def __escaped_split(inp_str, split_char): """ - Splits str on ch except when escaped. + Splits inp_str on split_char except when escaped. - :param str: String to split - :param ch: Split character + :param inp_str: String to split + :param split_char: Split character :return: List of splits """ - if len(ch) > 1: + if len(split_char) > 1: raise ValueError('Expected split character. Found string!') out = [] part = '' escape = False - for i in range(len(str)): - if not escape and str[i] == ch: + for character in inp_str: + if not escape and character == split_char: out.append(part) part = '' else: - part += str[i] - escape = not escape and str[i] == '\\' - if len(part): + part += character + escape = not escape and character == '\\' + if part: out.append(part) return out - def __parse(self, file): + def __parse(self, data_f): """ Parses data file using supplied file object. - :param file: Data file object + :param data_f: Data file object :return: """ - for line in file: + for line in data_f: line = line.strip() - if len(line) == 0: + if not line: continue # Read test name name = line # Check dependencies - deps = [] - line = file.next().strip() - m = re.search('depends_on\:(.*)', line) - if m: - deps = [int(x) for x in m.group(1).split(':')] - line = file.next().strip() + dependencies = [] + line = data_f.next().strip() + match = re.search('depends_on:(.*)', line) + if match: + dependencies = [int(x) for x in match.group(1).split(':')] + line = data_f.next().strip() # Read test vectors line = line.replace('\\n', '\n') parts = self.__escaped_split(line, ':') - function = int(parts[0]) - x = parts[1:] - l = len(x) - if l % 2 != 0: + function_name = int(parts[0]) + args = parts[1:] + args_count = len(args) + if args_count % 2 != 0: raise TestDataParserError("Number of test arguments should " "be even: %s" % line) - args = [(x[i * 2], x[(i * 2) + 1]) for i in range(len(x)/2)] - self.tests.append((name, function, deps, args)) + grouped_args = [(args[i * 2], args[(i * 2) + 1]) + for i in range(len(args)/2)] + self.tests.append((name, function_name, dependencies, + grouped_args)) def get_test_data(self): """ @@ -135,8 +137,8 @@ class TestDataParser(object): class MbedTlsTest(BaseHostTest): """ - Host test for mbedtls unit tests. This script is loaded at - run time by Greentea for executing mbedtls test suites. Each + Host test for Mbed TLS unit tests. This script is loaded at + run time by Greentea for executing Mbed TLS test suites. Each communication from the target is received in this object as an event, which is then handled by the event handler method decorated by the associated event. Ex: @event_callback('GO'). @@ -144,7 +146,7 @@ class MbedTlsTest(BaseHostTest): Target test sends requests for dispatching next test. It reads tests from the intermediate data file and sends test function identifier, dependency identifiers, expression identifiers and - the test data in binary form. Target test checks dependecnies + the test data in binary form. Target test checks dependencies , evaluate integer constant expressions and dispatches the test function with received test parameters. @@ -169,12 +171,18 @@ class MbedTlsTest(BaseHostTest): self.test_index = -1 self.dep_index = 0 self.error_str = dict() - self.error_str[self.DEPENDENCY_SUPPORTED] = 'DEPENDENCY_SUPPORTED' - self.error_str[self.KEY_VALUE_MAPPING_NOT_FOUND] = 'KEY_VALUE_MAPPING_NOT_FOUND' - self.error_str[self.DEPENDENCY_NOT_SUPPORTED] = 'DEPENDENCY_NOT_SUPPORTED' - self.error_str[self.DISPATCH_TEST_FN_NOT_FOUND] = 'DISPATCH_TEST_FN_NOT_FOUND' - self.error_str[self.DISPATCH_INVALID_TEST_DATA] = 'DISPATCH_INVALID_TEST_DATA' - self.error_str[self.DISPATCH_UNSUPPORTED_SUITE] = 'DISPATCH_UNSUPPORTED_SUITE' + self.error_str[self.DEPENDENCY_SUPPORTED] = \ + 'DEPENDENCY_SUPPORTED' + self.error_str[self.KEY_VALUE_MAPPING_NOT_FOUND] = \ + 'KEY_VALUE_MAPPING_NOT_FOUND' + self.error_str[self.DEPENDENCY_NOT_SUPPORTED] = \ + 'DEPENDENCY_NOT_SUPPORTED' + self.error_str[self.DISPATCH_TEST_FN_NOT_FOUND] = \ + 'DISPATCH_TEST_FN_NOT_FOUND' + self.error_str[self.DISPATCH_INVALID_TEST_DATA] = \ + 'DISPATCH_INVALID_TEST_DATA' + self.error_str[self.DISPATCH_UNSUPPORTED_SUITE] = \ + 'DISPATCH_UNSUPPORTED_SUITE' def setup(self): """ @@ -206,13 +214,13 @@ class MbedTlsTest(BaseHostTest): self.log('{{__testcase_name;%s}}' % name) @staticmethod - def align_32bit(b): + def align_32bit(data_bytes): """ 4 byte aligns input byte array. :return: """ - b += bytearray((4 - (len(b))) % 4) + data_bytes += bytearray((4 - (len(data_bytes))) % 4) @staticmethod def hex_str_bytes(hex_str): @@ -230,56 +238,56 @@ class MbedTlsTest(BaseHostTest): raise TestDataParserError("HEX parameter len should be mod of " "2: %s" % hex_str) - b = binascii.unhexlify(hex_str) - return b + data_bytes = binascii.unhexlify(hex_str) + return data_bytes @staticmethod - def int32_to_bigendian_bytes(i): + def int32_to_big_endian_bytes(i): """ - Coverts i to bytearray in big endian format. + Coverts i to byte array in big endian format. :param i: Input integer :return: Output bytes array in big endian or network order """ - b = bytearray([((i >> x) & 0xff) for x in [24, 16, 8, 0]]) - return b + data_bytes = bytearray([((i >> x) & 0xff) for x in [24, 16, 8, 0]]) + return data_bytes - def test_vector_to_bytes(self, function_id, deps, parameters): + def test_vector_to_bytes(self, function_id, dependencies, parameters): """ Converts test vector into a byte array that can be sent to the target. :param function_id: Test Function Identifier - :param deps: Dependency list + :param dependencies: Dependency list :param parameters: Test function input parameters :return: Byte array and its length """ - b = bytearray([len(deps)]) - if len(deps): - b += bytearray(deps) - b += bytearray([function_id, len(parameters)]) + data_bytes = bytearray([len(dependencies)]) + if dependencies: + data_bytes += bytearray(dependencies) + data_bytes += bytearray([function_id, len(parameters)]) for typ, param in parameters: if typ == 'int' or typ == 'exp': i = int(param) - b += 'I' if typ == 'int' else 'E' - self.align_32bit(b) - b += self.int32_to_bigendian_bytes(i) + data_bytes += 'I' if typ == 'int' else 'E' + self.align_32bit(data_bytes) + data_bytes += self.int32_to_big_endian_bytes(i) elif typ == 'char*': param = param.strip('"') i = len(param) + 1 # + 1 for null termination - b += 'S' - self.align_32bit(b) - b += self.int32_to_bigendian_bytes(i) - b += bytearray(list(param)) - b += '\0' # Null terminate + data_bytes += 'S' + self.align_32bit(data_bytes) + data_bytes += self.int32_to_big_endian_bytes(i) + data_bytes += bytearray(list(param)) + data_bytes += '\0' # Null terminate elif typ == 'hex': - hb = self.hex_str_bytes(param) - b += 'H' - self.align_32bit(b) - i = len(hb) - b += self.int32_to_bigendian_bytes(i) - b += hb - length = self.int32_to_bigendian_bytes(len(b)) - return b, length + binary_data = self.hex_str_bytes(param) + data_bytes += 'H' + self.align_32bit(data_bytes) + i = len(binary_data) + data_bytes += self.int32_to_big_endian_bytes(i) + data_bytes += binary_data + length = self.int32_to_big_endian_bytes(len(data_bytes)) + return data_bytes, length def run_next_test(self): """ @@ -289,25 +297,26 @@ class MbedTlsTest(BaseHostTest): self.test_index += 1 self.dep_index = 0 if self.test_index < len(self.tests): - name, function_id, deps, args = self.tests[self.test_index] - self.run_test(name, function_id, deps, args) + name, function_id, dependencies, args = self.tests[self.test_index] + self.run_test(name, function_id, dependencies, args) else: self.notify_complete(True) - def run_test(self, name, function_id, deps, args): + def run_test(self, name, function_id, dependencies, args): """ Execute the test on target by sending next test information. :param name: Test name :param function_id: function identifier - :param deps: Dependencies list + :param dependencies: Dependencies list :param args: test parameters :return: """ self.log("Running: %s" % name) - bytes, length = self.test_vector_to_bytes(function_id, deps, args) - self.send_kv(length, bytes) + param_bytes, length = self.test_vector_to_bytes(function_id, + dependencies, args) + self.send_kv(length, param_bytes) @staticmethod def get_result(value): @@ -319,52 +328,52 @@ class MbedTlsTest(BaseHostTest): try: return int(value) except ValueError: - ValueError("Result should return error number. Instead received %s" % value) + ValueError("Result should return error number. " + "Instead received %s" % value) return 0 @event_callback('GO') - def on_go(self, key, value, timestamp): + def on_go(self, _key, _value, _timestamp): """ Sent by the target to start first test. - :param key: Event key - :param value: Value. ignored - :param timestamp: Timestamp ignored. + :param _key: Event key + :param _value: Value. ignored + :param _timestamp: Timestamp ignored. :return: """ self.run_next_test() @event_callback("R") - def on_result(self, key, value, timestamp): + def on_result(self, _key, value, _timestamp): """ Handle result. Prints test start, finish required by Greentea to detect test execution. - :param key: Event key + :param _key: Event key :param value: Value. ignored - :param timestamp: Timestamp ignored. + :param _timestamp: Timestamp ignored. :return: """ int_val = self.get_result(value) - name, function, deps, args = self.tests[self.test_index] + name, _, _, _ = self.tests[self.test_index] self.log('{{__testcase_start;%s}}' % name) self.log('{{__testcase_finish;%s;%d;%d}}' % (name, int_val == 0, int_val != 0)) self.run_next_test() @event_callback("F") - def on_failure(self, key, value, timestamp): + def on_failure(self, _key, value, _timestamp): """ Handles test execution failure. That means dependency not supported or Test function not supported. Hence marking test as skipped. - :param key: Event key + :param _key: Event key :param value: Value. ignored - :param timestamp: Timestamp ignored. + :param _timestamp: Timestamp ignored. :return: """ int_val = self.get_result(value) - name, function, deps, args = self.tests[self.test_index] if int_val in self.error_str: err = self.error_str[int_val] else: diff --git a/tests/scripts/test_generate_test_code.py b/tests/scripts/test_generate_test_code.py index f1088a32a..f0a935d20 100755 --- a/tests/scripts/test_generate_test_code.py +++ b/tests/scripts/test_generate_test_code.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # Unit test for generate_test_code.py # # Copyright (C) 2018, ARM Limited, All Rights Reserved @@ -16,143 +16,184 @@ # See the License for the specific language governing permissions and # limitations under the License. # -# This file is part of mbed TLS (https://tls.mbed.org) - -from StringIO import StringIO -from unittest import TestCase, main as unittest_main -from mock import patch -from generate_test_code import * - +# This file is part of Mbed TLS (https://tls.mbed.org) """ Unit tests for generate_test_code.py """ +import sys +from StringIO import StringIO +from unittest import TestCase, main as unittest_main +from mock import patch +from generate_test_code import gen_dependencies, gen_dependencies_one_line +from generate_test_code import gen_function_wrapper, gen_dispatch +from generate_test_code import parse_until_pattern, GeneratorInputError +from generate_test_code import parse_suite_dependencies +from generate_test_code import parse_function_dependencies +from generate_test_code import parse_function_signature, parse_function_code +from generate_test_code import parse_functions, END_HEADER_REGEX +from generate_test_code import END_SUITE_HELPERS_REGEX, escaped_split +from generate_test_code import parse_test_data, gen_dep_check +from generate_test_code import gen_expression_check, write_dependencies +from generate_test_code import write_parameters, gen_suite_dep_checks +from generate_test_code import gen_from_test_data + + class GenDep(TestCase): """ Test suite for function gen_dep() """ - def test_deps_list(self): + def test_dependencies_list(self): """ - Test that gen_dep() correctly creates deps for given dependency list. + Test that gen_dep() correctly creates dependencies for given + dependency list. :return: """ - deps = ['DEP1', 'DEP2'] - dep_start, dep_end = gen_deps(deps) - ifdef1, ifdef2 = dep_start.splitlines() + dependencies = ['DEP1', 'DEP2'] + dep_start, dep_end = gen_dependencies(dependencies) + preprocessor1, preprocessor2 = dep_start.splitlines() endif1, endif2 = dep_end.splitlines() - self.assertEqual(ifdef1, '#if defined(DEP1)', 'ifdef generated incorrectly') - self.assertEqual(ifdef2, '#if defined(DEP2)', 'ifdef generated incorrectly') - self.assertEqual(endif1, '#endif /* DEP2 */', 'endif generated incorrectly') - self.assertEqual(endif2, '#endif /* DEP1 */', 'endif generated incorrectly') + self.assertEqual(preprocessor1, '#if defined(DEP1)', + 'Preprocessor generated incorrectly') + self.assertEqual(preprocessor2, '#if defined(DEP2)', + 'Preprocessor generated incorrectly') + self.assertEqual(endif1, '#endif /* DEP2 */', + 'Preprocessor generated incorrectly') + self.assertEqual(endif2, '#endif /* DEP1 */', + 'Preprocessor generated incorrectly') - def test_disabled_deps_list(self): + def test_disabled_dependencies_list(self): """ - Test that gen_dep() correctly creates deps for given dependency list. + Test that gen_dep() correctly creates dependencies for given + dependency list. :return: """ - deps = ['!DEP1', '!DEP2'] - dep_start, dep_end = gen_deps(deps) - ifdef1, ifdef2 = dep_start.splitlines() + dependencies = ['!DEP1', '!DEP2'] + dep_start, dep_end = gen_dependencies(dependencies) + preprocessor1, preprocessor2 = dep_start.splitlines() endif1, endif2 = dep_end.splitlines() - self.assertEqual(ifdef1, '#if !defined(DEP1)', 'ifdef generated incorrectly') - self.assertEqual(ifdef2, '#if !defined(DEP2)', 'ifdef generated incorrectly') - self.assertEqual(endif1, '#endif /* !DEP2 */', 'endif generated incorrectly') - self.assertEqual(endif2, '#endif /* !DEP1 */', 'endif generated incorrectly') + self.assertEqual(preprocessor1, '#if !defined(DEP1)', + 'Preprocessor generated incorrectly') + self.assertEqual(preprocessor2, '#if !defined(DEP2)', + 'Preprocessor generated incorrectly') + self.assertEqual(endif1, '#endif /* !DEP2 */', + 'Preprocessor generated incorrectly') + self.assertEqual(endif2, '#endif /* !DEP1 */', + 'Preprocessor generated incorrectly') - def test_mixed_deps_list(self): + def test_mixed_dependencies_list(self): """ - Test that gen_dep() correctly creates deps for given dependency list. + Test that gen_dep() correctly creates dependencies for given + dependency list. :return: """ - deps = ['!DEP1', 'DEP2'] - dep_start, dep_end = gen_deps(deps) - ifdef1, ifdef2 = dep_start.splitlines() + dependencies = ['!DEP1', 'DEP2'] + dep_start, dep_end = gen_dependencies(dependencies) + preprocessor1, preprocessor2 = dep_start.splitlines() endif1, endif2 = dep_end.splitlines() - self.assertEqual(ifdef1, '#if !defined(DEP1)', 'ifdef generated incorrectly') - self.assertEqual(ifdef2, '#if defined(DEP2)', 'ifdef generated incorrectly') - self.assertEqual(endif1, '#endif /* DEP2 */', 'endif generated incorrectly') - self.assertEqual(endif2, '#endif /* !DEP1 */', 'endif generated incorrectly') + self.assertEqual(preprocessor1, '#if !defined(DEP1)', + 'Preprocessor generated incorrectly') + self.assertEqual(preprocessor2, '#if defined(DEP2)', + 'Preprocessor generated incorrectly') + self.assertEqual(endif1, '#endif /* DEP2 */', + 'Preprocessor generated incorrectly') + self.assertEqual(endif2, '#endif /* !DEP1 */', + 'Preprocessor generated incorrectly') - def test_empty_deps_list(self): + def test_empty_dependencies_list(self): """ - Test that gen_dep() correctly creates deps for given dependency list. + Test that gen_dep() correctly creates dependencies for given + dependency list. :return: """ - deps = [] - dep_start, dep_end = gen_deps(deps) - self.assertEqual(dep_start, '', 'ifdef generated incorrectly') - self.assertEqual(dep_end, '', 'ifdef generated incorrectly') + dependencies = [] + dep_start, dep_end = gen_dependencies(dependencies) + self.assertEqual(dep_start, '', 'Preprocessor generated incorrectly') + self.assertEqual(dep_end, '', 'Preprocessor generated incorrectly') - def test_large_deps_list(self): + def test_large_dependencies_list(self): """ - Test that gen_dep() correctly creates deps for given dependency list. + Test that gen_dep() correctly creates dependencies for given + dependency list. :return: """ - deps = [] + dependencies = [] count = 10 for i in range(count): - deps.append('DEP%d' % i) - dep_start, dep_end = gen_deps(deps) - self.assertEqual(len(dep_start.splitlines()), count, 'ifdef generated incorrectly') - self.assertEqual(len(dep_end.splitlines()), count, 'ifdef generated incorrectly') + dependencies.append('DEP%d' % i) + dep_start, dep_end = gen_dependencies(dependencies) + self.assertEqual(len(dep_start.splitlines()), count, + 'Preprocessor generated incorrectly') + self.assertEqual(len(dep_end.splitlines()), count, + 'Preprocessor generated incorrectly') class GenDepOneLine(TestCase): """ - Test Suite for testing gen_deps_one_line() + Test Suite for testing gen_dependencies_one_line() """ - def test_deps_list(self): + def test_dependencies_list(self): """ - Test that gen_dep() correctly creates deps for given dependency list. + Test that gen_dep() correctly creates dependencies for given + dependency list. :return: """ - deps = ['DEP1', 'DEP2'] - dep_str = gen_deps_one_line(deps) - self.assertEqual(dep_str, '#if defined(DEP1) && defined(DEP2)', 'ifdef generated incorrectly') + dependencies = ['DEP1', 'DEP2'] + dep_str = gen_dependencies_one_line(dependencies) + self.assertEqual(dep_str, '#if defined(DEP1) && defined(DEP2)', + 'Preprocessor generated incorrectly') - def test_disabled_deps_list(self): + def test_disabled_dependencies_list(self): """ - Test that gen_dep() correctly creates deps for given dependency list. + Test that gen_dep() correctly creates dependencies for given + dependency list. :return: """ - deps = ['!DEP1', '!DEP2'] - dep_str = gen_deps_one_line(deps) - self.assertEqual(dep_str, '#if !defined(DEP1) && !defined(DEP2)', 'ifdef generated incorrectly') + dependencies = ['!DEP1', '!DEP2'] + dep_str = gen_dependencies_one_line(dependencies) + self.assertEqual(dep_str, '#if !defined(DEP1) && !defined(DEP2)', + 'Preprocessor generated incorrectly') - def test_mixed_deps_list(self): + def test_mixed_dependencies_list(self): """ - Test that gen_dep() correctly creates deps for given dependency list. + Test that gen_dep() correctly creates dependencies for given + dependency list. :return: """ - deps = ['!DEP1', 'DEP2'] - dep_str = gen_deps_one_line(deps) - self.assertEqual(dep_str, '#if !defined(DEP1) && defined(DEP2)', 'ifdef generated incorrectly') + dependencies = ['!DEP1', 'DEP2'] + dep_str = gen_dependencies_one_line(dependencies) + self.assertEqual(dep_str, '#if !defined(DEP1) && defined(DEP2)', + 'Preprocessor generated incorrectly') - def test_empty_deps_list(self): + def test_empty_dependencies_list(self): """ - Test that gen_dep() correctly creates deps for given dependency list. + Test that gen_dep() correctly creates dependencies for given + dependency list. :return: """ - deps = [] - dep_str = gen_deps_one_line(deps) - self.assertEqual(dep_str, '', 'ifdef generated incorrectly') + dependencies = [] + dep_str = gen_dependencies_one_line(dependencies) + self.assertEqual(dep_str, '', 'Preprocessor generated incorrectly') - def test_large_deps_list(self): + def test_large_dependencies_list(self): """ - Test that gen_dep() correctly creates deps for given dependency list. + Test that gen_dep() correctly creates dependencies for given + dependency list. :return: """ - deps = [] + dependencies = [] count = 10 for i in range(count): - deps.append('DEP%d' % i) - dep_str = gen_deps_one_line(deps) - expected = '#if ' + ' && '.join(['defined(%s)' % x for x in deps]) - self.assertEqual(dep_str, expected, 'ifdef generated incorrectly') + dependencies.append('DEP%d' % i) + dep_str = gen_dependencies_one_line(dependencies) + expected = '#if ' + ' && '.join(['defined(%s)' % + x for x in dependencies]) + self.assertEqual(dep_str, expected, + 'Preprocessor generated incorrectly') class GenFunctionWrapper(TestCase): @@ -182,7 +223,8 @@ void test_a_wrapper( void ** params ) :return: """ - code = gen_function_wrapper('test_a', 'int x = 1;', ('x', 'b', 'c', 'd')) + code = gen_function_wrapper('test_a', + 'int x = 1;', ('x', 'b', 'c', 'd')) expected = ''' void test_a_wrapper( void ** params ) { @@ -230,7 +272,7 @@ class GenDispatch(TestCase): ''' self.assertEqual(code, expected) - def test_empty_deps(self): + def test_empty_dependencies(self): """ Test empty dependency list. :return: @@ -246,7 +288,7 @@ class StringIOWrapper(StringIO, object): """ file like class to mock file object in tests. """ - def __init__(self, file_name, data, line_no = 1): + def __init__(self, file_name, data, line_no=1): """ Init file handle. @@ -260,17 +302,28 @@ class StringIOWrapper(StringIO, object): def next(self): """ - Iterator return impl. - :return: - """ - line = super(StringIOWrapper, self).next() - return line + Iterator method. This method overrides base class's + next method and extends the next method to count the line + numbers as each line is read. - def readline(self, limit=0): + :return: Line read from file. + """ + parent = super(StringIOWrapper, self) + line = parent.next() # Python 2 + if line: + self.line_no += 1 + # Convert byte array to string with correct encoding and + # strip any whitespaces added in the decoding process. + return line.decode(sys.getdefaultencoding()).strip() + "\n" + return None + + __next__ = next + + def readline(self, length=0): """ Wrap the base class readline. - :param limit: + :param length: :return: """ line = super(StringIOWrapper, self).readline() @@ -300,8 +353,8 @@ class ParseUntilPattern(TestCase): #define ECP_PF_UNKNOWN -1 ''' - s = StringIOWrapper('test_suite_ut.function', data, line_no=0) - headers = parse_until_pattern(s, END_HEADER_REGEX) + stream = StringIOWrapper('test_suite_ut.function', data, line_no=0) + headers = parse_until_pattern(stream, END_HEADER_REGEX) self.assertEqual(headers, expected) def test_line_no(self): @@ -321,13 +374,15 @@ class ParseUntilPattern(TestCase): #define ECP_PF_UNKNOWN -1 ''' % (offset_line_no + 1) - s = StringIOWrapper('test_suite_ut.function', data, offset_line_no) - headers = parse_until_pattern(s, END_HEADER_REGEX) + stream = StringIOWrapper('test_suite_ut.function', data, + offset_line_no) + headers = parse_until_pattern(stream, END_HEADER_REGEX) self.assertEqual(headers, expected) def test_no_end_header_comment(self): """ - Test that InvalidFileFormat is raised when end header comment is missing. + Test that InvalidFileFormat is raised when end header comment is + missing. :return: """ data = '''#include "mbedtls/ecp.h" @@ -335,16 +390,17 @@ class ParseUntilPattern(TestCase): #define ECP_PF_UNKNOWN -1 ''' - s = StringIOWrapper('test_suite_ut.function', data) - self.assertRaises(InvalidFileFormat, parse_until_pattern, s, END_HEADER_REGEX) + stream = StringIOWrapper('test_suite_ut.function', data) + self.assertRaises(GeneratorInputError, parse_until_pattern, stream, + END_HEADER_REGEX) -class ParseSuiteDeps(TestCase): +class ParseSuiteDependencies(TestCase): """ - Test Suite for testing parse_suite_deps(). + Test Suite for testing parse_suite_dependencies(). """ - def test_suite_deps(self): + def test_suite_dependencies(self): """ :return: @@ -355,9 +411,9 @@ class ParseSuiteDeps(TestCase): */ ''' expected = ['MBEDTLS_ECP_C'] - s = StringIOWrapper('test_suite_ut.function', data) - deps = parse_suite_deps(s) - self.assertEqual(deps, expected) + stream = StringIOWrapper('test_suite_ut.function', data) + dependencies = parse_suite_dependencies(stream) + self.assertEqual(dependencies, expected) def test_no_end_dep_comment(self): """ @@ -367,10 +423,11 @@ class ParseSuiteDeps(TestCase): data = ''' * depends_on:MBEDTLS_ECP_C ''' - s = StringIOWrapper('test_suite_ut.function', data) - self.assertRaises(InvalidFileFormat, parse_suite_deps, s) + stream = StringIOWrapper('test_suite_ut.function', data) + self.assertRaises(GeneratorInputError, parse_suite_dependencies, + stream) - def test_deps_split(self): + def test_dependencies_split(self): """ Test that InvalidFileFormat is raised when end dep comment is missing. :return: @@ -381,43 +438,47 @@ class ParseSuiteDeps(TestCase): */ ''' expected = ['MBEDTLS_ECP_C', 'A', 'B', 'C', 'D', 'F', 'G', '!H'] - s = StringIOWrapper('test_suite_ut.function', data) - deps = parse_suite_deps(s) - self.assertEqual(deps, expected) + stream = StringIOWrapper('test_suite_ut.function', data) + dependencies = parse_suite_dependencies(stream) + self.assertEqual(dependencies, expected) -class ParseFuncDeps(TestCase): +class ParseFuncDependencies(TestCase): """ - Test Suite for testing parse_function_deps() + Test Suite for testing parse_function_dependencies() """ - def test_function_deps(self): + def test_function_dependencies(self): """ - Test that parse_function_deps() correctly parses function dependencies. + Test that parse_function_dependencies() correctly parses function + dependencies. :return: """ - line = '/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */' + line = '/* BEGIN_CASE ' \ + 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */' expected = ['MBEDTLS_ENTROPY_NV_SEED', 'MBEDTLS_FS_IO'] - deps = parse_function_deps(line) - self.assertEqual(deps, expected) + dependencies = parse_function_dependencies(line) + self.assertEqual(dependencies, expected) - def test_no_deps(self): + def test_no_dependencies(self): """ - Test that parse_function_deps() correctly parses function dependencies. + Test that parse_function_dependencies() correctly parses function + dependencies. :return: """ line = '/* BEGIN_CASE */' - deps = parse_function_deps(line) - self.assertEqual(deps, []) + dependencies = parse_function_dependencies(line) + self.assertEqual(dependencies, []) - def test_poorly_defined_deps(self): + def test_tolerance(self): """ - Test that parse_function_deps() correctly parses function dependencies. + Test that parse_function_dependencies() correctly parses function + dependencies. :return: """ line = '/* BEGIN_CASE depends_on:MBEDTLS_FS_IO: A : !B:C : F*/' - deps = parse_function_deps(line) - self.assertEqual(deps, ['MBEDTLS_FS_IO', 'A', '!B', 'C', 'F']) + dependencies = parse_function_dependencies(line) + self.assertEqual(dependencies, ['MBEDTLS_FS_IO', 'A', '!B', 'C', 'F']) class ParseFuncSignature(TestCase): @@ -435,7 +496,9 @@ class ParseFuncSignature(TestCase): self.assertEqual(name, 'entropy_threshold') self.assertEqual(args, ['char*', 'int', 'int']) self.assertEqual(local, '') - self.assertEqual(arg_dispatch, ['(char *) params[0]', '*( (int *) params[1] )', '*( (int *) params[2] )']) + self.assertEqual(arg_dispatch, ['(char *) params[0]', + '*( (int *) params[1] )', + '*( (int *) params[2] )']) def test_hex_params(self): """ @@ -446,8 +509,12 @@ class ParseFuncSignature(TestCase): name, args, local, arg_dispatch = parse_function_signature(line) self.assertEqual(name, 'entropy_threshold') self.assertEqual(args, ['char*', 'hex', 'int']) - self.assertEqual(local, ' data_t hex1 = {(uint8_t *) params[1], *( (uint32_t *) params[2] )};\n') - self.assertEqual(arg_dispatch, ['(char *) params[0]', '&hex1', '*( (int *) params[3] )']) + self.assertEqual(local, + ' data_t hex1 = {(uint8_t *) params[1], ' + '*( (uint32_t *) params[2] )};\n') + self.assertEqual(arg_dispatch, ['(char *) params[0]', + '&hex1', + '*( (int *) params[3] )']) def test_non_void_function(self): """ @@ -493,8 +560,9 @@ No test function ''' - s = StringIOWrapper('test_suite_ut.function', data) - self.assertRaises(InvalidFileFormat, parse_function_code, s, [], []) + stream = StringIOWrapper('test_suite_ut.function', data) + self.assertRaises(GeneratorInputError, parse_function_code, stream, [], + []) def test_no_end_case_comment(self): """ @@ -506,11 +574,13 @@ void test_func() { } ''' - s = StringIOWrapper('test_suite_ut.function', data) - self.assertRaises(InvalidFileFormat, parse_function_code, s, [], []) + stream = StringIOWrapper('test_suite_ut.function', data) + self.assertRaises(GeneratorInputError, parse_function_code, stream, [], + []) @patch("generate_test_code.parse_function_signature") - def test_parse_function_signature_called(self, parse_function_signature_mock): + def test_function_called(self, + parse_function_signature_mock): """ Test parse_function_code() :return: @@ -521,26 +591,27 @@ void test_func() { } ''' - s = StringIOWrapper('test_suite_ut.function', data) - self.assertRaises(InvalidFileFormat, parse_function_code, s, [], []) + stream = StringIOWrapper('test_suite_ut.function', data) + self.assertRaises(GeneratorInputError, parse_function_code, + stream, [], []) self.assertTrue(parse_function_signature_mock.called) parse_function_signature_mock.assert_called_with('void test_func()\n') @patch("generate_test_code.gen_dispatch") - @patch("generate_test_code.gen_deps") + @patch("generate_test_code.gen_dependencies") @patch("generate_test_code.gen_function_wrapper") @patch("generate_test_code.parse_function_signature") def test_return(self, parse_function_signature_mock, - gen_function_wrapper_mock, - gen_deps_mock, - gen_dispatch_mock): + gen_function_wrapper_mock, + gen_dependencies_mock, + gen_dispatch_mock): """ Test generated code. :return: """ parse_function_signature_mock.return_value = ('func', [], '', []) gen_function_wrapper_mock.return_value = '' - gen_deps_mock.side_effect = gen_deps + gen_dependencies_mock.side_effect = gen_dependencies gen_dispatch_mock.side_effect = gen_dispatch data = ''' void func() @@ -550,10 +621,9 @@ void func() } /* END_CASE */ ''' - s = StringIOWrapper('test_suite_ut.function', data) - name, arg, code, dispatch_code = parse_function_code(s, [], []) + stream = StringIOWrapper('test_suite_ut.function', data) + name, arg, code, dispatch_code = parse_function_code(stream, [], []) - #self.assertRaises(InvalidFileFormat, parse_function_code, s, [], []) self.assertTrue(parse_function_signature_mock.called) parse_function_signature_mock.assert_called_with('void func()\n') gen_function_wrapper_mock.assert_called_with('test_func', '', []) @@ -572,20 +642,20 @@ exit: self.assertEqual(dispatch_code, "\n test_func_wrapper,\n") @patch("generate_test_code.gen_dispatch") - @patch("generate_test_code.gen_deps") + @patch("generate_test_code.gen_dependencies") @patch("generate_test_code.gen_function_wrapper") @patch("generate_test_code.parse_function_signature") def test_with_exit_label(self, parse_function_signature_mock, - gen_function_wrapper_mock, - gen_deps_mock, - gen_dispatch_mock): + gen_function_wrapper_mock, + gen_dependencies_mock, + gen_dispatch_mock): """ Test when exit label is present. :return: """ parse_function_signature_mock.return_value = ('func', [], '', []) gen_function_wrapper_mock.return_value = '' - gen_deps_mock.side_effect = gen_deps + gen_dependencies_mock.side_effect = gen_dependencies gen_dispatch_mock.side_effect = gen_dispatch data = ''' void func() @@ -598,8 +668,8 @@ exit: } /* END_CASE */ ''' - s = StringIOWrapper('test_suite_ut.function', data) - name, arg, code, dispatch_code = parse_function_code(s, [], []) + stream = StringIOWrapper('test_suite_ut.function', data) + _, _, code, _ = parse_function_code(stream, [], []) expected = '''#line 2 "test_suite_ut.function" void test_func() @@ -625,7 +695,8 @@ class ParseFunction(TestCase): Test that begin header is checked and parse_until_pattern() is called. :return: """ - def stop(this): + def stop(*_unused): + """Stop when parse_until_pattern is called.""" raise Exception parse_until_pattern_mock.side_effect = stop data = '''/* BEGIN_HEADER */ @@ -634,10 +705,10 @@ class ParseFunction(TestCase): #define ECP_PF_UNKNOWN -1 /* END_HEADER */ ''' - s = StringIOWrapper('test_suite_ut.function', data) - self.assertRaises(Exception, parse_functions, s) - parse_until_pattern_mock.assert_called_with(s, END_HEADER_REGEX) - self.assertEqual(s.line_no, 2) + stream = StringIOWrapper('test_suite_ut.function', data) + self.assertRaises(Exception, parse_functions, stream) + parse_until_pattern_mock.assert_called_with(stream, END_HEADER_REGEX) + self.assertEqual(stream.line_no, 2) @patch("generate_test_code.parse_until_pattern") def test_begin_helper(self, parse_until_pattern_mock): @@ -645,89 +716,97 @@ class ParseFunction(TestCase): Test that begin helper is checked and parse_until_pattern() is called. :return: """ - def stop(this): + def stop(*_unused): + """Stop when parse_until_pattern is called.""" raise Exception parse_until_pattern_mock.side_effect = stop data = '''/* BEGIN_SUITE_HELPERS */ -void print_helloworld() +void print_hello_world() { - printf ("Hello World!\n"); + 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) + stream = StringIOWrapper('test_suite_ut.function', data) + self.assertRaises(Exception, parse_functions, stream) + parse_until_pattern_mock.assert_called_with(stream, + END_SUITE_HELPERS_REGEX) + self.assertEqual(stream.line_no, 2) - @patch("generate_test_code.parse_suite_deps") - def test_begin_dep(self, parse_suite_deps_mock): + @patch("generate_test_code.parse_suite_dependencies") + def test_begin_dep(self, parse_suite_dependencies_mock): """ - Test that begin dep is checked and parse_suite_deps() is called. + Test that begin dep is checked and parse_suite_dependencies() is + called. :return: """ - def stop(this): + def stop(*_unused): + """Stop when parse_until_pattern is called.""" raise Exception - parse_suite_deps_mock.side_effect = stop + parse_suite_dependencies_mock.side_effect = stop data = '''/* BEGIN_DEPENDENCIES * depends_on:MBEDTLS_ECP_C * END_DEPENDENCIES */ ''' - s = StringIOWrapper('test_suite_ut.function', data) - self.assertRaises(Exception, parse_functions, s) - parse_suite_deps_mock.assert_called_with(s) - self.assertEqual(s.line_no, 2) + stream = StringIOWrapper('test_suite_ut.function', data) + self.assertRaises(Exception, parse_functions, stream) + parse_suite_dependencies_mock.assert_called_with(stream) + self.assertEqual(stream.line_no, 2) - @patch("generate_test_code.parse_function_deps") - def test_begin_function_dep(self, parse_function_deps_mock): + @patch("generate_test_code.parse_function_dependencies") + def test_begin_function_dep(self, func_mock): """ - Test that begin dep is checked and parse_function_deps() is called. + Test that begin dep is checked and parse_function_dependencies() is + called. :return: """ - def stop(this): + def stop(*_unused): + """Stop when parse_until_pattern is called.""" raise Exception - parse_function_deps_mock.side_effect = stop + func_mock.side_effect = stop - deps_str = '/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n' + dependencies_str = '/* BEGIN_CASE ' \ + 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n' data = '''%svoid test_func() { } -''' % deps_str - s = StringIOWrapper('test_suite_ut.function', data) - self.assertRaises(Exception, parse_functions, s) - parse_function_deps_mock.assert_called_with(deps_str) - self.assertEqual(s.line_no, 2) +''' % dependencies_str + stream = StringIOWrapper('test_suite_ut.function', data) + self.assertRaises(Exception, parse_functions, stream) + func_mock.assert_called_with(dependencies_str) + self.assertEqual(stream.line_no, 2) @patch("generate_test_code.parse_function_code") - @patch("generate_test_code.parse_function_deps") - def test_return(self, parse_function_deps_mock, parse_function_code_mock): + @patch("generate_test_code.parse_function_dependencies") + def test_return(self, func_mock1, func_mock2): """ Test that begin case is checked and parse_function_code() is called. :return: """ - def stop(this): - raise Exception - parse_function_deps_mock.return_value = [] - in_func_code= '''void test_func() + func_mock1.return_value = [] + in_func_code = '''void test_func() { } ''' func_dispatch = ''' test_func_wrapper, ''' - parse_function_code_mock.return_value = 'test_func', [], in_func_code, func_dispatch - deps_str = '/* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n' + func_mock2.return_value = 'test_func', [],\ + in_func_code, func_dispatch + dependencies_str = '/* BEGIN_CASE ' \ + 'depends_on:MBEDTLS_ENTROPY_NV_SEED:MBEDTLS_FS_IO */\n' data = '''%svoid test_func() { } -''' % deps_str - s = StringIOWrapper('test_suite_ut.function', data) - suite_deps, dispatch_code, func_code, func_info = parse_functions(s) - parse_function_deps_mock.assert_called_with(deps_str) - parse_function_code_mock.assert_called_with(s, [], []) - self.assertEqual(s.line_no, 5) - self.assertEqual(suite_deps, []) +''' % dependencies_str + stream = StringIOWrapper('test_suite_ut.function', data) + suite_dependencies, dispatch_code, func_code, func_info = \ + parse_functions(stream) + func_mock1.assert_called_with(dependencies_str) + func_mock2.assert_called_with(stream, [], []) + self.assertEqual(stream.line_no, 5) + self.assertEqual(suite_dependencies, []) expected_dispatch_code = '''/* Function Id: 0 */ test_func_wrapper, @@ -764,10 +843,11 @@ void func2() } /* END_CASE */ ''' - s = StringIOWrapper('test_suite_ut.function', data) - suite_deps, dispatch_code, func_code, func_info = parse_functions(s) - self.assertEqual(s.line_no, 23) - self.assertEqual(suite_deps, ['MBEDTLS_ECP_C']) + stream = StringIOWrapper('test_suite_ut.function', data) + suite_dependencies, dispatch_code, func_code, func_info = \ + parse_functions(stream) + self.assertEqual(stream.line_no, 23) + self.assertEqual(suite_dependencies, ['MBEDTLS_ECP_C']) expected_dispatch_code = '''/* Function Id: 0 */ @@ -827,7 +907,8 @@ void test_func2_wrapper( void ** params ) #endif /* MBEDTLS_ECP_C */ ''' self.assertEqual(func_code, expected_func_code) - self.assertEqual(func_info, {'test_func1': (0, []), 'test_func2': (1, [])}) + self.assertEqual(func_info, {'test_func1': (0, []), + 'test_func2': (1, [])}) def test_same_function_name(self): """ @@ -857,15 +938,16 @@ void func() } /* END_CASE */ ''' - s = StringIOWrapper('test_suite_ut.function', data) - self.assertRaises(GeneratorInputError, parse_functions, s) + stream = StringIOWrapper('test_suite_ut.function', data) + self.assertRaises(GeneratorInputError, parse_functions, stream) -class ExcapedSplit(TestCase): +class EscapedSplit(TestCase): """ Test suite for testing escaped_split(). - Note: Since escaped_split() output is used to write back to the intermediate data file. Any escape characters - in the input are retained in the output. + Note: Since escaped_split() output is used to write back to the + intermediate data file. Any escape characters in the input are + retained in the output. """ def test_invalid_input(self): @@ -877,7 +959,7 @@ class ExcapedSplit(TestCase): def test_empty_string(self): """ - Test empty strig input. + Test empty string input. :return: """ splits = escaped_split('', ':') @@ -885,39 +967,42 @@ class ExcapedSplit(TestCase): def test_no_escape(self): """ - Test with no escape character. The behaviour should be same as str.split() + Test with no escape character. The behaviour should be same as + str.split() :return: """ - s = 'yahoo:google' - splits = escaped_split(s, ':') - self.assertEqual(splits, s.split(':')) + test_str = 'yahoo:google' + splits = escaped_split(test_str, ':') + self.assertEqual(splits, test_str.split(':')) def test_escaped_input(self): """ - Test imput that has escaped delimiter. + Test input that has escaped delimiter. :return: """ - s = 'yahoo\:google:facebook' - splits = escaped_split(s, ':') - self.assertEqual(splits, ['yahoo\:google', 'facebook']) + test_str = r'yahoo\:google:facebook' + splits = escaped_split(test_str, ':') + self.assertEqual(splits, [r'yahoo\:google', 'facebook']) def test_escaped_escape(self): """ - Test imput that has escaped delimiter. + Test input that has escaped delimiter. :return: """ - s = 'yahoo\\\:google:facebook' - splits = escaped_split(s, ':') - self.assertEqual(splits, ['yahoo\\\\', 'google', 'facebook']) + test_str = r'yahoo\\\:google:facebook' + splits = escaped_split(test_str, ':') + self.assertEqual(splits, [r'yahoo\\\\', 'google', 'facebook']) def test_all_at_once(self): """ - Test imput that has escaped delimiter. + Test input that has escaped delimiter. :return: """ - s = 'yahoo\\\:google:facebook\:instagram\\\:bbc\\\\:wikipedia' - splits = escaped_split(s, ':') - self.assertEqual(splits, ['yahoo\\\\', 'google', 'facebook\:instagram\\\\', 'bbc\\\\', 'wikipedia']) + test_str = r'yahoo\\\:google:facebook\:instagram\\\:bbc\\\\:wikipedia' + splits = escaped_split(test_str, ':') + self.assertEqual(splits, [r'yahoo\\\\', r'google', + r'facebook\:instagram\\\\', + r'bbc\\\\', r'wikipedia']) class ParseTestData(TestCase): @@ -943,28 +1028,34 @@ dhm_do_dhm:10:"9345098382739712938719287391879381271":10:"9345098792137312973297 Diffie-Hellman selftest dhm_selftest: """ - s = StringIOWrapper('test_suite_ut.function', data) - tests = [(name, function, deps, args) for name, function, deps, args in parse_test_data(s)] - t1, t2, t3, t4 = tests - self.assertEqual(t1[0], 'Diffie-Hellman full exchange #1') - self.assertEqual(t1[1], 'dhm_do_dhm') - self.assertEqual(t1[2], []) - self.assertEqual(t1[3], ['10', '"23"', '10', '"5"']) + stream = StringIOWrapper('test_suite_ut.function', data) + tests = [(name, test_function, dependencies, args) + for name, test_function, dependencies, args in + parse_test_data(stream)] + test1, test2, test3, test4 = tests + self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1') + self.assertEqual(test1[1], 'dhm_do_dhm') + self.assertEqual(test1[2], []) + self.assertEqual(test1[3], ['10', '"23"', '10', '"5"']) - self.assertEqual(t2[0], 'Diffie-Hellman full exchange #2') - self.assertEqual(t2[1], 'dhm_do_dhm') - self.assertEqual(t2[2], []) - self.assertEqual(t2[3], ['10', '"93450983094850938450983409623"', '10', '"9345098304850938450983409622"']) + self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2') + self.assertEqual(test2[1], 'dhm_do_dhm') + self.assertEqual(test2[2], []) + self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"', + '10', '"9345098304850938450983409622"']) - self.assertEqual(t3[0], 'Diffie-Hellman full exchange #3') - self.assertEqual(t3[1], 'dhm_do_dhm') - self.assertEqual(t3[2], []) - self.assertEqual(t3[3], ['10', '"9345098382739712938719287391879381271"', '10', '"9345098792137312973297123912791271"']) + self.assertEqual(test3[0], 'Diffie-Hellman full exchange #3') + self.assertEqual(test3[1], 'dhm_do_dhm') + self.assertEqual(test3[2], []) + self.assertEqual(test3[3], ['10', + '"9345098382739712938719287391879381271"', + '10', + '"9345098792137312973297123912791271"']) - self.assertEqual(t4[0], 'Diffie-Hellman selftest') - self.assertEqual(t4[1], 'dhm_selftest') - self.assertEqual(t4[2], []) - self.assertEqual(t4[3], []) + self.assertEqual(test4[0], 'Diffie-Hellman selftest') + self.assertEqual(test4[1], 'dhm_selftest') + self.assertEqual(test4[2], []) + self.assertEqual(test4[3], []) def test_with_dependencies(self): """ @@ -980,22 +1071,26 @@ Diffie-Hellman full exchange #2 dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622" """ - s = StringIOWrapper('test_suite_ut.function', data) - tests = [(name, function, deps, args) for name, function, deps, args in parse_test_data(s)] - t1, t2 = tests - self.assertEqual(t1[0], 'Diffie-Hellman full exchange #1') - self.assertEqual(t1[1], 'dhm_do_dhm') - self.assertEqual(t1[2], ['YAHOO']) - self.assertEqual(t1[3], ['10', '"23"', '10', '"5"']) + stream = StringIOWrapper('test_suite_ut.function', data) + tests = [(name, function_name, dependencies, args) + for name, function_name, dependencies, args in + parse_test_data(stream)] + test1, test2 = tests + self.assertEqual(test1[0], 'Diffie-Hellman full exchange #1') + self.assertEqual(test1[1], 'dhm_do_dhm') + self.assertEqual(test1[2], ['YAHOO']) + self.assertEqual(test1[3], ['10', '"23"', '10', '"5"']) - self.assertEqual(t2[0], 'Diffie-Hellman full exchange #2') - self.assertEqual(t2[1], 'dhm_do_dhm') - self.assertEqual(t2[2], []) - self.assertEqual(t2[3], ['10', '"93450983094850938450983409623"', '10', '"9345098304850938450983409622"']) + self.assertEqual(test2[0], 'Diffie-Hellman full exchange #2') + self.assertEqual(test2[1], 'dhm_do_dhm') + self.assertEqual(test2[2], []) + self.assertEqual(test2[3], ['10', '"93450983094850938450983409623"', + '10', '"9345098304850938450983409622"']) def test_no_args(self): """ - Test GeneratorInputError 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 = """ @@ -1007,37 +1102,39 @@ Diffie-Hellman full exchange #2 dhm_do_dhm:10:"93450983094850938450983409623":10:"9345098304850938450983409622" """ - s = StringIOWrapper('test_suite_ut.function', data) - e = None + stream = StringIOWrapper('test_suite_ut.function', data) + err = None try: - for x, y, z, a in parse_test_data(s): + for _, _, _, _ in parse_test_data(stream): pass - except GeneratorInputError as e: + except GeneratorInputError as err: pass - self.assertEqual(type(e), GeneratorInputError) + self.assertEqual(type(err), GeneratorInputError) def test_incomplete_data(self): """ - Test GeneratorInputError 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 = """ Diffie-Hellman full exchange #1 depends_on:YAHOO """ - s = StringIOWrapper('test_suite_ut.function', data) - e = None + stream = StringIOWrapper('test_suite_ut.function', data) + err = None try: - for x, y, z, a in parse_test_data(s): + for _, _, _, _ in parse_test_data(stream): pass - except GeneratorInputError as e: + except GeneratorInputError as err: pass - self.assertEqual(type(e), GeneratorInputError) + self.assertEqual(type(err), GeneratorInputError) class GenDepCheck(TestCase): """ - Test suite for gen_dep_check(). It is assumed this function is called with valid inputs. + Test suite for gen_dep_check(). It is assumed this function is + called with valid inputs. """ def test_gen_dep_check(self): @@ -1058,7 +1155,7 @@ class GenDepCheck(TestCase): out = gen_dep_check(5, 'YAHOO') self.assertEqual(out, expected) - def test_noT(self): + def test_not_defined_dependency(self): """ Test dependency with !. :return: @@ -1093,7 +1190,8 @@ class GenDepCheck(TestCase): class GenExpCheck(TestCase): """ - Test suite for gen_expression_check(). It is assumed this function is called with valid inputs. + Test suite for gen_expression_check(). It is assumed this function + is called with valid inputs. """ def test_gen_exp_check(self): @@ -1122,34 +1220,36 @@ class GenExpCheck(TestCase): Test invalid expression id. :return: """ - self.assertRaises(GeneratorInputError, gen_expression_check, -1, 'YAHOO') + self.assertRaises(GeneratorInputError, gen_expression_check, + -1, 'YAHOO') -class WriteDeps(TestCase): +class WriteDependencies(TestCase): """ - Test suite for testing write_deps. + Test suite for testing write_dependencies. """ - def test_no_test_deps(self): + def test_no_test_dependencies(self): """ - Test when test_deps is empty. + Test when test dependencies input is empty. :return: """ - s = StringIOWrapper('test_suite_ut.data', '') - unique_deps = [] - dep_check_code = write_deps(s, [], unique_deps) + stream = StringIOWrapper('test_suite_ut.data', '') + unique_dependencies = [] + dep_check_code = write_dependencies(stream, [], unique_dependencies) self.assertEqual(dep_check_code, '') - self.assertEqual(len(unique_deps), 0) - self.assertEqual(s.getvalue(), '') + self.assertEqual(len(unique_dependencies), 0) + self.assertEqual(stream.getvalue(), '') def test_unique_dep_ids(self): """ :return: """ - s = StringIOWrapper('test_suite_ut.data', '') - unique_deps = [] - dep_check_code = write_deps(s, ['DEP3', 'DEP2', 'DEP1'], unique_deps) + stream = StringIOWrapper('test_suite_ut.data', '') + unique_dependencies = [] + dep_check_code = write_dependencies(stream, ['DEP3', 'DEP2', 'DEP1'], + unique_dependencies) expect_dep_check_code = ''' case 0: { @@ -1179,20 +1279,23 @@ class WriteDeps(TestCase): } break;''' self.assertEqual(dep_check_code, expect_dep_check_code) - self.assertEqual(len(unique_deps), 3) - self.assertEqual(s.getvalue(), 'depends_on:0:1:2\n') + self.assertEqual(len(unique_dependencies), 3) + self.assertEqual(stream.getvalue(), 'depends_on:0:1:2\n') def test_dep_id_repeat(self): """ :return: """ - s = StringIOWrapper('test_suite_ut.data', '') - unique_deps = [] + stream = StringIOWrapper('test_suite_ut.data', '') + unique_dependencies = [] dep_check_code = '' - dep_check_code += write_deps(s, ['DEP3', 'DEP2'], unique_deps) - dep_check_code += write_deps(s, ['DEP2', 'DEP1'], unique_deps) - dep_check_code += write_deps(s, ['DEP1', 'DEP3'], unique_deps) + dep_check_code += write_dependencies(stream, ['DEP3', 'DEP2'], + unique_dependencies) + dep_check_code += write_dependencies(stream, ['DEP2', 'DEP1'], + unique_dependencies) + dep_check_code += write_dependencies(stream, ['DEP1', 'DEP3'], + unique_dependencies) expect_dep_check_code = ''' case 0: { @@ -1222,8 +1325,9 @@ class WriteDeps(TestCase): } break;''' self.assertEqual(dep_check_code, expect_dep_check_code) - self.assertEqual(len(unique_deps), 3) - self.assertEqual(s.getvalue(), 'depends_on:0:1\ndepends_on:1:2\ndepends_on:2:0\n') + self.assertEqual(len(unique_dependencies), 3) + self.assertEqual(stream.getvalue(), + 'depends_on:0:1\ndepends_on:1:2\ndepends_on:2:0\n') class WriteParams(TestCase): @@ -1236,48 +1340,57 @@ class WriteParams(TestCase): Test with empty test_args :return: """ - s = StringIOWrapper('test_suite_ut.data', '') + stream = StringIOWrapper('test_suite_ut.data', '') unique_expressions = [] - expression_code = write_parameters(s, [], [], unique_expressions) + expression_code = write_parameters(stream, [], [], unique_expressions) self.assertEqual(len(unique_expressions), 0) self.assertEqual(expression_code, '') - self.assertEqual(s.getvalue(), '\n') + self.assertEqual(stream.getvalue(), '\n') def test_no_exp_param(self): """ Test when there is no macro or expression in the params. :return: """ - s = StringIOWrapper('test_suite_ut.data', '') + stream = StringIOWrapper('test_suite_ut.data', '') unique_expressions = [] - expression_code = write_parameters(s, ['"Yahoo"', '"abcdef00"', '0'], ['char*', 'hex', 'int'], + expression_code = write_parameters(stream, ['"Yahoo"', '"abcdef00"', + '0'], + ['char*', 'hex', 'int'], unique_expressions) self.assertEqual(len(unique_expressions), 0) self.assertEqual(expression_code, '') - self.assertEqual(s.getvalue(), ':char*:"Yahoo":hex:"abcdef00":int:0\n') + self.assertEqual(stream.getvalue(), + ':char*:"Yahoo":hex:"abcdef00":int:0\n') def test_hex_format_int_param(self): """ Test int parameter in hex format. :return: """ - s = StringIOWrapper('test_suite_ut.data', '') + stream = StringIOWrapper('test_suite_ut.data', '') unique_expressions = [] - expression_code = write_parameters(s, ['"Yahoo"', '"abcdef00"', '0xAA'], ['char*', 'hex', 'int'], + expression_code = write_parameters(stream, + ['"Yahoo"', '"abcdef00"', '0xAA'], + ['char*', 'hex', 'int'], unique_expressions) self.assertEqual(len(unique_expressions), 0) self.assertEqual(expression_code, '') - self.assertEqual(s.getvalue(), ':char*:"Yahoo":hex:"abcdef00":int:0xAA\n') + self.assertEqual(stream.getvalue(), + ':char*:"Yahoo":hex:"abcdef00":int:0xAA\n') def test_with_exp_param(self): """ Test when there is macro or expression in the params. :return: """ - s = StringIOWrapper('test_suite_ut.data', '') + stream = StringIOWrapper('test_suite_ut.data', '') unique_expressions = [] - expression_code = write_parameters(s, ['"Yahoo"', '"abcdef00"', '0', 'MACRO1', 'MACRO2', 'MACRO3'], - ['char*', 'hex', 'int', 'int', 'int', 'int'], + expression_code = write_parameters(stream, + ['"Yahoo"', '"abcdef00"', '0', + 'MACRO1', 'MACRO2', 'MACRO3'], + ['char*', 'hex', 'int', + 'int', 'int', 'int'], unique_expressions) self.assertEqual(len(unique_expressions), 3) self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3']) @@ -1298,21 +1411,29 @@ class WriteParams(TestCase): } break;''' self.assertEqual(expression_code, expected_expression_code) - self.assertEqual(s.getvalue(), ':char*:"Yahoo":hex:"abcdef00":int:0:exp:0:exp:1:exp:2\n') + self.assertEqual(stream.getvalue(), + ':char*:"Yahoo":hex:"abcdef00":int:0:exp:0:exp:1' + ':exp:2\n') - def test_with_repeate_calls(self): + def test_with_repeat_calls(self): """ Test when write_parameter() is called with same macro or expression. :return: """ - s = StringIOWrapper('test_suite_ut.data', '') + stream = StringIOWrapper('test_suite_ut.data', '') unique_expressions = [] expression_code = '' - expression_code += write_parameters(s, ['"Yahoo"', 'MACRO1', 'MACRO2'], ['char*', 'int', 'int'], + expression_code += write_parameters(stream, + ['"Yahoo"', 'MACRO1', 'MACRO2'], + ['char*', 'int', 'int'], unique_expressions) - expression_code += write_parameters(s, ['"abcdef00"', 'MACRO2', 'MACRO3'], ['hex', 'int', 'int'], + expression_code += write_parameters(stream, + ['"abcdef00"', 'MACRO2', 'MACRO3'], + ['hex', 'int', 'int'], unique_expressions) - expression_code += write_parameters(s, ['0', 'MACRO3', 'MACRO1'], ['int', 'int', 'int'], + expression_code += write_parameters(stream, + ['0', 'MACRO3', 'MACRO1'], + ['int', 'int', 'int'], unique_expressions) self.assertEqual(len(unique_expressions), 3) self.assertEqual(unique_expressions, ['MACRO1', 'MACRO2', 'MACRO3']) @@ -1337,31 +1458,34 @@ class WriteParams(TestCase): :hex:"abcdef00":exp:1:exp:2 :int:0:exp:2:exp:0 ''' - self.assertEqual(s.getvalue(), expected_data_file) + self.assertEqual(stream.getvalue(), expected_data_file) -class GenTestSuiteDepsChecks(TestCase): +class GenTestSuiteDependenciesChecks(TestCase): """ - + Test suite for testing gen_suite_dep_checks() """ - def test_empty_suite_deps(self): + def test_empty_suite_dependencies(self): """ - Test with empty suite_deps list. + Test with empty suite_dependencies list. :return: """ - dep_check_code, expression_code = gen_suite_deps_checks([], 'DEP_CHECK_CODE', 'EXPRESSION_CODE') + dep_check_code, expression_code = \ + gen_suite_dep_checks([], 'DEP_CHECK_CODE', 'EXPRESSION_CODE') self.assertEqual(dep_check_code, 'DEP_CHECK_CODE') self.assertEqual(expression_code, 'EXPRESSION_CODE') - def test_suite_deps(self): + def test_suite_dependencies(self): """ - Test with suite_deps list. + Test with suite_dependencies list. :return: """ - dep_check_code, expression_code = gen_suite_deps_checks(['SUITE_DEP'], 'DEP_CHECK_CODE', 'EXPRESSION_CODE') - exprectd_dep_check_code = ''' + dep_check_code, expression_code = \ + gen_suite_dep_checks(['SUITE_DEP'], 'DEP_CHECK_CODE', + 'EXPRESSION_CODE') + expected_dep_check_code = ''' #if defined(SUITE_DEP) DEP_CHECK_CODE #endif @@ -1371,7 +1495,7 @@ DEP_CHECK_CODE EXPRESSION_CODE #endif ''' - self.assertEqual(dep_check_code, exprectd_dep_check_code) + self.assertEqual(dep_check_code, expected_dep_check_code) self.assertEqual(expression_code, expected_expression_code) def test_no_dep_no_exp(self): @@ -1379,7 +1503,7 @@ EXPRESSION_CODE Test when there are no dependency and expression code. :return: """ - dep_check_code, expression_code = gen_suite_deps_checks([], '', '') + dep_check_code, expression_code = gen_suite_dep_checks([], '', '') self.assertEqual(dep_check_code, '') self.assertEqual(expression_code, '') @@ -1389,10 +1513,13 @@ class GenFromTestData(TestCase): Test suite for gen_from_test_data() """ - @patch("generate_test_code.write_deps") + @staticmethod + @patch("generate_test_code.write_dependencies") @patch("generate_test_code.write_parameters") - @patch("generate_test_code.gen_suite_deps_checks") - def test_intermediate_data_file(self, gen_suite_deps_checks_mock, write_parameters_mock, write_deps_mock): + @patch("generate_test_code.gen_suite_dependencies_checks") + def test_intermediate_data_file(func_mock1, + write_parameters_mock, + write_dependencies_mock): """ Test that intermediate data file is written with expected data. :return: @@ -1405,13 +1532,15 @@ func1:0 data_f = StringIOWrapper('test_suite_ut.data', data) out_data_f = StringIOWrapper('test_suite_ut.datax', '') func_info = {'test_func1': (1, ('int',))} - suite_deps = [] + suite_dependencies = [] write_parameters_mock.side_effect = write_parameters - write_deps_mock.side_effect = write_deps - gen_suite_deps_checks_mock.side_effect = gen_suite_deps_checks - gen_from_test_data(data_f, out_data_f, func_info, suite_deps) - write_deps_mock.assert_called_with(out_data_f, ['DEP1'], ['DEP1']) - write_parameters_mock.assert_called_with(out_data_f, ['0'], ('int',), []) + write_dependencies_mock.side_effect = write_dependencies + func_mock1.side_effect = gen_suite_dep_checks + gen_from_test_data(data_f, out_data_f, func_info, suite_dependencies) + write_dependencies_mock.assert_called_with(out_data_f, + ['DEP1'], ['DEP1']) + write_parameters_mock.assert_called_with(out_data_f, ['0'], + ('int',), []) expected_dep_check_code = ''' case 0: { @@ -1422,7 +1551,8 @@ func1:0 #endif } break;''' - gen_suite_deps_checks_mock.assert_called_with(suite_deps, expected_dep_check_code, '') + func_mock1.assert_called_with( + suite_dependencies, expected_dep_check_code, '') def test_function_not_found(self): """ @@ -1437,12 +1567,14 @@ func1:0 data_f = StringIOWrapper('test_suite_ut.data', data) out_data_f = StringIOWrapper('test_suite_ut.datax', '') func_info = {'test_func2': (1, ('int',))} - suite_deps = [] - self.assertRaises(GeneratorInputError, gen_from_test_data, data_f, out_data_f, func_info, suite_deps) + suite_dependencies = [] + self.assertRaises(GeneratorInputError, gen_from_test_data, + data_f, out_data_f, func_info, suite_dependencies) def test_different_func_args(self): """ - Test that AssertError is raised when no. of parameters and function args differ. + Test that AssertError is raised when no. of parameters and + function args differ. :return: """ data = ''' @@ -1452,9 +1584,10 @@ func1:0 ''' data_f = StringIOWrapper('test_suite_ut.data', data) out_data_f = StringIOWrapper('test_suite_ut.datax', '') - func_info = {'test_func2': (1, ('int','hex'))} - suite_deps = [] - self.assertRaises(GeneratorInputError, gen_from_test_data, data_f, out_data_f, func_info, suite_deps) + func_info = {'test_func2': (1, ('int', 'hex'))} + suite_dependencies = [] + self.assertRaises(GeneratorInputError, gen_from_test_data, data_f, + out_data_f, func_info, suite_dependencies) def test_output(self): """ @@ -1472,9 +1605,12 @@ func2:"yahoo":88:MACRO1 ''' data_f = StringIOWrapper('test_suite_ut.data', data) out_data_f = StringIOWrapper('test_suite_ut.datax', '') - func_info = {'test_func1': (0, ('int', 'int', 'int', 'int')), 'test_func2': (1, ('char*', 'int', 'int'))} - suite_deps = [] - dep_check_code, expression_code = gen_from_test_data(data_f, out_data_f, func_info, suite_deps) + func_info = {'test_func1': (0, ('int', 'int', 'int', 'int')), + 'test_func2': (1, ('char*', 'int', 'int'))} + suite_dependencies = [] + dep_check_code, expression_code = \ + gen_from_test_data(data_f, out_data_f, func_info, + suite_dependencies) expected_dep_check_code = ''' case 0: { @@ -1494,7 +1630,7 @@ func2:"yahoo":88:MACRO1 #endif } break;''' - expecrted_data = '''My test 1 + expected_data = '''My test 1 depends_on:0 0:int:0:int:0xfa:exp:0:exp:1 @@ -1515,9 +1651,9 @@ depends_on:0:1 } break;''' self.assertEqual(dep_check_code, expected_dep_check_code) - self.assertEqual(out_data_f.getvalue(), expecrted_data) + self.assertEqual(out_data_f.getvalue(), expected_data) self.assertEqual(expression_code, expected_expression_code) -if __name__=='__main__': +if __name__ == '__main__': unittest_main()