-
Notifications
You must be signed in to change notification settings - Fork 29
/
test_utilities.py
69 lines (51 loc) · 1.89 KB
/
test_utilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from collections.abc import Iterable
import numpy as np
import pytest
from psyneulink.core.globals.utilities import convert_all_elements_to_np_array, prune_unused_args
@pytest.mark.parametrize(
'arr, expected',
[
([[0], [0, 0]], np.array([np.array([0]), np.array([0, 0])], dtype=object)),
# should test these but numpy cannot easily create an array from them
# [np.ones((2,2)), np.zeros((2,1))]
# [np.array([[0]]), np.array([[[ 1., 1., 1.], [ 1., 1., 1.]]])]
]
)
def test_convert_all_elements_to_np_array(arr, expected):
converted = convert_all_elements_to_np_array(arr)
# no current numpy methods can test this
def check_equality_recursive(arr, expected):
if (
not isinstance(arr, Iterable)
or (isinstance(arr, np.ndarray) and arr.ndim == 0)
):
assert arr == expected
else:
assert isinstance(expected, type(arr))
assert len(arr) == len(expected)
for i in range(len(arr)):
check_equality_recursive(arr[i], expected[i])
check_equality_recursive(converted, expected)
def f():
pass
def g(a):
pass
def h(a, b=None):
pass
def i(b=None):
pass
@pytest.mark.parametrize(
'func, args, kwargs, expected_pruned_args, expected_pruned_kwargs', [
(f, 1, {'a': 1}, [], {}),
(g, 1, {'x': 1}, [1], {}),
(g, None, {'a': 1}, [], {'a': 1}),
(h, None, {'a': 1, 'b': 1, 'c': 1}, [], {'a': 1, 'b': 1}),
(h, [1, 2, 3], None, [1], {}),
(i, None, {'a': 1, 'b': 1, 'c': 1}, [], {'b': 1}),
(i, [1, 2, 3], None, [], {}),
]
)
def test_prune_unused_args(func, args, kwargs, expected_pruned_args, expected_pruned_kwargs):
pruned_args, pruned_kwargs = prune_unused_args(func, args, kwargs)
assert pruned_args == expected_pruned_args
assert pruned_kwargs == expected_pruned_kwargs