def test__parser__grammar_oneof_exclude(seg_list): """Test the OneOf grammar exclude option.""" fs = KeywordSegment.make("foo") bs = KeywordSegment.make("bar") g = OneOf(bs, exclude=Sequence(bs, fs)) with RootParseContext(dialect=None) as ctx: # Just against the first alone assert g.match(seg_list[:1], parse_context=ctx) # Now with the bit to exclude invluded assert not g.match(seg_list, parse_context=ctx)
def test__parser__grammar_oneof_templated(seg_list): """Test the OneOf grammar. NB: Should behave the same regardless of code_only. """ bs = StringParser("bar", KeywordSegment) fs = StringParser("foo", KeywordSegment) g = OneOf(fs, bs) with RootParseContext(dialect=None) as ctx: # This shouldn't match, but it *ALSO* shouldn't raise an exception. # https://github.com/sqlfluff/sqlfluff/issues/780 assert not g.match(seg_list[5:], parse_context=ctx)
def test__parser__grammar_oneof(seg_list, allow_gaps): """Test the OneOf grammar. NB: Should behave the same regardless of code_only. """ fs = KeywordSegment.make("foo") bs = KeywordSegment.make("bar") g = OneOf(fs, bs, allow_gaps=allow_gaps) with RootParseContext(dialect=None) as ctx: # Check directly assert g.match(seg_list, parse_context=ctx).matched_segments == (bs( "bar", seg_list[0].pos_marker), ) # Check with a bit of whitespace assert not g.match(seg_list[1:], parse_context=ctx)
def test__parser__grammar_oneof_take_longest_match(seg_list): """Test that the OneOf grammar takes the longest match.""" fooRegex = RegexParser(r"fo{2}", KeywordSegment) baar = StringParser("baar", KeywordSegment) foo = StringParser("foo", KeywordSegment) fooBaar = Sequence( foo, baar, ) # Even if fooRegex comes first, fooBaar # is a longer match and should be taken g = OneOf(fooRegex, fooBaar) with RootParseContext(dialect=None) as ctx: assert fooRegex.match(seg_list[2:], parse_context=ctx).matched_segments == ( KeywordSegment("foo", seg_list[2].pos_marker), ) assert g.match(seg_list[2:], parse_context=ctx).matched_segments == ( KeywordSegment("foo", seg_list[2].pos_marker), KeywordSegment("baar", seg_list[3].pos_marker), )
def test__parser__grammar__oneof__copy(): """Test grammar copying.""" bs = StringParser("bar", KeywordSegment) fs = StringParser("foo", KeywordSegment) g1 = OneOf(fs, bs) # Check copy g2 = g1.copy() assert g1 == g2 assert g1 is not g2 # Check copy insert (start) g3 = g1.copy(insert=[bs], at=0) assert g3 == OneOf(bs, fs, bs) # Check copy insert (mid) g4 = g1.copy(insert=[bs], at=1) assert g4 == OneOf(fs, bs, bs) # Check copy insert (end) g5 = g1.copy(insert=[bs], at=-1) assert g5 == OneOf(fs, bs, bs)
def test__parser__grammar__base__longest_trimmed_match__adv(seg_list, caplog): """Test the _longest_trimmed_match method of the BaseGrammar.""" bs = StringParser("bar", KeywordSegment) fs = StringParser("foo", KeywordSegment) matchers = [ bs, fs, Sequence(bs, fs), # This should be the winner. OneOf(bs, fs), Sequence(bs, fs), # Another to check we return the first ] with RootParseContext(dialect=None) as ctx: # Matching the first element of the list with caplog.at_level(logging.DEBUG, logger="sqluff.parser"): match, matcher = BaseGrammar._longest_trimmed_match(seg_list, matchers, ctx) # Check we got a match assert match # Check we got the right one. assert matcher is matchers[2] # And it matched the first three segments assert len(match) == 3
def test__parser__grammar_oneof_take_first(seg_list): """Test that the OneOf grammar takes first match in case they are of same length.""" fooRegex = RegexParser(r"fo{2}", KeywordSegment) foo = StringParser("foo", KeywordSegment) # Both segments would match "foo" # so we test that order matters g1 = OneOf(fooRegex, foo) g2 = OneOf(foo, fooRegex) with RootParseContext(dialect=None) as ctx: assert g1.match(seg_list[2:], parse_context=ctx).matched_segments == ( KeywordSegment("foo", seg_list[2].pos_marker), ) assert g2.match(seg_list[2:], parse_context=ctx).matched_segments == ( KeywordSegment("foo", seg_list[2].pos_marker), )
class TestSegment(BaseSegment): match_grammar = OneOf( StringParser("bar", KeywordSegment), ephemeral_name="foofoo" )