SomeClass   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 3
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A bla() 0 2 1
1
#!/usr/bin/env python
2
# coding=utf-8
3
from __future__ import division, print_function
4
5
import re
6
7
import pytest
8
from sacred.config.signature import Signature
9
10
11
# #############  function definitions to test on ##############################
12
def foo():
13
    return
14
15
16
def bariza(a, b, c):
17
    return a, b, c
18
19
20
def complex_function_name(a=1, b='fo', c=9):
21
    return a, b, c
22
23
24
# noinspection PyPep8Naming
25
def FunCTIonWithCAPItals(a, b, c=3, **kwargs):
26
    return a, b, c, kwargs
27
28
29
def _name_with_underscore_(fo, bar, *baz):
30
    return fo, bar, baz
31
32
33
def __double_underscore__(man, o, *men, **oo):
34
    return man, o, men, oo
35
36
37
def old_name(verylongvariablename):
38
    return verylongvariablename
39
40
41
def generic(*args, **kwargs):
42
    return args, kwargs
43
44
45
def onlykwrgs(**kwargs):
46
    return kwargs
47
48
49
renamed = old_name
50
51
functions = [foo, bariza, complex_function_name, FunCTIonWithCAPItals,
52
             _name_with_underscore_, __double_underscore__, old_name, renamed]
53
54
ids = ['foo', 'bariza', 'complex_function_name', 'FunCTIonWithCAPItals',
55
       '_name_with_underscore_', '__double_underscore__', 'old_name',
56
       'renamed']
57
58
names = ['foo', 'bariza', 'complex_function_name', 'FunCTIonWithCAPItals',
59
         '_name_with_underscore_', '__double_underscore__', 'old_name',
60
         'old_name']
61
62
arguments = [[], ['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', 'c'],
63
             ['fo', 'bar'], ['man', 'o'], ['verylongvariablename'],
64
             ['verylongvariablename']]
65
66
vararg_names = [None, None, None, None, 'baz', 'men', None, None]
67
68
kw_wc_names = [None, None, None, 'kwargs', None, 'oo', None, None]
69
70
pos_arguments = [[], ['a', 'b', 'c'], [], ['a', 'b'], ['fo', 'bar'],
71
                 ['man', 'o'], ['verylongvariablename'],
72
                 ['verylongvariablename']]
73
74
kwarg_list = [{}, {}, {'a': 1, 'b': 'fo', 'c': 9}, {'c': 3}, {}, {}, {}, {}]
75
76
77
class SomeClass(object):
78
    def bla(self, a, b, c):
79
        return a, b, c
80
81
82
# #######################  Tests  #############################################
83
84
@pytest.mark.parametrize("function, name", zip(functions, names), ids=ids)
85
def test_constructor_extract_function_name(function, name):
86
        s = Signature(function)
87
        assert s.name == name
88
89
90
@pytest.mark.parametrize("function, args", zip(functions, arguments), ids=ids)
91
def test_constructor_extracts_all_arguments(function, args):
92
        s = Signature(function)
93
        assert s.arguments == args
94
95
96
@pytest.mark.parametrize("function, vararg", zip(functions, vararg_names),
97
                         ids=ids)
98
def test_constructor_extract_vararg_name(function, vararg):
99
        s = Signature(function)
100
        assert s.vararg_name == vararg
101
102
103
@pytest.mark.parametrize("function, kw_wc", zip(functions, kw_wc_names),
104
                         ids=ids)
105
def test_constructor_extract_kwargs_wildcard_name(function, kw_wc):
106
        s = Signature(function)
107
        assert s.kw_wildcard_name == kw_wc
108
109
110
@pytest.mark.parametrize("function, pos_args", zip(functions, pos_arguments),
111
                         ids=ids)
112
def test_constructor_extract_positional_arguments(function, pos_args):
113
        s = Signature(function)
114
        assert s.positional_args == pos_args
115
116
117
@pytest.mark.parametrize("function, kwargs",
118
                         zip(functions, kwarg_list),
119
                         ids=ids)
120
def test_constructor_extract_kwargs(function, kwargs):
121
        s = Signature(function)
122
        assert s.kwargs == kwargs
123
124
125
def test_get_free_parameters():
126
    free = Signature(foo).get_free_parameters([], {})
127
    assert free == []
128
    free = Signature(bariza).get_free_parameters([], {'c': 3})
129
    assert free == ['a', 'b']
130
    free = Signature(complex_function_name).get_free_parameters([], {})
131
    assert free == ['a', 'b', 'c']
132
    free = Signature(_name_with_underscore_).get_free_parameters([], {})
133
    assert free == ['fo', 'bar']
134
    s = Signature(__double_underscore__)
135
    assert s.get_free_parameters([1, 2, 3], {}) == []
