commit b6a00eb46d798a18ead99c4c8e47ccb6d8609bf4
parent 065583bed4d5b26ecd8ac594bb9d034fcab2959e
Author: Amin Mesbah <mesbah.amin@gmail.com>
Date: Tue, 10 Jan 2017 16:24:05 -0800
Raise informative exception for unmappable words.
Diffstat:
2 files changed, 11 insertions(+), 1 deletion(-)
diff --git a/elemental_speller.py b/elemental_speller.py
@@ -79,7 +79,6 @@ def generate_groupings(word_length, group_sizes=(1, 2)):
return groupings
-# TODO(amin): Handle failure cases (grouping doesn't add up to word length)
def map_word(word, grouping):
"""Return a word mapped to a grouping.
@@ -87,6 +86,12 @@ def map_word(word, grouping):
>>> map_word('because', (1, 2, 1, 1, 2))
('b', 'ec', 'a', 'u', 'se')
"""
+ if len(word) != sum(grouping):
+ raise ValueError(
+ 'Word length ({}) != sum of elements in grouping ({})'.format(
+ len(word), sum(grouping))
+ )
+
chars = (c for c in word)
mapped = []
diff --git a/tests.py b/tests.py
@@ -1,3 +1,4 @@
+import pytest
import elemental_speller as es
ELEMENTS = (
@@ -34,6 +35,10 @@ def test_map_word():
assert es.map_word('because', (1, 2, 1, 1, 2)) == ('b', 'ec', 'a', 'u', 'se')
assert es.map_word('osiris', (1, 3, 2)) == ('o', 'sir', 'is')
+ with pytest.raises(ValueError):
+ es.map_word('toolong', (2, 1))
+ es.map_word('short', (2, 2, 2))
+
def test_elemental_spelling():
assert es.spell('amputation') == [