-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunitTests.py
123 lines (91 loc) · 7.87 KB
/
unitTests.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
import unittest
import re
from emailGenerator import _splitName, _getFormatListAndSeperator, _getSequencingWords, _getNewEmailFormat, generateEmailString # Assuming the file is saved as your_module.py
class TestEmailFunctions(unittest.TestCase):
def test_splitName(self):
self.assertEqual(_splitName("John Doe"), ("John", None, "Doe"))
self.assertEqual(_splitName("John Michael Doe"), ("John", "Michael", "Doe"))
self.assertEqual(_splitName("John"), ("John", None, None))
self.assertEqual(_splitName(""), (None, None, None))
self.assertEqual(_splitName(" John Doe "), ("John", None, "Doe")) # Handling extra spaces
self.assertEqual(_splitName(" "), (None, None, None))
self.assertEqual(_splitName("John Doe Smith "), ("John", "Doe", "Smith"))
def test_getFormatListAndSeperator(self):
self.assertEqual(_getFormatListAndSeperator("[first]_[last]"), (["first", "last"], ["_"]))
self.assertEqual(_getFormatListAndSeperator("[first].[last]"), (["first", "last"], ["."]))
self.assertEqual(_getFormatListAndSeperator("[first][last]"), (["first", "last"], [""]))
self.assertEqual(_getFormatListAndSeperator("[first]_[middle]_[last]"), (["first", "middle", "last"], ["_", "_"]))
self.assertEqual(_getFormatListAndSeperator("prefix[first]suffix"), (["first"], ["suffix"]))
self.assertEqual(_getFormatListAndSeperator("[first_initial]_[last]"), (["first_initial", "last"], ["_"]))
self.assertEqual(_getFormatListAndSeperator(""), ([], []))
self.assertEqual(_getFormatListAndSeperator("abc"), ([], []))
self.assertEqual(_getFormatListAndSeperator("[first][last][middle]"), (['first', 'last', 'middle'], ['', '']))
self.assertEqual(_getFormatListAndSeperator("[first]_[last]-"), (['first', 'last'], ['_', '-']))
self.assertEqual(_getFormatListAndSeperator("-[first]_[last]"), (['first', 'last'], ['-', '_']))
def test_getSequencingWords(self):
self.assertEqual(_getSequencingWords(["first_two\b", "last_two\b"]), ["two", "two"])
self.assertEqual(_getSequencingWords(["first_three\b", "last_two\b", "other"]), ["three", "two"])
self.assertEqual(_getSequencingWords(["first_initial\b", "last_initial\b"]), ["initial", "initial"])
self.assertEqual(_getSequencingWords([]), [])
self.assertEqual(_getSequencingWords(["no_match"]), [])
self.assertEqual(_getSequencingWords(["first_three\b", "middle_three\b", "last_one\b"]), ['three', 'three', 'one'])
self.assertEqual(_getSequencingWords(["first_123\b", "last_abc\b"]), ["123", "abc"])
def test_getNewEmailFormat(self):
emailFormat = "[first_two][last]"
self.assertEqual(_getNewEmailFormat("two", ['first_two', 'last'], emailFormat), ('first_two', '[first][last]'))
self.assertEqual(_getNewEmailFormat("last", ["first", 'last'], emailFormat), ("last", '[first_two][last]'))
emailFormat = "[first_initial]_[last]"
self.assertEqual(_getNewEmailFormat("initial", ['first_initial', 'last'], emailFormat), ('first_initial', '[first]_[last]'))
emailFormat = "[first][last_three]"
self.assertEqual(_getNewEmailFormat("three", ['first', 'last_three'], emailFormat), ('last_three', '[first][last]'))
emailFormat = "[first][middle][last]"
self.assertEqual(_getNewEmailFormat("middle", ['first', 'middle', 'last'], emailFormat, ["first", "middle", "last"]), ('middle', '[first][middle][last]'))
self.assertEqual(_getNewEmailFormat("invalid", ["first", "last"], emailFormat), (None, None))
emailFormat = "[first][last]"
self.assertEqual(_getNewEmailFormat("invalid", ["first", "last"], emailFormat), (None, None))
emailFormat = "[first_two]_[last_one]"
self.assertEqual(_getNewEmailFormat("two", ["first_two", "last_one"], emailFormat), ('first_two', '[first]_[last_one]'))
self.assertEqual(_getNewEmailFormat("one", ["first_two", "last_one"], emailFormat), ('last_one', '[first_two]_[last]'))
emailFormat = "[first][middle_three][last]"
self.assertEqual(_getNewEmailFormat("three", ['first', 'middle_three','last'], emailFormat, ["middle"]), ('middle_three', '[first][middle][last]'))
def test_generateEmailString(self):
#Basic Cases
self.assertEqual(generateEmailString("John Doe", "[first][last]", "example.com"), "[email protected]")
self.assertEqual(generateEmailString("John Doe", "[first]_[last]", "example.com"), "[email protected]")
self.assertEqual(generateEmailString("John Michael Doe", "[first].[last]", "example.com"), "[email protected]")
self.assertEqual(generateEmailString("John Doe", "[first_initial][last]", "example.com"), "[email protected]")
self.assertEqual(generateEmailString("John Doe", "[first][last_initial]", "example.com"), "[email protected]")
self.assertEqual(generateEmailString("John Doe", "[first_three][last_three]", "example.com"), "[email protected]")
#Case Sensitivity
self.assertEqual(generateEmailString("John Doe", "[FIRST][LAST]", "example.com"), "[email protected]")
self.assertEqual(generateEmailString("JOHN DOE", "[first][last]", "example.com"), "[email protected]")
#Middle Name
self.assertEqual(generateEmailString("John Michael Doe", "[first][middle][last]", "example.com"), "[email protected]")
self.assertEqual(generateEmailString("John Michael Doe", "[first_initial][middle_initial][last]", "example.com"), "[email protected]")
self.assertEqual(generateEmailString("John Michael Doe", "[first_three][middle_three][last_three]", "example.com"), "[email protected]")
#Middle name error
with self.assertRaises(NameError):
generateEmailString("John Doe", "[first][middle][last]", "example.com")
with self.assertRaises(NameError):
generateEmailString("John Doe", "[first_initial][middle_initial][last]", "example.com")
# Edge Cases with no names
self.assertIsNone(generateEmailString("", "[first][last]", "example.com"))
self.assertIsNone(generateEmailString(" ", "[first][last]", "example.com"))
# Edge Cases with domain format
self.assertEqual(generateEmailString("John Doe", "[first][last]", "@example.com"), "[email protected]")
self.assertEqual(generateEmailString("John Doe", "[first][last]", "EXAMPLE.COM"), "[email protected]")
#Edge Case if middle name is formatted but no middle name is given
with self.assertRaises(NameError):
generateEmailString("John Doe", "[first][middle_three][last]", "example.com")
#Edge cases with more than one separator
self.assertEqual(generateEmailString("John Doe", "[first]-[last]", "example.com"), "[email protected]")
self.assertEqual(generateEmailString("John Doe", "[first]-[last]-", "example.com"), "[email protected]")
self.assertEqual(generateEmailString("John Doe", "-[first]_[last]", "example.com"), "[email protected]")
self.assertEqual(generateEmailString("John Doe", "[first][last][middle]", "example.com"), "[email protected]")
#Edge cases with different format variations.
self.assertEqual(generateEmailString("John Doe", "[first_two][last]", "example.com"), "[email protected]")
self.assertEqual(generateEmailString("John Doe", "[first][last_one]", "example.com"), "[email protected]")
self.assertEqual(generateEmailString("John Michael Doe", "[first_two][middle_two][last]", "example.com"), "[email protected]")
self.assertEqual(generateEmailString("John Michael Doe", "[first][middle][last_one]", "example.com"), "[email protected]")
if __name__ == '__main__':
unittest.main(argv=['first-arg-is-ignored'], exit=False)