From a43bba760ef424f5c849666949b4323f090b6970 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 17 Jun 2021 11:41:36 +0300 Subject: internal: switch some tests to minicore --- .../src/handlers/convert_iter_for_each_to_for.rs | 121 +++++++-------------- crates/ide_db/src/helpers/famous_defs_fixture.rs | 70 ------------ 2 files changed, 37 insertions(+), 154 deletions(-) diff --git a/crates/ide_assists/src/handlers/convert_iter_for_each_to_for.rs b/crates/ide_assists/src/handlers/convert_iter_for_each_to_for.rs index 4e75a7b14..7fd73d4c7 100644 --- a/crates/ide_assists/src/handlers/convert_iter_for_each_to_for.rs +++ b/crates/ide_assists/src/handlers/convert_iter_for_each_to_for.rs @@ -77,9 +77,11 @@ fn validate_method_call_expr( expr: ast::MethodCallExpr, ) -> Option<(ast::Expr, ast::Expr)> { let name_ref = expr.name_ref()?; - if name_ref.syntax().text_range().intersect(ctx.frange.range).is_none() - || name_ref.text() != "for_each" - { + if name_ref.syntax().text_range().intersect(ctx.frange.range).is_none() { + cov_mark::hit!(test_for_each_not_applicable_invalid_cursor_pos); + return None; + } + if name_ref.text() != "for_each" { return None; } @@ -98,59 +100,27 @@ fn validate_method_call_expr( #[cfg(test)] mod tests { - use crate::tests::{self, check_assist}; + use crate::tests::{check_assist, check_assist_not_applicable}; use super::*; - const EMPTY_ITER_FIXTURE: &'static str = r" -//- /lib.rs deps:core crate:empty_iter -pub struct EmptyIter; -impl Iterator for EmptyIter { - type Item = usize; - fn next(&mut self) -> Option { None } -} -pub struct Empty; -impl Empty { - pub fn iter(&self) -> EmptyIter { EmptyIter } -} -"; - - fn check_assist_with_fixtures(before: &str, after: &str) { - let before = &format!( - "//- /main.rs crate:main deps:core,empty_iter{}{}{}", - before, - EMPTY_ITER_FIXTURE, - FamousDefs::FIXTURE, - ); - check_assist(convert_iter_for_each_to_for, before, after); - } - - fn check_assist_not_applicable(before: &str) { - let before = &format!( - "//- /main.rs crate:main deps:core,empty_iter{}{}{}", - before, - EMPTY_ITER_FIXTURE, - FamousDefs::FIXTURE, - ); - tests::check_assist_not_applicable(convert_iter_for_each_to_for, before); - } - #[test] fn test_for_each_in_method_stmt() { - check_assist_with_fixtures( + check_assist( + convert_iter_for_each_to_for, r#" -use empty_iter::*; +//- minicore: iterators fn main() { - let x = Empty; - x.iter().$0for_each(|(x, y)| { + let it = core::iter::repeat(92); + it.$0for_each(|(x, y)| { println!("x: {}, y: {}", x, y); }); -}"#, +} +"#, r#" -use empty_iter::*; fn main() { - let x = Empty; - for (x, y) in x.iter() { + let it = core::iter::repeat(92); + for (x, y) in it { println!("x: {}, y: {}", x, y); } } @@ -160,43 +130,21 @@ fn main() { #[test] fn test_for_each_in_method() { - check_assist_with_fixtures( + check_assist( + convert_iter_for_each_to_for, r#" -use empty_iter::*; +//- minicore: iterators fn main() { - let x = Empty; - x.iter().$0for_each(|(x, y)| { + let it = core::iter::repeat(92); + it.$0for_each(|(x, y)| { println!("x: {}, y: {}", x, y); }) -}"#, - r#" -use empty_iter::*; -fn main() { - let x = Empty; - for (x, y) in x.iter() { - println!("x: {}, y: {}", x, y); - } } "#, - ) - } - - #[test] - fn test_for_each_in_iter_stmt() { - check_assist_with_fixtures( - r#" -use empty_iter::*; -fn main() { - let x = Empty.iter(); - x.$0for_each(|(x, y)| { - println!("x: {}, y: {}", x, y); - }); -}"#, r#" -use empty_iter::*; fn main() { - let x = Empty.iter(); - for (x, y) in x { + let it = core::iter::repeat(92); + for (x, y) in it { println!("x: {}, y: {}", x, y); } } @@ -206,18 +154,19 @@ fn main() { #[test] fn test_for_each_without_braces_stmt() { - check_assist_with_fixtures( + check_assist( + convert_iter_for_each_to_for, r#" -use empty_iter::*; +//- minicore: iterators fn main() { - let x = Empty; - x.iter().$0for_each(|(x, y)| println!("x: {}, y: {}", x, y)); -}"#, + let it = core::iter::repeat(92); + it.$0for_each(|(x, y)| println!("x: {}, y: {}", x, y)); +} +"#, r#" -use empty_iter::*; fn main() { - let x = Empty; - for (x, y) in x.iter() { + let it = core::iter::repeat(92); + for (x, y) in it { println!("x: {}, y: {}", x, y) } } @@ -228,7 +177,9 @@ fn main() { #[test] fn test_for_each_not_applicable() { check_assist_not_applicable( + convert_iter_for_each_to_for, r#" +//- minicore: iterators fn main() { ().$0for_each(|x| println!("{}", x)); }"#, @@ -237,11 +188,13 @@ fn main() { #[test] fn test_for_each_not_applicable_invalid_cursor_pos() { + cov_mark::check!(test_for_each_not_applicable_invalid_cursor_pos); check_assist_not_applicable( + convert_iter_for_each_to_for, r#" -use empty_iter::*; +//- minicore: iterators fn main() { - Empty.iter().for_each(|(x, y)| $0println!("x: {}, y: {}", x, y)); + core::iter::repeat(92).for_each(|(x, y)| $0println!("x: {}, y: {}", x, y)); }"#, ) } diff --git a/crates/ide_db/src/helpers/famous_defs_fixture.rs b/crates/ide_db/src/helpers/famous_defs_fixture.rs index 312851966..5aa77a24d 100644 --- a/crates/ide_db/src/helpers/famous_defs_fixture.rs +++ b/crates/ide_db/src/helpers/famous_defs_fixture.rs @@ -26,76 +26,6 @@ pub mod default { } } -pub mod iter { - pub use self::traits::{collect::IntoIterator, iterator::Iterator}; - mod traits { - pub(crate) mod iterator { - use crate::option::Option; - pub trait Iterator { - type Item; - fn next(&mut self) -> Option; - fn by_ref(&mut self) -> &mut Self { - self - } - fn take(self, n: usize) -> crate::iter::Take { - crate::iter::Take { inner: self } - } - } - - impl Iterator for &mut I { - type Item = I::Item; - fn next(&mut self) -> Option { - (**self).next() - } - } - } - pub(crate) mod collect { - pub trait IntoIterator { - type Item; - } - } - } - - pub use self::sources::*; - pub(crate) mod sources { - use super::Iterator; - use crate::option::Option::{self, *}; - pub struct Repeat { - element: A, - } - - pub fn repeat(elt: T) -> Repeat { - Repeat { element: elt } - } - - impl Iterator for Repeat { - type Item = A; - - fn next(&mut self) -> Option { - None - } - } - } - - pub use self::adapters::*; - pub(crate) mod adapters { - use super::Iterator; - use crate::option::Option::{self, *}; - pub struct Take { - pub(crate) inner: I, - } - impl Iterator for Take - where - I: Iterator, - { - type Item = ::Item; - fn next(&mut self) -> Option<::Item> { - None - } - } - } -} - pub mod ops { #[lang = "fn"] pub trait Fn: FnMut { -- cgit v1.2.3