def test_either_fails(self): c = char('c') x = char('x') parser = either(c, x) stream = Stream('i') with self.assertRaises(ParseFailure): parser(stream)
def test_either_ok_two(self): c = char('c') x = char('x') parser = either(c, x) stream = Stream('xirotanibmoc') out, stream = parser(stream) self.assertEqual(out, 'x') self.assertEqual(stream.position, 1)
def test_either_ok_one(self): c = char('c') x = char('x') parser = either(c, x) stream = Stream('combinatorix') out, stream = parser(stream) self.assertEqual(out, 'c') self.assertEqual(stream.position, 1)
def test_zero_or_more_ok_two(self): c = char('c') parser = zero_or_more(c) stream = Stream('ccc') out, stream = parser(stream) self.assertEqual(out, ['c'] * 3) self.assertEqual(stream.position, 3)
def test_zero_or_more_ok_one(self): c = char('c') parser = zero_or_more(c) stream = Stream('') out, stream = parser(stream) self.assertEqual(out, []) self.assertEqual(stream.position, 0)
def test_sequence_ok_two(self): c = char('c') ccc = sequence(c, c, c) stream = Stream('cccc') out, stream = ccc(stream) self.assertEqual(out, list('ccc')) self.assertEqual(stream.position, 3)
def test_unless_ok(self): c = char('c') parser = unless(end_of_stream, c) stream = Stream('c') out, stream = parser(stream) self.assertEqual(out, 'c') self.assertEqual(stream.position, 1)
def test_when_ok(self): c = char('c') parser = when(c, c) stream = Stream('c') out, stream = parser(stream) self.assertEqual(out, 'c') self.assertEqual(stream.position, 1)
def test_one_or_more_ok_two(self): c = char('c') parser = one_or_more(c) stream = Stream('cccombinatorix') out, stream = parser(stream) self.assertEqual(out, ['c', 'c', 'c']) self.assertEqual(stream.position, 3)
def test_zero_or_more_ok_three(self): c = char('c') parser = zero_or_more(c) stream = Stream('combinatorix') out, stream = parser(stream) self.assertEqual(out, ['c']) self.assertEqual(stream.position, 1)
def test_sequence_fails_two(self): c = char('c') ccc = sequence(c, c, c) stream = Stream('ccx') with self.assertRaises(ParseFailure): ccc(stream)
def test_char_fail(self): parser = char('x') stream = Stream('c') with self.assertRaises(ParseFailure): parser(stream)
def test_char_ok(self): parser = char('c') stream = Stream('c') out, stream = parser(stream) self.assertEqual(out, 'c') self.assertEqual(stream.position, 1)
def test_one_or_more_fails(self): c = char('c') parser = one_or_more(c) stream = Stream('xxx') with self.assertRaises(ParseFailure): parser(stream)