def test__dialect__base_file_parse(dialect, file): """For given test examples, check successful parsing.""" raw = load_file(dialect, file) # Load the right dialect config = FluffConfig(overrides=dict(dialect=dialect)) context = ParseContext.from_config(config) fs, lex_vs = FileSegment.from_raw(raw, config=config) # From just the initial parse, check we're all there assert fs.raw == raw # Check we don't have lexing issues assert not lex_vs # Do the parse WITHOUT lots of logging # The logs get too long here to be useful. We should use # specfic segment tests if we want to debug logs. # with caplog.at_level(logging.DEBUG): print("Pre-parse structure: {0}".format(fs.to_tuple(show_raw=True))) print("Pre-parse structure: {0}".format(fs.stringify())) parsed = fs.parse( parse_context=context) # Optional: set recurse=1 to limit recursion print("Post-parse structure: {0}".format(fs.to_tuple(show_raw=True))) print("Post-parse structure: {0}".format(fs.stringify())) # 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__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__dialect__base_parse_struct(dialect, sqlfile, yamlfile, yaml_loader): """For given test examples, check parsed structure against yaml.""" # Load the right dialect config = FluffConfig(overrides=dict(dialect=dialect)) context = ParseContext.from_config(config) # Load the SQL raw = load_file(dialect, sqlfile) fs, _ = FileSegment.from_raw(raw, config=config) # Load the YAML res = yaml_loader(make_dialect_path(dialect, yamlfile)) # with caplog.at_level(logging.DEBUG): parsed = fs.parse(parse_context=context) assert parsed.to_tuple(code_only=True, show_raw=True) == res
def test__dialect__ansi_specific_segment_not_match(segmentref, raw, caplog): """Test that specific segments do not match. NB: We're testing the MATCH function not the PARSE function. This is the opposite to the above. """ config = FluffConfig(overrides=dict(dialect='ansi')) seg_list = lex(raw, config=config) Seg = validate_segment(segmentref, config=config) c = ParseContext.from_config(config) with caplog.at_level(logging.DEBUG): match = Seg.match(segments=seg_list, parse_context=c) assert not match
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. """ config = FluffConfig(overrides=dict(dialect='ansi')) seg_list = lex(raw, config=config) Seg = validate_segment(segmentref, config=config) c = ParseContext.from_config(config) # 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__dialect__base_file_parse(dialect, file, caplog): """For given test examples, check successful parsing.""" raw = load_file(dialect, file) # Load the right dialect config = FluffConfig(overrides=dict(dialect=dialect)) context = ParseContext.from_config(config) fs = FileSegment.from_raw(raw, config=config) # From just the initial parse, check we're all there assert fs.raw == raw # Do the parse with lots of logging with caplog.at_level(logging.DEBUG): logging.debug("Pre-parse structure: {0}".format(fs.to_tuple(show_raw=True))) logging.debug("Pre-parse structure: {0}".format(fs.stringify())) parsed = fs.parse(parse_context=context) # Optional: set recurse=1 to limit recursion logging.debug("Post-parse structure: {0}".format(fs.to_tuple(show_raw=True))) logging.debug("Post-parse structure: {0}".format(fs.stringify())) # 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