|
3 | 3 | from functools import wraps
|
4 | 4 |
|
5 | 5 | from easypy.decorations import lazy_decorator
|
| 6 | +from easypy.decorations import ensure_same_defaults, DefaultsMismatch |
6 | 7 | from easypy.misc import kwargs_resilient
|
7 | 8 |
|
8 | 9 |
|
@@ -136,3 +137,56 @@ def counter(self):
|
136 | 137 | foo2.ts += 1
|
137 | 138 | assert [foo1.inc(), foo2.inc()] == [2, 2]
|
138 | 139 | assert [foo1.counter, foo2.counter] == [1, 2] # foo1 was not updated since last sync - only foo2
|
| 140 | + |
| 141 | + |
| 142 | +def test_ensure_same_defaults(): |
| 143 | + def foo(a=1, b=2, c=3): |
| 144 | + return a, b, c |
| 145 | + |
| 146 | + |
| 147 | + @ensure_same_defaults(foo) |
| 148 | + def bar(a=1, b=2, c=3): |
| 149 | + return a, b, c |
| 150 | + |
| 151 | + # Test we did not change the actual function |
| 152 | + assert foo() == bar() |
| 153 | + assert foo(4, 5, 6) == bar(4, 5, 6) |
| 154 | + |
| 155 | + |
| 156 | + with pytest.raises(DefaultsMismatch) as exc: |
| 157 | + @ensure_same_defaults(foo) |
| 158 | + def baz(a=1, b=3, c=2): |
| 159 | + pass |
| 160 | + assert exc.value.param_names == ['b', 'c'] |
| 161 | + |
| 162 | + |
| 163 | +def test_ensure_same_defaults_skipping_params_with_no_default(): |
| 164 | + @ensure_same_defaults(lambda a=1, b=2: ...) |
| 165 | + def foo(a, b=2): |
| 166 | + pass |
| 167 | + |
| 168 | + @ensure_same_defaults(lambda a, b=2: ...) |
| 169 | + def foo(a=1, b=2): |
| 170 | + pass |
| 171 | + |
| 172 | + @ensure_same_defaults(lambda a, b=2: ...) |
| 173 | + def foo(a=1, c=3): |
| 174 | + pass |
| 175 | + |
| 176 | + with pytest.raises(DefaultsMismatch) as exc: |
| 177 | + @ensure_same_defaults(lambda a, b=2: ...) |
| 178 | + def foo(a, b=4): |
| 179 | + pass |
| 180 | + assert exc.value.param_names == ['b'] |
| 181 | + |
| 182 | + |
| 183 | +def test_ensure_same_defaults_ignore(): |
| 184 | + @ensure_same_defaults(lambda a=1, b=2: ..., ignore=('b',)) |
| 185 | + def foo(a=1, b=3): |
| 186 | + pass |
| 187 | + |
| 188 | + with pytest.raises(DefaultsMismatch) as exc: |
| 189 | + @ensure_same_defaults(lambda a=1, b=2: ..., ignore=('b',)) |
| 190 | + def foo(a=2, b=3): |
| 191 | + pass |
| 192 | + assert exc.value.param_names == ['a'] |
0 commit comments