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
def setUp(self): self.parser = TextParser()
help='Display board before and after solution') if __name__ == '__main__': args = parser.parse_args() _, infile_ext = os.path.splitext(args.infile.name) if infile_ext not in PARSER_FILE_EXT_MAPPING: print('Unknown file extension: {}'.format(args.infile.name)) _, outfile_ext = os.path.splitext(args.outfile.name) if outfile_ext not in PARSER_FILE_EXT_MAPPING: print('Unknown file extension: {}'.format(args.outfile.name)) 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: