@@ -49,17 +49,69 @@ def test_remaining_args():
4949
5050def test_history_span (hist ):
5151 h = hist
52+ assert h == ['first' , 'second' , 'third' , 'fourth' ]
5253 assert h .span ('-2..' ) == ['third' , 'fourth' ]
53- assert h .span ('2..3' ) == ['second' , 'third' ]
54+ assert h .span ('2..3' ) == ['second' , 'third' ] # Inclusive of end
5455 assert h .span ('3' ) == ['third' ]
55- assert h .span (':' ) == [ 'first' , 'second' , 'third' , 'fourth' ]
56+ assert h .span (':' ) == h
5657 assert h .span ('2..' ) == ['second' , 'third' , 'fourth' ]
5758 assert h .span ('-1' ) == ['fourth' ]
5859 assert h .span ('-2..-3' ) == ['third' , 'second' ]
60+ assert h .span ('*' ) == h
5961
60- def test_history_search (hist ):
61- assert hist .search ('o' ) == ['second' , 'fourth' ]
62- assert hist .search ('/IR/' ) == ['first' , 'third' ]
62+ def test_history_get (hist ):
63+ h = hist
64+ assert h == ['first' , 'second' , 'third' , 'fourth' ]
65+ assert h .get ('' ) == h
66+ assert h .get ('-2' ) == h [:- 2 ]
67+ assert h .get ('5' ) == []
68+ assert h .get ('2-3' ) == ['second' ] # Exclusive of end
69+ assert h .get ('ir' ) == ['first' , 'third' ] # Normal string search for all elements containing "ir"
70+ assert h .get ('/i.*d/' ) == ['third' ] # Regex string search "i", then anything, then "d"
71+
72+
73+ def test_cast ():
74+ cast = cmd2 .cast
75+
76+ # Boolean
77+ assert cast (True , True ) == True
78+ assert cast (True , False ) == False
79+ assert cast (True , 0 ) == False
80+ assert cast (True , 1 ) == True
81+ assert cast (True , 'on' ) == True
82+ assert cast (True , 'off' ) == False
83+ assert cast (True , 'ON' ) == True
84+ assert cast (True , 'OFF' ) == False
85+ assert cast (True , 'y' ) == True
86+ assert cast (True , 'n' ) == False
87+ assert cast (True , 't' ) == True
88+ assert cast (True , 'f' ) == False
89+
90+ # Non-boolean same type
91+ assert cast (1 , 5 ) == 5
92+ assert cast (3.4 , 2.7 ) == 2.7
93+ assert cast ('foo' , 'bar' ) == 'bar'
94+ assert cast ([1 ,2 ], [3 ,4 ]) == [3 ,4 ]
95+
96+
97+ def test_cast_problems (capsys ):
98+ cast = cmd2 .cast
99+
100+ expected = 'Problem setting parameter (now {}) to {}; incorrect type?\n '
101+
102+ # Boolean current, with new value not convertible to bool
103+ current = True
104+ new = [True , True ]
105+ assert cast (current , new ) == current
106+ out , err = capsys .readouterr ()
107+ assert out == expected .format (current , new )
108+
109+ # Non-boolean current, with new value not convertible to current type
110+ current = 1
111+ new = 'octopus'
112+ assert cast (current , new ) == current
113+ out , err = capsys .readouterr ()
114+ assert out == expected .format (current , new )
63115
64116
65117def test_parse_empty_string (parser ):
0 commit comments