7851: Compress tests r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2021-03-02 15:46:02 +00:00 committed by GitHub
commit 0ce539ec96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 459 additions and 651 deletions

View file

@ -12,7 +12,7 @@ use ide_db::{
RootDatabase,
};
use stdx::{format_to, trim_indent};
use syntax::{ast, AstNode, TextRange};
use syntax::TextRange;
use test_utils::{assert_eq_text, extract_offset};
use crate::{handlers::Handler, Assist, AssistConfig, AssistContext, AssistKind, Assists};
@ -180,50 +180,6 @@ fn labels(assists: &[Assist]) -> String {
labels.into_iter().collect::<String>()
}
pub(crate) type NameSuggestion = fn(&ast::Expr, &Semantics<'_, RootDatabase>) -> Option<String>;
#[track_caller]
pub(crate) fn check_name_suggestion(
suggestion: NameSuggestion,
ra_fixture: &str,
suggested_name: &str,
) {
check_name(suggestion, ra_fixture, Some(suggested_name));
}
#[track_caller]
pub(crate) fn check_name_suggestion_not_applicable(suggestion: NameSuggestion, ra_fixture: &str) {
check_name(suggestion, ra_fixture, None);
}
#[track_caller]
fn check_name(suggestion: NameSuggestion, ra_fixture: &str, expected: Option<&str>) {
let (db, file_with_carret_id, range_or_offset) = RootDatabase::with_range_or_offset(ra_fixture);
let frange = FileRange { file_id: file_with_carret_id, range: range_or_offset.into() };
let sema = Semantics::new(&db);
let source_file = sema.parse(frange.file_id);
let element = source_file.syntax().covering_element(frange.range);
let expr =
element.ancestors().find_map(ast::Expr::cast).expect("selection is not an expression");
assert_eq!(
expr.syntax().text_range(),
frange.range,
"selection is not an expression(yet contained in one)"
);
let name = suggestion(&expr, &sema);
match (name, expected) {
(Some(name), Some(expected_name)) => {
assert_eq_text!(&name, expected_name);
}
(Some(_), None) => panic!("name suggestion should not be applicable"),
(None, Some(_)) => panic!("name suggestion is not applicable"),
(None, None) => (),
}
}
#[test]
fn assist_order_field_struct() {
let before = "struct Foo { $0bar: u32 }";

View file

@ -11,17 +11,20 @@ use syntax::{
/// Trait names, that will be ignored when in `impl Trait` and `dyn Trait`
const USELESS_TRAITS: &[&str] = &["Send", "Sync", "Copy", "Clone", "Eq", "PartialEq"];
/// Identifier names that won't be suggested, ever
///
/// **NOTE**: they all must be snake lower case
const USELESS_NAMES: &[&str] =
&["new", "default", "option", "some", "none", "ok", "err", "str", "string"];
/// Generic types replaced by their first argument
///
/// # Examples
/// `Option<Name>` -> `Name`
/// `Result<User, Error>` -> `User`
const WRAPPER_TYPES: &[&str] = &["Box", "Option", "Result"];
/// Prefixes to strip from methods names
///
/// # Examples
@ -29,6 +32,7 @@ const WRAPPER_TYPES: &[&str] = &["Box", "Option", "Result"];
/// `args.into_config()` -> `config`
/// `bytes.to_vec()` -> `vec`
const USELESS_METHOD_PREFIXES: &[&str] = &["into_", "as_", "to_"];
/// Useless methods that are stripped from expression
///
/// # Examples
@ -253,625 +257,473 @@ fn trait_name(trait_: &hir::Trait, db: &RootDatabase) -> Option<String> {
#[cfg(test)]
mod tests {
use ide_db::base_db::{fixture::WithFixture, FileRange};
use super::*;
use crate::tests::check_name_suggestion;
#[track_caller]
fn check(ra_fixture: &str, expected: &str) {
let (db, file_id, range_or_offset) = RootDatabase::with_range_or_offset(ra_fixture);
let frange = FileRange { file_id, range: range_or_offset.into() };
mod from_func_call {
use super::*;
let sema = Semantics::new(&db);
let source_file = sema.parse(frange.file_id);
let element = source_file.syntax().covering_element(frange.range);
let expr =
element.ancestors().find_map(ast::Expr::cast).expect("selection is not an expression");
assert_eq!(
expr.syntax().text_range(),
frange.range,
"selection is not an expression(yet contained in one)"
);
let name = variable(&expr, &sema);
assert_eq!(&name, expected);
}
#[test]
fn no_args() {
check_name_suggestion(
|e, _| from_func_call(e),
r#"
fn foo() {
$0bar()$0
}"#,
"bar",
);
check(r#"fn foo() { $0bar()$0 }"#, "bar");
check(r#"fn foo() { $0bar.frobnicate()$0 }"#, "frobnicate");
}
#[test]
fn single_arg() {
check_name_suggestion(
|e, _| from_func_call(e),
r#"
fn foo() {
$0bar(1)$0
}"#,
"bar",
);
check(r#"fn foo() { $0bar(1)$0 }"#, "bar");
}
#[test]
fn many_args() {
check_name_suggestion(
|e, _| from_func_call(e),
r#"
fn foo() {
$0bar(1, 2, 3)$0
}"#,
"bar",
);
check(r#"fn foo() { $0bar(1, 2, 3)$0 }"#, "bar");
}
#[test]
fn path() {
check_name_suggestion(
|e, _| from_func_call(e),
r#"
fn foo() {
$0i32::bar(1, 2, 3)$0
}"#,
"bar",
);
check(r#"fn foo() { $0i32::bar(1, 2, 3)$0 }"#, "bar");
}
#[test]
fn generic_params() {
check_name_suggestion(
|e, _| from_func_call(e),
r#"
fn foo() {
$0bar::<i32>(1, 2, 3)$0
}"#,
"bar",
);
}
}
mod from_method_call {
use super::*;
#[test]
fn no_args() {
check_name_suggestion(
|e, _| from_method_call(e),
r#"
fn foo() {
$0bar.frobnicate()$0
}"#,
"frobnicate",
);
}
#[test]
fn generic_params() {
check_name_suggestion(
|e, _| from_method_call(e),
r#"
fn foo() {
$0bar.frobnicate::<i32, u32>()$0
}"#,
"frobnicate",
);
check(r#"fn foo() { $0bar::<i32>(1, 2, 3)$0 }"#, "bar");
check(r#"fn foo() { $0bar.frobnicate::<i32, u32>()$0 }"#, "frobnicate");
}
#[test]
fn to_name() {
check_name_suggestion(
|e, _| from_method_call(e),
check(
r#"
struct Args;
struct Config;
impl Args {
struct Args;
struct Config;
impl Args {
fn to_config(&self) -> Config {}
}
fn foo() {
}
fn foo() {
$0Args.to_config()$0;
}"#,
}
"#,
"config",
);
}
}
mod from_param {
use crate::tests::check_name_suggestion_not_applicable;
use super::*;
#[test]
fn plain_func() {
check_name_suggestion(
from_param,
check(
r#"
fn bar(n: i32, m: u32);
fn foo() {
bar($01$0, 2)
}"#,
fn bar(n: i32, m: u32);
fn foo() { bar($01$0, 2) }
"#,
"n",
);
}
#[test]
fn mut_param() {
check_name_suggestion(
from_param,
check(
r#"
fn bar(mut n: i32, m: u32);
fn foo() {
bar($01$0, 2)
}"#,
fn bar(mut n: i32, m: u32);
fn foo() { bar($01$0, 2) }
"#,
"n",
);
}
#[test]
fn func_does_not_exist() {
check_name_suggestion_not_applicable(
from_param,
r#"
fn foo() {
bar($01$0, 2)
}"#,
);
check(r#"fn foo() { bar($01$0, 2) }"#, "var_name");
}
#[test]
fn unnamed_param() {
check_name_suggestion_not_applicable(
from_param,
check(
r#"
fn bar(_: i32, m: u32);
fn foo() {
bar($01$0, 2)
}"#,
fn bar(_: i32, m: u32);
fn foo() { bar($01$0, 2) }
"#,
"var_name",
);
}
#[test]
fn tuple_pat() {
check_name_suggestion_not_applicable(
from_param,
check(
r#"
fn bar((n, k): (i32, i32), m: u32);
fn foo() {
fn bar((n, k): (i32, i32), m: u32);
fn foo() {
bar($0(1, 2)$0, 3)
}"#,
}
"#,
"var_name",
);
}
#[test]
fn ref_pat() {
check_name_suggestion(
from_param,
check(
r#"
fn bar(&n: &i32, m: u32);
fn foo() {
bar($0&1$0, 3)
}"#,
fn bar(&n: &i32, m: u32);
fn foo() { bar($0&1$0, 3) }
"#,
"n",
);
}
#[test]
fn box_pat() {
check_name_suggestion(
from_param,
check(
r#"
fn bar(box n: &i32, m: u32);
fn foo() {
bar($01$0, 3)
}"#,
fn bar(box n: &i32, m: u32);
fn foo() { bar($01$0, 3) }
"#,
"n",
);
}
#[test]
fn param_out_of_index() {
check_name_suggestion_not_applicable(
from_param,
check(
r#"
fn bar(n: i32, m: u32);
fn foo() {
bar(1, 2, $03$0)
}"#,
fn bar(n: i32, m: u32);
fn foo() { bar(1, 2, $03$0) }
"#,
"var_name",
);
}
#[test]
fn generic_param_resolved() {
check_name_suggestion(
from_param,
check(
r#"
fn bar<T>(n: T, m: u32);
fn foo() {
bar($01$0, 2)
}"#,
fn bar<T>(n: T, m: u32);
fn foo() { bar($01$0, 2) }
"#,
"n",
);
}
#[test]
fn generic_param_unresolved() {
check_name_suggestion(
from_param,
check(
r#"
fn bar<T>(n: T, m: u32);
fn foo<T>(x: T) {
bar($0x$0, 2)
}"#,
fn bar<T>(n: T, m: u32);
fn foo<T>(x: T) { bar($0x$0, 2) }
"#,
"n",
);
}
#[test]
fn method() {
check_name_suggestion(
from_param,
check(
r#"
struct S;
impl S {
fn bar(&self, n: i32, m: u32);
}
fn foo() {
S.bar($01$0, 2)
}"#,
struct S;
impl S { fn bar(&self, n: i32, m: u32); }
fn foo() { S.bar($01$0, 2) }
"#,
"n",
);
}
#[test]
fn method_ufcs() {
check_name_suggestion(
from_param,
check(
r#"
struct S;
impl S {
fn bar(&self, n: i32, m: u32);
}
fn foo() {
S::bar(&S, $01$0, 2)
}"#,
struct S;
impl S { fn bar(&self, n: i32, m: u32); }
fn foo() { S::bar(&S, $01$0, 2) }
"#,
"n",
);
}
#[test]
fn method_self() {
check_name_suggestion_not_applicable(
from_param,
check(
r#"
struct S;
impl S {
fn bar(&self, n: i32, m: u32);
}
fn foo() {
S::bar($0&S$0, 1, 2)
}"#,
struct S;
impl S { fn bar(&self, n: i32, m: u32); }
fn foo() { S::bar($0&S$0, 1, 2) }
"#,
"s",
);
}
#[test]
fn method_self_named() {
check_name_suggestion(
from_param,
check(
r#"
struct S;
impl S {
fn bar(strukt: &Self, n: i32, m: u32);
}
fn foo() {
S::bar($0&S$0, 1, 2)
}"#,
struct S;
impl S { fn bar(strukt: &Self, n: i32, m: u32); }
fn foo() { S::bar($0&S$0, 1, 2) }
"#,
"strukt",
);
}
}
mod from_type {
use crate::tests::check_name_suggestion_not_applicable;
use super::*;
#[test]
fn i32() {
check_name_suggestion_not_applicable(
from_type,
r#"
fn foo() {
let _: i32 = $01$0;
}"#,
);
check(r#"fn foo() { let _: i32 = $01$0; }"#, "var_name");
}
#[test]
fn u64() {
check_name_suggestion_not_applicable(
from_type,
r#"
fn foo() {
let _: u64 = $01$0;
}"#,
);
check(r#"fn foo() { let _: u64 = $01$0; }"#, "var_name");
}
#[test]
fn bool() {
check_name_suggestion_not_applicable(
from_type,
r#"
fn foo() {
let _: bool = $0true$0;
}"#,
);
check(r#"fn foo() { let _: bool = $0true$0; }"#, "var_name");
}
#[test]
fn struct_unit() {
check_name_suggestion(
from_type,
check(
r#"
struct Seed;
fn foo() {
let _ = $0Seed$0;
}"#,
struct Seed;
fn foo() { let _ = $0Seed$0; }
"#,
"seed",
);
}
#[test]
fn struct_unit_to_snake() {
check_name_suggestion(
from_type,
check(
r#"
struct SeedState;
fn foo() {
let _ = $0SeedState$0;
}"#,
struct SeedState;
fn foo() { let _ = $0SeedState$0; }
"#,
"seed_state",
);
}
#[test]
fn struct_single_arg() {
check_name_suggestion(
from_type,
check(
r#"
struct Seed(u32);
fn foo() {
let _ = $0Seed(0)$0;
}"#,
struct Seed(u32);
fn foo() { let _ = $0Seed(0)$0; }
"#,
"seed",
);
}
#[test]
fn struct_with_fields() {
check_name_suggestion(
from_type,
check(
r#"
struct Seed { value: u32 }
fn foo() {
let _ = $0Seed { value: 0 }$0;
}"#,
struct Seed { value: u32 }
fn foo() { let _ = $0Seed { value: 0 }$0; }
"#,
"seed",
);
}
#[test]
fn enum_() {
check_name_suggestion(
from_type,
check(
r#"
enum Kind { A, B }
fn foo() {
let _ = $0Kind::A$0;
}"#,
enum Kind { A, B }
fn foo() { let _ = $0Kind::A$0; }
"#,
"kind",
);
}
#[test]
fn enum_generic_resolved() {
check_name_suggestion(
from_type,
check(
r#"
enum Kind<T> { A(T), B }
fn foo() {
let _ = $0Kind::A(1)$0;
}"#,
enum Kind<T> { A { x: T }, B }
fn foo() { let _ = $0Kind::A { x:1 }$0; }
"#,
"kind",
);
}
#[test]
fn enum_generic_unresolved() {
check_name_suggestion(
from_type,
check(
r#"
enum Kind<T> { A(T), B }
fn foo<T>(x: T) {
let _ = $0Kind::A(x)$0;
}"#,
enum Kind<T> { A { x: T }, B }
fn foo<T>(x: T) { let _ = $0Kind::A { x }$0; }
"#,
"kind",
);
}
#[test]
fn dyn_trait() {
check_name_suggestion(
from_type,
check(
r#"
trait DynHandler {}
fn bar() -> dyn DynHandler {}
fn foo() {
$0bar()$0;
}"#,
trait DynHandler {}
fn bar() -> dyn DynHandler {}
fn foo() { $0(bar())$0; }
"#,
"dyn_handler",
);
}
#[test]
fn impl_trait() {
check_name_suggestion(
from_type,
check(
r#"
trait StaticHandler {}
fn bar() -> impl StaticHandler {}
fn foo() {
$0bar()$0;
}"#,
trait StaticHandler {}
fn bar() -> impl StaticHandler {}
fn foo() { $0(bar())$0; }
"#,
"static_handler",
);
}
#[test]
fn impl_trait_plus_clone() {
check_name_suggestion(
from_type,
check(
r#"
trait StaticHandler {}
trait Clone {}
fn bar() -> impl StaticHandler + Clone {}
fn foo() {
$0bar()$0;
}"#,
trait StaticHandler {}
trait Clone {}
fn bar() -> impl StaticHandler + Clone {}
fn foo() { $0(bar())$0; }
"#,
"static_handler",
);
}
#[test]
fn impl_trait_plus_lifetime() {
check_name_suggestion(
from_type,
check(
r#"
trait StaticHandler {}
trait Clone {}
fn bar<'a>(&'a i32) -> impl StaticHandler + 'a {}
fn foo() {
$0bar(&1)$0;
}"#,
trait StaticHandler {}
trait Clone {}
fn bar<'a>(&'a i32) -> impl StaticHandler + 'a {}
fn foo() { $0(bar(&1))$0; }
"#,
"static_handler",
);
}
#[test]
fn impl_trait_plus_trait() {
check_name_suggestion_not_applicable(
from_type,
check(
r#"
trait Handler {}
trait StaticHandler {}
fn bar() -> impl StaticHandler + Handler {}
fn foo() {
$0bar()$0;
}"#,
trait Handler {}
trait StaticHandler {}
fn bar() -> impl StaticHandler + Handler {}
fn foo() { $0(bar())$0; }
"#,
"bar",
);
}
#[test]
fn ref_value() {
check_name_suggestion(
from_type,
check(
r#"
struct Seed;
fn bar() -> &Seed {}
fn foo() {
$0bar()$0;
}"#,
struct Seed;
fn bar() -> &Seed {}
fn foo() { $0(bar())$0; }
"#,
"seed",
);
}
#[test]
fn box_value() {
check_name_suggestion(
from_type,
check(
r#"
struct Box<T>(*const T);
struct Seed;
fn bar() -> Box<Seed> {}
fn foo() {
$0bar()$0;
}"#,
struct Box<T>(*const T);
struct Seed;
fn bar() -> Box<Seed> {}
fn foo() { $0(bar())$0; }
"#,
"seed",
);
}
#[test]
fn box_generic() {
check_name_suggestion_not_applicable(
from_type,
check(
r#"
struct Box<T>(*const T);
fn bar<T>() -> Box<T> {}
fn foo<T>() {
$0bar::<T>()$0;
}"#,
struct Box<T>(*const T);
fn bar<T>() -> Box<T> {}
fn foo<T>() { $0(bar::<T>())$0; }
"#,
"bar",
);
}
#[test]
fn option_value() {
check_name_suggestion(
from_type,
check(
r#"
enum Option<T> { Some(T) }
struct Seed;
fn bar() -> Option<Seed> {}
fn foo() {
$0bar()$0;
}"#,
enum Option<T> { Some(T) }
struct Seed;
fn bar() -> Option<Seed> {}
fn foo() { $0(bar())$0; }
"#,
"seed",
);
}
#[test]
fn result_value() {
check_name_suggestion(
from_type,
check(
r#"
enum Result<T, E> { Ok(T), Err(E) }
struct Seed;
struct Error;
fn bar() -> Result<Seed, Error> {}
fn foo() {
$0bar()$0;
}"#,
enum Result<T, E> { Ok(T), Err(E) }
struct Seed;
struct Error;
fn bar() -> Result<Seed, Error> {}
fn foo() { $0(bar())$0; }
"#,
"seed",
);
}
}
mod variable {
use super::*;
#[test]
fn ref_call() {
check_name_suggestion(
|e, c| Some(variable(e, c)),
check(
r#"
fn foo() {
$0&bar(1, 3)$0
}"#,
fn foo() { $0&bar(1, 3)$0 }
"#,
"bar",
);
}
#[test]
fn name_to_string() {
check_name_suggestion(
|e, c| Some(variable(e, c)),
check(
r#"
fn foo() {
$0function.name().to_string()$0
}"#,
fn foo() { $0function.name().to_string()$0 }
"#,
"name",
);
}
#[test]
fn nested_useless_method() {
check_name_suggestion(
|e, c| Some(variable(e, c)),
check(
r#"
fn foo() {
$0function.name().as_ref().unwrap().to_string()$0
}"#,
fn foo() { $0function.name().as_ref().unwrap().to_string()$0 }
"#,
"name",
);
}
}
}