-
Notifications
You must be signed in to change notification settings - Fork 3
/
noglobtests.py
92 lines (55 loc) · 1.74 KB
/
noglobtests.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import pytest
from giveme.injector import Injector
@pytest.fixture
def gm():
return Injector()
def simple_dep():
return 42
def double_dep(simple_dep):
return simple_dep*2
def double_f(a, b, c, double_dep):
return a, b, c, double_dep
def simple_f(a, b, c, simple_dep):
return a, b, c, simple_dep
def kwargs_f(a, b, c, simple_dep, d=4):
return a, b, c, simple_dep, d
def varargs_f(*args, simple_dep):
return (*args, simple_dep)
class DepClass:
def __init__(self):
self.param = 42
def test_simple_f(gm):
gm.register(simple_dep)
result = gm.inject(simple_f)(11, 22, 33)
assert result == (11, 22, 33, 42)
def test_kwargs_f(gm):
gm.register(simple_dep)
result = gm.inject(kwargs_f)(1, 2, 3, d=5)
assert result == (1, 2, 3, 42, 5)
def test_dep_override_positional(gm):
gm.register(simple_dep)
result = gm.inject(simple_f)(1, 2, 3, 4)
assert result == (1, 2, 3, 4)
def test_dep_override_kw(gm):
gm.register(simple_dep)
result = gm.inject(simple_f)(1, 2, 3, simple_dep=4)
assert result == (1, 2, 3, 4)
def test_nested_dep(gm):
gm.register(simple_dep)
gm.register(gm.inject(double_dep))
result = gm.inject(double_f)(1, 2, 3)
assert result == (1, 2, 3, 42*2)
def test_with_varargs(gm):
gm.register(simple_dep)
result = gm.inject(varargs_f)(1, 2, 3, 4)
assert result == (1, 2, 3, 4, 42)
def test_class_dependency(gm):
gm.register(DepClass, name='simple_dep')
result = gm.inject(simple_f)(1, 2, 3)
assert result[:3] == (1, 2, 3)
assert isinstance(result[3], DepClass)
assert result[3].param == 42
def test_class_injection(gm):
gm.register(simple_dep)
class Thinger:
param = gm.resolve('simple_dep')