20
20
# this package
21
21
from domdf_python_tools import utils
22
22
from domdf_python_tools .testing import testing_boolean_values
23
- from domdf_python_tools .utils import (
24
- Len , chunks , convert_indents , double_chain , list2str , posargs2kwargs , pyversion , str2tuple , word_join
25
- )
26
23
27
24
28
25
def test_pyversion ():
29
- assert isinstance (pyversion , int )
26
+ assert isinstance (utils . pyversion , int )
30
27
31
28
32
29
@pytest .mark .parametrize (
@@ -76,42 +73,43 @@ def test_check_dependencies(capsys):
76
73
77
74
78
75
def test_chunks ():
79
- assert isinstance (chunks (list (range (100 )), 5 ), types .GeneratorType )
80
- assert list (chunks (list (range (100 )), 5 ))[0 ] == [0 , 1 , 2 , 3 , 4 ]
81
- assert list (chunks (['a' , 'b' , 'c' ], 1 )) == [['a' ], ['b' ], ['c' ]]
76
+ assert isinstance (utils . chunks (list (range (100 )), 5 ), types .GeneratorType )
77
+ assert list (utils . chunks (list (range (100 )), 5 ))[0 ] == [0 , 1 , 2 , 3 , 4 ]
78
+ assert list (utils . chunks (['a' , 'b' , 'c' ], 1 )) == [['a' ], ['b' ], ['c' ]]
82
79
83
80
84
81
# TODO: cmp
85
82
86
-
87
- @pytest .mark .parametrize (
88
- "value, expects" ,
89
- [
90
- ([1 , 2 , 3 ], "1,2,3" ),
91
- (['a' , 'b' , 'c' ], "a,b,c" ),
92
- (['a' , 'b' , 1 , 2 ], "a,b,1,2" ),
93
- (['a' , 2 , pathlib .Path ("foo.txt" )], "a,2,foo.txt" ),
94
- ],
95
- )
96
- def test_list2str (value , expects ):
97
- str_representation = list2str (value )
98
- assert isinstance (str_representation , str )
99
- assert str_representation == expects
100
-
101
-
102
- @pytest .mark .parametrize (
103
- "value, expects" ,
104
- [
105
- ([1 , 2 , 3 ], "1;2;3" ),
106
- (['a' , 'b' , 'c' ], "a;b;c" ),
107
- (['a' , 'b' , 1 , 2 ], "a;b;1;2" ),
108
- (['a' , 2 , pathlib .Path ("foo.txt" )], "a;2;foo.txt" ),
109
- ],
110
- )
111
- def test_list2str_semicolon (value , expects ):
112
- str_representation = list2str (value , sep = ';' )
113
- assert isinstance (str_representation , str )
114
- assert str_representation == expects
83
+ class TestList2Str :
84
+
85
+ @pytest .mark .parametrize (
86
+ "value, expects" ,
87
+ [
88
+ ([1 , 2 , 3 ], "1,2,3" ),
89
+ (['a' , 'b' , 'c' ], "a,b,c" ),
90
+ (['a' , 'b' , 1 , 2 ], "a,b,1,2" ),
91
+ (['a' , 2 , pathlib .Path ("foo.txt" )], "a,2,foo.txt" ),
92
+ ],
93
+ )
94
+ def test_list2str (self , value , expects ):
95
+ str_representation = utils .list2str (value )
96
+ assert isinstance (str_representation , str )
97
+ assert str_representation == expects
98
+
99
+
100
+ @pytest .mark .parametrize (
101
+ "value, expects" ,
102
+ [
103
+ ([1 , 2 , 3 ], "1;2;3" ),
104
+ (['a' , 'b' , 'c' ], "a;b;c" ),
105
+ (['a' , 'b' , 1 , 2 ], "a;b;1;2" ),
106
+ (['a' , 2 , pathlib .Path ("foo.txt" )], "a;2;foo.txt" ),
107
+ ],
108
+ )
109
+ def test_list2str_semicolon (self , value , expects ):
110
+ str_representation = utils .list2str (value , sep = ';' )
111
+ assert isinstance (str_representation , str )
112
+ assert str_representation == expects
115
113
116
114
117
115
def test_permutations ():
@@ -254,49 +252,51 @@ def test_split_len():
254
252
assert utils .split_len ("Spam Spam Spam Spam Spam Spam Spam Spam " , 5 ) == ["Spam " ] * 8
255
253
256
254
257
- @pytest .mark .parametrize (
258
- "value, expects" ,
259
- [
260
- ("1,2,3" , (1 , 2 , 3 )), # tests without spaces
261
- ("1, 2, 3" , (1 , 2 , 3 )), # tests with spaces
262
- ],
263
- )
264
- def test_str2tuple (value , expects ):
265
- assert isinstance (str2tuple (value ), tuple )
266
- assert str2tuple (value ) == expects
267
-
268
-
269
- @pytest .mark .parametrize (
270
- "value, expects" ,
271
- [
272
- ("1;2;3" , (1 , 2 , 3 )), # tests without semicolon
273
- ("1; 2; 3" , (1 , 2 , 3 )), # tests with semicolon
274
- ],
275
- )
276
- def test_str2tuple_semicolon (value , expects ):
277
- assert isinstance (str2tuple (value , sep = ';' ), tuple )
278
- assert str2tuple (value , sep = ';' ) == expects
279
-
280
-
281
- @testing_boolean_values (extra_truthy = [50 , - 1 ])
282
- def test_strtobool (boolean_string , expected_boolean ):
283
- assert utils .strtobool (boolean_string ) == expected_boolean
284
-
285
-
286
- @pytest .mark .parametrize (
287
- "obj, expects" ,
288
- [
289
- ("truthy" , ValueError ),
290
- ("foo" , ValueError ),
291
- ("bar" , ValueError ),
292
- (None , AttributeError ),
293
- (1.0 , AttributeError ),
294
- (0.0 , AttributeError ),
295
- ],
296
- )
297
- def test_strtobool_errors (obj , expects ):
298
- with pytest .raises (expects ):
299
- utils .strtobool (obj )
255
+ class TestStr2Tuple :
256
+
257
+ @pytest .mark .parametrize (
258
+ "value, expects" ,
259
+ [
260
+ ("1,2,3" , (1 , 2 , 3 )), # tests without spaces
261
+ ("1, 2, 3" , (1 , 2 , 3 )), # tests with spaces
262
+ ],
263
+ )
264
+ def test_str2tuple (self , value , expects ):
265
+ assert isinstance (utils .str2tuple (value ), tuple )
266
+ assert utils .str2tuple (value ) == expects
267
+
268
+ @pytest .mark .parametrize (
269
+ "value, expects" ,
270
+ [
271
+ ("1;2;3" , (1 , 2 , 3 )), # tests without semicolon
272
+ ("1; 2; 3" , (1 , 2 , 3 )), # tests with semicolon
273
+ ],
274
+ )
275
+ def test_str2tuple_semicolon (self , value , expects ):
276
+ assert isinstance (utils .str2tuple (value , sep = ';' ), tuple )
277
+ assert utils .str2tuple (value , sep = ';' ) == expects
278
+
279
+
280
+ class TestStrToBool :
281
+
282
+ @testing_boolean_values (extra_truthy = [50 , - 1 ])
283
+ def test_strtobool (self , boolean_string , expected_boolean ):
284
+ assert utils .strtobool (boolean_string ) == expected_boolean
285
+
286
+ @pytest .mark .parametrize (
287
+ "obj, expects" ,
288
+ [
289
+ ("truthy" , ValueError ),
290
+ ("foo" , ValueError ),
291
+ ("bar" , ValueError ),
292
+ (None , AttributeError ),
293
+ (1.0 , AttributeError ),
294
+ (0.0 , AttributeError ),
295
+ ],
296
+ )
297
+ def test_strtobool_errors (self , obj , expects ):
298
+ with pytest .raises (expects ):
299
+ utils .strtobool (obj )
300
300
301
301
302
302
@pytest .mark .parametrize (
@@ -364,20 +364,20 @@ def test_cmp():
364
364
]
365
365
)
366
366
def test_double_chain (value , expects ):
367
- assert list (double_chain (value )) == expects
367
+ assert list (utils . double_chain (value )) == expects
368
368
369
369
370
370
def test_len (capsys ):
371
- assert list (Len ("Hello" )) == [0 , 1 , 2 , 3 , 4 ]
372
- assert list (Len ("Hello World" )) == [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
371
+ assert list (utils . Len ("Hello" )) == [0 , 1 , 2 , 3 , 4 ]
372
+ assert list (utils . Len ("Hello World" )) == [0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
373
373
374
- for val in Len ("Hello World" ):
374
+ for val in utils . Len ("Hello World" ):
375
375
print (val )
376
376
377
377
captured = capsys .readouterr ()
378
378
assert captured .out .splitlines () == ["0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" ]
379
379
380
- assert Len ("Hello" ) == range (5 )
380
+ assert utils . Len ("Hello" ) == range (5 )
381
381
382
382
383
383
def demo_function (arg1 , arg2 , arg3 ):
@@ -398,58 +398,58 @@ def demo_function(arg1, arg2, arg3):
398
398
]
399
399
)
400
400
def test_posargs2kwargs (args , posarg_names , kwargs , expects ):
401
- assert posargs2kwargs (args , posarg_names , kwargs ) == expects
401
+ assert utils . posargs2kwargs (args , posarg_names , kwargs ) == expects
402
402
403
403
404
404
def test_word_join ():
405
- assert word_join ([]) == ''
405
+ assert utils . word_join ([]) == ''
406
406
407
- assert word_join (["bob" ]) == "bob"
408
- assert word_join (["bob" , "alice" ]) == "bob and alice"
409
- assert word_join (["bob" , "alice" , "fred" ]) == "bob, alice and fred"
407
+ assert utils . word_join (["bob" ]) == "bob"
408
+ assert utils . word_join (["bob" , "alice" ]) == "bob and alice"
409
+ assert utils . word_join (["bob" , "alice" , "fred" ]) == "bob, alice and fred"
410
410
411
- assert word_join (["bob" ], use_repr = True ) == "'bob'"
412
- assert word_join (["bob" , "alice" ], use_repr = True ) == "'bob' and 'alice'"
413
- assert word_join (["bob" , "alice" , "fred" ], use_repr = True ) == "'bob', 'alice' and 'fred'"
411
+ assert utils . word_join (["bob" ], use_repr = True ) == "'bob'"
412
+ assert utils . word_join (["bob" , "alice" ], use_repr = True ) == "'bob' and 'alice'"
413
+ assert utils . word_join (["bob" , "alice" , "fred" ], use_repr = True ) == "'bob', 'alice' and 'fred'"
414
414
415
- assert word_join (["bob" ], use_repr = True , oxford = True ) == "'bob'"
416
- assert word_join (["bob" , "alice" ], use_repr = True , oxford = True ) == "'bob' and 'alice'"
417
- assert word_join (["bob" , "alice" , "fred" ], use_repr = True , oxford = True ) == "'bob', 'alice', and 'fred'"
415
+ assert utils . word_join (["bob" ], use_repr = True , oxford = True ) == "'bob'"
416
+ assert utils . word_join (["bob" , "alice" ], use_repr = True , oxford = True ) == "'bob' and 'alice'"
417
+ assert utils . word_join (["bob" , "alice" , "fred" ], use_repr = True , oxford = True ) == "'bob', 'alice', and 'fred'"
418
418
419
- assert word_join (()) == ''
419
+ assert utils . word_join (()) == ''
420
420
421
- assert word_join (("bob" , )) == "bob"
422
- assert word_join (("bob" , "alice" )) == "bob and alice"
423
- assert word_join (("bob" , "alice" , "fred" )) == "bob, alice and fred"
421
+ assert utils . word_join (("bob" , )) == "bob"
422
+ assert utils . word_join (("bob" , "alice" )) == "bob and alice"
423
+ assert utils . word_join (("bob" , "alice" , "fred" )) == "bob, alice and fred"
424
424
425
- assert word_join (("bob" , ), use_repr = True ) == "'bob'"
426
- assert word_join (("bob" , "alice" ), use_repr = True ) == "'bob' and 'alice'"
427
- assert word_join (("bob" , "alice" , "fred" ), use_repr = True ) == "'bob', 'alice' and 'fred'"
425
+ assert utils . word_join (("bob" , ), use_repr = True ) == "'bob'"
426
+ assert utils . word_join (("bob" , "alice" ), use_repr = True ) == "'bob' and 'alice'"
427
+ assert utils . word_join (("bob" , "alice" , "fred" ), use_repr = True ) == "'bob', 'alice' and 'fred'"
428
428
429
- assert word_join (("bob" , ), use_repr = True , oxford = True ) == "'bob'"
430
- assert word_join (("bob" , "alice" ), use_repr = True , oxford = True ) == "'bob' and 'alice'"
431
- assert word_join (("bob" , "alice" , "fred" ), use_repr = True , oxford = True ) == "'bob', 'alice', and 'fred'"
429
+ assert utils . word_join (("bob" , ), use_repr = True , oxford = True ) == "'bob'"
430
+ assert utils . word_join (("bob" , "alice" ), use_repr = True , oxford = True ) == "'bob' and 'alice'"
431
+ assert utils . word_join (("bob" , "alice" , "fred" ), use_repr = True , oxford = True ) == "'bob', 'alice', and 'fred'"
432
432
433
433
434
434
def test_convert_indents ():
435
435
436
436
# TODO: test 'to'
437
437
438
- assert convert_indents ("hello world" ) == "hello world"
439
- assert convert_indents (" hello world" ) == " hello world"
440
- assert convert_indents (" hello world" ) == " hello world"
441
- assert convert_indents (" hello world" ) == " hello world"
438
+ assert utils . convert_indents ("hello world" ) == "hello world"
439
+ assert utils . convert_indents (" hello world" ) == " hello world"
440
+ assert utils . convert_indents (" hello world" ) == " hello world"
441
+ assert utils . convert_indents (" hello world" ) == " hello world"
442
442
443
- assert convert_indents ("hello world" , tab_width = 2 ) == "hello world"
444
- assert convert_indents (" hello world" , tab_width = 2 ) == " hello world"
445
- assert convert_indents (" hello world" , tab_width = 2 ) == " hello world"
446
- assert convert_indents (" hello world" , tab_width = 2 ) == " hello world"
443
+ assert utils . convert_indents ("hello world" , tab_width = 2 ) == "hello world"
444
+ assert utils . convert_indents (" hello world" , tab_width = 2 ) == " hello world"
445
+ assert utils . convert_indents (" hello world" , tab_width = 2 ) == " hello world"
446
+ assert utils . convert_indents (" hello world" , tab_width = 2 ) == " hello world"
447
447
448
- assert convert_indents ("hello world" , from_ = " " ) == "hello world"
449
- assert convert_indents (" hello world" , from_ = " " ) == " hello world"
450
- assert convert_indents (" hello world" , from_ = " " ) == " hello world"
451
- assert convert_indents (" hello world" , from_ = " " ) == " hello world"
448
+ assert utils . convert_indents ("hello world" , from_ = " " ) == "hello world"
449
+ assert utils . convert_indents (" hello world" , from_ = " " ) == " hello world"
450
+ assert utils . convert_indents (" hello world" , from_ = " " ) == " hello world"
451
+ assert utils . convert_indents (" hello world" , from_ = " " ) == " hello world"
452
452
453
- assert convert_indents ("hello world" , tab_width = 2 , from_ = " " ) == "hello world"
454
- assert convert_indents (" hello world" , tab_width = 2 , from_ = " " ) == " hello world"
455
- assert convert_indents (" hello world" , tab_width = 2 , from_ = " " ) == " hello world"
453
+ assert utils . convert_indents ("hello world" , tab_width = 2 , from_ = " " ) == "hello world"
454
+ assert utils . convert_indents (" hello world" , tab_width = 2 , from_ = " " ) == " hello world"
455
+ assert utils . convert_indents (" hello world" , tab_width = 2 , from_ = " " ) == " hello world"
0 commit comments