[tor-commits] [stem/master] Unit tests for types.get_entry examples

atagar at torproject.org atagar at torproject.org
Fri Nov 4 17:19:04 UTC 2011


commit 45d3c819b21dd29aec70178f66777a365200a617
Author: Damian Johnson <atagar at torproject.org>
Date:   Thu Nov 3 07:07:11 2011 -0700

    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.
---
 run_tests.py       |    2 ++
 stem/types.py      |    6 +++---
 test/unit/types.py |   29 +++++++++++++++++++++++++++++
 3 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/run_tests.py b/run_tests.py
index 2f2d6ae..15a6c92 100755
--- a/run_tests.py
+++ b/run_tests.py
@@ -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),
diff --git a/stem/types.py b/stem/types.py
index e6188ae..c149b3b 100644
--- a/stem/types.py
+++ b/stem/types.py
@@ -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())
 
diff --git a/test/unit/types.py b/test/unit/types.py
new file mode 100644
index 0000000..9c06ab2
--- /dev/null
+++ b/test/unit/types.py
@@ -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)
+





More information about the tor-commits mailing list