Skip to content

Commit b7f9771

Browse files
committed
fix: tests should work for convert_iter_for_each_to_for
1 parent 87dc9d1 commit b7f9771

File tree

1 file changed

+44
-11
lines changed

1 file changed

+44
-11
lines changed

crates/ide_assists/src/handlers/convert_iter_for_each_to_for.rs

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,28 @@ fn validate_method_call_expr(
8888

8989
#[cfg(test)]
9090
mod tests {
91-
use crate::tests::check_assist;
91+
use crate::tests::{check_assist, check_assist_not_applicable};
9292

9393
use super::*;
9494

95+
const EMPTY_ITER_FIXTURE: &'static str = r"
96+
//- /lib.rs deps:core crate:empty_iter
97+
pub struct EmptyIter;
98+
impl Iterator for EmptyIter {
99+
type Item = usize;
100+
fn next(&mut self) -> Option<Self::Item> { None }
101+
}
102+
pub struct Empty;
103+
impl Empty {
104+
pub fn iter(&self) -> EmptyIter { EmptyIter }
105+
}
106+
";
107+
95108
fn check_assist_with_fixtures(before: &str, after: &str) {
96109
let before = &format!(
97-
"//- /main.rs crate:main deps:core,empty_iter{}{}",
110+
"//- /main.rs crate:main deps:core,empty_iter{}{}{}",
98111
before,
112+
EMPTY_ITER_FIXTURE,
99113
FamousDefs::FIXTURE,
100114
);
101115
check_assist(convert_iter_for_each_to_for, before, after);
@@ -105,55 +119,74 @@ mod tests {
105119
fn test_for_each_in_method() {
106120
check_assist_with_fixtures(
107121
r#"
122+
use empty_iter::*;
108123
fn main() {
109-
let x = vec![(1, 1), (2, 2), (3, 3), (4, 4)];
124+
let x = Empty;
110125
x.iter().$0for_each(|(x, y)| {
111126
println!("x: {}, y: {}", x, y);
112127
});
113128
}"#,
114129
r#"
130+
use empty_iter::*;
115131
fn main() {
116-
let x = vec![(1, 1), (2, 2), (3, 3), (4, 4)];
132+
let x = Empty;
117133
for (x, y) in x.iter() {
118134
println!("x: {}, y: {}", x, y);
119135
};
120-
}"#,
136+
}
137+
"#,
121138
)
122139
}
123140

124141
#[test]
125142
fn test_for_each_without_braces() {
126143
check_assist_with_fixtures(
127144
r#"
145+
use empty_iter::*;
128146
fn main() {
129-
let x = vec![(1, 1), (2, 2), (3, 3), (4, 4)];
147+
let x = Empty;
130148
x.iter().$0for_each(|(x, y)| println!("x: {}, y: {}", x, y));
131149
}"#,
132150
r#"
151+
use empty_iter::*;
133152
fn main() {
134-
let x = vec![(1, 1), (2, 2), (3, 3), (4, 4)];
153+
let x = Empty;
135154
for (x, y) in x.iter() {
136155
println!("x: {}, y: {}", x, y)
137156
};
138-
}"#,
157+
}
158+
"#,
139159
)
140160
}
141161

142162
#[test]
143163
fn test_for_each_in_closure() {
144164
check_assist_with_fixtures(
145165
r#"
166+
use empty_iter::*;
146167
fn main() {
147-
let x = vec![(1, 1), (2, 2), (3, 3), (4, 4)];
168+
let x = Empty;
148169
x.iter().for_each($0|(x, y)| println!("x: {}, y: {}", x, y));
149170
}"#,
150171
r#"
172+
use empty_iter::*;
151173
fn main() {
152-
let x = vec![(1, 1), (2, 2), (3, 3), (4, 4)];
174+
let x = Empty;
153175
for (x, y) in x.iter() {
154176
println!("x: {}, y: {}", x, y)
155177
};
156-
}"#,
178+
}
179+
"#,
157180
)
158181
}
182+
183+
#[test]
184+
fn test_for_each_not_applicable() {
185+
check_assist_not_applicable(
186+
convert_iter_for_each_to_for,
187+
r#"
188+
fn main() {
189+
value.$0for_each(|x| println!("{}", x));
190+
}"#)
191+
}
159192
}

0 commit comments

Comments
 (0)