Fix more bugs in binary search used in tree queries

This binary search implementation differs from Rust's
`slice::binary_search_by` method in how they deal with ties.

In Rust's implementation:

> If there are multiple matches, then any one of the matches
> could be returned.

This implementation needs to return the index of the *first* match.
This commit is contained in:
Max Brunsfeld 2019-09-11 14:44:49 -07:00
parent 33f89522f6
commit d674bc139a
2 changed files with 70 additions and 15 deletions

View file

@ -143,7 +143,7 @@ fn test_query_exec_with_multiple_matches_same_root() {
}
#[test]
fn test_query_exec_multiple_patterns() {
fn test_query_exec_multiple_patterns_different_roots() {
allocations::record(|| {
let language = get_language("javascript");
let query = Query::new(
@ -178,6 +178,47 @@ fn test_query_exec_multiple_patterns() {
});
}
#[test]
fn test_query_exec_multiple_patterns_same_root() {
allocations::record(|| {
let language = get_language("javascript");
let query = Query::new(
language,
"
(pair
key: (property_identifier) @method-def
value: (function))
(pair
key: (property_identifier) @method-def
value: (arrow_function))
",
)
.unwrap();
let source = "
a = {
b: () => { return c; },
d: function() { return d; }
};
";
let mut parser = Parser::new();
parser.set_language(language).unwrap();
let tree = parser.parse(source, None).unwrap();
let context = query.context();
let matches = context.exec(tree.root_node());
assert_eq!(
collect_matches(matches, &query, source),
&[
(1, vec![("method-def", "b")]),
(0, vec![("method-def", "d")]),
],
);
});
}
#[test]
fn test_query_exec_nested_matches_without_fields() {
allocations::record(|| {