1
|
|
|
# pylint: disable=missing-docstring,unused-variable,expression-not-assigned |
2
|
|
|
|
3
|
|
|
import pytest |
4
|
|
|
from expecter import expect |
5
|
|
|
|
6
|
|
|
from yorm.types import Object, String, Integer, Float, Boolean |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
def describe_object(): |
10
|
|
|
|
11
|
|
|
obj = None |
12
|
|
|
pairs = [ |
13
|
|
|
(obj, obj), |
14
|
|
|
(None, None), |
15
|
|
|
(1, 1), |
16
|
|
|
(4.2, 4.2), |
17
|
|
|
(['a', 'b', 'c'], ['a', 'b', 'c']), |
18
|
|
|
] |
19
|
|
|
|
20
|
|
|
def describe_to_value(): |
21
|
|
|
|
22
|
|
|
@pytest.mark.parametrize("first,second", pairs) |
23
|
|
|
def it_converts_correctly(first, second): |
24
|
|
|
expect(Object.to_value(first)) == second |
25
|
|
|
|
26
|
|
|
def describe_to_data(): |
27
|
|
|
|
28
|
|
|
@pytest.mark.parametrize("first,second", pairs) |
29
|
|
|
def it_converts_correctly(first, second): |
30
|
|
|
expect(Object.to_data(first)) == second |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
def describe_string(): |
34
|
|
|
|
35
|
|
|
obj = "Hello, world!" |
36
|
|
|
pairs = [ |
37
|
|
|
(obj, obj), |
38
|
|
|
(None, ""), |
39
|
|
|
("1.2.3", "1.2.3"), |
40
|
|
|
(['a', 'b', 'c'], "a, b, c"), |
41
|
|
|
] |
42
|
|
|
pairs_to_value = pairs + [ |
43
|
|
|
(1, "1"), |
44
|
|
|
(4.2, "4.2"), |
45
|
|
|
(False, "false"), |
46
|
|
|
(True, "true"), |
47
|
|
|
] |
48
|
|
|
pairs_to_data = pairs + [ |
49
|
|
|
(42, 42), |
50
|
|
|
(4.2, 4.2), |
51
|
|
|
("true", True), |
52
|
|
|
("false", False), |
53
|
|
|
] |
54
|
|
|
|
55
|
|
|
def describe_to_value(): |
56
|
|
|
|
57
|
|
|
@pytest.mark.parametrize("first,second", pairs_to_value) |
58
|
|
|
def it_converts_correctly(first, second): |
59
|
|
|
expect(String.to_value(first)) == second |
60
|
|
|
|
61
|
|
|
def describe_to_data(): |
62
|
|
|
|
63
|
|
|
@pytest.mark.parametrize("first,second", pairs_to_data) |
64
|
|
|
def it_converts_correctly(first, second): |
65
|
|
|
expect(String.to_data(first)) == second |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
def describe_integer(): |
69
|
|
|
|
70
|
|
|
obj = 42 |
71
|
|
|
pairs = [ |
72
|
|
|
(obj, obj), |
73
|
|
|
(None, 0), |
74
|
|
|
("1", 1), |
75
|
|
|
("1.1", 1), |
76
|
|
|
(True, 1), |
77
|
|
|
(False, 0), |
78
|
|
|
] |
79
|
|
|
|
80
|
|
|
def describe_to_value(): |
81
|
|
|
|
82
|
|
|
@pytest.mark.parametrize("first,second", pairs) |
83
|
|
|
def it_converts_correctly(first, second): |
84
|
|
|
expect(Integer.to_value(first)) == second |
85
|
|
|
|
86
|
|
|
def it_raises_an_exception_when_unable_to_convert(): |
87
|
|
|
with expect.raises(ValueError): |
88
|
|
|
Integer.to_value("abc") |
89
|
|
|
|
90
|
|
|
def describe_to_data(): |
91
|
|
|
|
92
|
|
|
@pytest.mark.parametrize("first,second", pairs) |
93
|
|
|
def it_converts_correctly(first, second): |
94
|
|
|
expect(Integer.to_data(first)) == second |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
def describe_float(): |
98
|
|
|
|
99
|
|
|
obj = 4.2 |
100
|
|
|
pairs = [ |
101
|
|
|
(obj, obj), |
102
|
|
|
(None, 0.0), |
103
|
|
|
("1.0", 1.0), |
104
|
|
|
("1.1", 1.1), |
105
|
|
|
] |
106
|
|
|
|
107
|
|
|
def describe_to_value(): |
108
|
|
|
|
109
|
|
|
@pytest.mark.parametrize("first,second", pairs) |
110
|
|
|
def it_converts_correctly(first, second): |
111
|
|
|
expect(Float.to_value(first)) == second |
112
|
|
|
|
113
|
|
|
def it_raises_an_exception_when_unable_to_convert(): |
114
|
|
|
with expect.raises(ValueError): |
115
|
|
|
Float.to_value("abc") |
116
|
|
|
|
117
|
|
|
def describe_to_data(): |
118
|
|
|
|
119
|
|
|
@pytest.mark.parametrize("first,second", pairs) |
120
|
|
|
def it_converts_correctly(first, second): |
121
|
|
|
expect(Float.to_data(first)) == second |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
def describe_boolean(): |
125
|
|
|
|
126
|
|
|
obj = True |
127
|
|
|
pairs = [ |
128
|
|
|
(obj, obj), |
129
|
|
|
(None, False), |
130
|
|
|
(0, False), |
131
|
|
|
(1, True), |
132
|
|
|
("", False), |
133
|
|
|
("True", True), |
134
|
|
|
("False", False), |
135
|
|
|
("true", True), |
136
|
|
|
("false", False), |
137
|
|
|
("T", True), |
138
|
|
|
("F", False), |
139
|
|
|
("yes", True), |
140
|
|
|
("no", False), |
141
|
|
|
("Y", True), |
142
|
|
|
("N", False), |
143
|
|
|
("enabled", True), |
144
|
|
|
("disabled", False), |
145
|
|
|
("on", True), |
146
|
|
|
("off", False), |
147
|
|
|
("Hello, world!", True) |
148
|
|
|
] |
149
|
|
|
|
150
|
|
|
def describe_to_value(): |
151
|
|
|
|
152
|
|
|
@pytest.mark.parametrize("first,second", pairs) |
153
|
|
|
def it_converts_correctly(first, second): |
154
|
|
|
expect(Boolean.to_value(first)) == second |
155
|
|
|
|
156
|
|
|
def describe_to_data(): |
157
|
|
|
|
158
|
|
|
@pytest.mark.parametrize("first,second", pairs) |
159
|
|
|
def it_converts_correctly(first, second): |
160
|
|
|
expect(Boolean.to_data(first)) == second |
161
|
|
|
|