@@ -21,68 +21,87 @@ def test_pyversion():
21
21
assert isinstance (pyversion , int )
22
22
23
23
24
- def test_str2tuple ():
25
- assert isinstance (str2tuple ("1,2,3" ), tuple ) # tests without spaces
26
- assert isinstance (str2tuple ("1, 2, 3" ), tuple ) # tests with spaces
27
- assert isinstance (str2tuple ("1; 2; 3" , sep = ";" ), tuple ) # tests with semicolon
28
- assert str2tuple ("1,2,3" ) == (1 , 2 , 3 )
29
-
30
-
31
- def test_tuple2str ():
32
- assert isinstance (tuple2str (("1" , "2" , "3" )), str )
33
- assert tuple2str ((1 , 2 , 3 )) == "1,2,3"
34
-
35
- assert isinstance (tuple2str ((1 , 2 , 3 ), sep = ";" ), str ) # tests with semicolon
36
- assert tuple2str ((1 , 2 , 3 ), sep = ";" ) == "1;2;3"
24
+ @pytest .mark .parametrize ("value, expects" , [
25
+ (12345 , "12345" ),
26
+ (123.45 , "123.45" ),
27
+ ([123.45 ], "[123.45]" ),
28
+ ({123.45 }, "{123.45}" ),
29
+ ((123.45 , ), "(123.45,)" ),
30
+ (None , "" ),
31
+ (pathlib .Path ("." ), "." ),
32
+ (decimal .Decimal ("1234" ), "1234" ),
33
+ ])
34
+ def test_as_text (value , expects ):
35
+ assert utils .as_text (value ) == expects
36
+
37
+
38
+ def test_check_dependencies (capsys ):
39
+ deps = ["pytest" , "domdf_python_tools" , "madeup_module" ]
40
+
41
+ missing_deps = utils .check_dependencies (deps , False )
42
+ assert isinstance (missing_deps , list )
43
+ assert len (missing_deps ) == 1
44
+ assert missing_deps == ["madeup_module" ]
45
+
46
+ missing_deps = utils .check_dependencies (deps )
47
+ captured = capsys .readouterr ()
48
+ stdout = captured .out .split ("\n " )
49
+ assert stdout [0 ] == "The following modules are missing."
50
+ assert stdout [1 ] == "['madeup_module']"
51
+ assert stdout [2 ] == "Please check the documentation."
52
+ assert stdout [3 ] == ""
53
+ assert isinstance (missing_deps , list )
54
+ assert len (missing_deps ) == 1
55
+ assert missing_deps == ["madeup_module" ]
56
+
57
+ missing_deps = utils .check_dependencies (["pytest" ])
58
+ captured = capsys .readouterr ()
59
+ stdout = captured .out .split ("\n " )
60
+ assert stdout [0 ] == "All modules installed"
61
+ assert stdout [1 ] == ""
62
+ assert isinstance (missing_deps , list )
63
+ assert len (missing_deps ) == 0
64
+ assert missing_deps == []
37
65
38
66
39
67
def test_chunks ():
40
68
assert isinstance (chunks (list (range (100 )), 5 ), types .GeneratorType )
41
69
assert list (chunks (list (range (100 )), 5 ))[0 ] == [0 , 1 , 2 , 3 , 4 ]
70
+ assert list (chunks (["a" , "b" , "c" ], 1 )) == [["a" ], ["b" ], ["c" ]]
42
71
43
-
44
- def test_list2str ():
45
- assert isinstance (list2str ([1 , 2 , 3 ]), str )
46
- assert list2str ([1 , 2 , 3 ]) == "1,2,3"
47
-
48
- assert isinstance (list2str ([1 , 2 , 3 ], sep = ";" ), str ) # tests with semicolon
49
- assert list2str ((1 , 2 , 3 ), sep = ";" ) == "1;2;3"
50
-
51
- assert isinstance (list2string ([1 , 2 , 3 ]), str )
52
- assert list2string ([1 , 2 , 3 ]) == "1,2,3"
53
-
54
- assert isinstance (list2string ([1 , 2 , 3 ], sep = ";" ), str ) # tests with semicolon
55
- assert list2string ((1 , 2 , 3 ), sep = ";" ) == "1;2;3"
72
+ # TODO: cmp
56
73
57
74
58
- #
59
- #
60
- # def test_bdict_errors():
61
- # new_dict = bdict([("Key1", "Value1"), ("Key2", "Value2"), ("Key3", "Value3")])
62
- #
63
- # with pytest.raises(ValueError):
64
- # new_dict["Key1"] = 1234
65
- # with pytest.raises(ValueError):
66
- # new_dict["Value1"] = 1234
67
- # new_dict["Key1"] = "Value1"
68
- # new_dict["Value1"] = "Key1"
69
- #
70
- #
75
+ @pytest .mark .parametrize ("value, expects" , [
76
+ ([1 , 2 , 3 ], "1,2,3" ),
77
+ (["a" , "b" , "c" ], "a,b,c" ),
78
+ (["a" , "b" , 1 , 2 ], "a,b,1,2" ),
79
+ (["a" , 2 , pathlib .Path ("foo.txt" )], "a,2,foo.txt" ),
80
+ ])
81
+ def test_list2str (value , expects ):
82
+ str_representation = list2str (value )
83
+ assert isinstance (str_representation , str )
84
+ assert str_representation == expects
71
85
86
+ str_representation = list2string (value )
87
+ assert isinstance (str_representation , str )
88
+ assert str_representation == expects
72
89
73
- def test_as_text ():
74
- assert utils .as_text (12345 ) == "12345"
75
- assert utils .as_text (123.45 ) == "123.45"
76
- assert utils .as_text ([123.45 ]) == "[123.45]"
77
- assert utils .as_text ({123.45 }) == "{123.45}"
78
- assert utils .as_text ((123.45 , )) == "(123.45,)"
79
- assert utils .as_text (None ) == ""
80
- assert utils .as_text (pathlib .Path ("." )) == "."
81
- assert utils .as_text (decimal .Decimal ("1234" )) == "1234"
82
90
91
+ @pytest .mark .parametrize ("value, expects" , [
92
+ ([1 , 2 , 3 ], "1;2;3" ),
93
+ (["a" , "b" , "c" ], "a;b;c" ),
94
+ (["a" , "b" , 1 , 2 ], "a;b;1;2" ),
95
+ (["a" , 2 , pathlib .Path ("foo.txt" )], "a;2;foo.txt" ),
96
+ ])
97
+ def test_list2str_semicolon (value , expects ):
98
+ str_representation = list2str (value , sep = ";" )
99
+ assert isinstance (str_representation , str )
100
+ assert str_representation == expects
83
101
84
- def test_split_len ():
85
- assert utils .split_len ("Spam Spam Spam Spam Spam Spam Spam Spam " , 5 ) == ["Spam " ] * 8
102
+ str_representation = list2string (value , sep = ";" )
103
+ assert isinstance (str_representation , str )
104
+ assert str_representation == expects
86
105
87
106
88
107
def test_permutations ():
@@ -137,3 +156,74 @@ def test_permutations():
137
156
138
157
with pytest .raises (ValueError ):
139
158
utils .permutations (data , 0 )
159
+
160
+
161
+ class CustomRepr :
162
+ def __init__ (self ):
163
+ pass
164
+
165
+ def __repr__ (self ):
166
+ return "This is my custom __repr__!"
167
+
168
+
169
+ class NoRepr :
170
+ def __init__ (self ):
171
+ pass
172
+
173
+
174
+ def test_printr (capsys ):
175
+ utils .printr ("This is a test" )
176
+ utils .printr (pathlib .PurePosixPath ("foo.txt" ))
177
+ utils .printr (1234 )
178
+ utils .printr (12.34 )
179
+ utils .printr (CustomRepr ())
180
+ utils .printr (NoRepr ())
181
+
182
+ captured = capsys .readouterr ()
183
+ stdout = captured .out .split ("\n " )
184
+ assert stdout [0 ] == "'This is a test'"
185
+ assert stdout [1 ] == "PurePosixPath('foo.txt')"
186
+ assert stdout [2 ] == "1234"
187
+ assert stdout [3 ] == "12.34"
188
+ assert stdout [4 ] == "This is my custom __repr__!"
189
+ assert stdout [5 ].startswith ('' )
190
+
191
+
192
+ def test_printt (capsys ):
193
+ utils .printt ("This is a test" )
194
+ utils .printt (pathlib .PurePosixPath ("foo.txt" ))
195
+ utils .printt (1234 )
196
+ utils .printt (12.34 )
197
+ utils .printt (CustomRepr ())
198
+ utils .printt (NoRepr ())
199
+
200
+ captured = capsys .readouterr ()
201
+ stdout = captured .out .split ("\n " )
202
+ assert stdout [0 ] == "<class 'str'>"
203
+ assert stdout [1 ] == "<class 'pathlib.PurePosixPath'>"
204
+ assert stdout [2 ] == "<class 'int'>"
205
+ assert stdout [3 ] == "<class 'float'>"
206
+ assert stdout [4 ] == "<class 'tests.test_utils.CustomRepr'>"
207
+ assert stdout [5 ] == "<class 'tests.test_utils.NoRepr'>"
208
+
209
+
210
+ def test_split_len ():
211
+ assert utils .split_len ("Spam Spam Spam Spam Spam Spam Spam Spam " , 5 ) == ["Spam " ] * 8
212
+
213
+
214
+ @pytest .mark .parametrize ("value, expects" , [
215
+ ("1,2,3" , (1 , 2 , 3 )), # tests without spaces
216
+ ("1, 2, 3" , (1 , 2 , 3 )), # tests with spaces
217
+ ])
218
+ def test_str2tuple (value , expects ):
219
+ assert isinstance (str2tuple (value ), tuple )
220
+ assert str2tuple (value ) == expects
221
+
222
+
223
+ @pytest .mark .parametrize ("value, expects" , [
224
+ ("1;2;3" , (1 , 2 , 3 )), # tests without semicolon
225
+ ("1; 2; 3" , (1 , 2 , 3 )), # tests with semicolon
226
+ ])
227
+ def test_str2tuple_semicolon (value , expects ):
228
+ assert isinstance (str2tuple (value , sep = ";" ), tuple )
229
+ assert str2tuple (value , sep = ";" ) == expects
0 commit comments