Skip to content
Snippets Groups Projects
Commit a119da5e authored by ulif's avatar ulif
Browse files

Fix get_wordlist().

parent 02ad00b9
No related branches found
No related tags found
No related merge requests found
......@@ -3,8 +3,18 @@ import os
#: The directory in which wordlists are stored
SRC_DIR = os.path.dirname(__file__)
def get_wordlist(path):
"""Parse file at `path` and build a word list of it.
A wordlist is expected to contain lines of format::
<NUMS><TAB><WORD>\n
for instance::
136512\tTerm
"""
result = []
with open(path, 'r') as fd:
......@@ -12,7 +22,8 @@ def get_wordlist(path):
if not '\t' in line:
continue
term = line.split('\t')[1].strip()
result.append(term)
if term != '': # do not accept empty strings
result.append(term)
return result
......
......@@ -2,10 +2,36 @@ import os
from diceware.diceware import SRC_DIR, get_wordlist
def test_get_wordlist_en():
# we can get a list of words out of english wordlist.
en_src = os.path.join(SRC_DIR, 'wordlist_en.asc')
en_result = get_wordlist(en_src)
assert en_result[0] == 'a'
assert en_result[-1] == '@'
assert len(en_result) == 7776
class Test_GetWordList(object):
def test_get_wordlist_en(self):
# we can get a list of words out of english wordlist.
en_src = os.path.join(SRC_DIR, 'wordlist_en.asc')
en_result = get_wordlist(en_src)
assert en_result[0] == 'a'
assert en_result[-1] == '@'
assert len(en_result) == 7776
def test_get_wordlist_simple(self, tmpdir):
# simple wordlists can be created
in_file = tmpdir.mkdir("work").join("mywordlist")
in_file.write("01\ta\n02\tb\n")
assert ['a', 'b'] == get_wordlist(in_file.strpath)
def test_get_wordlist_ignore_empty_lines(self, tmpdir):
# we ignore empty lines in wordlists
in_file = tmpdir.mkdir("work").join("mywordlist")
in_file.write("\n\n\n")
assert [] == get_wordlist(in_file.strpath)
def test_get_wordlist_ignore_non_data(self, tmpdir):
# lines without tabs ('\t') are ignored
in_file = tmpdir.mkdir("work").join("mywordlist")
in_file.write("111111 a\n111112 b\n")
assert [] == get_wordlist(in_file.strpath)
def test_get_wordlist_broken(self, tmpdir):
# we handle broken lines gracefully
in_file = tmpdir.mkdir("work").join("mywordlist")
in_file.write("11\ta\t12\n22\t\n\n33\tc")
assert ['a', 'c'] == get_wordlist(in_file.strpath)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment