def test__dialect__ansi_specific_segment_parses(segmentref, raw, caplog): """Test that specific segments parse as expected. NB: We're testing the PARSE function not the MATCH function although this will be a recursive parse and so the match function of SUBSECTIONS will be tested if present. The match function of the parent will not be tested. """ # Set up the lexer config = FluffConfig(overrides=dict(dialect='ansi')) lex = Lexer(config=config) c = ParseContext.from_config(config) # Lex the string for matching. For a good test, this would # arguably happen as a fixture, but it's easier to pass strings # as parameters than pre-lexed segment strings. seg_list, vs = lex.lex(raw) assert not vs print(seg_list) # Get the segment class for matching Seg = config.get('dialect_obj').ref(segmentref) if not issubclass(Seg, BaseSegment): raise TypeError( "{0} is not of type Segment. Test is invalid.".format(segmentref)) # This test is different if we're working with RawSegment # derivatives or not. if issubclass(Seg, RawSegment): print("Raw route...") with caplog.at_level(logging.DEBUG): parsed = Seg.match(segments=seg_list, parse_context=c) assert isinstance(parsed, MatchResult) assert len(parsed.matched_segments) == 1 print(parsed) parsed = parsed.matched_segments[0] print(parsed) else: print("Base route...") # Construct an unparsed segment seg = Seg(seg_list, pos_marker=seg_list[0].pos_marker) # Perform the match (THIS IS THE MEAT OF THE TEST) with caplog.at_level(logging.DEBUG): parsed = seg.parse(parse_context=c) print(parsed) assert isinstance(parsed, Seg) # Check we get a good response print(parsed) print(type(parsed)) # print(type(parsed._reconstruct())) print(type(parsed.raw)) # Check we're all there. assert parsed.raw == raw # Check that there's nothing un parsable typs = parsed.type_set() assert 'unparsable' not in typs
def test__parser__lexer_fail(): """Test the how the lexer fails and reports errors.""" lex = Lexer(config=FluffConfig()) _, vs = lex.lex("Select \u0394") assert len(vs) == 1 err = vs[0] assert isinstance(err, SQLLexError) assert err.pos_marker().char_pos == 7
def lex(raw, config): """Basic parsing for the tests below.""" # Set up the lexer lex = Lexer(config=config) # Lex the string for matching. For a good test, this would # arguably happen as a fixture, but it's easier to pass strings # as parameters than pre-lexed segment strings. seg_list, vs = lex.lex(raw) assert not vs print(seg_list) return seg_list
def test__parser__lexer_obj(raw, res, caplog): """Test the lexer splits as expected in a selection of cases.""" lex = Lexer(config=FluffConfig()) with caplog.at_level(logging.DEBUG): lexing_segments, _ = lex.lex(raw) assert [seg.raw for seg in lexing_segments] == res