mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2024-11-26 17:35:46 +01:00
test_generate_test_code: remove Python 2 compatibility code
This makes the code cleaner. As a bonus, mypy no longer gets confused. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
parent
ac9e7c0b6e
commit
38b66dfc85
@ -20,21 +20,10 @@
|
|||||||
Unit tests for generate_test_code.py
|
Unit tests for generate_test_code.py
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# pylint: disable=wrong-import-order
|
from io import StringIO
|
||||||
try:
|
|
||||||
# Python 2
|
|
||||||
from StringIO import StringIO
|
|
||||||
except ImportError:
|
|
||||||
# Python 3
|
|
||||||
from io import StringIO
|
|
||||||
from unittest import TestCase, main as unittest_main
|
from unittest import TestCase, main as unittest_main
|
||||||
try:
|
from unittest.mock import patch
|
||||||
# Python 2
|
|
||||||
from mock import patch
|
|
||||||
except ImportError:
|
|
||||||
# Python 3
|
|
||||||
from unittest.mock import patch
|
|
||||||
# pylint: enable=wrong-import-order
|
|
||||||
from generate_test_code import gen_dependencies, gen_dependencies_one_line
|
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 gen_function_wrapper, gen_dispatch
|
||||||
from generate_test_code import parse_until_pattern, GeneratorInputError
|
from generate_test_code import parse_until_pattern, GeneratorInputError
|
||||||
@ -317,25 +306,16 @@ class StringIOWrapper(StringIO):
|
|||||||
:return: Line read from file.
|
:return: Line read from file.
|
||||||
"""
|
"""
|
||||||
parent = super(StringIOWrapper, self)
|
parent = super(StringIOWrapper, self)
|
||||||
if getattr(parent, 'next', None):
|
|
||||||
# Python 2
|
|
||||||
line = parent.next()
|
|
||||||
else:
|
|
||||||
# Python 3
|
|
||||||
line = parent.__next__()
|
line = parent.__next__()
|
||||||
return line
|
return line
|
||||||
|
|
||||||
# Python 3
|
def readline(self, _length=0):
|
||||||
__next__ = next
|
|
||||||
|
|
||||||
def readline(self, length=0):
|
|
||||||
"""
|
"""
|
||||||
Wrap the base class readline.
|
Wrap the base class readline.
|
||||||
|
|
||||||
:param length:
|
:param length:
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
# pylint: disable=unused-argument
|
|
||||||
line = super(StringIOWrapper, self).readline()
|
line = super(StringIOWrapper, self).readline()
|
||||||
if line is not None:
|
if line is not None:
|
||||||
self.line_no += 1
|
self.line_no += 1
|
||||||
@ -549,38 +529,6 @@ class ParseFunctionCode(TestCase):
|
|||||||
Test suite for testing parse_function_code()
|
Test suite for testing parse_function_code()
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def assert_raises_regex(self, exp, regex, func, *args):
|
|
||||||
"""
|
|
||||||
Python 2 & 3 portable wrapper of assertRaisesRegex(p)? function.
|
|
||||||
|
|
||||||
:param exp: Exception type expected to be raised by cb.
|
|
||||||
:param regex: Expected exception message
|
|
||||||
:param func: callable object under test
|
|
||||||
:param args: variable positional arguments
|
|
||||||
"""
|
|
||||||
parent = super(ParseFunctionCode, self)
|
|
||||||
|
|
||||||
# Pylint does not appreciate that the super method called
|
|
||||||
# conditionally can be available in other Python version
|
|
||||||
# then that of Pylint.
|
|
||||||
# Workaround is to call the method via getattr.
|
|
||||||
# Pylint ignores that the method got via getattr is
|
|
||||||
# conditionally executed. Method has to be a callable.
|
|
||||||
# Hence, using a dummy callable for getattr default.
|
|
||||||
dummy = lambda *x: None
|
|
||||||
# First Python 3 assertRaisesRegex is checked, since Python 2
|
|
||||||
# assertRaisesRegexp is also available in Python 3 but is
|
|
||||||
# marked deprecated.
|
|
||||||
for name in ('assertRaisesRegex', 'assertRaisesRegexp'):
|
|
||||||
method = getattr(parent, name, dummy)
|
|
||||||
if method is not dummy:
|
|
||||||
method(exp, regex, func, *args)
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
raise AttributeError(" 'ParseFunctionCode' object has no attribute"
|
|
||||||
" 'assertRaisesRegex' or 'assertRaisesRegexp'"
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_no_function(self):
|
def test_no_function(self):
|
||||||
"""
|
"""
|
||||||
Test no test function found.
|
Test no test function found.
|
||||||
@ -593,7 +541,7 @@ function
|
|||||||
'''
|
'''
|
||||||
stream = StringIOWrapper('test_suite_ut.function', data)
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
||||||
err_msg = 'file: test_suite_ut.function - Test functions not found!'
|
err_msg = 'file: test_suite_ut.function - Test functions not found!'
|
||||||
self.assert_raises_regex(GeneratorInputError, err_msg,
|
self.assertRaisesRegex(GeneratorInputError, err_msg,
|
||||||
parse_function_code, stream, [], [])
|
parse_function_code, stream, [], [])
|
||||||
|
|
||||||
def test_no_end_case_comment(self):
|
def test_no_end_case_comment(self):
|
||||||
@ -609,7 +557,7 @@ void test_func()
|
|||||||
stream = StringIOWrapper('test_suite_ut.function', data)
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
||||||
err_msg = r'file: test_suite_ut.function - '\
|
err_msg = r'file: test_suite_ut.function - '\
|
||||||
'end case pattern .*? not found!'
|
'end case pattern .*? not found!'
|
||||||
self.assert_raises_regex(GeneratorInputError, err_msg,
|
self.assertRaisesRegex(GeneratorInputError, err_msg,
|
||||||
parse_function_code, stream, [], [])
|
parse_function_code, stream, [], [])
|
||||||
|
|
||||||
@patch("generate_test_code.parse_function_arguments")
|
@patch("generate_test_code.parse_function_arguments")
|
||||||
@ -727,7 +675,7 @@ exit:
|
|||||||
data = 'int entropy_threshold( char * a, data_t * h, int result )'
|
data = 'int entropy_threshold( char * a, data_t * h, int result )'
|
||||||
err_msg = 'file: test_suite_ut.function - Test functions not found!'
|
err_msg = 'file: test_suite_ut.function - Test functions not found!'
|
||||||
stream = StringIOWrapper('test_suite_ut.function', data)
|
stream = StringIOWrapper('test_suite_ut.function', data)
|
||||||
self.assert_raises_regex(GeneratorInputError, err_msg,
|
self.assertRaisesRegex(GeneratorInputError, err_msg,
|
||||||
parse_function_code, stream, [], [])
|
parse_function_code, stream, [], [])
|
||||||
|
|
||||||
@patch("generate_test_code.gen_dispatch")
|
@patch("generate_test_code.gen_dispatch")
|
||||||
|
Loading…
Reference in New Issue
Block a user