Add wasm binding for running tree queries in a limited range

This commit is contained in:
Max Brunsfeld 2019-09-11 14:44:49 -07:00
parent 0528ad5f58
commit 49ce2fddb9
6 changed files with 143 additions and 24 deletions

View file

@ -56,8 +56,12 @@ fn test_query_errors_on_invalid_symbols() {
Err(QueryError::NodeType("non_existent3"))
);
assert_eq!(
Query::new(language, "(if_statement not_a_field: (identifier))"),
Err(QueryError::Field("not_a_field"))
Query::new(language, "(if_statement condit: (identifier))"),
Err(QueryError::Field("condit"))
);
assert_eq!(
Query::new(language, "(if_statement conditioning: (identifier))"),
Err(QueryError::Field("conditioning"))
);
});
}
@ -368,6 +372,67 @@ fn test_query_exec_within_byte_range() {
});
}
#[test]
fn test_query_exec_different_queries() {
allocations::record(|| {
let language = get_language("javascript");
let query1 = Query::new(
language,
"
(array (identifier) @id1)
",
)
.unwrap();
let query2 = Query::new(
language,
"
(array (identifier) @id1)
(pair (identifier) @id2)
",
)
.unwrap();
let query3 = Query::new(
language,
"
(array (identifier) @id1)
(pair (identifier) @id2)
(parenthesized_expression (identifier) @id3)
",
)
.unwrap();
let source = "[a, {b: b}, (c)];";
let mut parser = Parser::new();
let mut cursor = QueryCursor::new();
parser.set_language(language).unwrap();
let tree = parser.parse(&source, None).unwrap();
let matches = cursor.exec(&query1, tree.root_node());
assert_eq!(
collect_matches(matches, &query1, source),
&[(0, vec![("id1", "a")]),]
);
let matches = cursor.exec(&query3, tree.root_node());
assert_eq!(
collect_matches(matches, &query3, source),
&[
(0, vec![("id1", "a")]),
(1, vec![("id2", "b")]),
(2, vec![("id3", "c")]),
]
);
let matches = cursor.exec(&query2, tree.root_node());
assert_eq!(
collect_matches(matches, &query2, source),
&[(0, vec![("id1", "a")]), (1, vec![("id2", "b")]),]
);
});
}
#[test]
fn test_query_capture_names() {
allocations::record(|| {