class TestTextParser(TestCase): def setUp(self): self.parser = TextParser() def test_loads(self): matrix_string = ''' 3 1 4 2 4 _ * 1 1 _ 2 4 2 4 . 3 ''' matrix_expected = [ 3, 1, 4, 2, 4, 0, 0, 1, 1, 0, 2, 4, 2, 4, 0, 3, ] board = self.parser.loads(matrix_string) assert matrix_expected == board.matrix def test_loads_wrong_size_exception(self): matrix_string = '3 1 4 2 4 _ * 1' self.assertRaises(ParseError, self.parser.loads, matrix_string) def test_loads_wrong_values_exception(self): matrix_string = ''' 3 6 4 7 4 _ * 1 1 _ 2 4 2 5 . 3 ''' self.assertRaises(ParseError, self.parser.loads, matrix_string) def test_dumps(self): matrix_string = '''\ 31 42 4_ _1 1_ 24 24 _3''' matrix = [ 3, 1, 4, 2, 4, 0, 0, 1, 1, 0, 2, 4, 2, 4, 0, 3, ] board = Board(matrix) dump = self.parser.dumps(board) assert matrix_string == dump
infile_parser = PARSER_FILE_EXT_MAPPING[infile_ext]() outfile_parser = PARSER_FILE_EXT_MAPPING[outfile_ext]() text_parser = TextParser() try: puzzle = infile_parser.loads(args.infile.read()) except ParseError as e: print(e) exit(1) rules = RuleHandler() solver = BacktrackingSolver(rules) if args.display: print('Puzzle:') print(text_parser.dumps(puzzle)) try: solution = next(solver.solve(puzzle)) except StopIteration: print('No solution could be found.') exit(2) if args.display: print('Solution:') print(text_parser.dumps(solution)) else: print('Puzzle solved.') args.outfile.write(outfile_parser.dumps(solution))