mirror of
https://github.com/torproject/stem.git
synced 2024-12-12 04:17:00 +00:00
Unit tests for types.get_entry examples
Unit tests for the examples in the pydocs of the function and fixes for the issues they uncovered.
This commit is contained in:
parent
a090373e53
commit
45d3c819b2
@ -12,6 +12,7 @@ import unittest
|
||||
import test.runner
|
||||
import test.unit.message
|
||||
import test.unit.version
|
||||
import test.unit.types
|
||||
import test.integ.message
|
||||
import test.integ.system
|
||||
|
||||
@ -24,6 +25,7 @@ DIVIDER = "=" * 70
|
||||
# (name, class) tuples for all of our unit and integration tests
|
||||
UNIT_TESTS = (("stem.types.ControlMessage", test.unit.message.TestMessageFunctions),
|
||||
("stem.types.Version", test.unit.version.TestVerionFunctions),
|
||||
("stem.types.get_entry", test.unit.types.TestGetEntry),
|
||||
)
|
||||
|
||||
INTEG_TESTS = (("stem.types.ControlMessage", test.integ.message.TestMessageFunctions),
|
||||
|
@ -342,8 +342,8 @@ def get_entry(line, mapping = False, quoted = False, escaped = False):
|
||||
# entries.
|
||||
end_quote = remainder.find("\"", 1)
|
||||
|
||||
if is_escaped:
|
||||
while end_quote != -1 and remainder[end_quote - 1] == "/":
|
||||
if escaped:
|
||||
while end_quote != -1 and remainder[end_quote - 1] == "\\":
|
||||
end_quote = remainder.find("\"", end_quote + 1)
|
||||
|
||||
# Check that we have an ending quote.
|
||||
@ -360,5 +360,5 @@ def get_entry(line, mapping = False, quoted = False, escaped = False):
|
||||
for esc_sequence, replacement in CONTROL_ESCAPES.items():
|
||||
value = value.replace(esc_sequence, replacement)
|
||||
|
||||
return (key, value, remainder)
|
||||
return (key, value, remainder.lstrip())
|
||||
|
||||
|
29
test/unit/types.py
Normal file
29
test/unit/types.py
Normal file
@ -0,0 +1,29 @@
|
||||
"""
|
||||
Unit tests for the types.get_entry function.
|
||||
"""
|
||||
|
||||
import unittest
|
||||
import stem.types
|
||||
|
||||
class TestGetEntry(unittest.TestCase):
|
||||
"""
|
||||
Tests the types.get_entry function.
|
||||
"""
|
||||
|
||||
def test_examples(self):
|
||||
"""
|
||||
Checks that the examples from the pydoc are correct.
|
||||
"""
|
||||
|
||||
example_input = 'hello there random person'
|
||||
example_result = (None, "hello", "there random person")
|
||||
self.assertEquals(stem.types.get_entry(example_input), example_result)
|
||||
|
||||
example_input = 'version="0.1.2.3"'
|
||||
example_result = ("version", "0.1.2.3", "")
|
||||
self.assertEquals(stem.types.get_entry(example_input, True, True), example_result)
|
||||
|
||||
example_input = r'"this has a \" and \\ in it" foo=bar more_data'
|
||||
example_result = (None, r'this has a " and \ in it', "foo=bar more_data")
|
||||
self.assertEquals(stem.types.get_entry(example_input, False, True, True), example_result)
|
||||
|
Loading…
Reference in New Issue
Block a user