Added unittests for utils.py dedent() and list_to_string(). Adjusted list_to_string to correctly handle no endsep.
This commit is contained in:
parent
12c2402dac
commit
11ea572f00
2 changed files with 35 additions and 6 deletions
|
|
@ -24,13 +24,38 @@ class TestCrop(unittest.TestCase):
|
||||||
|
|
||||||
class TestDedent(unittest.TestCase):
|
class TestDedent(unittest.TestCase):
|
||||||
def test_dedent(self):
|
def test_dedent(self):
|
||||||
# self.assertEqual(expected, dedent(text))
|
#print "Did TestDedent run?"
|
||||||
assert True # TODO: implement your test here
|
# Empty string, return empty string
|
||||||
|
self.assertEqual("", utils.dedent(""))
|
||||||
|
# No leading whitespace
|
||||||
|
self.assertEqual("TestDedent", utils.dedent("TestDedent"))
|
||||||
|
# Leading whitespace, single line
|
||||||
|
self.assertEqual("TestDedent", utils.dedent(" TestDedent"))
|
||||||
|
# Leading whitespace, multi line
|
||||||
|
input_string = " hello\n world"
|
||||||
|
expected_string = "hello\nworld"
|
||||||
|
self.assertEqual(expected_string, utils.dedent(input_string))
|
||||||
|
|
||||||
class TestListToString(unittest.TestCase):
|
class TestListToString(unittest.TestCase):
|
||||||
|
"""
|
||||||
|
Default function header from utils.py:
|
||||||
|
list_to_string(inlist, endsep="and", addquote=False)
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
no endsep:
|
||||||
|
[1,2,3] -> '1, 2, 3'
|
||||||
|
with endsep=='and':
|
||||||
|
[1,2,3] -> '1, 2 and 3'
|
||||||
|
with addquote and endsep
|
||||||
|
[1,2,3] -> '"1", "2" and "3"'
|
||||||
|
"""
|
||||||
|
#print "Did TestListToString run?"
|
||||||
def test_list_to_string(self):
|
def test_list_to_string(self):
|
||||||
# self.assertEqual(expected, list_to_string(inlist, endsep, addquote))
|
self.assertEqual('1, 2, 3', utils.list_to_string([1,2,3], endsep=""))
|
||||||
assert True # TODO: implement your test here
|
self.assertEqual('"1", "2", "3"', utils.list_to_string([1,2,3], endsep="", addquote=True))
|
||||||
|
self.assertEqual('1, 2 and 3', utils.list_to_string([1,2,3]))
|
||||||
|
self.assertEqual('"1", "2" and "3"', utils.list_to_string([1,2,3], endsep="and", addquote=True))
|
||||||
|
|
||||||
|
|
||||||
class TestWildcardToRegexp(unittest.TestCase):
|
class TestWildcardToRegexp(unittest.TestCase):
|
||||||
def test_wildcard_to_regexp(self):
|
def test_wildcard_to_regexp(self):
|
||||||
|
|
|
||||||
|
|
@ -110,16 +110,20 @@ def list_to_string(inlist, endsep="and", addquote=False):
|
||||||
with addquote and endsep
|
with addquote and endsep
|
||||||
[1,2,3] -> '"1", "2" and "3"'
|
[1,2,3] -> '"1", "2" and "3"'
|
||||||
"""
|
"""
|
||||||
|
if not endsep:
|
||||||
|
endsep = ","
|
||||||
|
else:
|
||||||
|
endsep = " " + endsep
|
||||||
if not inlist:
|
if not inlist:
|
||||||
return ""
|
return ""
|
||||||
if addquote:
|
if addquote:
|
||||||
if len(inlist) == 1:
|
if len(inlist) == 1:
|
||||||
return "\"%s\"" % inlist[0]
|
return "\"%s\"" % inlist[0]
|
||||||
return ", ".join("\"%s\"" % v for v in inlist[:-1]) + " %s %s" % (endsep, "\"%s\"" % inlist[-1])
|
return ", ".join("\"%s\"" % v for v in inlist[:-1]) + "%s %s" % (endsep, "\"%s\"" % inlist[-1])
|
||||||
else:
|
else:
|
||||||
if len(inlist) == 1:
|
if len(inlist) == 1:
|
||||||
return str(inlist[0])
|
return str(inlist[0])
|
||||||
return ", ".join(str(v) for v in inlist[:-1]) + " %s %s" % (endsep, inlist[-1])
|
return ", ".join(str(v) for v in inlist[:-1]) + "%s %s" % (endsep, inlist[-1])
|
||||||
|
|
||||||
|
|
||||||
def wildcard_to_regexp(instring):
|
def wildcard_to_regexp(instring):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue