-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_formatter_naming.py
278 lines (255 loc) · 9.68 KB
/
test_formatter_naming.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# ------------------------------------------------------------------------------
# Copyright (c) 2022 Korawich Anuttra. All rights reserved.
# Licensed under the MIT License. See LICENSE in the project root for
# license information.
# ------------------------------------------------------------------------------
"""
Test the formatter object.
"""
import unittest
import fmtutil.formatter as fmt
class NamingTestCase(unittest.TestCase):
def setUp(self) -> None:
self.maxDiff = None
self.nm: fmt.Naming = fmt.Naming.parse("data engineer", "%n")
self.nm2: fmt.Naming = fmt.Naming(
{
"shorts": "dp",
"strings": "data pipeline",
}
)
self.nm3: fmt.Naming = fmt.Naming({"flats": "framework"})
self.nm4: fmt.Naming = fmt.Naming({"vowels": "dtengnr"})
self.nm5: fmt.Naming = fmt.Naming({"shorts": "de"})
self.nm_default: fmt.Naming = fmt.Naming()
self.nm_p: fmt.Naming = fmt.Naming.parse("dataEngineer", "%c")
def test_naming_formatter_raise(self):
with self.assertRaises(fmt.FormatterValueError) as context:
fmt.Naming.formatter(2023)
self.assertIn(
"Naming formatter does not support for value, 2023.",
str(context.exception),
)
def test_naming_values(self):
self.assertDictEqual(
{
"%n": "foo bar",
"%N": "FOO BAR",
"%-N": "Foo Bar",
"%u": "FOO BAR",
"%l": "foo bar",
"%t": "Foo Bar",
"%a": "fb",
"%A": "FB",
"%c": "fooBar",
"%-c": "FooBar",
"%p": "FooBar",
"%k": "foo-bar",
"%K": "FOO-BAR",
"%-K": "Foo-Bar",
"%f": "foobar",
"%F": "FOOBAR",
"%s": "foo_bar",
"%S": "FOO_BAR",
"%-S": "Foo_Bar",
"%T": "Foo-Bar",
"%v": "fbr",
"%V": "FBR",
},
fmt.Naming().values(["foo", "bar"]),
)
self.assertDictEqual(
{
"%n": "foo bar",
"%N": "FOO BAR",
"%-N": "Foo Bar",
"%u": "FOO BAR",
"%l": "foo bar",
"%t": "Foo Bar",
"%a": "fb",
"%A": "FB",
"%c": "fooBar",
"%-c": "FooBar",
"%p": "FooBar",
"%k": "foo-bar",
"%K": "FOO-BAR",
"%-K": "Foo-Bar",
"%f": "foobar",
"%F": "FOOBAR",
"%s": "foo_bar",
"%S": "FOO_BAR",
"%-S": "Foo_Bar",
"%T": "Foo-Bar",
"%v": "fbr",
"%V": "FBR",
},
fmt.Naming().values("foo bar"),
)
self.assertDictEqual(
{
"%n": "",
"%N": "",
"%-N": "",
"%u": "",
"%l": "",
"%t": "",
"%a": "",
"%A": "",
"%c": "",
"%-c": "",
"%p": "",
"%k": "",
"%K": "",
"%-K": "",
"%f": "",
"%F": "",
"%s": "",
"%S": "",
"%-S": "",
"%T": "",
"%v": "",
"%V": "",
},
fmt.Naming().values(),
)
def test_naming_regex(self):
self.assertDictEqual(
{
"%u": "(?P<strings_upper>[A-Z0-9]+(?:\\s[A-Z0-9]+)*)",
"%l": "(?P<strings>[a-z0-9]+(?:\\s[a-z0-9]+)*)",
"%t": (
"(?P<strings_title>[A-Z][a-z0-9]+(?:\\s[A-Z]+[a-z0-9]*)*)"
),
"%a": "(?P<shorts>[a-z0-9]+)",
"%A": "(?P<shorts_upper>[A-Z0-9]+)",
"%c": (
"(?P<strings_camel>[a-z]+((\\d)|([A-Z0-9][a-z0-9]+))*"
"([A-Z])?)"
),
"%p": (
"(?P<strings_pascal>[A-Z]([A-Z0-9]*[a-z][a-z0-9]*[A-Z]|"
"[a-z0-9]*[A-Z][A-Z0-9]*[a-z])[A-Za-z0-9]*)"
),
"%k": "(?P<strings_kebab>[a-z0-9]+(?:-[a-z0-9]+)*)",
"%K": "(?P<strings_kebab_upper>[A-Z0-9]+(?:-[A-Z0-9]+)*)",
"%-K": "(?P<strings_train>[A-Z][a-z0-9]+(?:-[A-Z]+[a-z0-9]*)*)",
"%f": "(?P<flats>[a-z0-9]+)",
"%F": "(?P<flats_upper>[A-Z0-9]+)",
"%s": "(?P<strings_snake>[a-z0-9]+(?:_[a-z0-9]+)*)",
"%S": "(?P<strings_snake_upper>[A-Z0-9]+(?:_[A-Z0-9]+)*)",
"%-S": (
"(?P<strings_snake_title>[A-Z][a-z0-9]+"
"(?:_[A-Z]+[a-z0-9]*)*)"
),
"%T": "(?P<strings_train>[A-Z][a-z0-9]+(?:-[A-Z]+[a-z0-9]*)*)",
"%v": "(?P<vowels>[b-df-hj-np-tv-z]+)",
"%V": "(?P<vowels_upper>[B-DF-HJ-NP-TV-Z]+)",
"%n": "(?P<strings>[a-z0-9]+(?:\\s[a-z0-9]+)*)",
"%N": "(?P<strings_upper>[A-Z0-9]+(?:\\s[A-Z0-9]+)*)",
"%-N": (
"(?P<strings_title>[A-Z][a-z0-9]+(?:\\s[A-Z]+[a-z0-9]*)*)"
),
"%-c": (
"(?P<strings_pascal>[A-Z]([A-Z0-9]*[a-z][a-z0-9]*[A-Z]|"
"[a-z0-9]*[A-Z][A-Z0-9]*[a-z])[A-Za-z0-9]*)"
),
},
fmt.Naming.regex(),
)
def test_naming_properties(self):
self.assertEqual(
"<Naming.parse('data engineer', '%n')>", self.nm.__repr__()
)
self.assertEqual(
"<Naming.parse('data pipeline', '%n')>", self.nm2.__repr__()
)
self.assertEqual(
"<Naming.parse('framework', '%n')>", self.nm3.__repr__()
)
self.assertEqual("data engineer", self.nm.__str__())
self.assertEqual("data pipeline", self.nm2.__str__())
self.assertEqual("framework", self.nm3.__str__())
self.assertEqual("dtengnr", self.nm4.__str__())
self.assertEqual("d e", self.nm5.__str__())
self.assertEqual("", self.nm_default.__str__())
self.assertEqual("data engineer", self.nm_p.__str__())
# Test `cls.string` property
self.assertEqual("data engineer", self.nm.string)
# Test `cls.value` property
self.assertEqual(["data", "engineer"], self.nm.value)
# Test default `self.vowels`
self.assertEqual(["dtngnr"], self.nm.vowels)
# Test default `self.shorts`
self.assertEqual(["d", "e"], self.nm.shorts)
# Test default `self.flats`
self.assertEqual(["dataengineer"], self.nm.flats)
def test_naming_parser(self):
self.assertEqual(
"data engineer",
fmt.Naming.parse("dataEngineer%|data engineer", r"%c%\|%n").string,
)
with self.assertRaises(fmt.FormatterValueError) as context:
fmt.Naming.parse("monkey-d-luffy ddd", "%k %a")
self.assertIn(
(
"Parsing value does not valid with short from strings: "
"['m', 'd', 'l'] and shorts: ['d', 'd', 'd']."
),
str(context.exception),
)
with self.assertRaises(fmt.FormatterValueError) as context:
fmt.Naming.parse("data_is_new_oil bcd", "%s %v")
self.assertIn(
(
"Parsing value does not valid with vowel from strings: "
"['dtsnwl'] and vowels: ['bcd']."
),
str(context.exception),
)
with self.assertRaises(fmt.FormatterValueError) as context:
fmt.Naming.parse("data_engine_framework dataframework", "%s %f")
self.assertIn(
(
"Parsing value does not valid with flat from strings: "
"['dataengineframework'] and flats: ['dataframework']."
),
str(context.exception),
)
with self.assertRaises(fmt.FormatterValueError) as context:
fmt.Naming.parse("datadriven|dtdr", r"%f\|%v")
self.assertIn(
(
"Flat and Vowel that were parsed are not equal, datadriven and "
"dtdr."
),
str(context.exception),
)
with self.assertRaises(fmt.FormatterValueError) as context:
fmt.Naming.parse("foobar ff", r"%f %a")
self.assertIn(
"Flat and Shortname that were parsed are not equal, foobar and ff.",
str(context.exception),
)
with self.assertRaises(fmt.FormatterValueError) as context:
fmt.Naming.parse("fbr ff", r"%v %a")
self.assertIn(
"Shortname and Vowel that were parsed are not equal, ff and fbr.",
str(context.exception),
)
def test_naming_format(self):
self.assertEqual("data_engineer", self.nm.format("%s"))
self.assertEqual("DATA-ENGINEER", self.nm.format("%K"))
self.assertEqual("de", self.nm.format("%a"))
self.assertEqual("dataEngineer", self.nm.format("%c"))
self.assertEqual("data_pipeline", self.nm2.format("%s"))
def test_naming_operation(self):
self.assertEqual(
["data", "engineer", "data", "pipeline"],
(self.nm + self.nm2).value,
)
with self.assertRaises(TypeError) as context:
(self.nm - self.nm2)
self.assertEqual(
"unsupported operand type(s) for -: 'Naming' and 'Naming'",
str(context.exception),
)