mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
internal: use standard style for tests
This commit is contained in:
parent
cea589b3b5
commit
6c21d04307
2 changed files with 100 additions and 55 deletions
|
@ -9,7 +9,7 @@ incremental = false
|
|||
|
||||
# Disabling debug info speeds up builds a bunch,
|
||||
# and we don't rely on it for debugging that much.
|
||||
debug = 1
|
||||
debug = 0
|
||||
|
||||
[profile.dev.package]
|
||||
# These speed up local tests.
|
||||
|
|
|
@ -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<T: Clone> {$0}",
|
||||
"struct Foo<T: Clone> {}
|
||||
r#"
|
||||
struct Foo<T: Clone> {$0}
|
||||
"#,
|
||||
r#"
|
||||
struct Foo<T: Clone> {}
|
||||
|
||||
impl<T: Clone> Foo<T> {
|
||||
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<i32> $0}",
|
||||
"struct Foo { baz: String, qux: Vec<i32> }
|
||||
r#"
|
||||
struct Foo { baz: String, qux: Vec<i32> $0}
|
||||
"#,
|
||||
r#"
|
||||
struct Foo { baz: String, qux: Vec<i32> }
|
||||
|
||||
impl Foo {
|
||||
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(
|
||||
generate_new,
|
||||
"struct Foo { pub baz: String, pub qux: Vec<i32> $0}",
|
||||
"struct Foo { pub baz: String, pub qux: Vec<i32> }
|
||||
r#"
|
||||
struct Foo { pub baz: String, pub qux: Vec<i32> $0}
|
||||
"#,
|
||||
r#"
|
||||
struct Foo { pub baz: String, pub qux: Vec<i32> }
|
||||
|
||||
impl Foo {
|
||||
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(
|
||||
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<N: AstNode> {
|
||||
file_id: HirFileId,
|
||||
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> {
|
||||
Source { file_id: self.file_id, ast: f(self.ast) }
|
||||
}
|
||||
}"##,
|
||||
r##"
|
||||
}
|
||||
"#,
|
||||
r#"
|
||||
pub struct AstId<N: AstNode> {
|
||||
file_id: HirFileId,
|
||||
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> {
|
||||
Source { file_id: self.file_id, ast: f(self.ast) }
|
||||
}
|
||||
}"##,
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue