Remove duplication

This commit is contained in:
Aleksey Kladov 2020-06-24 11:31:30 +02:00
parent c6795fb83a
commit c749fe223b
8 changed files with 34 additions and 41 deletions

View File

@ -215,7 +215,7 @@ fn parameters(&self) -> &[String] {
mod tests { mod tests {
use test_utils::mark; use test_utils::mark;
use crate::mock_analysis::single_file_with_position; use crate::mock_analysis::analysis_and_position;
use super::*; use super::*;
@ -231,7 +231,7 @@ fn label(&self) -> String {
} }
fn call_info_helper(text: &str) -> Option<CallInfo> { fn call_info_helper(text: &str) -> Option<CallInfo> {
let (analysis, position) = single_file_with_position(text); let (analysis, position) = analysis_and_position(text);
analysis.call_info(position).unwrap() analysis.call_info(position).unwrap()
} }
@ -530,7 +530,7 @@ pub fn foo(mut r: WriteHandler<()>) {
#[test] #[test]
fn call_info_bad_offset() { fn call_info_bad_offset() {
mark::check!(call_info_bad_offset); mark::check!(call_info_bad_offset);
let (analysis, position) = single_file_with_position( let (analysis, position) = analysis_and_position(
r#"fn foo(x: u32, y: u32) -> u32 {x + y} r#"fn foo(x: u32, y: u32) -> u32 {x + y}
fn bar() { foo <|> (3, ); }"#, fn bar() { foo <|> (3, ); }"#,
); );

View File

@ -2,7 +2,7 @@
use crate::{ use crate::{
completion::{completion_item::CompletionKind, CompletionConfig}, completion::{completion_item::CompletionKind, CompletionConfig},
mock_analysis::{analysis_and_position, single_file_with_position}, mock_analysis::analysis_and_position,
CompletionItem, CompletionItem,
}; };
use hir::Semantics; use hir::Semantics;
@ -33,7 +33,7 @@ fn get_all_completion_items(code: &str, options: &CompletionConfig) -> Vec<Compl
let (analysis, position) = if code.contains("//-") { let (analysis, position) = if code.contains("//-") {
analysis_and_position(code) analysis_and_position(code)
} else { } else {
single_file_with_position(code) analysis_and_position(code)
}; };
analysis.completions(options, position).unwrap().unwrap().into() analysis.completions(options, position).unwrap().unwrap().into()
} }
@ -55,7 +55,7 @@ pub(crate) fn completion_list_with_options(
} }
pub(crate) fn check_pattern_is_applicable(code: &str, check: fn(SyntaxElement) -> bool) { pub(crate) fn check_pattern_is_applicable(code: &str, check: fn(SyntaxElement) -> bool) {
let (analysis, pos) = single_file_with_position(code); let (analysis, pos) = analysis_and_position(code);
analysis analysis
.with_db(|db| { .with_db(|db| {
let sema = Semantics::new(db); let sema = Semantics::new(db);

View File

@ -315,12 +315,12 @@ fn adj_comments(comment: &ast::Comment, dir: Direction) -> ast::Comment {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::mock_analysis::single_file_with_position; use crate::mock_analysis::analysis_and_position;
use super::*; use super::*;
fn do_check(before: &str, afters: &[&str]) { fn do_check(before: &str, afters: &[&str]) {
let (analysis, position) = single_file_with_position(&before); let (analysis, position) = analysis_and_position(&before);
let before = analysis.file_text(position.file_id).unwrap(); let before = analysis.file_text(position.file_id).unwrap();
let range = TextRange::empty(position.offset); let range = TextRange::empty(position.offset);
let mut frange = FileRange { file_id: position.file_id, range }; let mut frange = FileRange { file_id: position.file_id, range };

View File

@ -399,7 +399,7 @@ mod tests {
use ra_db::FileLoader; use ra_db::FileLoader;
use ra_syntax::TextRange; use ra_syntax::TextRange;
use crate::mock_analysis::{analysis_and_position, single_file_with_position}; use crate::mock_analysis::analysis_and_position;
fn trim_markup(s: &str) -> &str { fn trim_markup(s: &str) -> &str {
s.trim_start_matches("```rust\n").trim_end_matches("\n```") s.trim_start_matches("```rust\n").trim_end_matches("\n```")
@ -442,7 +442,7 @@ fn check_hover_no_result(fixture: &str) {
#[test] #[test]
fn hover_shows_type_of_an_expression() { fn hover_shows_type_of_an_expression() {
let (analysis, position) = single_file_with_position( let (analysis, position) = analysis_and_position(
r#" r#"
pub fn foo() -> u32 { 1 } pub fn foo() -> u32 { 1 }
@ -641,7 +641,7 @@ fn main() {
#[test] #[test]
fn hover_some() { fn hover_some() {
let (analysis, position) = single_file_with_position( let (analysis, position) = analysis_and_position(
" "
enum Option<T> { Some(T) } enum Option<T> { Some(T) }
use Option::Some; use Option::Some;
@ -654,7 +654,7 @@ fn main() {
let hover = analysis.hover(position).unwrap().unwrap(); let hover = analysis.hover(position).unwrap().unwrap();
assert_eq!(trim_markup_opt(hover.info.first()), Some("Option\n```\n\n```rust\nSome")); assert_eq!(trim_markup_opt(hover.info.first()), Some("Option\n```\n\n```rust\nSome"));
let (analysis, position) = single_file_with_position( let (analysis, position) = analysis_and_position(
" "
enum Option<T> { Some(T) } enum Option<T> { Some(T) }
use Option::Some; use Option::Some;
@ -720,21 +720,21 @@ fn main() {
#[test] #[test]
fn hover_for_local_variable() { fn hover_for_local_variable() {
let (analysis, position) = single_file_with_position("fn func(foo: i32) { fo<|>o; }"); let (analysis, position) = analysis_and_position("fn func(foo: i32) { fo<|>o; }");
let hover = analysis.hover(position).unwrap().unwrap(); let hover = analysis.hover(position).unwrap().unwrap();
assert_eq!(trim_markup_opt(hover.info.first()), Some("i32")); assert_eq!(trim_markup_opt(hover.info.first()), Some("i32"));
} }
#[test] #[test]
fn hover_for_local_variable_pat() { fn hover_for_local_variable_pat() {
let (analysis, position) = single_file_with_position("fn func(fo<|>o: i32) {}"); let (analysis, position) = analysis_and_position("fn func(fo<|>o: i32) {}");
let hover = analysis.hover(position).unwrap().unwrap(); let hover = analysis.hover(position).unwrap().unwrap();
assert_eq!(trim_markup_opt(hover.info.first()), Some("i32")); assert_eq!(trim_markup_opt(hover.info.first()), Some("i32"));
} }
#[test] #[test]
fn hover_local_var_edge() { fn hover_local_var_edge() {
let (analysis, position) = single_file_with_position( let (analysis, position) = analysis_and_position(
" "
fn func(foo: i32) { if true { <|>foo; }; } fn func(foo: i32) { if true { <|>foo; }; }
", ",
@ -745,14 +745,14 @@ fn hover_local_var_edge() {
#[test] #[test]
fn hover_for_param_edge() { fn hover_for_param_edge() {
let (analysis, position) = single_file_with_position("fn func(<|>foo: i32) {}"); let (analysis, position) = analysis_and_position("fn func(<|>foo: i32) {}");
let hover = analysis.hover(position).unwrap().unwrap(); let hover = analysis.hover(position).unwrap().unwrap();
assert_eq!(trim_markup_opt(hover.info.first()), Some("i32")); assert_eq!(trim_markup_opt(hover.info.first()), Some("i32"));
} }
#[test] #[test]
fn test_hover_infer_associated_method_result() { fn test_hover_infer_associated_method_result() {
let (analysis, position) = single_file_with_position( let (analysis, position) = analysis_and_position(
" "
struct Thing { x: u32 } struct Thing { x: u32 }
@ -773,7 +773,7 @@ fn main() {
#[test] #[test]
fn test_hover_infer_associated_method_exact() { fn test_hover_infer_associated_method_exact() {
let (analysis, position) = single_file_with_position( let (analysis, position) = analysis_and_position(
" "
mod wrapper { mod wrapper {
struct Thing { x: u32 } struct Thing { x: u32 }
@ -799,7 +799,7 @@ fn main() {
#[test] #[test]
fn test_hover_infer_associated_const_in_pattern() { fn test_hover_infer_associated_const_in_pattern() {
let (analysis, position) = single_file_with_position( let (analysis, position) = analysis_and_position(
" "
struct X; struct X;
impl X { impl X {
@ -821,7 +821,7 @@ fn main() {
#[test] #[test]
fn test_hover_self() { fn test_hover_self() {
let (analysis, position) = single_file_with_position( let (analysis, position) = analysis_and_position(
" "
struct Thing { x: u32 } struct Thing { x: u32 }
impl Thing { impl Thing {
@ -835,7 +835,7 @@ fn new() -> Self {
assert_eq!(trim_markup_opt(hover.info.first()), Some("Thing")); assert_eq!(trim_markup_opt(hover.info.first()), Some("Thing"));
/* FIXME: revive these tests /* FIXME: revive these tests
let (analysis, position) = single_file_with_position( let (analysis, position) = analysis_and_position(
" "
struct Thing { x: u32 } struct Thing { x: u32 }
impl Thing { impl Thing {
@ -849,7 +849,7 @@ fn new() -> Self<|> {
let hover = analysis.hover(position).unwrap().unwrap(); let hover = analysis.hover(position).unwrap().unwrap();
assert_eq!(trim_markup_opt(hover.info.first()), Some("Thing")); assert_eq!(trim_markup_opt(hover.info.first()), Some("Thing"));
let (analysis, position) = single_file_with_position( let (analysis, position) = analysis_and_position(
" "
enum Thing { A } enum Thing { A }
impl Thing { impl Thing {
@ -862,7 +862,7 @@ pub fn new() -> Self<|> {
let hover = analysis.hover(position).unwrap().unwrap(); let hover = analysis.hover(position).unwrap().unwrap();
assert_eq!(trim_markup_opt(hover.info.first()), Some("enum Thing")); assert_eq!(trim_markup_opt(hover.info.first()), Some("enum Thing"));
let (analysis, position) = single_file_with_position( let (analysis, position) = analysis_and_position(
" "
enum Thing { A } enum Thing { A }
impl Thing { impl Thing {
@ -878,7 +878,7 @@ pub fn thing(a: Self<|>) {
#[test] #[test]
fn test_hover_shadowing_pat() { fn test_hover_shadowing_pat() {
let (analysis, position) = single_file_with_position( let (analysis, position) = analysis_and_position(
" "
fn x() {} fn x() {}
@ -894,7 +894,7 @@ fn y() {
#[test] #[test]
fn test_hover_macro_invocation() { fn test_hover_macro_invocation() {
let (analysis, position) = single_file_with_position( let (analysis, position) = analysis_and_position(
" "
macro_rules! foo { macro_rules! foo {
() => {} () => {}
@ -911,7 +911,7 @@ fn f() {
#[test] #[test]
fn test_hover_tuple_field() { fn test_hover_tuple_field() {
let (analysis, position) = single_file_with_position( let (analysis, position) = analysis_and_position(
" "
struct TS(String, i32<|>); struct TS(String, i32<|>);
", ",

View File

@ -221,11 +221,6 @@ pub fn single_file(ra_fixture: &str) -> (Analysis, FileId) {
(mock.analysis(), file_id) (mock.analysis(), file_id)
} }
/// Creates analysis for a single file, returns position marked with <|>.
pub fn single_file_with_position(ra_fixture: &str) -> (Analysis, FilePosition) {
analysis_and_position(ra_fixture)
}
/// Creates analysis for a single file, returns range marked with a pair of <|>. /// Creates analysis for a single file, returns range marked with a pair of <|>.
pub fn single_file_with_range(ra_fixture: &str) -> (Analysis, FileRange) { pub fn single_file_with_range(ra_fixture: &str) -> (Analysis, FileRange) {
let mut mock = MockAnalysis::new(); let mut mock = MockAnalysis::new();

View File

@ -191,7 +191,7 @@ fn get_struct_def_name_for_struct_literal_search(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{ use crate::{
mock_analysis::{analysis_and_position, single_file_with_position, MockAnalysis}, mock_analysis::{analysis_and_position, MockAnalysis},
Declaration, Reference, ReferenceSearchResult, SearchScope, Declaration, Reference, ReferenceSearchResult, SearchScope,
}; };
@ -653,7 +653,7 @@ fn g() {
} }
fn get_all_refs(ra_fixture: &str) -> ReferenceSearchResult { fn get_all_refs(ra_fixture: &str) -> ReferenceSearchResult {
let (analysis, position) = single_file_with_position(ra_fixture); let (analysis, position) = analysis_and_position(ra_fixture);
analysis.find_all_refs(position, None).unwrap().unwrap() analysis.find_all_refs(position, None).unwrap().unwrap()
} }

View File

@ -271,12 +271,10 @@ fn rename_reference(
mod tests { mod tests {
use insta::assert_debug_snapshot; use insta::assert_debug_snapshot;
use ra_text_edit::TextEditBuilder; use ra_text_edit::TextEditBuilder;
use stdx::trim_indent;
use test_utils::{assert_eq_text, mark}; use test_utils::{assert_eq_text, mark};
use crate::{ use crate::{mock_analysis::analysis_and_position, FileId};
mock_analysis::analysis_and_position, mock_analysis::single_file_with_position, FileId,
};
use stdx::trim_indent;
#[test] #[test]
fn test_rename_to_underscore() { fn test_rename_to_underscore() {
@ -310,7 +308,7 @@ fn main() {
#[test] #[test]
fn test_rename_to_invalid_identifier() { fn test_rename_to_invalid_identifier() {
let (analysis, position) = single_file_with_position( let (analysis, position) = analysis_and_position(
" "
fn main() { fn main() {
let i<|> = 1; let i<|> = 1;
@ -1056,7 +1054,7 @@ fn f(foo: &Foo) -> i32 {
fn test_rename(ra_fixture_before: &str, new_name: &str, ra_fixture_after: &str) { fn test_rename(ra_fixture_before: &str, new_name: &str, ra_fixture_after: &str) {
let ra_fixture_after = &trim_indent(ra_fixture_after); let ra_fixture_after = &trim_indent(ra_fixture_after);
let (analysis, position) = single_file_with_position(ra_fixture_before); let (analysis, position) = analysis_and_position(ra_fixture_before);
let source_change = analysis.rename(position, new_name).unwrap(); let source_change = analysis.rename(position, new_name).unwrap();
let mut text_edit_builder = TextEditBuilder::default(); let mut text_edit_builder = TextEditBuilder::default();
let mut file_id: Option<FileId> = None; let mut file_id: Option<FileId> = None;

View File

@ -77,11 +77,11 @@ fn node_indent(file: &SourceFile, token: &SyntaxToken) -> Option<SmolStr> {
mod tests { mod tests {
use test_utils::assert_eq_text; use test_utils::assert_eq_text;
use crate::mock_analysis::single_file_with_position; use crate::mock_analysis::analysis_and_position;
use stdx::trim_indent; use stdx::trim_indent;
fn apply_on_enter(before: &str) -> Option<String> { fn apply_on_enter(before: &str) -> Option<String> {
let (analysis, position) = single_file_with_position(&before); let (analysis, position) = analysis_and_position(&before);
let result = analysis.on_enter(position).unwrap()?; let result = analysis.on_enter(position).unwrap()?;
let mut actual = analysis.file_text(position.file_id).unwrap().to_string(); let mut actual = analysis.file_text(position.file_id).unwrap().to_string();