예제 #1
0
    def __init__(self):
        self.mapper = FuncMapper()  # initialise the funcmapper

        # Map the class methods using functional mapping
        self.mapper.map(r'call my_func', self.my_func)
        self.mapper.map(r'(?P<first>\d+)\+(?P<second>\d+)', self.adder)
        self.mapper.map(r'I am Fred', self.multi_name)
        self.mapper.map(r'No, I am Joe', self.multi_name)
예제 #2
0
class TestClass:
    def __init__(self):
        self.mapper = FuncMapper()  # initialise the funcmapper

        # Map the class methods using functional mapping
        self.mapper.map(r'call my_func', self.my_func)
        self.mapper.map(r'(?P<first>\d+)\+(?P<second>\d+)', self.adder)
        self.mapper.map(r'I am Fred', self.multi_name)
        self.mapper.map(r'No, I am Joe', self.multi_name)

    def my_func(self):
        """We can map simple functions.

        Just pass a phrase to the map decorator.
        """
        return 'I, my_func, have been called'

    def adder(self, first, second):
        """We can map functions with arguments.

        All named capture groups (this is a capture group named first: (?P<first>\d+)) are passed as keyword-arguments to
        function. So make sure the names of your capture groups fit the names of your function arguments.
        Unnamed capture groups as positional arguments are NOT supported to avoid some edge-cases!
        """
        return '{} + {} = {}'.format(first, second, int(first) + int(second))

    def multi_name(self):
        """We can map multiple Regex-expressions to the same function."""
        return 'I have multiple names!'
예제 #3
0
class TestRetrieve(unittest.TestCase):
    """Test Cases for retrieving and calling mapped functions."""
    def setUp(self):
        """Set up."""
        self.mapper = FuncMapper()

    def test_simple_function_call(self):
        """Test a simple function call."""
        simple_test_func = MagicMock(return_value='simple_test_func called')
        self.mapper.map('test')(simple_test_func)
        return_value = self.mapper('test')
        self.assertEqual(return_value, 'simple_test_func called')
        simple_test_func.assert_called_once_with()

    def test_kwargs_function_call(self):
        """Test a function call with kwargs."""
        simple_test_func = MagicMock(return_value='kwargs_test_func called')
        self.mapper.map(r'test (?P<kwarg1>.+) (?P<kwarg2>.+)')(
            simple_test_func)
        return_value = self.mapper('test 1 foo')
        self.assertEqual(return_value, 'kwargs_test_func called')
        simple_test_func.assert_called_once_with(kwarg1='1', kwarg2='foo')

    def test_multiple_function_calls(self):
        """Test to register and retrive multiple functions"""
        simple_test_func1 = MagicMock(return_value='simple_test_func1 called')
        self.mapper.map('test1')(simple_test_func1)
        simple_test_func2 = MagicMock(return_value='simple_test_func2 called')
        self.mapper.map('test2')(simple_test_func2)
        return_value1 = self.mapper('test1')
        return_value2 = self.mapper('test2')
        self.assertEqual(return_value1, 'simple_test_func1 called')
        self.assertEqual(return_value2, 'simple_test_func2 called')
        simple_test_func1.assert_called_once_with()
        simple_test_func2.assert_called_once_with()

    def test_raise_no_function_exception(self):
        """Test that an exception is raised if no match is found."""
        with self.assertRaises(KeyError):
            self.mapper('test1')
예제 #4
0
class TestAdditionalArguments(unittest.TestCase):
    """Test to call the functions with additional arguments"""
    def setUp(self):
        """Set up."""
        self.mapper = FuncMapper()

    def test_additional_args_no_string_kwargs(self):
        simple_test_func = MagicMock(return_value='simple_test_func1 called')
        args = ['test_arg_1', 'test_arg_2']
        kwargs = dict(test_kwarg_1='test_kwarg_1', test_kwarg_2='test_kwarg_2')
        self.mapper.map('test')(simple_test_func)
        self.mapper('test', *args, **kwargs)
        simple_test_func.assert_called_once_with(*args, **kwargs)

    def test_additional_args_with_string_kwargs(self):
        simple_test_func = MagicMock(return_value='simple_test_func1 called')
        args = ['test_arg_1', 'test_arg_2']
        kwargs = dict(test_kwarg_1='test_kwarg_1', test_kwarg_2='test_kwarg_2')
        self.mapper.map(r'test (?P<string_kwarg_1>.+) (?P<string_kwarg_2>.+)')(
            simple_test_func)
        self.mapper('test string_kwarg_1 string_kwarg_2', *args, **kwargs)
        simple_test_func.assert_called_once_with(
            *args,
            string_kwarg_1='string_kwarg_1',
            string_kwarg_2='string_kwarg_2',
            **kwargs)

    def test_additional_kwargs_overwrite_string_kwargs(self):
        simple_test_func = MagicMock(return_value='simple_test_func1 called')
        args = ['test_arg_1', 'test_arg_2']
        kwargs = dict(test_kwarg_1='test_kwarg_1',
                      overwrite_kwarg='test_kwarg_2')
        self.mapper.map(r'test (?P<string_kwarg_1>.+) (?P<overwrite_kwarg>.+)'
                        )(simple_test_func)
        self.mapper('test string_kwarg_1 string_kwarg_2', *args, **kwargs)
        simple_test_func.assert_called_once_with(
            *args, string_kwarg_1='string_kwarg_1', **kwargs)
예제 #5
0
# -*- coding: utf-8 -*-
from funcmap import FuncMapper

mapper = FuncMapper()  # initialise the funcmapper


@mapper.map(r'call my_func')
def my_func():
    """We can map simple functions.

    Just pass a phrase to the map decorator.
    """
    return 'I, my_func, have been called'


@mapper.map(r'(?P<first>\d+)\+(?P<second>\d+)')
def adder(first, second):
    """We can map functions with arguments.

    All named capture groups (this is a capture group named first: (?P<first>\d+)) are passed as keyword-arguments to
    function. So make sure the names of your capture groups fit the names of your function arguments.
    Unnamed capture groups as positional arguments are NOT supported to avoid some edge-cases!
    """
    return '{} + {} = {}'.format(first, second, int(first) + int(second))


@mapper.map(r'I am Fred')
@mapper.map(r'No, I am Joe')
def multi_name():
    """We can map multiple Regex-expressions to the same function."""
    return 'I have multiple names!'
예제 #6
0
 def setUp(self):
     """Set up."""
     self.mapper = FuncMapper()