internal: use standard style for tests

This commit is contained in:
Aleksey Kladov 2021-05-14 18:53:53 +03:00
parent cea589b3b5
commit 6c21d04307
2 changed files with 100 additions and 55 deletions

View file

@ -9,7 +9,7 @@ incremental = false
# Disabling debug info speeds up builds a bunch, # Disabling debug info speeds up builds a bunch,
# and we don't rely on it for debugging that much. # and we don't rely on it for debugging that much.
debug = 1 debug = 0
[profile.dev.package] [profile.dev.package]
# These speed up local tests. # These speed up local tests.

View file

@ -1,4 +1,3 @@
use ast::Adt;
use itertools::Itertools; use itertools::Itertools;
use stdx::format_to; use stdx::format_to;
use syntax::ast::{self, AstNode, NameOwner, StructKind, VisibilityOwner}; 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 // 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(); let target = strukt.syntax().text_range();
acc.add(AssistId("generate_new", AssistKind::Generate), "Generate `new`", target, |builder| { 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 let start_offset = impl_def
.and_then(|impl_def| find_impl_block_start(impl_def, &mut buf)) .and_then(|impl_def| find_impl_block_start(impl_def, &mut buf))
.unwrap_or_else(|| { .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() strukt.syntax().text_range().end()
}); });
@ -81,101 +80,132 @@ mod tests {
use super::*; use super::*;
#[test] #[test]
#[rustfmt::skip]
fn test_generate_new() { fn test_generate_new() {
// Check output of generation
check_assist( check_assist(
generate_new, generate_new,
"struct Foo {$0}", r#"
"struct Foo {} struct Foo {$0}
"#,
r#"
struct Foo {}
impl Foo { impl Foo {
fn $0new() -> Self { Self { } } fn $0new() -> Self { Self { } }
}", }
"#,
); );
check_assist( check_assist(
generate_new, generate_new,
"struct Foo<T: Clone> {$0}", r#"
"struct Foo<T: Clone> {} struct Foo<T: Clone> {$0}
"#,
r#"
struct Foo<T: Clone> {}
impl<T: Clone> Foo<T> { impl<T: Clone> Foo<T> {
fn $0new() -> Self { Self { } } fn $0new() -> Self { Self { } }
}", }
"#,
); );
check_assist( check_assist(
generate_new, generate_new,
"struct Foo<'a, T: Foo<'a>> {$0}", r#"
"struct Foo<'a, T: Foo<'a>> {} struct Foo<'a, T: Foo<'a>> {$0}
"#,
r#"
struct Foo<'a, T: Foo<'a>> {}
impl<'a, T: Foo<'a>> Foo<'a, T> { impl<'a, T: Foo<'a>> Foo<'a, T> {
fn $0new() -> Self { Self { } } fn $0new() -> Self { Self { } }
}", }
"#,
); );
check_assist( check_assist(
generate_new, generate_new,
"struct Foo { baz: String $0}", r#"
"struct Foo { baz: String } struct Foo { baz: String $0}
"#,
r#"
struct Foo { baz: String }
impl Foo { impl Foo {
fn $0new(baz: String) -> Self { Self { baz } } fn $0new(baz: String) -> Self { Self { baz } }
}", }
"#,
); );
check_assist( check_assist(
generate_new, generate_new,
"struct Foo { baz: String, qux: Vec<i32> $0}", r#"
"struct Foo { baz: String, qux: Vec<i32> } struct Foo { baz: String, qux: Vec<i32> $0}
"#,
r#"
struct Foo { baz: String, qux: Vec<i32> }
impl Foo { impl Foo {
fn $0new(baz: String, qux: Vec<i32>) -> Self { Self { baz, qux } } fn $0new(baz: String, qux: Vec<i32>) -> 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( check_assist(
generate_new, generate_new,
"struct Foo { pub baz: String, pub qux: Vec<i32> $0}", r#"
"struct Foo { pub baz: String, pub qux: Vec<i32> } struct Foo { pub baz: String, pub qux: Vec<i32> $0}
"#,
r#"
struct Foo { pub baz: String, pub qux: Vec<i32> }
impl Foo { impl Foo {
fn $0new(baz: String, qux: Vec<i32>) -> Self { Self { baz, qux } } fn $0new(baz: String, qux: Vec<i32>) -> Self { Self { baz, qux } }
}", }
"#,
); );
}
// Check that it reuses existing impls #[test]
fn check_it_reuses_existing_impls() {
check_assist( check_assist(
generate_new, generate_new,
"struct Foo {$0} r#"
struct Foo {$0}
impl Foo {} impl Foo {}
", "#,
"struct Foo {} r#"
struct Foo {}
impl Foo { impl Foo {
fn $0new() -> Self { Self { } } fn $0new() -> Self { Self { } }
} }
", "#,
); );
check_assist( check_assist(
generate_new, generate_new,
"struct Foo {$0} r#"
struct Foo {$0}
impl Foo { impl Foo {
fn qux(&self) {} fn qux(&self) {}
} }
", "#,
"struct Foo {} r#"
struct Foo {}
impl Foo { impl Foo {
fn $0new() -> Self { Self { } } fn $0new() -> Self { Self { } }
fn qux(&self) {} fn qux(&self) {}
} }
", "#,
); );
check_assist( check_assist(
generate_new, generate_new,
"struct Foo {$0} r#"
struct Foo {$0}
impl Foo { impl Foo {
fn qux(&self) {} fn qux(&self) {}
@ -183,8 +213,9 @@ impl Foo {
5 5
} }
} }
", "#,
"struct Foo {} r#"
struct Foo {}
impl Foo { impl Foo {
fn $0new() -> Self { Self { } } fn $0new() -> Self { Self { } }
@ -194,27 +225,37 @@ impl Foo {
5 5
} }
} }
", "#,
); );
}
// Check visibility of new fn based on struct #[test]
fn check_visibility_of_new_fn_based_on_struct() {
check_assist( check_assist(
generate_new, generate_new,
"pub struct Foo {$0}", r#"
"pub struct Foo {} pub struct Foo {$0}
"#,
r#"
pub struct Foo {}
impl Foo { impl Foo {
pub fn $0new() -> Self { Self { } } pub fn $0new() -> Self { Self { } }
}", }
"#,
); );
check_assist( check_assist(
generate_new, generate_new,
"pub(crate) struct Foo {$0}", r#"
"pub(crate) struct Foo {} pub(crate) struct Foo {$0}
"#,
r#"
pub(crate) struct Foo {}
impl Foo { impl Foo {
pub(crate) fn $0new() -> Self { Self { } } pub(crate) fn $0new() -> Self { Self { } }
}", }
"#,
); );
} }
@ -222,26 +263,28 @@ impl Foo {
fn generate_new_not_applicable_if_fn_exists() { fn generate_new_not_applicable_if_fn_exists() {
check_assist_not_applicable( check_assist_not_applicable(
generate_new, generate_new,
" r#"
struct Foo {$0} struct Foo {$0}
impl Foo { impl Foo {
fn new() -> Self { fn new() -> Self {
Self Self
} }
}", }
"#,
); );
check_assist_not_applicable( check_assist_not_applicable(
generate_new, generate_new,
" r#"
struct Foo {$0} struct Foo {$0}
impl Foo { impl Foo {
fn New() -> Self { fn New() -> Self {
Self Self
} }
}", }
"#,
); );
} }
@ -249,12 +292,12 @@ impl Foo {
fn generate_new_target() { fn generate_new_target() {
check_assist_target( check_assist_target(
generate_new, generate_new,
" r#"
struct SomeThingIrrelevant; struct SomeThingIrrelevant;
/// Has a lifetime parameter /// Has a lifetime parameter
struct Foo<'a, T: Foo<'a>> {$0} struct Foo<'a, T: Foo<'a>> {$0}
struct EvenMoreIrrelevant; struct EvenMoreIrrelevant;
", "#,
"/// Has a lifetime parameter "/// Has a lifetime parameter
struct Foo<'a, T: Foo<'a>> {}", struct Foo<'a, T: Foo<'a>> {}",
); );
@ -264,7 +307,7 @@ struct Foo<'a, T: Foo<'a>> {}",
fn test_unrelated_new() { fn test_unrelated_new() {
check_assist( check_assist(
generate_new, generate_new,
r##" r#"
pub struct AstId<N: AstNode> { pub struct AstId<N: AstNode> {
file_id: HirFileId, file_id: HirFileId,
file_ast_id: FileAstId<N>, file_ast_id: FileAstId<N>,
@ -285,8 +328,9 @@ impl<T> Source<T> {
pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> { pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> {
Source { file_id: self.file_id, ast: f(self.ast) } Source { file_id: self.file_id, ast: f(self.ast) }
} }
}"##, }
r##" "#,
r#"
pub struct AstId<N: AstNode> { pub struct AstId<N: AstNode> {
file_id: HirFileId, file_id: HirFileId,
file_ast_id: FileAstId<N>, file_ast_id: FileAstId<N>,
@ -309,7 +353,8 @@ impl<T> Source<T> {
pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> { pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> {
Source { file_id: self.file_id, ast: f(self.ast) } Source { file_id: self.file_id, ast: f(self.ast) }
} }
}"##, }
"#,
); );
} }
} }