def test_get_free_parameters():
    free = Signature(foo).get_free_parameters([], {})
    assert free == []
    free = Signature(bariza).get_free_parameters([], {'c': 3})
    assert free == ['a', 'b']
    free = Signature(complex_function_name).get_free_parameters([], {})
    assert free == ['a', 'b', 'c']
def test_construct_arguments_without_duplicates_passes():
    s = Signature(bariza)
    s.construct_arguments([1, 2], {"c": 5}, {})

    s = Signature(complex_function_name)
    s.construct_arguments([1], {"b": 4}, {})

    s = Signature(FunCTIonWithCAPItals)
    s.construct_arguments([], {"a": 6, "b": 6, "c": 6}, {})
def test_construct_arguments_completes_kwargs_from_options():
    s = Signature(bariza)
    args, kwargs = s.construct_arguments([2, 4], {}, {'c': 6})
    assert args == [2, 4]
    assert kwargs == {'c': 6}
    s = Signature(complex_function_name)
    args, kwargs = s.construct_arguments([], {'c': 6, 'b': 7}, {'a': 1})
    assert args == []
    assert kwargs == {'a': 1, 'c': 6, 'b': 7}
Exemple #4
0
def test_construct_arguments_with_duplicate_args_raises_typeerror():
    multiple_values = re.compile(".*multiple values.*")
    with pytest.raises(TypeError) as excinfo:
        s = Signature(bariza)
        s.construct_arguments([1, 2, 3], {'a': 4, 'b': 5}, {})
    assert multiple_values.match(excinfo.value.args[0])
    with pytest.raises(TypeError) as excinfo:
        s = Signature(complex_function_name)
        s.construct_arguments([1], {'a': 4}, {})
    assert multiple_values.match(excinfo.value.args[0])
def test_get_free_parameters():
    free = Signature(foo).get_free_parameters([], {})
    assert free == []
    free = Signature(bariza).get_free_parameters([], {"c": 3})
    assert free == ["a", "b"]
    free = Signature(complex_function_name).get_free_parameters([], {})
    assert free == ["a", "b", "c"]
    free = Signature(_name_with_underscore_).get_free_parameters([], {})
    assert free == ["fo", "bar"]
    s = Signature(__double_underscore__)
    assert s.get_free_parameters([1, 2, 3], {}) == []
Exemple #6
0
def test_get_free_parameters():
    free = Signature(foo).get_free_parameters([], {})
    assert free == []
    free = Signature(bariza).get_free_parameters([], {'c': 3})
    assert free == ['a', 'b']
    free = Signature(complex_function_name).get_free_parameters([], {})
    assert free == ['a', 'b', 'c']
    free = Signature(_name_with_underscore_).get_free_parameters([], {})
    assert free == ['fo', 'bar']
    s = Signature(__double_underscore__)
    assert s.get_free_parameters([1, 2, 3], {}) == []
def test_construct_arguments_completes_kwargs_from_options():
    s = Signature(bariza)
    args, kwargs = s.construct_arguments([2, 4], {}, {"c": 6})
    assert args == [2, 4]
    assert kwargs == {"c": 6}
    s = Signature(complex_function_name)
    args, kwargs = s.construct_arguments([], {"c": 6, "b": 7}, {"a": 1})
    assert args == []
    assert kwargs == {"a": 1, "c": 6, "b": 7}

    s = Signature(_name_with_underscore_)
    args, kwargs = s.construct_arguments([], {}, {"fo": 7, "bar": 6})
    assert args == []
    assert kwargs == {"fo": 7, "bar": 6}
def test_construct_arguments_without_options_returns_same_args_kwargs():
    s = Signature(foo)
    args, kwargs = s.construct_arguments([], {}, {})
    assert args == []
    assert kwargs == {}

    s = Signature(bariza)
    args, kwargs = s.construct_arguments([2, 4, 6], {}, {})
    assert args == [2, 4, 6]
    assert kwargs == {}

    s = Signature(complex_function_name)
    args, kwargs = s.construct_arguments([2], {'c': 6, 'b': 7}, {})
    assert args == [2]
    assert kwargs == {'c': 6, 'b': 7}
