Fix tests

This commit is contained in:
Aleksei Sidorov 2020-09-04 17:55:27 +03:00
parent a1c060c8d0
commit ef0a1b2e58
3 changed files with 25 additions and 18 deletions

View File

@ -7,11 +7,11 @@
// Replaces `impl Trait` function argument with the named generic. // Replaces `impl Trait` function argument with the named generic.
// //
// ``` // ```
// fn foo<G>(bar: <|>impl Bar) {} // fn foo(bar: <|>impl Bar) {}
// ``` // ```
// -> // ->
// ``` // ```
// fn foo<B: Bar>(bar: B) {} // fn foo<B: Bar,>(bar: B) {}
// ``` // ```
pub(crate) fn replace_impl_trait_with_generic( pub(crate) fn replace_impl_trait_with_generic(
acc: &mut Assists, acc: &mut Assists,
@ -21,13 +21,7 @@ pub(crate) fn replace_impl_trait_with_generic(
let type_param = type_impl_trait.syntax().parent().and_then(ast::Param::cast)?; let type_param = type_impl_trait.syntax().parent().and_then(ast::Param::cast)?;
let type_fn = type_param.syntax().ancestors().find_map(ast::Fn::cast)?; let type_fn = type_param.syntax().ancestors().find_map(ast::Fn::cast)?;
let impl_trait_ty = type_impl_trait let impl_trait_ty = type_impl_trait.type_bound_list()?;
.syntax()
.descendants()
.last()
.and_then(ast::NameRef::cast)?
.text()
.to_string();
let target = type_fn.syntax().text_range(); let target = type_fn.syntax().text_range();
acc.add( acc.add(
@ -35,7 +29,7 @@ pub(crate) fn replace_impl_trait_with_generic(
"Replace impl trait with generic", "Replace impl trait with generic",
target, target,
|edit| { |edit| {
let generic_letter = impl_trait_ty.chars().next().unwrap().to_string(); let generic_letter = impl_trait_ty.to_string().chars().next().unwrap().to_string();
let generic_param_list = type_fn let generic_param_list = type_fn
.generic_param_list() .generic_param_list()
@ -65,7 +59,7 @@ fn replace_impl_trait_with_generic_params() {
fn foo<G>(bar: <|>impl Bar) {} fn foo<G>(bar: <|>impl Bar) {}
"#, "#,
r#" r#"
fn foo<G, B: Bar>(bar: B) {} fn foo<G, B: Bar,>(bar: B) {}
"#, "#,
); );
} }
@ -78,7 +72,7 @@ fn replace_impl_trait_without_generic_params() {
fn foo(bar: <|>impl Bar) {} fn foo(bar: <|>impl Bar) {}
"#, "#,
r#" r#"
fn foo<B: Bar>(bar: B) {} fn foo<B: Bar,>(bar: B) {}
"#, "#,
); );
} }
@ -91,7 +85,7 @@ fn replace_two_impl_trait_with_generic_params() {
fn foo<G>(foo: impl Foo, bar: <|>impl Bar) {} fn foo<G>(foo: impl Foo, bar: <|>impl Bar) {}
"#, "#,
r#" r#"
fn foo<G, B: Bar>(foo: impl Foo, bar: B) {} fn foo<G, B: Bar,>(foo: impl Foo, bar: B) {}
"#, "#,
); );
} }
@ -104,7 +98,7 @@ fn replace_impl_trait_with_empty_generic_params() {
fn foo<>(bar: <|>impl Bar) {} fn foo<>(bar: <|>impl Bar) {}
"#, "#,
r#" r#"
fn foo<B: Bar>(bar: B) {} fn foo<B: Bar,>(bar: B) {}
"#, "#,
); );
} }
@ -133,7 +127,7 @@ fn replace_impl_trait_with_exist_generic_letter() {
fn foo<B>(bar: <|>impl Bar) {} fn foo<B>(bar: <|>impl Bar) {}
"#, "#,
r#" r#"
fn foo<B, C: Bar>(bar: C) {} fn foo<B, C: Bar,>(bar: C) {}
"#, "#,
); );
} }
@ -158,4 +152,17 @@ fn foo<
"#, "#,
); );
} }
#[test]
fn replace_impl_trait_multiple() {
check_assist(
replace_impl_trait_with_generic,
r#"
fn foo(bar: <|>impl Foo + Bar) {}
"#,
r#"
fn foo<F: Foo + Bar,>(bar: F) {}
"#,
);
}
} }

View File

@ -819,10 +819,10 @@ fn doctest_replace_impl_trait_with_generic() {
check_doc_test( check_doc_test(
"replace_impl_trait_with_generic", "replace_impl_trait_with_generic",
r#####" r#####"
fn foo<G>(bar: <|>impl Bar) {} fn foo(bar: <|>impl Bar) {}
"#####, "#####,
r#####" r#####"
fn foo<B: Bar>(bar: B) {} fn foo<B: Bar,>(bar: B) {}
"#####, "#####,
) )
} }

View File

@ -294,7 +294,7 @@ pub fn param_list(pats: impl IntoIterator<Item = ast::Param>) -> ast::ParamList
ast_from_text(&format!("fn f({}) {{ }}", args)) ast_from_text(&format!("fn f({}) {{ }}", args))
} }
pub fn generic_param(name: String, ty: Option<String>) -> ast::GenericParam { pub fn generic_param(name: String, ty: Option<ast::TypeBoundList>) -> ast::GenericParam {
let bound = match ty { let bound = match ty {
Some(it) => format!(": {}", it), Some(it) => format!(": {}", it),
None => String::new(), None => String::new(),