# HG changeset patch # User Sune Foldager # Date 2011-05-03 20:28:08 # Node ID fa2b596db18201408981e82ef7f9625a838e3ec9 # Parent d8ba6fb2ce15add4937ce9c3710e8dbd99648818 ui: add configint function and tests diff --git a/mercurial/ui.py b/mercurial/ui.py --- a/mercurial/ui.py +++ b/mercurial/ui.py @@ -164,6 +164,26 @@ class ui(object): return v def configbool(self, section, name, default=False, untrusted=False): + """parse a configuration element as a boolean + + >>> u = ui(); s = 'foo' + >>> u.setconfig(s, 'true', 'yes') + >>> u.configbool(s, 'true') + True + >>> u.setconfig(s, 'false', 'no') + >>> u.configbool(s, 'false') + False + >>> u.configbool(s, 'unknown') + False + >>> u.configbool(s, 'unknown', True) + True + >>> u.setconfig(s, 'invalid', 'somevalue') + >>> u.configbool(s, 'invalid') + Traceback (most recent call last): + ... + ConfigError: foo.invalid is not a boolean ('somevalue') + """ + v = self.config(section, name, None, untrusted) if v is None: return default @@ -171,12 +191,47 @@ class ui(object): return v b = util.parsebool(v) if b is None: - raise error.ConfigError(_("%s.%s not a boolean ('%s')") + raise error.ConfigError(_("%s.%s is not a boolean ('%s')") % (section, name, v)) return b + def configint(self, section, name, default=None, untrusted=False): + """parse a configuration element as an integer + + >>> u = ui(); s = 'foo' + >>> u.setconfig(s, 'int1', '42') + >>> u.configint(s, 'int1') + 42 + >>> u.setconfig(s, 'int2', '-42') + >>> u.configint(s, 'int2') + -42 + >>> u.configint(s, 'unknown', 7) + 7 + >>> u.setconfig(s, 'invalid', 'somevalue') + >>> u.configint(s, 'invalid') + Traceback (most recent call last): + ... + ConfigError: foo.invalid is not an integer ('somevalue') + """ + + v = self.config(section, name, None, untrusted) + if v is None: + return default + try: + return int(v) + except ValueError: + raise error.ConfigError(_("%s.%s is not an integer ('%s')") + % (section, name, v)) + def configlist(self, section, name, default=None, untrusted=False): - """Return a list of comma/space separated strings""" + """parse a configuration element as a list of comma/space separated + strings + + >>> u = ui(); s = 'foo' + >>> u.setconfig(s, 'list1', 'this,is "a small" ,test') + >>> u.configlist(s, 'list1') + ['this', 'is', 'a small', 'test'] + """ def _parse_plain(parts, s, offset): whitespace = False diff --git a/tests/test-doctest.py b/tests/test-doctest.py --- a/tests/test-doctest.py +++ b/tests/test-doctest.py @@ -16,6 +16,9 @@ doctest.testmod(mercurial.match) import mercurial.store doctest.testmod(mercurial.store) +import mercurial.ui +doctest.testmod(mercurial.ui) + import mercurial.url doctest.testmod(mercurial.url) diff --git a/tests/test-ui-config.py b/tests/test-ui-config.py --- a/tests/test-ui-config.py +++ b/tests/test-ui-config.py @@ -5,6 +5,10 @@ parsed = dispatch._parseconfig(testui, [ 'values.string=string value', 'values.bool1=true', 'values.bool2=false', + 'values.boolinvalid=foo', + 'values.int1=42', + 'values.int2=-42', + 'values.intinvalid=foo', 'lists.list1=foo', 'lists.list2=foo bar baz', 'lists.list3=alice, bob', @@ -23,7 +27,7 @@ parsed = dispatch._parseconfig(testui, [ 'lists.list16="longer quotation" with "no ending quotation', 'lists.list17=this is \\" "not a quotation mark"', 'lists.list18=\n \n\nding\ndong', -]) + ]) print repr(testui.configitems('values')) print repr(testui.configitems('lists')) @@ -43,6 +47,9 @@ print repr(testui.configbool('values', ' print repr(testui.configbool('values', 'unknown')) print repr(testui.configbool('values', 'unknown', True)) print "---" +print repr(testui.configint('values', 'int1')) +print repr(testui.configint('values', 'int2')) +print "---" print repr(testui.configlist('lists', 'list1')) print repr(testui.configlist('lists', 'list2')) print repr(testui.configlist('lists', 'list3')) @@ -79,3 +86,13 @@ def function(): # values that aren't strings should work testui.setconfig('hook', 'commit', function) print function == testui.config('hook', 'commit') + +# invalid values +try: + testui.configbool('values', 'boolinvalid') +except error.ConfigError: + print 'boolinvalid' +try: + testui.configint('values', 'intinvalid') +except error.ConfigError: + print 'intinvalid' diff --git a/tests/test-ui-config.py.out b/tests/test-ui-config.py.out --- a/tests/test-ui-config.py.out +++ b/tests/test-ui-config.py.out @@ -1,4 +1,4 @@ -[('string', 'string value'), ('bool1', 'true'), ('bool2', 'false')] +[('string', 'string value'), ('bool1', 'true'), ('bool2', 'false'), ('boolinvalid', 'foo'), ('int1', '42'), ('int2', '-42'), ('intinvalid', 'foo')] [('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')] --- 'string value' @@ -6,13 +6,16 @@ 'false' None --- -values.string not a boolean ('string value') +values.string is not a boolean ('string value') True False False False True --- +42 +-42 +--- ['foo'] ['foo', 'bar', 'baz'] ['alice', 'bob'] @@ -42,3 +45,5 @@ True ['foo', 'bar'] None True +boolinvalid +intinvalid