[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Test words_with example



commit 740a22364f55ea6566c536f0891bc279d04cb91a
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Fri Oct 2 14:09:41 2020 -0700

    Test words_with example
---
 docs/_static/example/words_with.py | 12 ++++++++----
 test/unit/examples.py              | 20 +++++++++++++++++---
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/docs/_static/example/words_with.py b/docs/_static/example/words_with.py
index ae616f9e..b6670c51 100644
--- a/docs/_static/example/words_with.py
+++ b/docs/_static/example/words_with.py
@@ -18,7 +18,7 @@ def get_words_with(target, attr):
   word_matcher = re.compile('(.*)(%s)(.*)' % target, re.I)
 
   with open('/etc/dictionaries-common/words') as dictionary_file:
-    for word in dictionary_file:
+    for word in dictionary_file.readlines():
       match = word_matcher.match(word)
 
       if match:
@@ -29,11 +29,15 @@ def get_words_with(target, attr):
         ))
 
 
-if __name__ == '__main__':
-  target = raw_input("What substring would you like to look for? We'll get words containing it: ")
+def main():
+  target = input("What substring would you like to look for? We'll get words containing it: ")
   attr = (Attr.BOLD, Color.YELLOW)
 
   print("Words with '%s' include...\n" % term.format(target, *attr))
 
-  for words in itertools.izip_longest(*(get_words_with(target, attr),) * 4):
+  for words in itertools.zip_longest(*(get_words_with(target, attr),) * 4):
     print('%-30s%-30s%-30s%-30s' % tuple([w if w else '' for w in words]))
+
+
+if __name__ == '__main__':
+  main()
diff --git a/test/unit/examples.py b/test/unit/examples.py
index b713932c..faea5e6c 100644
--- a/test/unit/examples.py
+++ b/test/unit/examples.py
@@ -28,7 +28,7 @@ from stem.directory import DIRECTORY_AUTHORITIES
 from stem.exit_policy import ExitPolicy
 from stem.response import ControlMessage
 from stem.util.connection import Connection, Resolver
-from unittest.mock import Mock, patch
+from unittest.mock import Mock, mock_open, patch
 
 EXAMPLE_DIR = os.path.join(test.STEM_BASE, 'docs', '_static', 'example')
 DESC_DIR = os.path.join(test.STEM_BASE, 'test', 'unit', 'descriptor', 'data')
@@ -259,6 +259,12 @@ Getting maatuska's vote from http://171.25.193.9:443/tor/status-vote/current/aut
   6313 measured entries and 1112 unmeasured
 """
 
+EXPECTED_WORDS_WITH = """\
+Words with 'hel' include...
+
+hello                         hellena
+"""
+
 
 def _make_circ_event(circ_id, hop1, hop2, hop3):
   path = '$%s=%s,$%s=%s,$%s=%s' % (hop1[0], hop1[1], hop2[0], hop2[1], hop3[0], hop3[1])
@@ -1033,5 +1039,13 @@ class TestExamples(unittest.TestCase):
 
     self.assertEqual(EXPECTED_VOTES_BY_BANDWIDTH_AUTHORITIES, stdout_mock.getvalue())
 
-  def test_words_with(self):
-    pass
+  @patch('builtins.input', Mock(return_value = 'hel'))
+  @patch('builtins.open', mock_open(read_data = 'hello\nnope\nhellena'))
+  @patch('stem.util.term.format', Mock(side_effect = lambda msg, *args: msg))
+  @patch('sys.stdout', new_callable = io.StringIO)
+  def test_words_with(self, stdout_mock):
+    import words_with
+
+    words_with.main()
+
+    self.assertEqual(EXPECTED_WORDS_WITH.rstrip(), stdout_mock.getvalue().rstrip())



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits