Merge pull request #1547 from the-mikedavis/md-test-tags
test tags queries in 'tree-sitter test'
This commit is contained in:
commit
5eb0a3090f
7 changed files with 255 additions and 1 deletions
|
|
@ -4,6 +4,7 @@ use std::path::{Path, PathBuf};
|
|||
use tree_sitter::Language;
|
||||
use tree_sitter_highlight::HighlightConfiguration;
|
||||
use tree_sitter_loader::Loader;
|
||||
use tree_sitter_tags::TagsConfiguration;
|
||||
|
||||
include!("./dirs.rs");
|
||||
|
||||
|
|
@ -54,6 +55,14 @@ pub fn get_highlight_config(
|
|||
result
|
||||
}
|
||||
|
||||
pub fn get_tags_config(language_name: &str) -> TagsConfiguration {
|
||||
let language = get_language(language_name);
|
||||
let queries_path = get_language_queries_path(language_name);
|
||||
let tags_query = fs::read_to_string(queries_path.join("tags.scm")).unwrap();
|
||||
let locals_query = fs::read_to_string(queries_path.join("locals.scm")).unwrap_or(String::new());
|
||||
TagsConfiguration::new(language, &tags_query, &locals_query).unwrap()
|
||||
}
|
||||
|
||||
pub fn get_test_language(name: &str, parser_code: &str, path: Option<&Path>) -> Language {
|
||||
let parser_c_path = SCRATCH_DIR.join(&format!("{}-parser.c", name));
|
||||
if !fs::read_to_string(&parser_c_path)
|
||||
|
|
|
|||
|
|
@ -7,4 +7,5 @@ mod pathological_test;
|
|||
mod query_test;
|
||||
mod tags_test;
|
||||
mod test_highlight_test;
|
||||
mod test_tags_test;
|
||||
mod tree_test;
|
||||
|
|
|
|||
66
cli/src/tests/test_tags_test.rs
Normal file
66
cli/src/tests/test_tags_test.rs
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
use super::helpers::fixtures::{get_language, get_tags_config};
|
||||
use crate::query_testing::{parse_position_comments, Assertion};
|
||||
use crate::test_tags::get_tag_positions;
|
||||
use tree_sitter::{Parser, Point};
|
||||
use tree_sitter_tags::TagsContext;
|
||||
|
||||
#[test]
|
||||
fn test_tags_test_with_basic_test() {
|
||||
let language = get_language("python");
|
||||
let config = get_tags_config("python");
|
||||
let source = [
|
||||
"# hi",
|
||||
"def abc(d):",
|
||||
" # <- definition.function",
|
||||
" e = fgh(d)",
|
||||
" # ^ reference.call",
|
||||
" return d(e)",
|
||||
" # ^ reference.call",
|
||||
"",
|
||||
]
|
||||
.join("\n");
|
||||
|
||||
let assertions =
|
||||
parse_position_comments(&mut Parser::new(), language, source.as_bytes()).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
assertions,
|
||||
&[
|
||||
Assertion {
|
||||
position: Point::new(1, 4),
|
||||
expected_capture_name: "definition.function".to_string(),
|
||||
},
|
||||
Assertion {
|
||||
position: Point::new(3, 9),
|
||||
expected_capture_name: "reference.call".to_string(),
|
||||
},
|
||||
Assertion {
|
||||
position: Point::new(5, 11),
|
||||
expected_capture_name: "reference.call".to_string(),
|
||||
},
|
||||
]
|
||||
);
|
||||
|
||||
let mut tags_context = TagsContext::new();
|
||||
let tag_positions = get_tag_positions(&mut tags_context, &config, source.as_bytes()).unwrap();
|
||||
assert_eq!(
|
||||
tag_positions,
|
||||
&[
|
||||
(
|
||||
Point::new(1, 4),
|
||||
Point::new(1, 7),
|
||||
"definition.function".to_string()
|
||||
),
|
||||
(
|
||||
Point::new(3, 8),
|
||||
Point::new(3, 11),
|
||||
"reference.call".to_string()
|
||||
),
|
||||
(
|
||||
Point::new(5, 11),
|
||||
Point::new(5, 12),
|
||||
"reference.call".to_string()
|
||||
),
|
||||
]
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue