Skip to content
Snippets Groups Projects
Commit fa2b596d authored by Sune Foldager's avatar Sune Foldager
Browse files

ui: add configint function and tests

parent d8ba6fb2
No related branches found
No related tags found
No related merge requests found
......@@ -164,6 +164,26 @@
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,7 +191,7 @@
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
......@@ -175,4 +195,32 @@
% (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):
......@@ -178,5 +226,12 @@
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
......
......@@ -16,6 +16,9 @@
import mercurial.store
doctest.testmod(mercurial.store)
import mercurial.ui
doctest.testmod(mercurial.ui)
import mercurial.url
doctest.testmod(mercurial.url)
......
......@@ -5,6 +5,10 @@
'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 @@
'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', '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 @@
# 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'
[('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,10 +6,10 @@
'false'
None
---
values.string not a boolean ('string value')
values.string is not a boolean ('string value')
True
False
False
False
True
---
......@@ -10,9 +10,12 @@
True
False
False
False
True
---
42
-42
---
['foo']
['foo', 'bar', 'baz']
['alice', 'bob']
......@@ -42,3 +45,5 @@
['foo', 'bar']
None
True
boolinvalid
intinvalid
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment