diff --git a/docs/api.md b/docs/api.md index 656a94f..2299a21 100644 --- a/docs/api.md +++ b/docs/api.md @@ -211,12 +211,13 @@ The code-entry Helper object has the following API: `get_word_completions("pr")` will return `{"ocessor", "ovincial", "oximate"}`, while `get_word_completions("opulent-pr")` will return `{"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 - the prefix is complete (the last word matches something in the completion - list, and there are no longer extension words), although the code may not - yet be complete if there are additional words. The completions will never - include a hyphen: the UI frontend must supply these if desired. The - frontend is also responsible for sorting the results before display. + yet known, this returns an empty set. It will include an empty string in + the returned set if the prefix is complete (the last word is an exact match + for something in the completion list), but will include additional strings + if the completion list includes extensions of the last word. The + completions will never include a hyphen: the UI frontend must supply these + 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 code. It does not return anything, but will cause the Wormhole's `w.when_code()` (or corresponding delegate) to fire, and triggers the diff --git a/src/wormhole/_wordlist.py b/src/wormhole/_wordlist.py index 71d3f1a..3da0e15 100644 --- a/src/wormhole/_wordlist.py +++ b/src/wormhole/_wordlist.py @@ -167,7 +167,7 @@ class PGPWordList(object): lp = len(last_partial_word) completions = set() for word in words: - if word.startswith(prefix): + if word.startswith(last_partial_word): completions.add(word[lp:]) return completions diff --git a/src/wormhole/test/test_wordlist.py b/src/wormhole/test/test_wordlist.py new file mode 100644 index 0000000..56cac4b --- /dev/null +++ b/src/wormhole/test/test_wordlist.py @@ -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")