def test_parse_tokens(self): other_vals = parsy_test_item(lambda i: i not in [self.START, self.STOP], "not START/STOP") bracketed = match_item(self.START) >> other_vals.many() << match_item(self.STOP) stream = [self.START, "hello", 1, 2, "goodbye", self.STOP] result = bracketed.parse(stream) self.assertEqual(result, ["hello", 1, 2, "goodbye"])
def test_test_item(self): start_stop = parsy_test_item(lambda i: i in [self.START, self.STOP], "START/STOP") self.assertEqual(start_stop.parse([self.START]), self.START) self.assertEqual(start_stop.parse([self.STOP]), self.STOP) with self.assertRaises(ParseError) as err: start_stop.many().parse([self.START, "hello"]) ex = err.exception self.assertEqual(str(ex), "expected one of 'EOF', 'START/STOP' at 1") self.assertEqual(ex.expected, {'EOF', 'START/STOP'}) self.assertEqual(ex.index, 1)