commit e06b76b8ae68446edc3c3123412a514a00e2fba7 Author: Damian Johnson atagar@torproject.org Date: Fri Oct 19 13:33:19 2012 -0700
Expanding str_tools unit tests
Testing the common edge cases that come to mind, and making a few small fixes. I was also missing examples for a couple functions. --- stem/util/str_tools.py | 35 +++++++++++++++++++++- test/unit/util/str_tools.py | 68 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 1 deletions(-)
diff --git a/stem/util/str_tools.py b/stem/util/str_tools.py index b874425..e0a6d3e 100644 --- a/stem/util/str_tools.py +++ b/stem/util/str_tools.py @@ -51,6 +51,8 @@ def to_camel_case(label, word_divider = " "):
:param str label: input string to be converted :param str word_divider: string used to replace underscores + + :returns: camel cased string """
words = [] @@ -84,6 +86,8 @@ def get_size_label(byte_count, decimal = 0, is_long = False, is_bytes = True): :param int decimal: number of decimal digits to be included :param bool is_long: expands units label :param bool is_bytes: provides units in bytes if true, bits otherwise + + :returns: str with human readable representation of the size """
if is_bytes: return _get_label(SIZE_UNITS_BYTES, byte_count, decimal, is_long) @@ -113,6 +117,8 @@ def get_time_label(seconds, decimal = 0, is_long = False): :param int seconds: number of seconds to be converted :param int decimal: number of decimal digits to be included :param bool is_long: expands units label + + :returns: str with human readable representation of the time """
return _get_label(TIME_UNITS, seconds, decimal, is_long) @@ -133,12 +139,14 @@ def get_time_labels(seconds, is_long = False):
:param int seconds: number of seconds to be converted :param bool is_long: expands units label + + :returns: list of strings with human readable representations of the time """
time_labels = []
for count_per_unit, _, _ in TIME_UNITS: - if seconds >= count_per_unit: + if abs(seconds) >= count_per_unit: time_labels.append(_get_label(TIME_UNITS, seconds, 0, is_long)) seconds %= count_per_unit
@@ -149,9 +157,24 @@ def get_short_time_label(seconds): Provides a time in the following format: [[dd-]hh:]mm:ss
+ :: + + >>> get_short_time_label(111) + '01:51' + + >>> get_short_time_label(544100) + '6-07:08:20' + :param int seconds: number of seconds to be converted + + :returns: str with the short representation for the time + + :raises: ValueError if the input is negative """
+ if seconds < 0: + raise ValueError("Input needs to be a non-negative integer, got '%i'" % seconds) + time_comp = {}
for amount, _, label in TIME_UNITS: @@ -174,8 +197,18 @@ def parse_short_time_label(label): cputime and etime fields of ps: [[dd-]hh:]mm:ss or mm:ss.ss
+ :: + + >>> parse_short_time_label('01:51') + 111 + + >>> parse_short_time_label('6-07:08:20') + 544100 + :param str label: time entry to be parsed
+ :returns: int with the number of seconds represented by the label + :raises: ValueError if input is malformed """
diff --git a/test/unit/util/str_tools.py b/test/unit/util/str_tools.py index ce5539f..3d42154 100644 --- a/test/unit/util/str_tools.py +++ b/test/unit/util/str_tools.py @@ -31,6 +31,18 @@ class TestStrTools(unittest.TestCase): self.assertEquals('1 MB', str_tools.get_size_label(2000000)) self.assertEquals('1.02 KB', str_tools.get_size_label(1050, 2)) self.assertEquals('1.025 Kilobytes', str_tools.get_size_label(1050, 3, True)) + + self.assertEquals('0 B', str_tools.get_size_label(0)) + self.assertEquals('0 Bytes', str_tools.get_size_label(0, is_long = True)) + self.assertEquals('0.00 B', str_tools.get_size_label(0, 2)) + self.assertEquals('-10 B', str_tools.get_size_label(-10)) + self.assertEquals('80 b', str_tools.get_size_label(10, is_bytes = False)) + + # checking that we round down + self.assertEquals('23.43 Kb', str_tools.get_size_label(3000, 2, is_bytes = False)) + + self.assertRaises(TypeError, str_tools.get_size_label, None) + self.assertRaises(TypeError, str_tools.get_size_label, 'hello world')
def test_get_time_label(self): """ @@ -41,6 +53,14 @@ class TestStrTools(unittest.TestCase): self.assertEquals('2h', str_tools.get_time_label(10000)) self.assertEquals('1.0 minute', str_tools.get_time_label(61, 1, True)) self.assertEquals('1.01 minutes', str_tools.get_time_label(61, 2, True)) + + self.assertEquals('0s', str_tools.get_time_label(0)) + self.assertEquals('0 seconds', str_tools.get_time_label(0, is_long = True)) + self.assertEquals('0.00s', str_tools.get_time_label(0, 2)) + self.assertEquals('-10s', str_tools.get_time_label(-10)) + + self.assertRaises(TypeError, str_tools.get_time_label, None) + self.assertRaises(TypeError, str_tools.get_time_label, 'hello world')
def test_get_time_labels(self): """ @@ -50,4 +70,52 @@ class TestStrTools(unittest.TestCase): # test the pydoc examples self.assertEquals(['6m', '40s'], str_tools.get_time_labels(400)) self.assertEquals(['1 hour', '40 seconds'], str_tools.get_time_labels(3640, True)) + + self.assertEquals([], str_tools.get_time_labels(0)) + self.assertEquals(['-10s'], str_tools.get_time_labels(-10)) + + self.assertRaises(TypeError, str_tools.get_time_labels, None) + self.assertRaises(TypeError, str_tools.get_time_labels, 'hello world') + + def test_get_short_time_label(self): + """ + Checks the get_short_time_label() function. + """ + + # test the pydoc examples + self.assertEquals('01:51', str_tools.get_short_time_label(111)) + self.assertEquals('6-07:08:20', str_tools.get_short_time_label(544100)) + + self.assertEquals('00:00', str_tools.get_short_time_label(0)) + + self.assertRaises(TypeError, str_tools.get_short_time_label, None) + self.assertRaises(TypeError, str_tools.get_short_time_label, 'hello world') + self.assertRaises(ValueError, str_tools.get_short_time_label, -5) + + def test_parse_short_time_label(self): + """ + Checks the parse_short_time_label() function. + """ + + # test the pydoc examples + self.assertEquals(111, str_tools.parse_short_time_label('01:51')) + self.assertEquals(544100, str_tools.parse_short_time_label('6-07:08:20')) + + self.assertEquals(110, str_tools.parse_short_time_label('01:50.62')) + self.assertEquals(0, str_tools.parse_short_time_label('00:00')) + + # these aren't technically valid, but might as well allow unnecessary + # digits to be dropped + + self.assertEquals(300, str_tools.parse_short_time_label('05:0')) + self.assertEquals(300, str_tools.parse_short_time_label('5:00')) + + self.assertRaises(TypeError, str_tools.parse_short_time_label, None) + self.assertRaises(TypeError, str_tools.parse_short_time_label, 100) + + self.assertRaises(ValueError, str_tools.parse_short_time_label, 'blarg') + self.assertRaises(ValueError, str_tools.parse_short_time_label, '00') + self.assertRaises(ValueError, str_tools.parse_short_time_label, '05:') + self.assertRaises(ValueError, str_tools.parse_short_time_label, '05a:00') + self.assertRaises(ValueError, str_tools.parse_short_time_label, '-05:00')