1212import pytest
1313
1414from unittest import TestCase
15+ from unittest .mock import patch
1516
1617from cs_util import args
1718
@@ -51,13 +52,17 @@ def setUp(self):
5152 "p_Bool" : "bool option, set to True if given" ,
5253 "p_str" : "string option, default={}" ,
5354 }
55+ self ._string0 = None
56+ self ._string1 = "abc"
57+ self ._string3 = "a b c"
5458
5559 def tearDown (self ):
5660 """Unset test parameter values."""
5761 self ._params_def = None
5862 self ._types = None
59-
63+ self . _short_options = None
6064 self ._help_strings = None
65+ self ._string1 = None
6166
6267 def test_parse_options (self ):
6368 """Test `cs_util.args.parse_options` method."""
@@ -79,4 +84,47 @@ def test_parse_options(self):
7984 npt .assert_equal (self ._options ["p_Bool" ], False )
8085 npt .assert_equal (self ._options ["p_str_def" ], "second_string" )
8186
82- # Test exceptions TBD
87+ # Test default args array
88+ test_argv = [None , "-i" , "2" ]
89+ with patch ("sys.argv" , test_argv ):
90+ self ._options = args .parse_options (
91+ self ._params_def ,
92+ self ._short_options ,
93+ self ._types ,
94+ self ._help_strings ,
95+ args = None ,
96+ )
97+ npt .assert_equal (self ._options ["p_int" ], 2 )
98+
99+ def test_my_string_split (self ):
100+ """Test `cs_util.args.my_string_split` method."""
101+
102+ # Test string=None
103+ results = args .my_string_split (self ._string0 )
104+ self .assertIsNone (results )
105+
106+ # Test string without separator
107+ results = args .my_string_split (self ._string1 )
108+ npt .assert_equal (len (results ), 1 )
109+ npt .assert_equal (results [0 ], self ._string1 )
110+
111+ # Test string with separators
112+ results = args .my_string_split (self ._string3 )
113+ npt .assert_equal (len (results ), 3 )
114+ for idx in (0 , 1 , 2 ):
115+ npt .assert_equal (results [idx ], self ._string3 [idx * 2 ])
116+
117+ # Test different separator
118+ results = args .my_string_split (self ._string3 , sep = "_" )
119+ npt .assert_equal (len (results ), 1 )
120+ npt .assert_equal (results [0 ], self ._string3 )
121+
122+ # Test mismatching number of substrings
123+ # Exception for stop=True
124+ with self .assertRaises (ValueError ):
125+ args .my_string_split (self ._string3 , num = 2 , stop = True )
126+ # Return value when stop is False
127+ results = args .my_string_split (self ._string3 , num = 2 , stop = False )
128+ npt .assert_equal (len (results ), 3 )
129+ for idx in (0 , 1 , 2 ):
130+ npt .assert_equal (results [idx ], self ._string3 [idx * 2 ])
0 commit comments