##// END OF EJS Templates
ui: add configint function and tests
Sune Foldager -
r14171:fa2b596d default
parent child Browse files
Show More
@@ -164,6 +164,26 b' class ui(object):'
164 164 return v
165 165
166 166 def configbool(self, section, name, default=False, untrusted=False):
167 """parse a configuration element as a boolean
168
169 >>> u = ui(); s = 'foo'
170 >>> u.setconfig(s, 'true', 'yes')
171 >>> u.configbool(s, 'true')
172 True
173 >>> u.setconfig(s, 'false', 'no')
174 >>> u.configbool(s, 'false')
175 False
176 >>> u.configbool(s, 'unknown')
177 False
178 >>> u.configbool(s, 'unknown', True)
179 True
180 >>> u.setconfig(s, 'invalid', 'somevalue')
181 >>> u.configbool(s, 'invalid')
182 Traceback (most recent call last):
183 ...
184 ConfigError: foo.invalid is not a boolean ('somevalue')
185 """
186
167 187 v = self.config(section, name, None, untrusted)
168 188 if v is None:
169 189 return default
@@ -171,12 +191,47 b' class ui(object):'
171 191 return v
172 192 b = util.parsebool(v)
173 193 if b is None:
174 raise error.ConfigError(_("%s.%s not a boolean ('%s')")
194 raise error.ConfigError(_("%s.%s is not a boolean ('%s')")
175 195 % (section, name, v))
176 196 return b
177 197
198 def configint(self, section, name, default=None, untrusted=False):
199 """parse a configuration element as an integer
200
201 >>> u = ui(); s = 'foo'
202 >>> u.setconfig(s, 'int1', '42')
203 >>> u.configint(s, 'int1')
204 42
205 >>> u.setconfig(s, 'int2', '-42')
206 >>> u.configint(s, 'int2')
207 -42
208 >>> u.configint(s, 'unknown', 7)
209 7
210 >>> u.setconfig(s, 'invalid', 'somevalue')
211 >>> u.configint(s, 'invalid')
212 Traceback (most recent call last):
213 ...
214 ConfigError: foo.invalid is not an integer ('somevalue')
215 """
216
217 v = self.config(section, name, None, untrusted)
218 if v is None:
219 return default
220 try:
221 return int(v)
222 except ValueError:
223 raise error.ConfigError(_("%s.%s is not an integer ('%s')")
224 % (section, name, v))
225
178 226 def configlist(self, section, name, default=None, untrusted=False):
179 """Return a list of comma/space separated strings"""
227 """parse a configuration element as a list of comma/space separated
228 strings
229
230 >>> u = ui(); s = 'foo'
231 >>> u.setconfig(s, 'list1', 'this,is "a small" ,test')
232 >>> u.configlist(s, 'list1')
233 ['this', 'is', 'a small', 'test']
234 """
180 235
181 236 def _parse_plain(parts, s, offset):
182 237 whitespace = False
@@ -16,6 +16,9 b' doctest.testmod(mercurial.match)'
16 16 import mercurial.store
17 17 doctest.testmod(mercurial.store)
18 18
19 import mercurial.ui
20 doctest.testmod(mercurial.ui)
21
19 22 import mercurial.url
20 23 doctest.testmod(mercurial.url)
21 24
@@ -5,6 +5,10 b' parsed = dispatch._parseconfig(testui, ['
5 5 'values.string=string value',
6 6 'values.bool1=true',
7 7 'values.bool2=false',
8 'values.boolinvalid=foo',
9 'values.int1=42',
10 'values.int2=-42',
11 'values.intinvalid=foo',
8 12 'lists.list1=foo',
9 13 'lists.list2=foo bar baz',
10 14 'lists.list3=alice, bob',
@@ -43,6 +47,9 b" print repr(testui.configbool('values', '"
43 47 print repr(testui.configbool('values', 'unknown'))
44 48 print repr(testui.configbool('values', 'unknown', True))
45 49 print "---"
50 print repr(testui.configint('values', 'int1'))
51 print repr(testui.configint('values', 'int2'))
52 print "---"
46 53 print repr(testui.configlist('lists', 'list1'))
47 54 print repr(testui.configlist('lists', 'list2'))
48 55 print repr(testui.configlist('lists', 'list3'))
@@ -79,3 +86,13 b' def function():'
79 86 # values that aren't strings should work
80 87 testui.setconfig('hook', 'commit', function)
81 88 print function == testui.config('hook', 'commit')
89
90 # invalid values
91 try:
92 testui.configbool('values', 'boolinvalid')
93 except error.ConfigError:
94 print 'boolinvalid'
95 try:
96 testui.configint('values', 'intinvalid')
97 except error.ConfigError:
98 print 'intinvalid'
@@ -1,4 +1,4 b''
1 [('string', 'string value'), ('bool1', 'true'), ('bool2', 'false')]
1 [('string', 'string value'), ('bool1', 'true'), ('bool2', 'false'), ('boolinvalid', 'foo'), ('int1', '42'), ('int2', '-42'), ('intinvalid', 'foo')]
2 2 [('list1', 'foo'), ('list2', 'foo bar baz'), ('list3', 'alice, bob'), ('list4', 'foo bar baz alice, bob'), ('list5', 'abc d"ef"g "hij def"'), ('list6', '"hello world", "how are you?"'), ('list7', 'Do"Not"Separate'), ('list8', '"Do"Separate'), ('list9', '"Do\\"NotSeparate"'), ('list10', 'string "with extraneous" quotation mark"'), ('list11', 'x, y'), ('list12', '"x", "y"'), ('list13', '""" key = "x", "y" """'), ('list14', ',,,, '), ('list15', '" just with starting quotation'), ('list16', '"longer quotation" with "no ending quotation'), ('list17', 'this is \\" "not a quotation mark"'), ('list18', '\n \n\nding\ndong')]
3 3 ---
4 4 'string value'
@@ -6,13 +6,16 b''
6 6 'false'
7 7 None
8 8 ---
9 values.string not a boolean ('string value')
9 values.string is not a boolean ('string value')
10 10 True
11 11 False
12 12 False
13 13 False
14 14 True
15 15 ---
16 42
17 -42
18 ---
16 19 ['foo']
17 20 ['foo', 'bar', 'baz']
18 21 ['alice', 'bob']
@@ -42,3 +45,5 b' True'
42 45 ['foo', 'bar']
43 46 None
44 47 True
48 boolinvalid
49 intinvalid
General Comments 0
You need to be logged in to leave comments. Login now