test and fix wordlist methods

This commit is contained in:
Brian Warner 2017-03-19 18:38:14 +01:00
parent 3873f55d64
commit e66d2df9f1
3 changed files with 28 additions and 7 deletions

View File

@ -211,12 +211,13 @@ The code-entry Helper object has the following API:
`get_word_completions("pr")` will return `{"ocessor", "ovincial", `get_word_completions("pr")` will return `{"ocessor", "ovincial",
"oximate"}`, while `get_word_completions("opulent-pr")` will return "oximate"}`, while `get_word_completions("opulent-pr")` will return
`{"eclude", "efer", "eshrunk", "inter", "owler"}`. If the wordlist is not `{"eclude", "efer", "eshrunk", "inter", "owler"}`. If the wordlist is not
yet known, this returns an empty set. It will also return an empty set if yet known, this returns an empty set. It will include an empty string in
the prefix is complete (the last word matches something in the completion the returned set if the prefix is complete (the last word is an exact match
list, and there are no longer extension words), although the code may not for something in the completion list), but will include additional strings
yet be complete if there are additional words. The completions will never if the completion list includes extensions of the last word. The
include a hyphen: the UI frontend must supply these if desired. The completions will never include a hyphen: the UI frontend must supply these
frontend is also responsible for sorting the results before display. if desired. The frontend is also responsible for sorting the results before
display.
* `h.choose_words(words)`: call this when the user is finished typing in the * `h.choose_words(words)`: call this when the user is finished typing in the
code. It does not return anything, but will cause the Wormhole's code. It does not return anything, but will cause the Wormhole's
`w.when_code()` (or corresponding delegate) to fire, and triggers the `w.when_code()` (or corresponding delegate) to fire, and triggers the

View File

@ -167,7 +167,7 @@ class PGPWordList(object):
lp = len(last_partial_word) lp = len(last_partial_word)
completions = set() completions = set()
for word in words: for word in words:
if word.startswith(prefix): if word.startswith(last_partial_word):
completions.add(word[lp:]) completions.add(word[lp:])
return completions return completions

View File

@ -0,0 +1,20 @@
from __future__ import print_function, unicode_literals
import mock
from twisted.trial import unittest
from .._wordlist import PGPWordList
class Completions(unittest.TestCase):
def test_completions(self):
wl = PGPWordList()
gc = wl.get_completions
self.assertEqual(gc("ar"), {"mistice", "ticle"})
self.assertEqual(gc("armis"), {"tice"})
self.assertEqual(gc("armistice-ba"),
{"boon", "ckfield", "ckward", "njo"})
self.assertEqual(gc("armistice-baboon"), {""})
class Choose(unittest.TestCase):
def test_choose_words(self):
wl = PGPWordList()
with mock.patch("os.urandom", side_effect=[b"\x04", b"\x10"]):
self.assertEqual(wl.choose_words(2), "alkali-assume")