-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_single_dispatch.py
63 lines (40 loc) · 1.17 KB
/
test_single_dispatch.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
from __future__ import division
import pytest
try:
from functools import singledispatch
except ImportError:
from singledispatch import singledispatch
import variants
###
# Example implementation - single dispatched function
@variants.primary
@singledispatch
def add_one(arg):
return arg + 1
@add_one.variant('from_list')
@add_one.register(list)
def add_one(arg):
return arg + [1]
@add_one.variant('from_tuple')
@add_one.register(tuple)
def add_one(arg):
return arg + (1,)
### Tests
def test_single_dispatch_int():
assert add_one(1) == 2
def test_single_dispatch_list():
assert add_one([2]) == [2, 1]
def test_single_dispatch_tuple():
assert add_one((2,)) == (2, 1)
def test_dispatch_list_variant_succeeds():
assert add_one.from_list([4]) == [4, 1]
@pytest.mark.parametrize('arg', [3, (2,)])
def test_dispatch_list_variant_fails(arg):
with pytest.raises(TypeError):
add_one.from_list(arg)
def test_dispatch_tuple_variant_succeeds():
assert add_one.from_tuple((2,)) == (2, 1)
@pytest.mark.parametrize('arg', [3, [2]])
def test_dispatch_tuple_variant_fails(arg):
with pytest.raises(TypeError):
add_one.from_tuple(arg)