1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
import pytest |
4
|
|
|
|
5
|
|
|
from .typeconv import str2bool |
6
|
|
|
from .typeconv import bool201 |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
class Test_str2bool(object): |
10
|
|
|
@pytest.mark.parametrize('s, d, exp', [ |
11
|
|
|
(True, None, True), |
12
|
|
|
('1', None, True), |
13
|
|
|
('t', None, True), |
14
|
|
|
('yEs', None, True), |
15
|
|
|
('true', None, True), |
16
|
|
|
('TRUE', None, True), |
17
|
|
|
('oN', None, True), |
18
|
|
|
|
19
|
|
|
(False, None, False), |
20
|
|
|
('0', None, False), |
21
|
|
|
('f', None, False), |
22
|
|
|
('n', None, False), |
23
|
|
|
('FaLse', None, False), |
24
|
|
|
('nO', None, False), |
25
|
|
|
('Off', None, False), |
26
|
|
|
]) |
27
|
|
|
def test_str2bool_ignore_default(self, s, d, exp): |
28
|
|
|
act = str2bool(s) |
29
|
|
|
assert act == exp |
30
|
|
|
|
31
|
|
|
act = str2bool(s, d) |
32
|
|
|
assert act == exp |
33
|
|
|
|
34
|
|
|
@pytest.mark.parametrize("s, d, exp", [ |
35
|
|
|
(None, 3, 3), # noqa: E241 |
36
|
|
|
('01', -1, -1), # noqa: E241 |
37
|
|
|
('fka', -2, -2), # noqa: E241 |
38
|
|
|
('-1', -3, -3), # noqa: E241 |
39
|
|
|
]) |
40
|
|
|
def test_str2bool_meaningful_defaults(self, s, d, exp): |
41
|
|
|
act = str2bool(s, default=d) |
42
|
|
|
assert act == exp |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
class Test_bool201(object): |
46
|
|
|
@pytest.mark.parametrize('b, exp', [ |
47
|
|
|
(True, '1'), |
48
|
|
|
(False, '0'), |
49
|
|
|
]) |
50
|
|
|
def test_normal(self, b, exp): |
51
|
|
|
act = bool201(b) |
52
|
|
|
|
53
|
|
|
assert act == exp |
54
|
|
|
|
55
|
|
|
@pytest.mark.parametrize('b', [ |
56
|
|
|
(None), |
57
|
|
|
]) |
58
|
|
|
def test_raises_TypeError(self, b): |
59
|
|
|
with pytest.raises(TypeError): |
60
|
|
|
bool201(b) |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
class TestFunctionCompositions(object): |
64
|
|
|
|
65
|
|
|
@pytest.mark.parametrize('b', [ |
66
|
|
|
(u'0'), |
67
|
|
|
(u'1'), |
68
|
|
|
]) |
69
|
|
|
def test_bool201_str2bool(self, b): |
70
|
|
|
act = bool201(str2bool(b)) |
71
|
|
|
|
72
|
|
|
assert act == b |
73
|
|
|
|
74
|
|
|
@pytest.mark.parametrize('b', [ |
75
|
|
|
(False), |
76
|
|
|
(True), |
77
|
|
|
]) |
78
|
|
|
def test_str2bool_bool201(self, b): |
79
|
|
|
act = str2bool(bool201(b)) |
80
|
|
|
|
81
|
|
|
assert act == b |
82
|
|
|
|