def test_choose_trees(build_terms): with open(make_path("pseudo_bnf.lark")) as f: pseudo_bnf = f.read() trees = build_syntax_trees(pseudo_bnf, build_terms) chosen = choose_trees(["geom-command", "file-command", "something"], trees) assert len(trees) == 8 assert len(chosen) == 2 assert "geom-command" in chosen assert "file-command" in chosen assert not "something" in chosen
def test_find_related_trees(build_terms): with open(make_path("pseudo_bnf.lark")) as f: pseudo_bnf = f.read() trees = build_syntax_trees(pseudo_bnf, build_terms) found = find_related_trees("eval-command", trees) assert len(found) == 4 assert { "eval-command", "eval-function", "eval-function-call", "eval-expression" } == set(found.keys())
def test_build_syntax_trees(build_terms): with open(make_path("pseudo_bnf.lark")) as f: pseudo_bnf = f.read() trees = build_syntax_trees(pseudo_bnf, build_terms) assert len(trees) == 8 assert next( trees["fieldsummary-command"].find_data("argument")).children == [ "maxvals" ] assert get_token_data( next(trees["fieldsummary-command"].find_data( "argument"))) == "maxvals"
def test_local_fetch_spl_terms(file): resource = LocalSplResource(make_path(file)) resource.fetch_spl_terms() assert len(resource.spl_terms) == 3 resource.fetch_spl_terms(["stats-command", "abstract-command"]) assert len(resource.spl_terms) == 2 stats = resource.spl_terms["stats"] assert stats.name == "stats" assert stats.syntax == "stats <stats-command-arguments>" assert stats.description
def test_build_syntax_trees_errors(): valid_term = SPLTerm("fieldsummary-command", "fieldsummary (maxvals=<num>)? <wc-field-list>?") terms = {"fieldsummary-command": valid_term} with pytest.raises(InitError): trees = build_syntax_trees("", terms) with open(make_path("pseudo_bnf.lark")) as f: pseudo_bnf = f.read() invalid_term = SPLTerm("term", ")(") terms = {"term": invalid_term} trees = build_syntax_trees(pseudo_bnf, terms) assert not trees
def test_parse_syntax_tree(build_terms): with open(make_path("pseudo_bnf.lark")) as f: pseudo_bnf = f.read() trees = build_syntax_trees(pseudo_bnf, build_terms) a, f, o = parse_syntax_tree(trees["autoregress-command"]) assert "AS" in o assert "p" in a a, f, o = parse_syntax_tree(trees["eval-function"]) assert "abs" in f assert not a a, f, o = parse_syntax_tree(trees["geom-command"]) assert "gen" in a assert not f assert not o
def test_spl_command_parsing(build_terms): with open(make_path("pseudo_bnf.lark")) as f: pseudo_bnf = f.read() trees = build_syntax_trees(pseudo_bnf, build_terms) command = SPLCommand( "eval-command", 'eval <eval-field>=<eval-expression> ("," <eval-field>=<eval-expression>)*' ) command.parse(trees) assert not command.arguments assert len(command.functions) == 2 assert command.functions == {"abs", "case"} command = SPLCommand( "autoregress-command", 'autoregress <field> (AS <field:newfield>)? (p=<int:p_start>("-"<int:p_end>)?)?' ) command.parse(trees) assert command.arguments == {"p"} assert not command.functions assert command.operators == {"AS"}
def test_local_fetch_spl_terms_errors(file): spl_resource = LocalSplResource(make_path(file)) with pytest.raises(ParsingError): spl_resource.fetch_spl_terms()