From 05f3e3027fde527c32ac3f91472e957efbe96e35 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Fri, 11 May 2018 13:40:13 +0300 Subject: [PATCH] Convert to an exception object. To avoid getting wedged. --- pysollib/hint.py | 18 +++++++++++++----- tests/lib/pysol_tests/import_file1.py | 13 +++++++++++++ .../data/624-invalid-foundations-line.board | 9 +++++++++ 3 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 tests/unit/data/624-invalid-foundations-line.board diff --git a/pysollib/hint.py b/pysollib/hint.py index 9dd6fc7a..a01b37af 100644 --- a/pysollib/hint.py +++ b/pysollib/hint.py @@ -893,9 +893,14 @@ class FreeCellSolver_Hint(Base_Solver_Hint): put(target, SUITS_S.index(str_[-1]), (RANKS_S.index(str_[0]) if len(str_) == 2 else 9)) - def my_find_re(RE, m): + def my_find_re(RE, m, msg): s = m.group(1) - assert re.match(r'^\s*(?:' + RE + r')?(?:\s+' + RE + r')*\s*$', s) + if not re.match(r'^\s*(?:' + RE + r')?(?:\s+' + RE + r')*\s*$', s): + raise PySolHintLayoutImportError( + msg, + [], + line_num + ) return re.findall(r'\b' + RE + r'\b', s) # Based on https://stackoverflow.com/questions/8898294 - thanks! @@ -916,7 +921,8 @@ class FreeCellSolver_Hint(Base_Solver_Hint): m = re.match(r'^(?:Foundations:|Founds?:)\s*(.*)', line) if m: for gm in my_find_re( - r'(' + SUITS_RE + r')-([' + RANKS0_S + r'])', m): + r'(' + SUITS_RE + r')-([' + RANKS0_S + r'])', m, + "Invalid Foundations line"): for foundat in game.foundations: suit = foundat.cap.suit if SUITS_S[suit] == gm[0]: @@ -931,7 +937,8 @@ class FreeCellSolver_Hint(Base_Solver_Hint): continue m = re.match(r'^(?:FC:|Freecells:)\s*(.*)', line) if m: - g = my_find_re(r'(' + CARD_RE + r'|\-)', m) + g = my_find_re(r'(' + CARD_RE + r'|\-)', m, + "Invalid Freecells line") while len(g) < len(game.reserves): g.append('-') for i, gm in enumerate(g): @@ -941,7 +948,8 @@ class FreeCellSolver_Hint(Base_Solver_Hint): continue m = re.match(r'^:?\s*(.*)', line) assert m - for str_ in my_find_re(r'(' + CARD_RE + r')', m): + for str_ in my_find_re(r'(' + CARD_RE + r')', m, + "Invalid column text"): put_str(game.rows[stack_idx], str_) stack_idx += 1 diff --git a/tests/lib/pysol_tests/import_file1.py b/tests/lib/pysol_tests/import_file1.py index e8f88e34..f7796dfa 100644 --- a/tests/lib/pysol_tests/import_file1.py +++ b/tests/lib/pysol_tests/import_file1.py @@ -146,6 +146,19 @@ KD QC 5C QH 6S 3D return self.fail("No exception thrown.") + def test_throw_error_on_invalid_foundations_line(self): + s_game = Mock_S_Game() + h = FreeCellSolver_Hint(s_game, None) + fh = open('tests/unit/data/624-invalid-foundations-line.board', 'r+b') + try: + h.importFileHelper(fh, s_game) + except PySolHintLayoutImportError as err: + self.assertEqual(err.msg, "Invalid Foundations line") + self.assertEqual(err.cards, []) + self.assertEqual(err.line_num, 1) + return + self.fail("No exception thrown.") + def mymain(): from pycotap import TAPTestRunner diff --git a/tests/unit/data/624-invalid-foundations-line.board b/tests/unit/data/624-invalid-foundations-line.board new file mode 100644 index 00000000..c7c6566a --- /dev/null +++ b/tests/unit/data/624-invalid-foundations-line.board @@ -0,0 +1,9 @@ +Foundations: zzz +: KC 6H 4C QS 2D 4S AS +: 4H TH 2S JH 2H 9S AH +: 3S 6C 9H AD KH QD 7C +: 3C JS 5H KS TC 9C 8C +: 4D 9D 7S JC 5D TS +: KD QC 5C QH 6S 3D +: 5S JD 8D 6D TD 8H +: 8S 7H 3H 2C AC 7D