def test_that_end_of_file_parser_parsers_multiple_times(n): parser = reduce( lambda accu, _: chain(accu, end_of_file()), range(0, n), unit(True), ) assert run_parser(chain(parser, unit(True)), "")
def test_if_a_choice_failes_in_the_middle_of_chain_it_retries_other_option( runner ): p = choice( try_parser(chain( literal('a'), literal('a') )), chain( literal('a'), literal('b'), ) ) assert runner(p, 'ab').value == 'b'
def test_chain_can_take_3_parsers_as_args(runner): p = chain( literal('a'), literal('b'), literal('c') ) assert runner(p, 'abc').value == 'c'
def test_simple_failing_parser_after_n_newlines_has_linenumber_n1_in_error( n, runner, ): p = chain( literal('\n' * n), fail('error') ) with pytest.raises(ParsingFailed) as err: runner(p, ('\n' * n) + 'x') assert display_location(line=n + 1, column=0) in str(err.value)
def test_choice_should_not_retry_if_the_parser_after_choice_fails(runner): p = choice( literal('a'), # this should match the incomming input literal('aa'), ) p = chain( p, literal('b') ) with pytest.raises(ParsingFailed): runner(p, 'aab')
def test_that_end_of_file_parser_does_only_accept_empty_input(data): if data: with pytest.raises(ParsingFailed): run_parser( end_of_file(), data, ) else: assert run_parser( chain(end_of_file(), unit(True)), data, )
def wrapper(document): parser = unit(True) for character in document: parser = chain(parser, literal(character)) return parser
def test_chain_executes_two_parsers_and_returns_result_of_second_one(runner): p = chain( literal('a'), literal('b') ) assert runner(p, 'ab').value == 'b'
def test_that_end_of_file_parser_accepts_empty_input(): assert run_parser( chain(end_of_file(), unit(True)), "", )