@@ -102,3 +102,58 @@ def test_error_values(self):
102102 expect = {'green' : '100%' }
103103 self .assertDictEqual (expect , actual )
104104 self .assertEqual (None , failhere )
105+
106+
107+ class TestNonNegativeAction (utils .TestCase ):
108+ def test_negative_values (self ):
109+ parser = argparse .ArgumentParser ()
110+
111+ # Set up our typical usage
112+ parser .add_argument (
113+ '--foo' ,
114+ metavar = '<foo>' ,
115+ type = int ,
116+ action = parseractions .NonNegativeAction ,
117+ )
118+
119+ self .assertRaises (
120+ argparse .ArgumentTypeError ,
121+ parser .parse_args ,
122+ "--foo -1" .split ()
123+ )
124+
125+ def test_zero_values (self ):
126+ parser = argparse .ArgumentParser ()
127+
128+ # Set up our typical usage
129+ parser .add_argument (
130+ '--foo' ,
131+ metavar = '<foo>' ,
132+ type = int ,
133+ action = parseractions .NonNegativeAction ,
134+ )
135+
136+ results = parser .parse_args (
137+ '--foo 0' .split ()
138+ )
139+
140+ actual = getattr (results , 'foo' , None )
141+ self .assertEqual (actual , 0 )
142+
143+ def test_positive_values (self ):
144+ parser = argparse .ArgumentParser ()
145+
146+ # Set up our typical usage
147+ parser .add_argument (
148+ '--foo' ,
149+ metavar = '<foo>' ,
150+ type = int ,
151+ action = parseractions .NonNegativeAction ,
152+ )
153+
154+ results = parser .parse_args (
155+ '--foo 1' .split ()
156+ )
157+
158+ actual = getattr (results , 'foo' , None )
159+ self .assertEqual (actual , 1 )
0 commit comments