mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Add MockAnalysis to make testing easier
This commit is contained in:
parent
41adf1bc4f
commit
dfba29e4fb
7 changed files with 166 additions and 75 deletions
|
@ -368,18 +368,15 @@ fn complete_fn(name_ref: ast::NameRef, scopes: &FnScopes, acc: &mut Vec<Completi
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use test_utils::{assert_eq_dbg, extract_offset};
|
||||
use test_utils::{assert_eq_dbg};
|
||||
|
||||
use crate::FileId;
|
||||
use crate::mock_analysis::MockAnalysis;
|
||||
use crate::mock_analysis::{single_file_with_position};
|
||||
|
||||
use super::*;
|
||||
|
||||
fn check_scope_completion(code: &str, expected_completions: &str) {
|
||||
let (off, code) = extract_offset(&code);
|
||||
let analysis = MockAnalysis::with_files(&[("/main.rs", &code)]).analysis();
|
||||
let file_id = FileId(1);
|
||||
let completions = scope_completion(&analysis.imp.db, file_id, off)
|
||||
let (analysis, position) = single_file_with_position(code);
|
||||
let completions = scope_completion(&analysis.imp.db, position.file_id, position.offset)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.filter(|c| c.snippet.is_none())
|
||||
|
@ -388,10 +385,8 @@ mod tests {
|
|||
}
|
||||
|
||||
fn check_snippet_completion(code: &str, expected_completions: &str) {
|
||||
let (off, code) = extract_offset(&code);
|
||||
let analysis = MockAnalysis::with_files(&[("/main.rs", &code)]).analysis();
|
||||
let file_id = FileId(1);
|
||||
let completions = scope_completion(&analysis.imp.db, file_id, off)
|
||||
let (analysis, position) = single_file_with_position(code);
|
||||
let completions = scope_completion(&analysis.imp.db, position.file_id, position.offset)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.filter(|c| c.snippet.is_some())
|
||||
|
|
|
@ -25,7 +25,7 @@ impl CrateGraph {
|
|||
pub fn new() -> CrateGraph {
|
||||
CrateGraph::default()
|
||||
}
|
||||
pub fn add_crate_root(&mut self, file_id: FileId) -> CrateId{
|
||||
pub fn add_crate_root(&mut self, file_id: FileId) -> CrateId {
|
||||
let crate_id = CrateId(self.crate_roots.len() as u32);
|
||||
let prev = self.crate_roots.insert(crate_id, file_id);
|
||||
assert!(prev.is_none());
|
||||
|
|
|
@ -13,7 +13,7 @@ mod imp;
|
|||
mod symbol_index;
|
||||
mod completion;
|
||||
mod syntax_ptr;
|
||||
mod mock_analysis;
|
||||
pub mod mock_analysis;
|
||||
|
||||
use std::{
|
||||
fmt,
|
||||
|
@ -33,7 +33,6 @@ pub use crate::{
|
|||
descriptors::function::FnDescriptor,
|
||||
completion::CompletionItem,
|
||||
input::{FileId, FileResolver, CrateGraph, CrateId},
|
||||
mock_analysis::MockAnalysis,
|
||||
};
|
||||
pub use ra_editor::{
|
||||
FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable,
|
||||
|
|
|
@ -2,11 +2,19 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use relative_path::{RelativePath, RelativePathBuf};
|
||||
use ra_syntax::TextUnit;
|
||||
use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER};
|
||||
|
||||
use crate::{
|
||||
AnalysisChange, Analysis, AnalysisHost, FileId, FileResolver,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FilePosition {
|
||||
pub file_id: FileId,
|
||||
pub offset: TextUnit,
|
||||
}
|
||||
|
||||
/// Mock analysis is used in test to bootstrap an AnalysisHost/Analysis
|
||||
/// from a set of in-memory files.
|
||||
#[derive(Debug, Default)]
|
||||
|
@ -18,11 +26,57 @@ impl MockAnalysis {
|
|||
pub fn new() -> MockAnalysis {
|
||||
MockAnalysis::default()
|
||||
}
|
||||
pub fn with_files(files: &[(&str, &str)]) -> MockAnalysis {
|
||||
let files = files.iter()
|
||||
.map(|it| (it.0.to_string(), it.1.to_string()))
|
||||
.collect();
|
||||
MockAnalysis { files }
|
||||
/// Creates `MockAnalysis` using a fixture data in the following format:
|
||||
///
|
||||
/// ```notrust
|
||||
/// //- /main.rs
|
||||
/// mod foo;
|
||||
/// fn main() {}
|
||||
///
|
||||
/// //- /foo.rs
|
||||
/// struct Baz;
|
||||
/// ```
|
||||
pub fn with_files(fixture: &str) -> MockAnalysis {
|
||||
let mut res = MockAnalysis::new();
|
||||
for entry in parse_fixture(fixture) {
|
||||
res.add_file(&entry.meta, &entry.text);
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
/// Same as `with_files`, but requires that a single file contains a `<|>` marker,
|
||||
/// whose position is also returned.
|
||||
pub fn with_files_and_position(fixture: &str) -> (MockAnalysis, FilePosition) {
|
||||
let mut position = None;
|
||||
let mut res = MockAnalysis::new();
|
||||
for entry in parse_fixture(fixture) {
|
||||
if entry.text.contains(CURSOR_MARKER) {
|
||||
assert!(position.is_none(), "only one marker (<|>) per fixture is allowed");
|
||||
position = Some(res.add_file_with_position(&entry.meta, &entry.text));
|
||||
} else {
|
||||
res.add_file(&entry.meta, &entry.text);
|
||||
}
|
||||
}
|
||||
let position = position.expect("expected a marker (<|>)");
|
||||
(res, position)
|
||||
}
|
||||
|
||||
pub fn add_file(&mut self, path: &str, text: &str) -> FileId {
|
||||
let file_id = FileId((self.files.len() + 1) as u32);
|
||||
self.files.push((path.to_string(), text.to_string()));
|
||||
file_id
|
||||
}
|
||||
pub fn add_file_with_position(&mut self, path: &str, text: &str) -> FilePosition {
|
||||
let (offset, text) = extract_offset(text);
|
||||
let file_id = FileId((self.files.len() + 1) as u32);
|
||||
self.files.push((path.to_string(), text.to_string()));
|
||||
FilePosition { file_id, offset }
|
||||
}
|
||||
pub fn id_of(&self, path: &str) -> FileId {
|
||||
let (idx, _) = self.files.iter().enumerate()
|
||||
.find(|(_, (p, _text))| path == p)
|
||||
.expect("no file in this mock");
|
||||
FileId(idx as u32 + 1)
|
||||
}
|
||||
pub fn analysis_host(self) -> AnalysisHost {
|
||||
let mut host = AnalysisHost::new();
|
||||
|
@ -44,6 +98,26 @@ impl MockAnalysis {
|
|||
}
|
||||
}
|
||||
|
||||
/// Creates analysis from a multi-file fixture, returns positions marked with <|>.
|
||||
pub fn analysis_and_position(fixture: &str) -> (Analysis, FilePosition) {
|
||||
let (mock, position) = MockAnalysis::with_files_and_position(fixture);
|
||||
(mock.analysis(), position)
|
||||
}
|
||||
|
||||
/// Creates analysis for a single file.
|
||||
pub fn single_file(code: &str) -> (Analysis, FileId) {
|
||||
let mut mock = MockAnalysis::new();
|
||||
let file_id = mock.add_file("/main.rs", code);
|
||||
(mock.analysis(), file_id)
|
||||
}
|
||||
|
||||
/// Creates analysis for a single file, returns position marked with <|>.
|
||||
pub fn single_file_with_position(code: &str) -> (Analysis, FilePosition) {
|
||||
let mut mock = MockAnalysis::new();
|
||||
let pos = mock.add_file_with_position("/main.rs", code);
|
||||
(mock.analysis(), pos)
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct FileMap(Vec<(FileId, RelativePathBuf)>);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue