def test__parser__grammar_delimited(caplog, generate_test_segments, fresh_ansi_dialect): """Test the Delimited grammar.""" seg_list = generate_test_segments( ['bar', ' \t ', ',', ' ', 'bar', ' ']) bs = KeywordSegment.make('bar') comma = KeywordSegment.make(',', name='comma') expectation = ( bs('bar', seg_list[0].pos_marker), seg_list[1], # This will be the whitespace segment comma(',', seg_list[2].pos_marker), seg_list[3], # This will be the whitespace segment bs('bar', seg_list[4].pos_marker), seg_list[5] # This will be the whitespace segment ) g = Delimited(bs, delimiter=comma) gt = Delimited(bs, delimiter=comma, allow_trailing=True) with RootParseContext(dialect=fresh_ansi_dialect) as ctx: with caplog.at_level(logging.DEBUG, logger="sqluff.parser"): # Matching not quite the full list shouldn't work logging.info("#### TEST 1") assert not g.match(seg_list[:4], parse_context=ctx) # Matching not quite the full list should work if we allow trailing logging.info("#### TEST 1") assert gt.match(seg_list[:4], parse_context=ctx) # Matching up to 'bar' should logging.info("#### TEST 3") assert g._match( seg_list[:5], parse_context=ctx).matched_segments == expectation[:5] # Matching the full list ALSO should, because it's just whitespace logging.info("#### TEST 4") assert g.match( seg_list, parse_context=ctx).matched_segments == expectation[:6]
def test__parser__grammar_delimited_not_code_only(caplog, generate_test_segments, fresh_ansi_dialect): """Test the Delimited grammar when not code_only.""" seg_list_a = generate_test_segments(['bar', ' \t ', '.', ' ', 'bar']) seg_list_b = generate_test_segments(['bar', '.', 'bar']) bs = KeywordSegment.make('bar') dot = KeywordSegment.make('.', name='dot') g = Delimited(bs, delimiter=dot, code_only=False) with RootParseContext(dialect=fresh_ansi_dialect) as ctx: with caplog.at_level(logging.DEBUG, logger="sqluff.parser"): # Matching with whitespace shouldn't match # TODO: dots should be parsed out EARLY logging.info("#### TEST 1") assert not g.match(seg_list_a, parse_context=ctx) # Matching up to 'bar' should logging.info("#### TEST 2") assert g.match(seg_list_b, parse_context=ctx) is not None