import inspect as _inspect
import sys as _sys
import unittest as _unittest

try:
    import wrapt as _wrapt
except ImportError as e:
    _wrapt = None
    _wrapt_error = e

import bes.trace as _bes_trace


def skip_if_missing_wrapt(obj):
    if _wrapt is None:
        return _unittest.skip(str(_wrapt_error))(obj)
    return obj


def skip_if_python_version_less_than(version):
    skip = _sys.version_info < version
    def decorator(obj):
        if skip:
            return _unittest.skip(str(_wrapt_error))(obj)
        return obj
    return decorator


class Logger (object):
    def __init__(self):
        self.messages = []

    def __call__(self, *args, **kwargs):
        self.messages.append({'args': args, 'kwargs': kwargs})


class TraceTestCase (_unittest.TestCase):
    def test_trace(self):
        logger = Logger()

        @_bes_trace.trace(logger=logger)
        def foo(*args, **kwargs):
            logger('running')

        foo(1, 'a', b=3)

        self.assertEqual(
            logger.messages,
            [
                {
                    'args': (),