136
137
138
@pytest.mark.parametrize('function',
139
                         [foo, bariza, complex_function_name,
140
                          _name_with_underscore_, old_name, renamed])
141
def test_construct_arguments_with_unexpected_kwargs_raises_typeerror(function):
142
    kwargs = {'zimbabwe': 23}
143
    unexpected = re.compile(".*unexpected.*zimbabwe.*")
144
    with pytest.raises(TypeError) as excinfo:
145
        Signature(function).construct_arguments([], kwargs, {})
146
    assert unexpected.match(excinfo.value.args[0])
147
148
149
@pytest.mark.parametrize('func,args', [
150
    (foo, [1]),
151
    (bariza, [1, 2, 3, 4]),
152
    (complex_function_name, [1, 2, 3, 4]),
153
    (old_name, [1, 2]),
154
    (renamed, [1, 2])
155
])
156
def test_construct_arguments_with_unexpected_args_raises_typeerror(func, args):
157
    unexpected = re.compile(".*unexpected.*")
158
    with pytest.raises(TypeError) as excinfo:
159
        Signature(func).construct_arguments(args, {}, {})
160
    assert unexpected.match(excinfo.value.args[0])
161
162
163
def test_construct_arguments_with_varargs_doesnt_raise():
164
    Signature(generic).construct_arguments([1, 2, 3], {}, {})
165
    Signature(__double_underscore__).construct_arguments(
166
        [1, 2, 3, 4, 5], {}, {})
167
    Signature(_name_with_underscore_).construct_arguments(
168
        [1, 2, 3, 4], {}, {})
169
170
171
def test_construct_arguments_with_kwargswildcard_doesnt_raise():
172
    kwargs = {'zimbabwe': 23}
173
    Signature(FunCTIonWithCAPItals).construct_arguments(
174
        [1, 2, 3], kwargs, {})
175
    Signature(__double_underscore__).construct_arguments([1, 2], kwargs, {})
176
177
178
def test_construct_arguments_with_expected_kwargs_does_not_raise():
179
    s = Signature(complex_function_name)
180
    s.construct_arguments([], {'a': 4, 'b': 3, 'c': 2}, {})
181
    s = Signature(FunCTIonWithCAPItals)
182
    s.construct_arguments([1, 2], {'c': 5}, {})
183
184
185
def test_construct_arguments_with_kwargs_for_posargs_does_not_raise():
186
    Signature(bariza).construct_arguments([], {'a': 4, 'b': 3, 'c': 2}, {})
187
    s = Signature(FunCTIonWithCAPItals)
188
    s.construct_arguments([], {'a': 4, 'b': 3, 'c': 2, 'd': 6}, {})
189
190
191
def test_construct_arguments_with_duplicate_args_raises_typeerror():
192
    multiple_values = re.compile(".*multiple values.*")
193
    with pytest.raises(TypeError) as excinfo:
194
        s = Signature(bariza)
195
        s.construct_arguments([1, 2, 3], {'a': 4, 'b': 5}, {})
196
    assert multiple_values.match(excinfo.value.args[0])
197
    with pytest.raises(TypeError) as excinfo:
198
        s = Signature(complex_function_name)
199
        s.construct_arguments([1], {'a': 4}, {})
200
    assert multiple_values.match(excinfo.value.args[0])
201
    with pytest.raises(TypeError) as excinfo:
202
        s = Signature(FunCTIonWithCAPItals)
203
        s.construct_arguments([1, 2, 3], {'c': 6}, {})
204
    assert multiple_values.match(excinfo.value.args[0])
205
206
207
def test_construct_arguments_without_duplicates_passes():
208
    s = Signature(bariza)
209
    s.construct_arguments([1, 2], {'c': 5}, {})
210
211
    s = Signature(complex_function_name)
212
    s.construct_arguments([1], {'b': 4}, {})
213
214
    s = Signature(FunCTIonWithCAPItals)
215
    s.construct_arguments([], {'a': 6, 'b': 6, 'c': 6}, {})
216
217
218
def test_construct_arguments_without_options_returns_same_args_kwargs():
219
    s = Signature(foo)
220
    args, kwargs = s.construct_arguments([], {}, {})
221
    assert args == []
222
    assert kwargs == {}
223
224
    s = Signature(bariza)
225
    args, kwargs = s.construct_arguments([2, 4, 6], {}, {})
226
    assert args == [2, 4, 6]
227
    assert kwargs == {}
228
229
    s = Signature(complex_function_name)
230
    args, kwargs = s.construct_arguments([2], {'c': 6, 'b': 7}, {})
231
    assert args == [2]
232
    assert kwargs == {'c': 6, 'b': 7}
233
234
    s = Signature(_name_with_underscore_)
235
    args, kwargs = s.construct_arguments([], {'fo': 7, 'bar': 6}, {})
236
    assert args == []
237
    assert kwargs == {'fo': 7, 'bar': 6}
