[tor-commits] [stem/master] Extending doctests to cover the controller

atagar at torproject.org atagar at torproject.org
Sun Oct 27 18:13:00 UTC 2013


commit 177104b20de090b3521c66c441cdeb05bbbb9e5c
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Oct 27 09:44:02 2013 -0700

    Extending doctests to cover the controller
    
    Doctests provides a 'globs' argument we can use for a mock controller object.
    This in turn lets us easily test the control.py module (I'm starting to really
    like doctest...).
---
 stem/control.py      |    6 +++---
 test/settings.cfg    |    2 +-
 test/unit/doctest.py |   13 ++++++++++++-
 3 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/stem/control.py b/stem/control.py
index 1d4ecc8..646edd8 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -2023,11 +2023,11 @@ class Controller(BaseController):
 
     ::
 
-      >>> control.extend_circuit('0', ["718BCEA286B531757ACAFF93AE04910EA73DE617", "30BAB8EE7606CBD12F3CC269AE976E0153E7A58D", "2765D8A8C4BBA3F89585A9FFE0E8575615880BEB"])
+      >>> controller.extend_circuit('0', ["718BCEA286B531757ACAFF93AE04910EA73DE617", "30BAB8EE7606CBD12F3CC269AE976E0153E7A58D", "2765D8A8C4BBA3F89585A9FFE0E8575615880BEB"])
       19
-      >>> control.extend_circuit('0')
+      >>> controller.extend_circuit('0')
       20
-      >>> print control.get_info('circuit-status')
+      >>> print controller.get_info('circuit-status')
       20 EXTENDED $718BCEA286B531757ACAFF93AE04910EA73DE617=KsmoinOK,$649F2D0ACF418F7CFC6539AB2257EB2D5297BAFA=Eskimo BUILD_FLAGS=NEED_CAPACITY PURPOSE=GENERAL TIME_CREATED=2012-12-06T13:51:11.433755
       19 BUILT $718BCEA286B531757ACAFF93AE04910EA73DE617=KsmoinOK,$30BAB8EE7606CBD12F3CC269AE976E0153E7A58D=Pascal1,$2765D8A8C4BBA3F89585A9FFE0E8575615880BEB=Anthracite PURPOSE=GENERAL TIME_CREATED=2012-12-06T13:50:56.969938
 
diff --git a/test/settings.cfg b/test/settings.cfg
index 9717d24..1f40ef7 100644
--- a/test/settings.cfg
+++ b/test/settings.cfg
@@ -130,7 +130,7 @@ pep8.ignore E127
 # issue.
 
 pyflakes.ignore run_tests.py => 'unittest' imported but unused
-pyflakes.ignore stem/control.py => undefined name 'control'
+pyflakes.ignore stem/control.py => undefined name 'controller'
 pyflakes.ignore stem/prereq.py => 'RSA' imported but unused
 pyflakes.ignore stem/prereq.py => 'asn1' imported but unused
 pyflakes.ignore stem/prereq.py => 'unittest' imported but unused
diff --git a/test/unit/doctest.py b/test/unit/doctest.py
index a7a2a20..d016372 100644
--- a/test/unit/doctest.py
+++ b/test/unit/doctest.py
@@ -18,6 +18,10 @@ try:
 except ImportError:
   from mock import Mock, patch
 
+EXPECTED_CIRCUIT_STATUS = """\
+20 EXTENDED $718BCEA286B531757ACAFF93AE04910EA73DE617=KsmoinOK,$649F2D0ACF418F7CFC6539AB2257EB2D5297BAFA=Eskimo BUILD_FLAGS=NEED_CAPACITY PURPOSE=GENERAL TIME_CREATED=2012-12-06T13:51:11.433755
+19 BUILT $718BCEA286B531757ACAFF93AE04910EA73DE617=KsmoinOK,$30BAB8EE7606CBD12F3CC269AE976E0153E7A58D=Pascal1,$2765D8A8C4BBA3F89585A9FFE0E8575615880BEB=Anthracite PURPOSE=GENERAL TIME_CREATED=2012-12-06T13:50:56.969938\
+"""
 
 class TestDocumentation(unittest.TestCase):
   def test_examples(self):
@@ -33,7 +37,14 @@ class TestDocumentation(unittest.TestCase):
       elif path.endswith('/stem/response/__init__.py'):
         pass  # the escaped slashes seem to be confusing doctest
       elif path.endswith('/stem/control.py'):
-        pass  # examples refrence a control instance
+        controller = Mock()
+        controller.extend_circuit.side_effect = [19, 20]
+        controller.get_info.side_effect = lambda arg: {
+          'circuit-status': EXPECTED_CIRCUIT_STATUS,
+        }[arg]
+
+        args['globs'] = {'controller': controller}
+        test_run = doctest.testfile(path, **args)
       elif path.endswith('/stem/version.py'):
         with patch('stem.version.get_system_tor_version', Mock(return_value = stem.version.Version('0.2.1.30'))):
           test_run = doctest.testfile(path, **args)





More information about the tor-commits mailing list