def test_then(): check_succ_cont((digit().then(digit()), '1234'), ('2'), '34') check_type((digit().then(alpha()), '1234'), ParseError, '234') check_succ_cont((digit() >> digit(), '1234'), ('2'), '34') check_type((digit() >> alpha(), '1234'), ParseError, '234')
def test_digit(): check_succ_cont((digit(), '123'), ('1'), '23') check_succ_cont((digit(), '0?'), ('0'), '?') check_type((space(), 'abc'), ParseError, 'abc')
def test_or(): check_succ_cont(((digit() >> digit()) | alpha(), '1abc'), 'a', 'bc') check_succ_cont(((digit() >> digit()) | alpha(), '12abc'), '2', 'abc') check_succ_cont(((digit() >> digit()) | alpha(), 'abc'), 'a', 'bc') check_succ_cont(((digit() >> digit()) | alpha(), '123abc'), '2', '3abc') check_succ_cont((string('134') | string('23'), '123abc'), '23', 'abc')
def test_bind(): p = digit().flatmap(lambda c1: digit().flatmap(lambda c2: digit().flatmap( lambda c3: just(int(c1 + c2 + c3))))) check_succ_cont((p, '1234'), 123, '4') check_type((p, 'a4'), ParseError, 'a4')
def test_skip_many(): check_succ_cont((skip_many(digit()) >> string('abc'), '9876abcd'), ('abc'), 'd')
def test_skip(): check_succ_cont((skip(spaces()) >> digit(), ' 123'), ('1'), '23') check_succ_cont((skip(spaces()) >> digit(), '123'), ('1'), '23')
def test_many1(): check_succ_cont((many1(digit()), '123abc'), (['1', '2', '3']), 'abc') check_type((many1(space()) >> digit(), '12'), ParseError, '12')
def test_many(): check_succ_cont((many(digit()), '123abc'), (['1', '2', '3']), 'abc') check_succ_cont((many(space()) >> digit(), ' \t\n12'), ('1'), '2') check_succ_cont((many(space()) >> digit(), '12'), ('1'), '2')
def test_map(): check_succ_cont((digit().map(int), '123'), 1, '23') check_succ_cont((alpha().map(lambda x: x.upper()), 'abc'), 'A', 'bc')