2019-09-10 00:54:28 +00:00
|
|
|
# Created By: Virgil Dupras
|
|
|
|
# Created On: 2010-11-14
|
|
|
|
# Copyright 2015 Hardcoded Software (http://www.hardcoded.net)
|
|
|
|
#
|
|
|
|
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
|
|
|
|
# which should be included with this package. The terms are also available at
|
|
|
|
# http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
|
|
|
|
import threading
|
|
|
|
import py.path
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def eq_(a, b, msg=None):
|
|
|
|
__tracebackhide__ = True
|
|
|
|
assert a == b, msg or "%r != %r" % (a, b)
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def eq_sorted(a, b, msg=None):
|
|
|
|
"""If both a and b are iterable sort them and compare using eq_, otherwise just pass them through to eq_ anyway."""
|
|
|
|
try:
|
|
|
|
eq_(sorted(a), sorted(b), msg)
|
|
|
|
except TypeError:
|
|
|
|
eq_(a, b, msg)
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def assert_almost_equal(a, b, places=7):
|
|
|
|
__tracebackhide__ = True
|
|
|
|
assert round(a, ndigits=places) == round(b, ndigits=places)
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def callcounter():
|
|
|
|
def f(*args, **kwargs):
|
|
|
|
f.callcount += 1
|
|
|
|
|
|
|
|
f.callcount = 0
|
|
|
|
return f
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
class TestData:
|
|
|
|
def __init__(self, datadirpath):
|
|
|
|
self.datadirpath = py.path.local(datadirpath)
|
|
|
|
|
|
|
|
def filepath(self, relative_path, *args):
|
|
|
|
"""Returns the path of a file in testdata.
|
|
|
|
|
|
|
|
'relative_path' can be anything that can be added to a Path
|
|
|
|
if args is not empty, it will be joined to relative_path
|
|
|
|
"""
|
|
|
|
resultpath = self.datadirpath.join(relative_path)
|
|
|
|
if args:
|
|
|
|
resultpath = resultpath.join(*args)
|
|
|
|
assert resultpath.check()
|
|
|
|
return str(resultpath)
|
|
|
|
|
|
|
|
|
|
|
|
class CallLogger:
|
|
|
|
"""This is a dummy object that logs all calls made to it.
|
|
|
|
|
|
|
|
It is used to simulate the GUI layer.
|
|
|
|
"""
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.calls = []
|
|
|
|
|
|
|
|
def __getattr__(self, func_name):
|
|
|
|
def func(*args, **kw):
|
|
|
|
self.calls.append(func_name)
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
return func
|
|
|
|
|
|
|
|
def clear_calls(self):
|
|
|
|
del self.calls[:]
|
|
|
|
|
|
|
|
def check_gui_calls(self, expected, verify_order=False):
|
|
|
|
"""Checks that the expected calls have been made to 'self', then clears the log.
|
|
|
|
|
|
|
|
`expected` is an iterable of strings representing method names.
|
|
|
|
If `verify_order` is True, the order of the calls matters.
|
|
|
|
"""
|
|
|
|
__tracebackhide__ = True
|
|
|
|
if verify_order:
|
|
|
|
eq_(self.calls, expected)
|
|
|
|
else:
|
|
|
|
eq_(set(self.calls), set(expected))
|
|
|
|
self.clear_calls()
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
def check_gui_calls_partial(
|
|
|
|
self, expected=None, not_expected=None, verify_order=False
|
|
|
|
):
|
2019-09-10 00:54:28 +00:00
|
|
|
"""Checks that the expected calls have been made to 'self', then clears the log.
|
|
|
|
|
|
|
|
`expected` is an iterable of strings representing method names. Order doesn't matter.
|
|
|
|
Moreover, if calls have been made that are not in expected, no failure occur.
|
|
|
|
`not_expected` can be used for a more explicit check (rather than calling `check_gui_calls`
|
|
|
|
with an empty `expected`) to assert that calls have *not* been made.
|
|
|
|
"""
|
|
|
|
__tracebackhide__ = True
|
|
|
|
if expected is not None:
|
|
|
|
not_called = set(expected) - set(self.calls)
|
2020-01-01 02:16:27 +00:00
|
|
|
assert not not_called, "These calls haven't been made: {0}".format(
|
|
|
|
not_called
|
|
|
|
)
|
2019-09-10 00:54:28 +00:00
|
|
|
if verify_order:
|
|
|
|
max_index = 0
|
|
|
|
for call in expected:
|
|
|
|
index = self.calls.index(call)
|
|
|
|
if index < max_index:
|
2020-01-01 02:16:27 +00:00
|
|
|
raise AssertionError(
|
|
|
|
"The call {0} hasn't been made in the correct order".format(
|
|
|
|
call
|
|
|
|
)
|
|
|
|
)
|
2019-09-10 00:54:28 +00:00
|
|
|
max_index = index
|
|
|
|
if not_expected is not None:
|
|
|
|
called = set(not_expected) & set(self.calls)
|
2020-01-01 02:16:27 +00:00
|
|
|
assert not called, "These calls shouldn't have been made: {0}".format(
|
|
|
|
called
|
|
|
|
)
|
2019-09-10 00:54:28 +00:00
|
|
|
self.clear_calls()
|
|
|
|
|
|
|
|
|
|
|
|
class TestApp:
|
|
|
|
def __init__(self):
|
|
|
|
self._call_loggers = []
|
|
|
|
|
|
|
|
def clear_gui_calls(self):
|
|
|
|
for logger in self._call_loggers:
|
|
|
|
logger.clear_calls()
|
|
|
|
|
|
|
|
def make_logger(self, logger=None):
|
|
|
|
if logger is None:
|
|
|
|
logger = CallLogger()
|
|
|
|
self._call_loggers.append(logger)
|
|
|
|
return logger
|
|
|
|
|
|
|
|
def make_gui(self, name, class_, view=None, parent=None, holder=None):
|
|
|
|
if view is None:
|
|
|
|
view = self.make_logger()
|
|
|
|
if parent is None:
|
|
|
|
# The attribute "default_parent" has to be set for this to work correctly
|
|
|
|
parent = self.default_parent
|
|
|
|
if holder is None:
|
|
|
|
holder = self
|
2020-01-01 02:16:27 +00:00
|
|
|
setattr(holder, "{0}_gui".format(name), view)
|
2019-09-10 00:54:28 +00:00
|
|
|
gui = class_(parent)
|
|
|
|
gui.view = view
|
|
|
|
setattr(holder, name, gui)
|
|
|
|
return gui
|
|
|
|
|
|
|
|
|
|
|
|
# To use @with_app, you have to import pytest_funcarg__app in your conftest.py file.
|
|
|
|
def with_app(setupfunc):
|
|
|
|
def decorator(func):
|
|
|
|
func.setupfunc = setupfunc
|
|
|
|
return func
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
return decorator
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def pytest_funcarg__app(request):
|
|
|
|
setupfunc = request.function.setupfunc
|
2020-01-01 02:16:27 +00:00
|
|
|
if hasattr(setupfunc, "__code__"):
|
|
|
|
argnames = setupfunc.__code__.co_varnames[: setupfunc.__code__.co_argcount]
|
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def getarg(name):
|
2020-01-01 02:16:27 +00:00
|
|
|
if name == "self":
|
2019-09-10 00:54:28 +00:00
|
|
|
return request.function.__self__
|
|
|
|
else:
|
|
|
|
return request.getfixturevalue(name)
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
args = [getarg(argname) for argname in argnames]
|
|
|
|
else:
|
|
|
|
args = []
|
|
|
|
app = setupfunc(*args)
|
|
|
|
return app
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def jointhreads():
|
|
|
|
"""Join all threads to the main thread"""
|
|
|
|
for thread in threading.enumerate():
|
2020-01-01 02:16:27 +00:00
|
|
|
if hasattr(thread, "BUGGY"):
|
2019-09-10 00:54:28 +00:00
|
|
|
continue
|
2020-01-01 02:16:27 +00:00
|
|
|
if thread.getName() != "MainThread" and thread.isAlive():
|
|
|
|
if hasattr(thread, "close"):
|
2019-09-10 00:54:28 +00:00
|
|
|
thread.close()
|
|
|
|
thread.join(1)
|
|
|
|
if thread.isAlive():
|
|
|
|
print("Thread problem. Some thread doesn't want to stop.")
|
|
|
|
thread.BUGGY = True
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def _unify_args(func, args, kwargs, args_to_ignore=None):
|
2020-01-01 02:16:27 +00:00
|
|
|
""" Unify args and kwargs in the same dictionary.
|
2019-09-10 00:54:28 +00:00
|
|
|
|
|
|
|
The result is kwargs with args added to it. func.func_code.co_varnames is used to determine
|
|
|
|
under what key each elements of arg will be mapped in kwargs.
|
|
|
|
|
|
|
|
if you want some arguments not to be in the results, supply a list of arg names in
|
|
|
|
args_to_ignore.
|
|
|
|
|
|
|
|
if f is a function that takes *args, func_code.co_varnames is empty, so args will be put
|
|
|
|
under 'args' in kwargs.
|
|
|
|
|
|
|
|
def foo(bar, baz)
|
|
|
|
_unifyArgs(foo, (42,), {'baz': 23}) --> {'bar': 42, 'baz': 23}
|
|
|
|
_unifyArgs(foo, (42,), {'baz': 23}, ['bar']) --> {'baz': 23}
|
2020-01-01 02:16:27 +00:00
|
|
|
"""
|
2019-09-10 00:54:28 +00:00
|
|
|
result = kwargs.copy()
|
2020-01-01 02:16:27 +00:00
|
|
|
if hasattr(func, "__code__"): # built-in functions don't have func_code
|
2019-09-10 00:54:28 +00:00
|
|
|
args = list(args)
|
2020-01-01 02:16:27 +00:00
|
|
|
if (
|
|
|
|
getattr(func, "__self__", None) is not None
|
|
|
|
): # bound method, we have to add self to args list
|
2019-09-10 00:54:28 +00:00
|
|
|
args = [func.__self__] + args
|
|
|
|
defaults = list(func.__defaults__) if func.__defaults__ is not None else []
|
|
|
|
arg_count = func.__code__.co_argcount
|
|
|
|
arg_names = list(func.__code__.co_varnames)
|
2020-01-01 02:16:27 +00:00
|
|
|
if len(args) < arg_count: # We have default values
|
2019-09-10 00:54:28 +00:00
|
|
|
required_arg_count = arg_count - len(args)
|
|
|
|
args = args + defaults[-required_arg_count:]
|
|
|
|
for arg_name, arg in zip(arg_names, args):
|
|
|
|
# setdefault is used because if the arg is already in kwargs, we don't want to use default values
|
|
|
|
result.setdefault(arg_name, arg)
|
|
|
|
else:
|
2020-01-01 02:16:27 +00:00
|
|
|
# 'func' has a *args argument
|
|
|
|
result["args"] = args
|
2019-09-10 00:54:28 +00:00
|
|
|
if args_to_ignore:
|
|
|
|
for kw in args_to_ignore:
|
|
|
|
del result[kw]
|
|
|
|
return result
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def log_calls(func):
|
2020-01-01 02:16:27 +00:00
|
|
|
""" Logs all func calls' arguments under func.calls.
|
2019-09-10 00:54:28 +00:00
|
|
|
|
|
|
|
func.calls is a list of _unify_args() result (dict).
|
|
|
|
|
|
|
|
Mostly used for unit testing.
|
2020-01-01 02:16:27 +00:00
|
|
|
"""
|
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
unifiedArgs = _unify_args(func, args, kwargs)
|
|
|
|
wrapper.calls.append(unifiedArgs)
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
|
|
|
|
wrapper.calls = []
|
|
|
|
return wrapper
|