commit c01e5aba622afe0eb59b8f0ea3c4f636d79d9b2f
parent 141351e7d062ea3ee6bee840ba9912bfbc87386b
Author: amin <dev@aminmesbah.com>
Date: Wed, 11 Jan 2017 00:24:05 +0000
Raise informative exception for unmappable words.
FossilOrigin-Name: 4582c5acc8ecaa8e4bfff675b0ffce945bf3e21b662c1e7c7e1d962046187fd8
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') == [