-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_functions.py
101 lines (65 loc) · 2.11 KB
/
test_functions.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
90
91
92
93
94
95
96
97
98
99
100
101
from __future__ import division
import math
import variants
from ._division_data import DivisionData
import pytest
import six
###
# Example implementation - division function
@variants.primary
def divide(x, y):
"""A function that divides x by y."""
return x / y
@divide.variant('round')
def divide(x, y):
"""A version of divide that also rounds."""
return round(x / y)
@divide.variant('round_callmain')
def divide(x, y):
return round(divide(x, y))
@divide.variant('floor')
def divide(x, y):
return math.floor(divide(x, y))
@divide.variant('ceil')
def divide(x, y):
return math.ceil(divide(x, y))
@divide.variant('mode')
def divide(x, y, mode=None):
funcs = {
None: divide,
'round': divide.round,
'floor': divide.floor,
'ceil': divide.ceil
}
return funcs[mode](x, y)
###
# Division Function Tests
@pytest.mark.parametrize('x, y, expected', DivisionData.DIV_VALS)
def test_main(x, y, expected):
assert divide(x, y) == expected
@pytest.mark.parametrize('x,y,expected', DivisionData.ROUND_VALS)
def test_round(x, y, expected):
assert divide.round(x, y) == expected
@pytest.mark.parametrize('x,y,expected', DivisionData.ROUND_VALS)
def test_round_callmain(x, y, expected):
assert divide.round_callmain(x, y) == expected
@pytest.mark.parametrize('x,y,expected', DivisionData.FLOOR_VALS)
def test_floor(x, y, expected):
assert divide.floor(x, y) == expected
@pytest.mark.parametrize('x,y,expected', DivisionData.CEIL_VALS)
def test_ceil(x, y, expected):
assert divide.ceil(x, y) == expected
@pytest.mark.parametrize('x,y,expected,mode', DivisionData.MODE_VALS)
def test_mode(x, y, expected, mode):
assert divide.mode(x, y, mode) == expected
###
# Division function metadata tests
def test_name():
assert divide.__name__ == 'divide'
@pytest.mark.skipif(six.PY2, reason='__qualname__ introduced in Python 3.3')
def test_qualname():
assert divide.__qualname__ == 'divide'
def test_docstring():
assert divide.__doc__ == """A function that divides x by y."""
def test_repr():
assert repr(divide) == '<VariantFunction divide>'