From 6c21d04307edf130851aefad406bacce9edbde23 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 14 May 2021 18:53:53 +0300 Subject: internal: use standard style for tests --- crates/ide_assists/src/handlers/generate_new.rs | 153 +++++++++++++++--------- 1 file changed, 99 insertions(+), 54 deletions(-) (limited to 'crates') diff --git a/crates/ide_assists/src/handlers/generate_new.rs b/crates/ide_assists/src/handlers/generate_new.rs index 8ce5930b7..959a1f86c 100644 --- a/crates/ide_assists/src/handlers/generate_new.rs +++ b/crates/ide_assists/src/handlers/generate_new.rs @@ -1,4 +1,3 @@ -use ast::Adt; use itertools::Itertools; use stdx::format_to; use syntax::ast::{self, AstNode, NameOwner, StructKind, VisibilityOwner}; @@ -37,7 +36,7 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()> }; // Return early if we've found an existing new fn - let impl_def = find_struct_impl(&ctx, &Adt::Struct(strukt.clone()), "new")?; + let impl_def = find_struct_impl(&ctx, &ast::Adt::Struct(strukt.clone()), "new")?; let target = strukt.syntax().text_range(); acc.add(AssistId("generate_new", AssistKind::Generate), "Generate `new`", target, |builder| { @@ -60,7 +59,7 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()> let start_offset = impl_def .and_then(|impl_def| find_impl_block_start(impl_def, &mut buf)) .unwrap_or_else(|| { - buf = generate_impl_text(&Adt::Struct(strukt.clone()), &buf); + buf = generate_impl_text(&ast::Adt::Struct(strukt.clone()), &buf); strukt.syntax().text_range().end() }); @@ -81,101 +80,132 @@ mod tests { use super::*; #[test] - #[rustfmt::skip] fn test_generate_new() { - // Check output of generation check_assist( generate_new, -"struct Foo {$0}", -"struct Foo {} + r#" +struct Foo {$0} +"#, + r#" +struct Foo {} impl Foo { fn $0new() -> Self { Self { } } -}", +} +"#, ); check_assist( generate_new, -"struct Foo {$0}", -"struct Foo {} + r#" +struct Foo {$0} +"#, + r#" +struct Foo {} impl Foo { fn $0new() -> Self { Self { } } -}", +} +"#, ); check_assist( generate_new, -"struct Foo<'a, T: Foo<'a>> {$0}", -"struct Foo<'a, T: Foo<'a>> {} + r#" +struct Foo<'a, T: Foo<'a>> {$0} +"#, + r#" +struct Foo<'a, T: Foo<'a>> {} impl<'a, T: Foo<'a>> Foo<'a, T> { fn $0new() -> Self { Self { } } -}", +} +"#, ); check_assist( generate_new, -"struct Foo { baz: String $0}", -"struct Foo { baz: String } + r#" +struct Foo { baz: String $0} +"#, + r#" +struct Foo { baz: String } impl Foo { fn $0new(baz: String) -> Self { Self { baz } } -}", +} +"#, ); check_assist( generate_new, -"struct Foo { baz: String, qux: Vec $0}", -"struct Foo { baz: String, qux: Vec } + r#" +struct Foo { baz: String, qux: Vec $0} +"#, + r#" +struct Foo { baz: String, qux: Vec } impl Foo { fn $0new(baz: String, qux: Vec) -> Self { Self { baz, qux } } -}", +} +"#, ); + } - // Check that visibility modifiers don't get brought in for fields + #[test] + fn check_that_visibility_modifiers_dont_get_brought_in() { check_assist( generate_new, -"struct Foo { pub baz: String, pub qux: Vec $0}", -"struct Foo { pub baz: String, pub qux: Vec } + r#" +struct Foo { pub baz: String, pub qux: Vec $0} +"#, + r#" +struct Foo { pub baz: String, pub qux: Vec } impl Foo { fn $0new(baz: String, qux: Vec) -> Self { Self { baz, qux } } -}", +} +"#, ); + } - // Check that it reuses existing impls + #[test] + fn check_it_reuses_existing_impls() { check_assist( generate_new, -"struct Foo {$0} + r#" +struct Foo {$0} impl Foo {} -", -"struct Foo {} +"#, + r#" +struct Foo {} impl Foo { fn $0new() -> Self { Self { } } } -", +"#, ); check_assist( generate_new, -"struct Foo {$0} + r#" +struct Foo {$0} impl Foo { fn qux(&self) {} } -", -"struct Foo {} +"#, + r#" +struct Foo {} impl Foo { fn $0new() -> Self { Self { } } fn qux(&self) {} } -", +"#, ); check_assist( generate_new, -"struct Foo {$0} + r#" +struct Foo {$0} impl Foo { fn qux(&self) {} @@ -183,8 +213,9 @@ impl Foo { 5 } } -", -"struct Foo {} +"#, + r#" +struct Foo {} impl Foo { fn $0new() -> Self { Self { } } @@ -194,27 +225,37 @@ impl Foo { 5 } } -", +"#, ); + } - // Check visibility of new fn based on struct + #[test] + fn check_visibility_of_new_fn_based_on_struct() { check_assist( generate_new, -"pub struct Foo {$0}", -"pub struct Foo {} + r#" +pub struct Foo {$0} +"#, + r#" +pub struct Foo {} impl Foo { pub fn $0new() -> Self { Self { } } -}", +} +"#, ); check_assist( generate_new, -"pub(crate) struct Foo {$0}", -"pub(crate) struct Foo {} + r#" +pub(crate) struct Foo {$0} +"#, + r#" +pub(crate) struct Foo {} impl Foo { pub(crate) fn $0new() -> Self { Self { } } -}", +} +"#, ); } @@ -222,26 +263,28 @@ impl Foo { fn generate_new_not_applicable_if_fn_exists() { check_assist_not_applicable( generate_new, - " + r#" struct Foo {$0} impl Foo { fn new() -> Self { Self } -}", +} +"#, ); check_assist_not_applicable( generate_new, - " + r#" struct Foo {$0} impl Foo { fn New() -> Self { Self } -}", +} +"#, ); } @@ -249,12 +292,12 @@ impl Foo { fn generate_new_target() { check_assist_target( generate_new, - " + r#" struct SomeThingIrrelevant; /// Has a lifetime parameter struct Foo<'a, T: Foo<'a>> {$0} struct EvenMoreIrrelevant; -", +"#, "/// Has a lifetime parameter struct Foo<'a, T: Foo<'a>> {}", ); @@ -264,7 +307,7 @@ struct Foo<'a, T: Foo<'a>> {}", fn test_unrelated_new() { check_assist( generate_new, - r##" + r#" pub struct AstId { file_id: HirFileId, file_ast_id: FileAstId, @@ -285,8 +328,9 @@ impl Source { pub fn map U, U>(self, f: F) -> Source { Source { file_id: self.file_id, ast: f(self.ast) } } -}"##, - r##" +} +"#, + r#" pub struct AstId { file_id: HirFileId, file_ast_id: FileAstId, @@ -309,7 +353,8 @@ impl Source { pub fn map U, U>(self, f: F) -> Source { Source { file_id: self.file_id, ast: f(self.ast) } } -}"##, +} +"#, ); } } -- cgit v1.2.3