mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
Add Analysis#teype_of test
This commit is contained in:
parent
a4c30d7504
commit
f5992964ed
2 changed files with 44 additions and 2 deletions
|
@ -55,6 +55,26 @@ impl MockAnalysis {
|
||||||
(res, position)
|
(res, position)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Same as `with_files`, but requires that a single file contains two `<|>` marker,
|
||||||
|
/// whose range is also returned.
|
||||||
|
pub fn with_files_and_range(fixture: &str) -> (MockAnalysis, FileRange) {
|
||||||
|
let mut range = None;
|
||||||
|
let mut res = MockAnalysis::new();
|
||||||
|
for entry in parse_fixture(fixture) {
|
||||||
|
if entry.text.contains(CURSOR_MARKER) {
|
||||||
|
assert!(
|
||||||
|
range.is_none(),
|
||||||
|
"only two marker (<|>) per fixture is allowed"
|
||||||
|
);
|
||||||
|
range = Some(res.add_file_with_range(&entry.meta, &entry.text));
|
||||||
|
} else {
|
||||||
|
res.add_file(&entry.meta, &entry.text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let range = range.expect("expected two marker (<|>)");
|
||||||
|
(res, range)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn add_file(&mut self, path: &str, text: &str) -> FileId {
|
pub fn add_file(&mut self, path: &str, text: &str) -> FileId {
|
||||||
let file_id = FileId((self.files.len() + 1) as u32);
|
let file_id = FileId((self.files.len() + 1) as u32);
|
||||||
self.files.push((path.to_string(), text.to_string()));
|
self.files.push((path.to_string(), text.to_string()));
|
||||||
|
@ -102,12 +122,18 @@ impl MockAnalysis {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates analysis from a multi-file fixture, returns positions marked with <|>.
|
/// Creates analysis from a multi-file fixture, returns positions marked with a <|>.
|
||||||
pub fn analysis_and_position(fixture: &str) -> (Analysis, FilePosition) {
|
pub fn analysis_and_position(fixture: &str) -> (Analysis, FilePosition) {
|
||||||
let (mock, position) = MockAnalysis::with_files_and_position(fixture);
|
let (mock, position) = MockAnalysis::with_files_and_position(fixture);
|
||||||
(mock.analysis(), position)
|
(mock.analysis(), position)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates analysis from a multi-file fixture, returns ranges marked with two <|>.
|
||||||
|
pub fn analysis_and_range(fixture: &str) -> (Analysis, FileRange) {
|
||||||
|
let (mock, range) = MockAnalysis::with_files_and_range(fixture);
|
||||||
|
(mock.analysis(), range)
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates analysis for a single file.
|
/// Creates analysis for a single file.
|
||||||
pub fn single_file(code: &str) -> (Analysis, FileId) {
|
pub fn single_file(code: &str) -> (Analysis, FileId) {
|
||||||
let mut mock = MockAnalysis::new();
|
let mut mock = MockAnalysis::new();
|
||||||
|
|
|
@ -2,7 +2,7 @@ use ra_syntax::TextRange;
|
||||||
use test_utils::{assert_eq_dbg, assert_eq_text};
|
use test_utils::{assert_eq_dbg, assert_eq_text};
|
||||||
|
|
||||||
use ra_analysis::{
|
use ra_analysis::{
|
||||||
mock_analysis::{analysis_and_position, single_file, single_file_with_position, MockAnalysis},
|
mock_analysis::{analysis_and_position, analysis_and_range, single_file, single_file_with_position, MockAnalysis},
|
||||||
AnalysisChange, CrateGraph, FileId, FnSignatureInfo,
|
AnalysisChange, CrateGraph, FileId, FnSignatureInfo,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -10,6 +10,22 @@ fn get_signature(text: &str) -> (FnSignatureInfo, Option<usize>) {
|
||||||
let (analysis, position) = single_file_with_position(text);
|
let (analysis, position) = single_file_with_position(text);
|
||||||
analysis.resolve_callable(position).unwrap().unwrap()
|
analysis.resolve_callable(position).unwrap().unwrap()
|
||||||
}
|
}
|
||||||
|
#[test]
|
||||||
|
fn test_type_of() {
|
||||||
|
let (analysis, range) = analysis_and_range(
|
||||||
|
"
|
||||||
|
//- /lib.rs
|
||||||
|
pub fn foo() -> u32 {
|
||||||
|
1
|
||||||
|
};
|
||||||
|
|
||||||
|
let <|>foo_test<|> = foo();
|
||||||
|
",
|
||||||
|
);
|
||||||
|
|
||||||
|
let type_name = analysis.type_of(range).unwrap().unwrap();
|
||||||
|
assert_eq_dbg("u32", &type_name);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn approximate_resolve_works_in_items() {
|
fn approximate_resolve_works_in_items() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue