1
+ import os
2
+ import pytest
3
+
4
+
5
+ files = [f for f in os .listdir (os .path .dirname (__file__ )) if f .startswith ("emails" )]
6
+ for f in files :
7
+ exec ("import " + f [:- 3 ] + " as " + f [:- 3 ])
8
+
9
+
10
+ # test the file if it has the Emails class
11
+ def test_names ():
12
+ for f in files :
13
+ assert "Emails" in dir (eval (f [:- 3 ])), (
14
+ "Emails is not defined in " + f [:- 3 ]
15
+ )
16
+
17
+
18
+ # test the Emails class if it is a subclass of list
19
+ def test_subclass ():
20
+ for f in files :
21
+ assert issubclass (eval (f [:- 3 ]).Emails , list ), (
22
+ "Emails is not a subclass of list in " + f [:- 3 ]
23
+ )
24
+
25
+
26
+ # check if the Emails class is an instance of the list class
27
+ def test_instance ():
28
+ for f in files :
29
+ assert isinstance (eval (f [:- 3 ]).Emails ([]), list ), (
30
+ "Emails is not an instance of list in " + f [:- 3 ]
31
+ )
32
+
33
+
34
+ # test the Emails class if it has the validate method
35
+ def test_validate ():
36
+ for f in files :
37
+ assert "validate" in dir (eval (f [:- 3 ]).Emails ), (
38
+ "validate is not defined in " + f [:- 3 ]
39
+ )
40
+ assert callable (eval (f [:- 3 ]).Emails .validate ), (
41
+ "validate is not callable in " + f [:- 3 ]
42
+ )
43
+
44
+
45
+ # test the Emails class if it has the __init__ method
46
+ def test_init ():
47
+ for f in files :
48
+ assert "__init__" in dir (eval (f [:- 3 ]).Emails ), (
49
+ "__init__ is not defined in " + f [:- 3 ]
50
+ )
51
+ assert callable (eval (f [:- 3 ]).Emails .__init__ ), (
52
+ "__init__ is not callable in " + f [:- 3 ]
53
+ )
54
+
55
+
56
+ # test the Emails class if it has the __repr__ method
57
+ def test_repr ():
58
+ for f in files :
59
+ assert "__repr__" in dir (eval (f [:- 3 ]).Emails ), (
60
+ "__repr__ is not defined in " + f [:- 3 ]
61
+ )
62
+ assert callable (eval (f [:- 3 ]).Emails .__repr__ ), (
63
+ "__repr__ is not callable in " + f [:- 3 ]
64
+ )
65
+
66
+
67
+ # check Emails class can reproduce itself with the __repr__ method
68
+ def test_repr_output ():
69
+ for f in files :
70
+ assert repr (eval (f [:- 3 ]).Emails (
71
+ [
72
+
73
+ ])) == (eval (f [:- 3 ]).Emails (
74
+ [
75
+
76
+ ])).__repr__ (), (
77
+ "The Emails class does not reproduce itself with the __repr__ method in " + f [:- 3 ]
78
+ )
79
+
80
+
81
+ # test the Emails class if it has the __str__ method
82
+ def test_str ():
83
+ for f in files :
84
+ assert "__str__" in dir (eval (f [:- 3 ]).Emails ), (
85
+ "__str__ is not defined in " + f [:- 3 ]
86
+ )
87
+ assert callable (eval (f [:- 3 ]).Emails .__str__ ), (
88
+ "__str__ is not callable in " + f [:- 3 ]
89
+ )
90
+
91
+
92
+ # check if the validate method allows only strings
93
+ def test_validate_strings ():
94
+ for f in files :
95
+ with pytest .raises (ValueError ) as e :
96
+ eval (f [:- 3 ]).Emails ([1 , 2 , 3 ])
97
+ assert str (e .type ) == "<class 'ValueError'>" , (
98
+ "The validate method does not allow only strings in " + f [:- 3 ]
99
+ )
100
+
101
+
102
+ # check if the validate method allows only valid email addresses, and raises ValueError otherwise
103
+ def test_validate_emails ():
104
+ for f in files :
105
+ with pytest .raises (ValueError ) as e :
106
+ eval (
f [:
- 3 ]).
Emails ([
"[email protected] " ,
"canbula.com" ])
107
+ assert str (e .type ) == "<class 'ValueError'>" , (
108
+ "The validate method does not allow only valid email addresses in " + f [:- 3 ]
109
+ )
110
+
111
+
112
+ # check Emails class does not allow duplicate email addresses, the order is not important
113
+ def test_validate_duplicates ():
114
+ for f in files :
115
+ assert eval (f [:- 3 ]).Emails (
116
+ [
117
+
118
+ ]).
data .
count (
"[email protected] " )
== 1 , (
119
+ "The Emails class does not allow duplicate email addresses in " + f [:- 3 ]
120
+ )
121
+
0 commit comments