Exemple #9
0
def test_construct_arguments_does_not_overwrite_args_and_kwargs():
    s = Signature(bariza)
    args, kwargs = s.construct_arguments([1, 2], {'c': 3}, {
        'a': 6,
        'b': 6,
        'c': 6
    })
    assert args == [1, 2]
    assert kwargs == {'c': 3}
Exemple #10
0
def test_construct_arguments_ignores_excess_options():
    s = Signature(bariza)
    args, kwargs = s.construct_arguments([2], {'b': 4}, {
        'c': 6,
        'foo': 9,
        'bar': 0
    })
    assert args == [2]
    assert kwargs == {'b': 4, 'c': 6}
Exemple #11
0
def create_captured_function(function, prefix=None):
    sig = Signature(function)
    function.signature = sig
    function.uses_randomness = "_seed" in sig.arguments or "_rnd" in sig.arguments
    function.logger = None
    function.config = {}
    function.rnd = None
    function.run = None
    function.prefix = prefix
    return captured_function(function)
Exemple #12
0
def test_construct_arguments_without_options_returns_same_args_kwargs():
    s = Signature(foo)
    args, kwargs = s.construct_arguments([], {}, {})
    assert args == []
    assert kwargs == {}

    s = Signature(bariza)
    args, kwargs = s.construct_arguments([2, 4, 6], {}, {})
    assert args == [2, 4, 6]
    assert kwargs == {}

    s = Signature(complex_function_name)
    args, kwargs = s.construct_arguments([2], {"c": 6, "b": 7}, {})
    assert args == [2]
    assert kwargs == {"c": 6, "b": 7}

    s = Signature(_name_with_underscore_)
    args, kwargs = s.construct_arguments([], {"fo": 7, "bar": 6}, {})
    assert args == []
    assert kwargs == {"fo": 7, "bar": 6}
Exemple #13
0
def test_construct_arguments_raises_if_args_unfilled():
    s = Signature(bariza)
    missing = re.compile(".*missing.*")
    with pytest.raises(MissingConfigError) as excinfo:
        s.construct_arguments([], {}, {})
    assert missing.match(excinfo.value.args[0])
    with pytest.raises(MissingConfigError) as excinfo:
        s.construct_arguments([1, 2], {}, {})
    assert missing.match(excinfo.value.args[0])
    with pytest.raises(MissingConfigError) as excinfo:
        s.construct_arguments([1], {"b": 3}, {})
    assert missing.match(excinfo.value.args[0])
    with pytest.raises(MissingConfigError) as excinfo:
        s.construct_arguments([1], {"c": 5}, {})
    assert missing.match(excinfo.value.args[0])
Exemple #14
0
    def option_hook(self, function):
        """
        Decorator for adding an option hook function.

        An option hook is a function that is called right before a run
        is created. It receives (and potentially modifies) the options
        dictionary. That is, the dictionary of commandline options used for
        this run.

        .. note::
            The decorated function MUST have an argument called options.

            The options also contain ``'COMMAND'`` and ``'UPDATE'`` entries,
            but changing them has no effect. Only modification on
            flags (entries starting with ``'--'``) are considered.
        """
        sig = Signature(function)
        if "options" not in sig.arguments:
            raise KeyError("option_hook functions must have an argument called"
                           " 'options', but got {}".format(sig.arguments))
        self.option_hooks.append(function)
        return function
Exemple #15
0
def test_construct_arguments_with_kwargs_for_posargs_does_not_raise():
    Signature(bariza).construct_arguments([], {"a": 4, "b": 3, "c": 2}, {})
    s = Signature(FunCTIonWithCAPItals)
    s.construct_arguments([], {"a": 4, "b": 3, "c": 2, "d": 6}, {})
Exemple #16
0
def test_unicode_special():
    str_signature = "complex_function_name(a=5, b='fo', c=9)"
    assert str_signature in str(Signature(complex_function_name))
