File: str_tools.py

package info (click to toggle)
python-stem 1.4.1b-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 5,132 kB
  • sloc: python: 22,776; makefile: 130; sh: 3
file content (159 lines) | stat: -rw-r--r-- 5,866 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""
Unit tests for the stem.util.str_tools functions.
"""

import datetime
import unittest

from stem.util import str_tools


class TestStrTools(unittest.TestCase):
  def test_to_camel_case(self):
    """
    Checks the _to_camel_case() function.
    """

    # test the pydoc example
    self.assertEqual('I Like Pepperjack!', str_tools._to_camel_case('I_LIKE_PEPPERJACK!'))

    # check a few edge cases
    self.assertEqual('', str_tools._to_camel_case(''))
    self.assertEqual('Hello', str_tools._to_camel_case('hello'))
    self.assertEqual('Hello', str_tools._to_camel_case('HELLO'))
    self.assertEqual('Hello  World', str_tools._to_camel_case('hello__world'))
    self.assertEqual('Hello\tworld', str_tools._to_camel_case('hello\tWORLD'))
    self.assertEqual('Hello\t\tWorld', str_tools._to_camel_case('hello__world', '_', '\t'))

  def test_crop(self):
    # test the pydoc examples
    self.assertEqual('This is a looo...', str_tools.crop('This is a looooong message', 17))
    self.assertEqual('This is a...', str_tools.crop('This is a looooong message', 12))
    self.assertEqual('', str_tools.crop('This is a looooong message', 3))

  def test_size_label(self):
    """
    Checks the size_label() function.
    """

    # test the pydoc examples
    self.assertEqual('1 MB', str_tools.size_label(2000000))
    self.assertEqual('1.02 KB', str_tools.size_label(1050, 2))
    self.assertEqual('1.025 Kilobytes', str_tools.size_label(1050, 3, True))

    self.assertEqual('0 B', str_tools.size_label(0))
    self.assertEqual('0 Bytes', str_tools.size_label(0, is_long = True))
    self.assertEqual('0.00 B', str_tools.size_label(0, 2))
    self.assertEqual('-10 B', str_tools.size_label(-10))
    self.assertEqual('80 b', str_tools.size_label(10, is_bytes = False))
    self.assertEqual('-1 MB', str_tools.size_label(-2000000))

    # checking that we round down
    self.assertEqual('23.43 Kb', str_tools.size_label(3000, 2, is_bytes = False))

    self.assertRaises(TypeError, str_tools.size_label, None)
    self.assertRaises(TypeError, str_tools.size_label, 'hello world')

  def test_time_label(self):
    """
    Checks the time_label() function.
    """

    # test the pydoc examples
    self.assertEqual('2h', str_tools.time_label(10000))
    self.assertEqual('1.0 minute', str_tools.time_label(61, 1, True))
    self.assertEqual('1.01 minutes', str_tools.time_label(61, 2, True))

    self.assertEqual('0s', str_tools.time_label(0))
    self.assertEqual('0 seconds', str_tools.time_label(0, is_long = True))
    self.assertEqual('0.00s', str_tools.time_label(0, 2))
    self.assertEqual('-10s', str_tools.time_label(-10))

    self.assertRaises(TypeError, str_tools.time_label, None)
    self.assertRaises(TypeError, str_tools.time_label, 'hello world')

  def test_time_labels(self):
    """
    Checks the time_labels() function.
    """

    # test the pydoc examples
    self.assertEqual(['6m', '40s'], str_tools.time_labels(400))
    self.assertEqual(['1 hour', '40 seconds'], str_tools.time_labels(3640, True))

    self.assertEqual([], str_tools.time_labels(0))
    self.assertEqual(['-10s'], str_tools.time_labels(-10))

    self.assertRaises(TypeError, str_tools.time_labels, None)
    self.assertRaises(TypeError, str_tools.time_labels, 'hello world')

  def test_short_time_label(self):
    """
    Checks the short_time_label() function.
    """

    # test the pydoc examples
    self.assertEqual('01:51', str_tools.short_time_label(111))
    self.assertEqual('6-07:08:20', str_tools.short_time_label(544100))

    self.assertEqual('00:00', str_tools.short_time_label(0))

    self.assertRaises(TypeError, str_tools.short_time_label, None)
    self.assertRaises(TypeError, str_tools.short_time_label, 'hello world')
    self.assertRaises(ValueError, str_tools.short_time_label, -5)

  def test_parse_short_time_label(self):
    """
    Checks the parse_short_time_label() function.
    """

    # test the pydoc examples
    self.assertEqual(111, str_tools.parse_short_time_label('01:51'))
    self.assertEqual(544100, str_tools.parse_short_time_label('6-07:08:20'))

    self.assertEqual(110, str_tools.parse_short_time_label('01:50.62'))
    self.assertEqual(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.assertEqual(300, str_tools.parse_short_time_label('05:0'))
    self.assertEqual(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')

  def test_parse_iso_timestamp(self):
    """
    Checks the _parse_iso_timestamp() function.
    """

    test_inputs = {
      '2012-11-08T16:48:41.420251':
        datetime.datetime(2012, 11, 8, 16, 48, 41, 420251),
      '2012-11-08T16:48:41.000000':
        datetime.datetime(2012, 11, 8, 16, 48, 41, 0),
      '2012-11-08T16:48:41':
        datetime.datetime(2012, 11, 8, 16, 48, 41, 0),
    }

    for arg, expected in test_inputs.items():
      self.assertEqual(expected, str_tools._parse_iso_timestamp(arg))

    invalid_input = [
      None,
      32,
      'hello world',
      '2012-11-08T16:48:41.42025',    # too few microsecond digits
      '2012-11-08T16:48:41.4202511',  # too many microsecond digits
      '2012-11-08T16:48',
    ]

    for arg in invalid_input:
      self.assertRaises(ValueError, str_tools._parse_iso_timestamp, arg)