def test_odd_and_even_neither_validate_2_nor_5(): p = (odd & even).validates( fmap(lambda xs: int(''.join(xs)), many1(character()))) with pytest.raises(ParsingFailed): run_parser(p, '2') with pytest.raises(ParsingFailed): run_parser(p, '5')
def test_that_fmap_does_not_change_error_messages(runner): parser = literal('a') with pytest.raises(ParsingFailed) as error_message_without_fmap: runner(parser, 'b') with pytest.raises(ParsingFailed) as error_message_with_fmap: runner(fmap(lambda char: char + 1, parser), 'b') assert ( str(error_message_without_fmap.value) == str(error_message_with_fmap.value) )
def test_even_validates_2(): p = even.validates(fmap(lambda xs: int(''.join(xs)), many1(character()))) assert run_parser(p, '2').value == 2
def test_odd_or_even_validates_2_and_5(): p = (odd | even).validates( fmap(lambda xs: int(''.join(xs)), many1(character()))) assert run_parser(p, '2').value == 2 assert run_parser(p, '5').value == 5
def test_odd_does_validate_5(): p = odd.validates(fmap(lambda xs: int(''.join(xs)), many1(character()))) assert run_parser(p, '5').value == 5
def test_even_does_not_validate_5(): p = even.validates(fmap(lambda xs: int(''.join(xs)), many1(character()))) with pytest.raises(ParsingFailed): run_parser(p, '3')
def test_fmap_can_map_1000_times(runner): parser = literal('a') for i in range(0, 1000): parser = fmap(lambda x: 'b', parser) assert runner(parser, 'a').value == 'b'
def test_fmap_can_replace_parsing_result(runner): parser = fmap( lambda x: 'b', literal('a') ) assert runner(parser, 'a').value == 'b'