-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fstrings.py
More file actions
202 lines (141 loc) · 4.83 KB
/
test_fstrings.py
File metadata and controls
202 lines (141 loc) · 4.83 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import unittest
import fstrings
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
from fstrings import f, fdocstring, printf
URL = '/api/v1.0'
NUM = 10
class OBJ: NUM = 20
STRING = 'STRING'
class Test_f(unittest.TestCase):
def test_args_kwargs(self):
'''f format with args kwargs'''
class LCLOBJ: NUM = 20
expected = '/api/v1.0/10/STRING/020'
result = f(
'{}/{}/{STRING}/{OBJ.NUM:0>3}',
'/api/v1.0',
'10',
STRING='STRING',
OBJ=LCLOBJ
)
assert result == expected
def test_locals(self):
'''f format with locals'''
URL = '/api/v2.0'
NUM = 20
class OBJ: NUM = 40
STRING = 'LCLSTRING'
expected = '/api/v2.0/20/LCLSTRING/040'
result = f('{URL}/{NUM}/{STRING}/{OBJ.NUM:0>3}')
assert result == expected
def test_globals(self):
'''f format with globals'''
expected = '/api/v1.0/10/STRING/020'
result = f('{URL}/{NUM}/{STRING}/{OBJ.NUM:0>3}')
assert result == expected
def test_override(self):
'''f format with keyword override'''
expected = '/api/v1.0/10/FUNKY/020'
result = f('{URL}/{NUM}/{STRING}/{OBJ.NUM:0>3}', STRING='FUNKY')
assert result == expected
class Test_fdocstring(unittest.TestCase):
def test_fn_args_kwargs(self):
'''fdocstring func with args kwargs'''
class OOBJ: NUM = 40
expected = '/api/v2.0/20/OVRSTRING/040'
@fdocstring('/api/v2.0', 20, STRING='OVRSTRING', OBJ=OOBJ)
def func():
'''{}/{}/{STRING}/{OBJ.NUM:0>3}'''
assert func.__doc__ == expected
def test_cls_args_kwargs(self):
'''fdocstring class with args kwargs'''
class OOBJ: NUM = 40
expected = '/api/v2.0/20/OVRSTRING/040'
@fdocstring('/api/v2.0', 20, STRING='OVRSTRING', OBJ=OOBJ)
class obj(object):
'''{}/{}/{STRING}/{OBJ.NUM:0>3}'''
def method(self):
'''{}/{}/{STRING}/{OBJ.NUM:0>3}'''
assert obj.__doc__ == expected
assert obj.method.__doc__ == expected
def test_fn_locals(self):
'''fdocstring func with locals'''
URL = '/api/v2.0'
NUM = 20
class OBJ: NUM = 40
STRING = 'LCLSTRING'
expected = '/api/v2.0/20/LCLSTRING/040'
@fdocstring()
def func():
'''{URL}/{NUM}/{STRING}/{OBJ.NUM:0>3}'''
assert func.__doc__ == expected
def test_fn_globals(self):
'''fdocstring func with globals'''
expected = '/api/v1.0/10/STRING/020'
@fdocstring()
def func():
'''{URL}/{NUM}/{STRING}/{OBJ.NUM:0>3}'''
assert func.__doc__ == expected
def test_cls_globals(self):
'''fdocstring class with globals'''
expected = '/api/v1.0/10/STRING/020'
@fdocstring()
class obj(object):
'''{URL}/{NUM}/{STRING}/{OBJ.NUM:0>3}'''
def method(self):
'''{URL}/{NUM}/{STRING}/{OBJ.NUM:0>3}'''
assert obj.__doc__ == expected
assert obj.method.__doc__ == expected
def test_cls_locals(self):
'''fdocstring class with locals'''
URL = '/api/v2.0'
NUM = 20
class OBJ: NUM = 40
STRING = 'LCLSTRING'
expected = '/api/v2.0/20/LCLSTRING/040'
@fdocstring()
class obj(object):
'''{URL}/{NUM}/{STRING}/{OBJ.NUM:0>3}'''
def method(self):
'''{URL}/{NUM}/{STRING}/{OBJ.NUM:0>3}'''
assert obj.__doc__ == expected
assert obj.method.__doc__ == expected
class Test_printf(unittest.TestCase):
def setUp(self):
self.stream = StringIO()
def tearDown(self):
self.stream.close()
self.stream = None
def test_printf_args_kwargs(self):
'''printf args kwargs'''
class OOBJ: NUM = 40
expected = '/api/v2.0/20/LCLSTRING/040\n'
printf(
'{}/{}/{STRING}/{OBJ.NUM:0>3}',
'/api/v2.0',
20,
STRING='OVRSTRING',
OBJ=OOBJ,
_stream_=self.stream,
)
def test_printf_locals(self):
'''printf locals'''
URL = '/api/v2.0'
NUM = 20
class OBJ: NUM = 40
STRING = 'LCLSTRING'
expected = '/api/v2.0/20/LCLSTRING/040\n'
printf('{URL}/{NUM}/{STRING}/{OBJ.NUM:0>3}', _stream_=self.stream)
result = self.stream.getvalue()
assert result == expected
def test_printf_globals(self):
'''printf globals'''
expected = '/api/v1.0/10/STRING/020\n'
printf('{URL}/{NUM}/{STRING}/{OBJ.NUM:0>3}', _stream_=self.stream)
assert self.stream.getvalue() == expected
if __name__ == '__main__':
doctest.testmod(fstrings)
unittest.main(verbosity=2)