Beispiel #1
0
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')
Beispiel #2
0
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)
    )
Beispiel #3
0
def test_even_validates_2():
    p = even.validates(fmap(lambda xs: int(''.join(xs)), many1(character())))
    assert run_parser(p, '2').value == 2
Beispiel #4
0
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
Beispiel #5
0
def test_odd_does_validate_5():
    p = odd.validates(fmap(lambda xs: int(''.join(xs)), many1(character())))
    assert run_parser(p, '5').value == 5
Beispiel #6
0
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')
Beispiel #7
0
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'
Beispiel #8
0
def test_fmap_can_replace_parsing_result(runner):
    parser = fmap(
        lambda x: 'b',
        literal('a')
    )
    assert runner(parser, 'a').value == 'b'