mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-26 20:09:19 +00:00
Add tests
This commit is contained in:
parent
1895716c88
commit
cd5eeb904e
1 changed files with 77 additions and 3 deletions
|
@ -1,3 +1,6 @@
|
||||||
|
//! This module implements a methods and free functions search in the specified file.
|
||||||
|
//! We have to skip tests, so cannot reuse file_structure module.
|
||||||
|
|
||||||
use hir::Semantics;
|
use hir::Semantics;
|
||||||
use ide_db::RootDatabase;
|
use ide_db::RootDatabase;
|
||||||
use syntax::{ast, ast::NameOwner, AstNode, SyntaxNode};
|
use syntax::{ast, ast::NameOwner, AstNode, SyntaxNode};
|
||||||
|
@ -10,7 +13,7 @@ pub(crate) fn find_all_methods(db: &RootDatabase, file_id: FileId) -> Vec<FileRa
|
||||||
source_file.syntax().descendants().filter_map(|it| method_range(it, file_id)).collect()
|
source_file.syntax().descendants().filter_map(|it| method_range(it, file_id)).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn method_range(item: SyntaxNode, file_id: FileId) -> Option<FileRange> {
|
fn method_range(item: SyntaxNode, file_id: FileId) -> Option<FileRange> {
|
||||||
ast::Fn::cast(item).and_then(|fn_def| {
|
ast::Fn::cast(item).and_then(|fn_def| {
|
||||||
if has_test_related_attribute(&fn_def) {
|
if has_test_related_attribute(&fn_def) {
|
||||||
None
|
None
|
||||||
|
@ -19,3 +22,74 @@ pub(crate) fn method_range(item: SyntaxNode, file_id: FileId) -> Option<FileRang
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::mock_analysis::analysis_and_position;
|
||||||
|
use crate::{FileRange, TextSize};
|
||||||
|
use std::ops::RangeInclusive;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_find_all_methods() {
|
||||||
|
let (analysis, pos) = analysis_and_position(
|
||||||
|
r#"
|
||||||
|
//- /lib.rs
|
||||||
|
fn private_fn() {<|>}
|
||||||
|
|
||||||
|
pub fn pub_fn() {}
|
||||||
|
|
||||||
|
pub fn generic_fn<T>(arg: T) {}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
|
||||||
|
let refs = analysis.find_all_methods(pos.file_id).unwrap();
|
||||||
|
check_result(&refs, &[3..=13, 27..=33, 47..=57]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_find_trait_methods() {
|
||||||
|
let (analysis, pos) = analysis_and_position(
|
||||||
|
r#"
|
||||||
|
//- /lib.rs
|
||||||
|
trait Foo {
|
||||||
|
fn bar() {<|>}
|
||||||
|
fn baz() {}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
|
||||||
|
let refs = analysis.find_all_methods(pos.file_id).unwrap();
|
||||||
|
check_result(&refs, &[19..=22, 35..=38]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_skip_tests() {
|
||||||
|
let (analysis, pos) = analysis_and_position(
|
||||||
|
r#"
|
||||||
|
//- /lib.rs
|
||||||
|
#[test]
|
||||||
|
fn foo() {<|>}
|
||||||
|
|
||||||
|
pub fn pub_fn() {}
|
||||||
|
|
||||||
|
mod tests {
|
||||||
|
#[test]
|
||||||
|
fn bar() {}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
|
||||||
|
let refs = analysis.find_all_methods(pos.file_id).unwrap();
|
||||||
|
check_result(&refs, &[28..=34]);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_result(refs: &[FileRange], expected: &[RangeInclusive<u32>]) {
|
||||||
|
assert_eq!(refs.len(), expected.len());
|
||||||
|
|
||||||
|
for (i, item) in refs.iter().enumerate() {
|
||||||
|
let range = &expected[i];
|
||||||
|
assert_eq!(TextSize::from(*range.start()), item.range.start());
|
||||||
|
assert_eq!(TextSize::from(*range.end()), item.range.end());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue