From 11ea572f00e2aa04d1907e5fbb793349d1b3cb0d Mon Sep 17 00:00:00 2001 From: asechrest Date: Tue, 6 May 2014 12:34:08 -0400 Subject: [PATCH] Added unittests for utils.py dedent() and list_to_string(). Adjusted list_to_string to correctly handle no endsep. --- src/tests/test_utils_utils.py | 33 +++++++++++++++++++++++++++++---- src/utils/utils.py | 8 ++++++-- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/tests/test_utils_utils.py b/src/tests/test_utils_utils.py index 7c3b98bb8b..bb2af71bce 100644 --- a/src/tests/test_utils_utils.py +++ b/src/tests/test_utils_utils.py @@ -24,13 +24,38 @@ class TestCrop(unittest.TestCase): class TestDedent(unittest.TestCase): def test_dedent(self): - # self.assertEqual(expected, dedent(text)) - assert True # TODO: implement your test here + #print "Did TestDedent run?" + # 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): + """ + 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): - # self.assertEqual(expected, list_to_string(inlist, endsep, addquote)) - assert True # TODO: implement your test here + self.assertEqual('1, 2, 3', utils.list_to_string([1,2,3], endsep="")) + 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): def test_wildcard_to_regexp(self): diff --git a/src/utils/utils.py b/src/utils/utils.py index 2cd86cfc91..2f0f1c7728 100644 --- a/src/utils/utils.py +++ b/src/utils/utils.py @@ -110,16 +110,20 @@ def list_to_string(inlist, endsep="and", addquote=False): with addquote and endsep [1,2,3] -> '"1", "2" and "3"' """ + if not endsep: + endsep = "," + else: + endsep = " " + endsep if not inlist: return "" if addquote: if len(inlist) == 1: 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: if len(inlist) == 1: 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):