Exemple #17
0
def test_repr_(name, func):
    regex = "<Signature at 0x[0-9a-fA-F]+ for '%s'>"
    assert re.match(regex % name, Signature(func).__repr__())
Exemple #18
0
def test_construct_arguments_for_bound_method():
    s = Signature(SomeClass.bla)
    args, kwargs = s.construct_arguments([1], {"b": 2}, {"c": 3}, bound=True)
    assert args == [1]
    assert kwargs == {"b": 2, "c": 3}
Exemple #19
0
def test_unicode_(func, expected):
    assert str(Signature(func)) == expected
Exemple #20
0
def test_construct_arguments_does_not_raise_if_all_args_filled():
    s = Signature(bariza)
    s.construct_arguments([1, 2, 3], {}, {})
    s.construct_arguments([1, 2], {"c": 6}, {})
    s.construct_arguments([1], {"b": 6, "c": 6}, {})
    s.construct_arguments([], {"a": 6, "b": 6, "c": 6}, {})
Exemple #21
0
def test_construct_arguments_does_not_raise_for_missing_defaults():
    s = Signature(complex_function_name)
    s.construct_arguments([], {}, {})
Exemple #22
0
def test_construct_arguments_overwrites_defaults():
    s = Signature(complex_function_name)
    args, kwargs = s.construct_arguments([], {}, {"a": 11, "b": 12, "c": 7})
    assert args == []
    assert kwargs == {"a": 11, "b": 12, "c": 7}
Exemple #23
0
def test_constructor_extract_kwargs(function, kwargs):
    s = Signature(function)
    assert s.kwargs == kwargs
Exemple #24
0
def test_construct_arguments_ignores_excess_options():
    s = Signature(bariza)
    args, kwargs = s.construct_arguments([2], {"b": 4}, {"c": 6, "foo": 9, "bar": 0})
    assert args == [2]
    assert kwargs == {"b": 4, "c": 6}
Exemple #25
0
def test_construct_arguments_does_not_overwrite_args_and_kwargs():
    s = Signature(bariza)
    args, kwargs = s.construct_arguments([1, 2], {"c": 3}, {"a": 6, "b": 6, "c": 6})
    assert args == [1, 2]
    assert kwargs == {"c": 3}
Exemple #26
0
def test_construct_arguments_with_unexpected_kwargs_raises_typeerror(function):
    kwargs = {"zimbabwe": 23}
    unexpected = re.compile(".*unexpected.*zimbabwe.*")
    with pytest.raises(TypeError) as excinfo:
        Signature(function).construct_arguments([], kwargs, {})
    assert unexpected.match(excinfo.value.args[0])
Exemple #27
0
def test_construct_arguments_with_unexpected_args_raises_typeerror(func, args):
    unexpected = re.compile(".*unexpected.*")
    with pytest.raises(TypeError) as excinfo:
        Signature(func).construct_arguments(args, {}, {})
    assert unexpected.match(excinfo.value.args[0])
Exemple #28
0
def test_construct_arguments_with_kwargswildcard_doesnt_raise():
    kwargs = {"zimbabwe": 23}
    Signature(__double_underscore__).construct_arguments([1, 2], kwargs, {})
    Signature(FunCTIonWithCAPItals).construct_arguments([1, 2, 3], kwargs, {})
Exemple #29
0
def test_construct_arguments_with_varargs_doesnt_raise():
    Signature(generic).construct_arguments([1, 2, 3], {}, {})
    Signature(__double_underscore__).construct_arguments([1, 2, 3, 4, 5], {}, {})
    Signature(_name_with_underscore_).construct_arguments([1, 2, 3, 4], {}, {})
Exemple #30
0
def test_construct_arguments_with_expected_kwargs_does_not_raise():
    s = Signature(complex_function_name)
    s.construct_arguments([], {"a": 4, "b": 3, "c": 2}, {})
    s = Signature(FunCTIonWithCAPItals)
    s.construct_arguments([1, 2], {"c": 5}, {})