def test_one_match(self): grammar = OneOrMore(Token("hello")) input = Cursor(["hello", "goodbye", "sunshine"]) (result, end) = grammar.parse(input) self.assertEqual(result.value, ["hello"]) self.assertEqual(end, input.at(1))
def test_last_option_match(self): input = Cursor(["sunshine", "rain"]) (result, end) = OneOfTest.grammar.parse(input) self.assertEqual(result.value, "sunshine") self.assertEqual(end, input.at(1))
def test_all_match(self): input = Cursor(["hello", "goodbye", "sunshine", "rain", "clouds"]) (result, end) = AllOfTest.grammar.parse(input) # Result.value is just True for this one - to retain values use `keep` self.assertEqual(result, Result(["hello", "goodbye", "sunshine"])) self.assertEqual(end, input.at(3))
def test_middle_option_match(self): input = Cursor(["goodbye", "rain"]) (result, end) = OneOfTest.grammar.parse(input) self.assertEqual(result.value, "goodbye") self.assertEqual(end, input.at(1))
def test_more_than_two_match(self): grammar = OneOrMore(Token("hello")) input = Cursor(["hello", "hello", "hello", "goodbye"]) (result, end) = grammar.parse(input) self.assertEqual(result.value, ["hello", "hello", "hello"]) self.assertEqual(end, input.at(3))
def test_false_midway(self): start = Cursor([1, 2, 3]) (mappings, end) = start.map_while(lambda n: n < 3 and (n * -1)) self.assertEqual(mappings, [-1, -2]) self.assertEqual(end, start.at(2))
def test_multiple_crawls(self): start = Cursor(range(15)) (mappings, end) = crawl(start) self.assertEqual(mappings, [[0, -1], [-2, -3, -4], [-5, -6, -7, -8], [-9]]) self.assertEqual(end, start.at(10))
def test_crawl_once(self): start = Cursor([1, 2, 10, 11, 12]) (mappings, end) = crawl(start) self.assertEqual(mappings, [[-1, -2]]) self.assertEqual(end, start.at(2))