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)) 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())) with RootParseContext.from_config( config) as ctx: # Optional: set recurse=1 to limit recursion parsed = fs.parse(parse_context=ctx) 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. """ config = FluffConfig(overrides=dict(dialect='ansi')) seg_list = lex(raw, config=config) Seg = validate_segment(segmentref, config=config) # This test is different if we're working with RawSegment # derivatives or not. if issubclass(Seg, RawSegment): print("Raw route...") with RootParseContext.from_config(config) as ctx: with caplog.at_level(logging.DEBUG): parsed = Seg.match(segments=seg_list, parse_context=ctx) 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 RootParseContext.from_config(config) as ctx: with caplog.at_level(logging.DEBUG): parsed = seg.parse(parse_context=ctx) 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, code_only, yamlfile, yaml_loader): """For given test examples, check parsed structure against yaml.""" # Load the right dialect config = FluffConfig(overrides=dict(dialect=dialect)) # 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 RootParseContext.from_config(config) as ctx: parsed = fs.parse(parse_context=ctx) assert parsed.to_tuple(code_only=code_only, 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) with RootParseContext.from_config(config) as ctx: with caplog.at_level(logging.DEBUG): match = Seg.match(segments=seg_list, parse_context=ctx) assert not match