This repository has been archived by the owner on Mar 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
testsStrings.py
340 lines (281 loc) · 13 KB
/
testsStrings.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
**testsStrings.py**
**Platform:**
Windows, Linux, Mac Os X.
**Description:**
This module defines units tests for :mod:`foundations.strings` module.
**Others:**
"""
#**********************************************************************************************************************
#*** External imports.
#**********************************************************************************************************************
import os
import platform
import re
import sys
if sys.version_info[:2] == (2, 6):
import unittest2 as unittest
else:
import unittest
#**********************************************************************************************************************
#*** Internal imports.
#**********************************************************************************************************************
import foundations.strings as strings
#**********************************************************************************************************************
#*** Module attributes.
#**********************************************************************************************************************
__author__ = "Thomas Mansencal"
__copyright__ = "Copyright (C) 2008 - 2012 - Thomas Mansencal"
__license__ = "GPL V3.0 - http://www.gnu.org/licenses/"
__maintainer__ = "Thomas Mansencal"
__email__ = "thomas.mansencal@gmail.com"
__status__ = "Production"
__all__ = ["EncodeTestCase",
"GetNiceNameTestCase",
"GetVersionRankTestCase",
"GetSplitextBasenameTestCase",
"GetCommonAncestorTestCase",
"GetCommonPathsAncestorTestCase",
"GetWordsTestCase",
"FilterWordsTestCase",
"ReplaceTestCase",
"ToForwardSlashesTestCase",
"ToBackwardSlashesTestCase",
"ToPosixPathTestCase",
"GetNormalizedPathTestCase",
"IsEmailTestCase",
"IsWebsiteTestCase"]
#**********************************************************************************************************************
#*** Module classes and definitions.
#**********************************************************************************************************************
class EncodeTestCase(unittest.TestCase):
"""
This class defines :func:`foundations.strings.encode` definition units tests methods.
"""
def testEncode(self):
"""
This method tests :func:`foundations.strings.encode` definition.
"""
self.assertIsInstance(strings.encode("myData"), unicode)
self.assertIsInstance(strings.encode(0), unicode)
self.assertIsInstance(strings.encode(None), unicode)
self.assertIsInstance(strings.encode(True), unicode)
class GetNiceNameTestCase(unittest.TestCase):
"""
This class defines :func:`foundations.strings.getNiceName` definition units tests methods.
"""
def testGetNiceName(self):
"""
This method tests :func:`foundations.strings.getNiceName` definition.
"""
self.assertIsInstance(strings.getNiceName("testGetNiceName"), str)
self.assertEqual(strings.getNiceName("testGetNiceName"), "Test Get Nice Name")
self.assertEqual(strings.getNiceName("TestGetNiceName"), "Test Get Nice Name")
self.assertEqual(strings.getNiceName("_testGetNiceName"), "_Test Get Nice Name")
self.assertEqual(strings.getNiceName("Test Get NiceName"), "Test Get Nice Name")
self.assertEqual(strings.getNiceName("testGetMeANiceName"), "Test Get Me A Nice Name")
class GetVersionRankTestCase(unittest.TestCase):
"""
This class defines :func:`foundations.strings.getVersionRank` definition units tests methods.
"""
def testGetVersionRank(self):
"""
This method tests :func:`foundations.strings.getVersionRank` definition.
"""
self.assertIsInstance(strings.getVersionRank("0.0.0"), int)
self.assertEqual(strings.getVersionRank("0.0.0"), 0)
self.assertEqual(strings.getVersionRank("0.1.0"), 10)
self.assertEqual(strings.getVersionRank("1.1.0"), 110)
self.assertEqual(strings.getVersionRank("1.2.3.4.5"), 12345)
class GetSplitextBasenameTestCase(unittest.TestCase):
"""
This class defines :func:`foundations.strings.getSplitextBasename` definition units tests methods.
"""
def testGetSplitextBasename(self):
"""
This method tests :func:`foundations.strings.getSplitextBasename` definition.
"""
self.assertIsInstance(strings.getSplitextBasename("/Users/JohnDoe/Documents"), str)
self.assertEqual(strings.getSplitextBasename("/Users/JohnDoe/Documents/Test.txt"), "Test")
self.assertEqual(strings.getSplitextBasename("/Users/JohnDoe/Documents/Test"), "Test")
self.assertEqual(strings.getSplitextBasename("/Users/JohnDoe/Documents/Test/"), "Test")
class GetCommonAncestorTestCase(unittest.TestCase):
"""
This class defines :func:`foundations.strings.getCommonAncestor` definition units tests methods.
"""
def testGetCommonAncestor(self):
"""
This method tests :func:`foundations.strings.getCommonAncestor` definition.
"""
self.assertTupleEqual(strings.getCommonAncestor(("1", "2", "3"), ("1", "2", "0"), ("1", "2", "3", "4")),
("1", "2"))
self.assertEqual(strings.getCommonAncestor("azerty", "azetty", "azello"), "aze")
self.assertEqual(strings.getCommonAncestor("/Users/JohnDoe/Documents", "/Users/JohnDoe/Documents/Test.txt"),
"/Users/JohnDoe/Documents")
self.assertFalse(strings.getCommonAncestor("azerty", "qwerty"))
class GetCommonPathsAncestorTestCase(unittest.TestCase):
"""
This class defines :func:`foundations.strings.getCommonPathsAncestor` definition units tests methods.
"""
def testGetCommonPathsAncestor(self):
"""
This method tests :func:`foundations.strings.getCommonPathsAncestor` definition.
"""
self.assertEqual(strings.getCommonPathsAncestor("{0}{1}".format(os.sep,
os.sep.join(("Users", "JohnDoe", "Documents"))),
"{0}{1}".format(os.sep,
os.sep.join(("Users", "JohnDoe", "Documents", "Test.txt")))),
"{0}{1}".format(os.sep,
os.sep.join(("Users", "JohnDoe", "Documents"))))
self.assertFalse(strings.getCommonPathsAncestor("{0}{1}".format(os.sep, os.sep.join(("JohnDoe", "Documents"))),
"{0}{1}".format(os.sep,
os.sep.join(("Users", "JohnDoe", "Documents", "Test.txt")))))
class GetWordsTestCase(unittest.TestCase):
"""
This class defines :func:`foundations.strings.getWords` definition units tests methods.
"""
def testGetWords(self):
"""
This method tests :func:`foundations.strings.getWords` definition.
"""
self.assertIsInstance(strings.getWords("Users are John Doe and Jane Doe."), list)
self.assertListEqual(strings.getWords("Users are John Doe and Jane Doe."),
"Users are John Doe and Jane Doe".split())
self.assertListEqual(strings.getWords("Users are: John Doe, Jane Doe, Z6PO."),
"Users are John Doe Jane Doe Z6PO".split())
class FilterWordsTestCase(unittest.TestCase):
"""
This class defines :func:`foundations.strings.filterWords` definition units tests methods.
"""
def testFilterWords(self):
"""
This method tests :func:`foundations.strings.filterWords` definition.
"""
self.assertIsInstance(strings.filterWords("Users are John Doe and Jane Doe".split()), list)
self.assertListEqual(strings.filterWords("Users are John Doe and Jane Doe".split(),
filtersIn=("Users", "John")),
"Users John".split())
self.assertListEqual(strings.filterWords("Users are John Doe and Jane Doe".split(),
filtersIn=("users", "john"),
flags=re.IGNORECASE),
"Users John".split())
self.assertListEqual(strings.filterWords("Users are John Doe and Jane Doe".split(),
filtersIn=("Nemo",)),
[])
self.assertListEqual(strings.filterWords("Users are John Doe and Jane Doe".split(),
filtersOut=("Users", "John")),
"are Doe and Jane Doe".split())
self.assertListEqual(strings.filterWords("Users are John Doe and Jane Doe".split(),
filtersOut=("Users are John Doe and Jane Doe".split())),
[])
self.assertListEqual(strings.filterWords("Users are John Doe and Jane Doe".split(),
filtersIn=("Users",),
filtersOut=("Users",)),
[])
class ReplaceTestCase(unittest.TestCase):
"""
This class defines :func:`foundations.strings.replace` definition units tests methods.
"""
def testReplace(self):
"""
This method tests :func:`foundations.strings.replace` definition.
"""
self.assertIsInstance(strings.replace("To@Forward|Slashes@Test|Case", {}), str)
self.assertEqual(strings.replace("To@Forward|Slashes@Test|Case", {"@":"|", "|":":"}),
"To:Forward:Slashes:Test:Case")
self.assertEqual(strings.replace("To@Forward@Slashes@Test@Case", {"@":"|", "|":"@"}),
"To@Forward@Slashes@Test@Case")
class RemoveStripTestCase(unittest.TestCase):
"""
This class defines :func:`foundations.strings.removeStrip` definition units tests methods.
"""
def testRemoveStrip(self):
"""
This method tests :func:`foundations.strings.removeStrip` definition.
"""
self.assertIsInstance(strings.removeStrip("John Doe", "John"), str)
self.assertEqual(strings.removeStrip("John Doe", "John"), "Doe")
self.assertEqual(strings.removeStrip("John Doe", "Doe"), "John")
class ToForwardSlashesTestCase(unittest.TestCase):
"""
This class defines :func:`foundations.strings.toForwardSlashes` definition units tests methods.
"""
def testToForwardSlashes(self):
"""
This method tests :func:`foundations.strings.toForwardSlashes` definition.
"""
self.assertIsInstance(strings.toForwardSlashes("To\Forward\Slashes\Test\Case"), str)
self.assertEqual(strings.toForwardSlashes("To\Forward\Slashes\Test\Case"), "To/Forward/Slashes/Test/Case")
self.assertEqual(strings.toForwardSlashes("\Users/JohnDoe\Documents"), "/Users/JohnDoe/Documents")
class ToBackwardSlashesTestCase(unittest.TestCase):
"""
This class defines :func:`foundations.strings.toBackwardSlashes` definition units tests methods.
"""
def testToBackwardSlashes(self):
"""
This method tests :func:`foundations.strings.toBackwardSlashes` definition.
"""
self.assertIsInstance(strings.toBackwardSlashes("\Users\JohnDoe\Documents"), str)
self.assertEqual(strings.toBackwardSlashes("To/Forward/Slashes/Test/Case"), "To\Forward\Slashes\Test\Case")
self.assertEqual(strings.toBackwardSlashes("/Users/JohnDoe/Documents"), "\Users\JohnDoe\Documents")
class ToPosixPathTestCase(unittest.TestCase):
"""
This class defines :func:`foundations.strings.toPosixPath` definition units tests methods.
"""
def testToPosixPath(self):
"""
This method tests :func:`foundations.strings.toPosixPath` definition.
"""
self.assertIsInstance(strings.toPosixPath("c:\\Users\\JohnDoe\\Documents"), str)
self.assertEqual(strings.toPosixPath("c:\\Users\\JohnDoe\\Documents"), "/Users/JohnDoe/Documents")
self.assertEqual(strings.toPosixPath("\\Server\Users\\JohnDoe\\Documents"), "/Server/Users/JohnDoe/Documents")
class GetNormalizedPathTestCase(unittest.TestCase):
"""
This class defines :func:`foundations.strings.getNormalizedPath` definition units tests methods.
"""
def testGetNormalizedPath(self):
"""
This method tests :func:`foundations.strings.getNormalizedPath` definition.
"""
self.assertIsInstance(strings.getNormalizedPath("/Users/JohnDoe/Documents"), str)
if platform.system() == "Windows" or platform.system() == "Microsoft":
self.assertEqual(strings.getNormalizedPath("C:/Users\JohnDoe/Documents"), r"C:\\Users\\JohnDoe\\Documents")
self.assertEqual(strings.getNormalizedPath("C://Users\/JohnDoe//Documents/"), r"C:\\Users\\JohnDoe\\Documents")
self.assertEqual(strings.getNormalizedPath("C:\\Users\\JohnDoe\\Documents"), r"C:\\Users\\JohnDoe\\Documents")
else:
self.assertEqual(strings.getNormalizedPath("/Users/JohnDoe/Documents/"), "/Users/JohnDoe/Documents")
self.assertEqual(strings.getNormalizedPath("/Users\JohnDoe/Documents"), "/Users\JohnDoe/Documents")
class IsEmailTestCase(unittest.TestCase):
"""
This class defines :func:`foundations.strings.isEmail` definition units tests methods.
"""
def testIsEmail(self):
"""
This method tests :func:`foundations.strings.isEmail` definition.
"""
self.assertIsInstance(strings.isEmail("john.doe@domain.com"), bool)
self.assertTrue(strings.isEmail("john.doe@domain.com"))
self.assertTrue(strings.isEmail("john.doe@domain.server.department.company.com"))
self.assertFalse(strings.isEmail("john.doe"))
self.assertFalse(strings.isEmail("john.doe@domain"))
class IsWebsiteTestCase(unittest.TestCase):
"""
This class defines :func:`foundations.strings.isWebsite` definition units tests methods.
"""
def testIsWebsite(self):
"""
This method tests :func:`foundations.strings.isWebsite` definition.
"""
self.assertIsInstance(strings.isWebsite("http://domain.com"), bool)
self.assertTrue(strings.isWebsite("http://www.domain.com"))
self.assertTrue(strings.isWebsite("http://domain.com"))
self.assertTrue(strings.isWebsite("https://domain.com"))
self.assertTrue(strings.isWebsite("ftp://domain.com"))
self.assertTrue(strings.isWebsite("http://domain.subdomain.com"))
self.assertFalse(strings.isWebsite(".com"))
self.assertFalse(strings.isWebsite("domain.com"))
if __name__ == "__main__":
import foundations.tests.utilities
unittest.main()