238
239
240
def test_construct_arguments_completes_kwargs_from_options():
241
    s = Signature(bariza)
242
    args, kwargs = s.construct_arguments([2, 4], {}, {'c': 6})
243
    assert args == [2, 4]
244
    assert kwargs == {'c': 6}
245
    s = Signature(complex_function_name)
246
    args, kwargs = s.construct_arguments([], {'c': 6, 'b': 7}, {'a': 1})
247
    assert args == []
248
    assert kwargs == {'a': 1, 'c': 6, 'b': 7}
249
250
    s = Signature(_name_with_underscore_)
251
    args, kwargs = s.construct_arguments([], {}, {'fo': 7, 'bar': 6})
252
    assert args == []
253
    assert kwargs == {'fo': 7, 'bar': 6}
254
255
256
def test_construct_arguments_ignores_excess_options():
257
    s = Signature(bariza)
258
    args, kwargs = s.construct_arguments([2], {'b': 4},
259
                                         {'c': 6, 'foo': 9, 'bar': 0})
260
    assert args == [2]
261
    assert kwargs == {'b': 4, 'c': 6}
262
263
264
def test_construct_arguments_does_not_overwrite_args_and_kwargs():
265
    s = Signature(bariza)
266
    args, kwargs = s.construct_arguments([1, 2], {'c': 3},
267
                                         {'a': 6, 'b': 6, 'c': 6})
268
    assert args == [1, 2]
269
    assert kwargs == {'c': 3}
270
271
272
def test_construct_arguments_overwrites_defaults():
273
    s = Signature(complex_function_name)
274
    args, kwargs = s.construct_arguments([], {}, {'a': 11, 'b': 12, 'c': 7})
275
    assert args == []
276
    assert kwargs == {'a': 11, 'b': 12, 'c': 7}
277
278
279
def test_construct_arguments_raises_if_args_unfilled():
280
    s = Signature(bariza)
281
    missing = re.compile(".*missing.*")
282
    with pytest.raises(TypeError) as excinfo:
283
        s.construct_arguments([], {}, {})
284
    assert missing.match(excinfo.value.args[0])
285
    with pytest.raises(TypeError) as excinfo:
286
        s.construct_arguments([1, 2], {}, {})
287
    assert missing.match(excinfo.value.args[0])
288
    with pytest.raises(TypeError) as excinfo:
289
        s.construct_arguments([1], {'b': 3}, {})
290
    assert missing.match(excinfo.value.args[0])
291
    with pytest.raises(TypeError) as excinfo:
292
        s.construct_arguments([1], {'c': 5}, {})
293
    assert missing.match(excinfo.value.args[0])
294
295
296
def test_construct_arguments_does_not_raise_if_all_args_filled():
297
    s = Signature(bariza)
298
    s.construct_arguments([1, 2, 3], {}, {})
299
    s.construct_arguments([1, 2], {'c': 6}, {})
300
    s.construct_arguments([1], {'b': 6, 'c': 6}, {})
301
    s.construct_arguments([], {'a': 6, 'b': 6, 'c': 6}, {})
302
303
304
def test_construct_arguments_does_not_raise_for_missing_defaults():
305
    s = Signature(complex_function_name)
306
    s.construct_arguments([], {}, {})
307
308
309
def test_construct_arguments_for_bound_method():
310
    s = Signature(SomeClass.bla)
311
    args, kwargs = s.construct_arguments([1], {'b': 2}, {'c': 3}, bound=True)
312
    assert args == [1]
313
    assert kwargs == {'b': 2, 'c': 3}
314
315
316
@pytest.mark.parametrize('func,expected', [
317
    (foo, "foo()"),
318
    (bariza, "bariza(a, b, c)"),
319
    (FunCTIonWithCAPItals, "FunCTIonWithCAPItals(a, b, c=3, **kwargs)"),
320
    (_name_with_underscore_, "_name_with_underscore_(fo, bar, *baz)"),
321
    (__double_underscore__, "__double_underscore__(man, o, *men, **oo)"),
322
    (old_name, "old_name(verylongvariablename)"),
323
    (renamed, "old_name(verylongvariablename)"),
324
    (generic, "generic(*args, **kwargs)"),
325
    (onlykwrgs, "onlykwrgs(**kwargs)")
326
])
327
def test_unicode_(func, expected):
328
    assert Signature(func).__unicode__() == expected
329
330
331
def test_unicode_special():
332
    assert re.match("complex_function_name\(a=1, b=u?'fo', c=9\)",
333
                    Signature(complex_function_name).__unicode__())
334
335
336
@pytest.mark.parametrize('name,func', zip(names, functions))
337
def test_repr_(name, func):
338
    regex = "<Signature at 0x[0-9a-fA-F]+ for '%s'>"
339
    assert re.match(regex % name, Signature(func).__repr__())
340