aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Aleksanov <[email protected]>2020-10-02 12:51:20 +0100
committerIgor Aleksanov <[email protected]>2020-10-02 12:51:20 +0100
commit97f2905dec269891eb81a75cf0d639408a3f7268 (patch)
tree4902d4e9735bea5dba8fc3c222ab240d33e6db0b
parent76d0546ac7789ce94d15d305b07269d8b5e10038 (diff)
Use expect_test to make format_str_parser test more data-driven
-rw-r--r--crates/ide/src/completion/complete_postfix/format_like.rs70
1 files changed, 35 insertions, 35 deletions
diff --git a/crates/ide/src/completion/complete_postfix/format_like.rs b/crates/ide/src/completion/complete_postfix/format_like.rs
index b663ba171..0287fc803 100644
--- a/crates/ide/src/completion/complete_postfix/format_like.rs
+++ b/crates/ide/src/completion/complete_postfix/format_like.rs
@@ -204,54 +204,54 @@ impl FormatStrParser {
204#[cfg(test)] 204#[cfg(test)]
205mod tests { 205mod tests {
206 use super::*; 206 use super::*;
207 use expect_test::{expect, Expect};
208
209 fn check(input: &str, expect: &Expect) {
210 let mut parser = FormatStrParser::new((*input).to_owned());
211 let outcome_repr = if parser.parse().is_ok() {
212 // Parsing should be OK, expected repr is "string; expr_1, expr_2".
213 if parser.extracted_expressions.is_empty() {
214 parser.output
215 } else {
216 format!("{}; {}", parser.output, parser.extracted_expressions.join(", "))
217 }
218 } else {
219 // Parsing should fail, expected repr is "-".
220 "-".to_owned()
221 };
222
223 expect.assert_eq(&outcome_repr);
224 }
207 225
208 #[test] 226 #[test]
209 fn format_str_parser() { 227 fn format_str_parser() {
210 let test_vector = &[ 228 let test_vector = &[
211 ("no expressions", Some(("no expressions", vec![]))), 229 ("no expressions", expect![["no expressions"]]),
212 ("{expr} is {2 + 2}", Some(("{} is {}", vec!["expr", "2 + 2"]))), 230 ("{expr} is {2 + 2}", expect![["{} is {}; expr, 2 + 2"]]),
213 ("{expr:?}", Some(("{:?}", vec!["expr"]))), 231 ("{expr:?}", expect![["{:?}; expr"]]),
214 ("{malformed", None), 232 ("{malformed", expect![["-"]]),
215 ("malformed}", None), 233 ("malformed}", expect![["-"]]),
216 ("{{correct", Some(("{{correct", vec![]))), 234 ("{{correct", expect![["{{correct"]]),
217 ("correct}}", Some(("correct}}", vec![]))), 235 ("correct}}", expect![["correct}}"]]),
218 ("{correct}}}", Some(("{}}}", vec!["correct"]))), 236 ("{correct}}}", expect![["{}}}; correct"]]),
219 ("{correct}}}}}", Some(("{}}}}}", vec!["correct"]))), 237 ("{correct}}}}}", expect![["{}}}}}; correct"]]),
220 ("{incorrect}}", None), 238 ("{incorrect}}", expect![["-"]]),
221 ("placeholders {} {}", Some(("placeholders {} {}", vec!["$1", "$2"]))), 239 ("placeholders {} {}", expect![["placeholders {} {}; $1, $2"]]),
222 ("mixed {} {2 + 2} {}", Some(("mixed {} {} {}", vec!["$1", "2 + 2", "$2"]))), 240 ("mixed {} {2 + 2} {}", expect![["mixed {} {} {}; $1, 2 + 2, $2"]]),
223 ( 241 (
224 "{SomeStruct { val_a: 0, val_b: 1 }}", 242 "{SomeStruct { val_a: 0, val_b: 1 }}",
225 Some(("{}", vec!["SomeStruct { val_a: 0, val_b: 1 }"])), 243 expect![["{}; SomeStruct { val_a: 0, val_b: 1 }"]],
226 ), 244 ),
227 ("{expr:?} is {2.32f64:.5}", Some(("{:?} is {:.5}", vec!["expr", "2.32f64"]))), 245 ("{expr:?} is {2.32f64:.5}", expect![["{:?} is {:.5}; expr, 2.32f64"]]),
228 ( 246 (
229 "{SomeStruct { val_a: 0, val_b: 1 }:?}", 247 "{SomeStruct { val_a: 0, val_b: 1 }:?}",
230 Some(("{:?}", vec!["SomeStruct { val_a: 0, val_b: 1 }"])), 248 expect![["{:?}; SomeStruct { val_a: 0, val_b: 1 }"]],
231 ), 249 ),
232 ("{ 2 + 2 }", Some(("{}", vec!["2 + 2"]))), 250 ("{ 2 + 2 }", expect![["{}; 2 + 2"]]),
233 ]; 251 ];
234 252
235 for (input, output) in test_vector { 253 for (input, output) in test_vector {
236 let mut parser = FormatStrParser::new((*input).to_owned()); 254 check(input, output)
237 let outcome = parser.parse();
238
239 if let Some((result_str, result_args)) = output {
240 assert!(
241 outcome.is_ok(),
242 "Outcome is error for input: {}, but the expected outcome is {:?}",
243 input,
244 output
245 );
246 assert_eq!(parser.output, *result_str);
247 assert_eq!(&parser.extracted_expressions, result_args);
248 } else {
249 assert!(
250 outcome.is_err(),
251 "Outcome is OK for input: {}, but the expected outcome is error",
252 input
253 );
254 }
255 } 255 }
256 } 256 }
257 257