Fixed unit tests in test_generate_test_code.py

This commit is contained in:
Azim Khan 2018-07-05 14:20:08 +01:00 committed by Mohammad Azim Khan
parent 8d686bfdb1
commit 4084ec7ae5
2 changed files with 29 additions and 33 deletions

View File

@ -450,6 +450,7 @@ def parse_function_dependencies(line):
return dependencies return dependencies
def parse_function_signature(line): def parse_function_signature(line):
""" """
Parses test function signature for validation and generates Parses test function signature for validation and generates

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# Unit test for generate_test_code.py # Unit test for generate_test_code.py
# #
# Copyright (C) 2018, ARM Limited, All Rights Reserved # Copyright (C) 2018, Arm Limited, All Rights Reserved
# SPDX-License-Identifier: Apache-2.0 # SPDX-License-Identifier: Apache-2.0
# #
# Licensed under the Apache License, Version 2.0 (the "License"); you may # Licensed under the Apache License, Version 2.0 (the "License"); you may
@ -23,7 +23,6 @@ Unit tests for generate_test_code.py
""" """
import sys
from StringIO import StringIO from StringIO import StringIO
from unittest import TestCase, main as unittest_main from unittest import TestCase, main as unittest_main
from mock import patch from mock import patch
@ -288,7 +287,7 @@ class StringIOWrapper(StringIO, object):
""" """
file like class to mock file object in tests. 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=0):
""" """
Init file handle. Init file handle.
@ -308,14 +307,8 @@ class StringIOWrapper(StringIO, object):
:return: Line read from file. :return: Line read from file.
""" """
parent = super(StringIOWrapper, self) line = super(StringIOWrapper, self).next()
line = parent.next() # Python 2 return line
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 __next__ = next
@ -327,7 +320,7 @@ class StringIOWrapper(StringIO, object):
:return: :return:
""" """
line = super(StringIOWrapper, self).readline() line = super(StringIOWrapper, self).readline()
if line: if line is not None:
self.line_no += 1 self.line_no += 1
return line return line
@ -510,10 +503,10 @@ class ParseFuncSignature(TestCase):
self.assertEqual(name, 'entropy_threshold') self.assertEqual(name, 'entropy_threshold')
self.assertEqual(args, ['char*', 'hex', 'int']) self.assertEqual(args, ['char*', 'hex', 'int'])
self.assertEqual(local, self.assertEqual(local,
' data_t hex1 = {(uint8_t *) params[1], ' ' data_t data1 = {(uint8_t *) params[1], '
'*( (uint32_t *) params[2] )};\n') '*( (uint32_t *) params[2] )};\n')
self.assertEqual(arg_dispatch, ['(char *) params[0]', self.assertEqual(arg_dispatch, ['(char *) params[0]',
'&hex1', '&data1',
'*( (int *) params[3] )']) '*( (int *) params[3] )'])
def test_non_void_function(self): def test_non_void_function(self):
@ -629,13 +622,14 @@ void func()
gen_function_wrapper_mock.assert_called_with('test_func', '', []) gen_function_wrapper_mock.assert_called_with('test_func', '', [])
self.assertEqual(name, 'test_func') self.assertEqual(name, 'test_func')
self.assertEqual(arg, []) self.assertEqual(arg, [])
expected = '''#line 2 "test_suite_ut.function" expected = '''#line 1 "test_suite_ut.function"
void test_func() void test_func()
{ {
ba ba black sheep ba ba black sheep
have you any wool have you any wool
exit: exit:
;; ;
} }
''' '''
self.assertEqual(code, expected) self.assertEqual(code, expected)
@ -671,7 +665,8 @@ exit:
stream = StringIOWrapper('test_suite_ut.function', data) stream = StringIOWrapper('test_suite_ut.function', data)
_, _, code, _ = parse_function_code(stream, [], []) _, _, code, _ = parse_function_code(stream, [], [])
expected = '''#line 2 "test_suite_ut.function" expected = '''#line 1 "test_suite_ut.function"
void test_func() void test_func()
{ {
ba ba black sheep ba ba black sheep
@ -708,7 +703,7 @@ class ParseFunction(TestCase):
stream = StringIOWrapper('test_suite_ut.function', data) stream = StringIOWrapper('test_suite_ut.function', data)
self.assertRaises(Exception, parse_functions, stream) self.assertRaises(Exception, parse_functions, stream)
parse_until_pattern_mock.assert_called_with(stream, END_HEADER_REGEX) parse_until_pattern_mock.assert_called_with(stream, END_HEADER_REGEX)
self.assertEqual(stream.line_no, 2) self.assertEqual(stream.line_no, 1)
@patch("generate_test_code.parse_until_pattern") @patch("generate_test_code.parse_until_pattern")
def test_begin_helper(self, parse_until_pattern_mock): def test_begin_helper(self, parse_until_pattern_mock):
@ -731,7 +726,7 @@ void print_hello_world()
self.assertRaises(Exception, parse_functions, stream) self.assertRaises(Exception, parse_functions, stream)
parse_until_pattern_mock.assert_called_with(stream, parse_until_pattern_mock.assert_called_with(stream,
END_SUITE_HELPERS_REGEX) END_SUITE_HELPERS_REGEX)
self.assertEqual(stream.line_no, 2) self.assertEqual(stream.line_no, 1)
@patch("generate_test_code.parse_suite_dependencies") @patch("generate_test_code.parse_suite_dependencies")
def test_begin_dep(self, parse_suite_dependencies_mock): def test_begin_dep(self, parse_suite_dependencies_mock):
@ -752,7 +747,7 @@ void print_hello_world()
stream = StringIOWrapper('test_suite_ut.function', data) stream = StringIOWrapper('test_suite_ut.function', data)
self.assertRaises(Exception, parse_functions, stream) self.assertRaises(Exception, parse_functions, stream)
parse_suite_dependencies_mock.assert_called_with(stream) parse_suite_dependencies_mock.assert_called_with(stream)
self.assertEqual(stream.line_no, 2) self.assertEqual(stream.line_no, 1)
@patch("generate_test_code.parse_function_dependencies") @patch("generate_test_code.parse_function_dependencies")
def test_begin_function_dep(self, func_mock): def test_begin_function_dep(self, func_mock):
@ -775,7 +770,7 @@ void print_hello_world()
stream = StringIOWrapper('test_suite_ut.function', data) stream = StringIOWrapper('test_suite_ut.function', data)
self.assertRaises(Exception, parse_functions, stream) self.assertRaises(Exception, parse_functions, stream)
func_mock.assert_called_with(dependencies_str) func_mock.assert_called_with(dependencies_str)
self.assertEqual(stream.line_no, 2) self.assertEqual(stream.line_no, 1)
@patch("generate_test_code.parse_function_code") @patch("generate_test_code.parse_function_code")
@patch("generate_test_code.parse_function_dependencies") @patch("generate_test_code.parse_function_dependencies")
@ -866,17 +861,17 @@ void func2()
''' '''
self.assertEqual(dispatch_code, expected_dispatch_code) self.assertEqual(dispatch_code, expected_dispatch_code)
expected_func_code = '''#if defined(MBEDTLS_ECP_C) expected_func_code = '''#if defined(MBEDTLS_ECP_C)
#line 3 "test_suite_ut.function" #line 2 "test_suite_ut.function"
#include "mbedtls/ecp.h" #include "mbedtls/ecp.h"
#define ECP_PF_UNKNOWN -1 #define ECP_PF_UNKNOWN -1
#if defined(MBEDTLS_ENTROPY_NV_SEED) #if defined(MBEDTLS_ENTROPY_NV_SEED)
#if defined(MBEDTLS_FS_IO) #if defined(MBEDTLS_FS_IO)
#line 14 "test_suite_ut.function" #line 13 "test_suite_ut.function"
void test_func1() void test_func1()
{ {
exit: exit:
;; ;
} }
void test_func1_wrapper( void ** params ) void test_func1_wrapper( void ** params )
@ -889,11 +884,11 @@ void test_func1_wrapper( void ** params )
#endif /* MBEDTLS_ENTROPY_NV_SEED */ #endif /* MBEDTLS_ENTROPY_NV_SEED */
#if defined(MBEDTLS_ENTROPY_NV_SEED) #if defined(MBEDTLS_ENTROPY_NV_SEED)
#if defined(MBEDTLS_FS_IO) #if defined(MBEDTLS_FS_IO)
#line 20 "test_suite_ut.function" #line 19 "test_suite_ut.function"
void test_func2() void test_func2()
{ {
exit: exit:
;; ;
} }
void test_func2_wrapper( void ** params ) void test_func2_wrapper( void ** params )
@ -989,20 +984,20 @@ class EscapedSplit(TestCase):
Test input that has escaped delimiter. Test input that has escaped delimiter.
:return: :return:
""" """
test_str = r'yahoo\\\:google:facebook' test_str = r'yahoo\\:google:facebook'
splits = escaped_split(test_str, ':') splits = escaped_split(test_str, ':')
self.assertEqual(splits, [r'yahoo\\\\', 'google', 'facebook']) self.assertEqual(splits, [r'yahoo\\', 'google', 'facebook'])
def test_all_at_once(self): def test_all_at_once(self):
""" """
Test input that has escaped delimiter. Test input that has escaped delimiter.
:return: :return:
""" """
test_str = r'yahoo\\\:google:facebook\:instagram\\\:bbc\\\\:wikipedia' test_str = r'yahoo\\:google:facebook\:instagram\\:bbc\\:wikipedia'
splits = escaped_split(test_str, ':') splits = escaped_split(test_str, ':')
self.assertEqual(splits, [r'yahoo\\\\', r'google', self.assertEqual(splits, [r'yahoo\\', r'google',
r'facebook\:instagram\\\\', r'facebook\:instagram\\',
r'bbc\\\\', r'wikipedia']) r'bbc\\', r'wikipedia'])
class ParseTestData(TestCase): class ParseTestData(TestCase):
@ -1516,7 +1511,7 @@ class GenFromTestData(TestCase):
@staticmethod @staticmethod
@patch("generate_test_code.write_dependencies") @patch("generate_test_code.write_dependencies")
@patch("generate_test_code.write_parameters") @patch("generate_test_code.write_parameters")
@patch("generate_test_code.gen_suite_dependencies_checks") @patch("generate_test_code.gen_suite_dep_checks")
def test_intermediate_data_file(func_mock1, def test_intermediate_data_file(func_mock1,
write_parameters_mock, write_parameters_mock,
write_dependencies_mock): write_dependencies_mock):