diff options
Diffstat (limited to 'test_utils/src/lib.rs')
| -rw-r--r-- | test_utils/src/lib.rs | 70 |
1 files changed, 69 insertions, 1 deletions
diff --git a/test_utils/src/lib.rs b/test_utils/src/lib.rs index 6ca0a43..6708c81 100644 --- a/test_utils/src/lib.rs +++ b/test_utils/src/lib.rs | |||
| @@ -336,6 +336,49 @@ impl CliTesterInputPrompt<'_> { | |||
| 336 | } | 336 | } |
| 337 | 337 | ||
| 338 | pub fn succeeds_with(&mut self, input: &str) -> Result<&mut Self> { | 338 | pub fn succeeds_with(&mut self, input: &str) -> Result<&mut Self> { |
| 339 | self.succeeds_with_optional_shortened_report(input, false) | ||
| 340 | } | ||
| 341 | |||
| 342 | pub fn succeeds_with_optional_shortened_report( | ||
| 343 | &mut self, | ||
| 344 | input: &str, | ||
| 345 | shorten_report_to_15_chars: bool, | ||
| 346 | ) -> Result<&mut Self> { | ||
| 347 | self.tester.send_line(input)?; | ||
| 348 | self.tester | ||
| 349 | .expect(input) | ||
| 350 | .context("expect input to be printed")?; | ||
| 351 | self.tester | ||
| 352 | .expect("\r") | ||
| 353 | .context("expect new line after input to be printed")?; | ||
| 354 | |||
| 355 | let mut s = String::new(); | ||
| 356 | let printed_input = if shorten_report_to_15_chars { | ||
| 357 | shorten_string(input) | ||
| 358 | } else { | ||
| 359 | input.to_string() | ||
| 360 | }; | ||
| 361 | self.tester | ||
| 362 | .formatter | ||
| 363 | .format_input_prompt_selection(&mut s, self.prompt.as_str(), &printed_input) | ||
| 364 | .expect("diagluer theme formatter should succeed"); | ||
| 365 | if !s.contains(self.prompt.as_str()) { | ||
| 366 | panic!("dialoguer must be broken as formatted prompt success doesnt contain prompt"); | ||
| 367 | } | ||
| 368 | let formatted_success = format!("{}\r\n", sanatize(s)); | ||
| 369 | |||
| 370 | self.tester | ||
| 371 | .expect(formatted_success.as_str()) | ||
| 372 | .context("expect immediate prompt success")?; | ||
| 373 | Ok(self) | ||
| 374 | } | ||
| 375 | |||
| 376 | pub fn fails_with_optional_shortened_report( | ||
| 377 | &mut self, | ||
| 378 | input: &str, | ||
| 379 | prefix: Option<&str>, | ||
| 380 | shorten_report_to_15_chars: bool, | ||
| 381 | ) -> Result<&mut Self> { | ||
| 339 | self.tester.send_line(input)?; | 382 | self.tester.send_line(input)?; |
| 340 | self.tester | 383 | self.tester |
| 341 | .expect(input) | 384 | .expect(input) |
| @@ -345,9 +388,26 @@ impl CliTesterInputPrompt<'_> { | |||
| 345 | .context("expect new line after input to be printed")?; | 388 | .context("expect new line after input to be printed")?; |
| 346 | 389 | ||
| 347 | let mut s = String::new(); | 390 | let mut s = String::new(); |
| 391 | let printed_input = if shorten_report_to_15_chars { | ||
| 392 | shorten_string(input) | ||
| 393 | } else { | ||
| 394 | input.to_string() | ||
| 395 | }; | ||
| 348 | self.tester | 396 | self.tester |
| 349 | .formatter | 397 | .formatter |
| 350 | .format_input_prompt_selection(&mut s, self.prompt.as_str(), input) | 398 | .format_error( |
| 399 | &mut s, | ||
| 400 | &format!( | ||
| 401 | "{}{}: {}", | ||
| 402 | prefix.unwrap_or_default(), | ||
| 403 | &self.prompt, | ||
| 404 | if input.is_empty() { | ||
| 405 | "empty".to_string() | ||
| 406 | } else { | ||
| 407 | format!("\"{printed_input}\"") | ||
| 408 | } | ||
| 409 | ), | ||
| 410 | ) | ||
| 351 | .expect("diagluer theme formatter should succeed"); | 411 | .expect("diagluer theme formatter should succeed"); |
| 352 | if !s.contains(self.prompt.as_str()) { | 412 | if !s.contains(self.prompt.as_str()) { |
| 353 | panic!("dialoguer must be broken as formatted prompt success doesnt contain prompt"); | 413 | panic!("dialoguer must be broken as formatted prompt success doesnt contain prompt"); |
| @@ -361,6 +421,14 @@ impl CliTesterInputPrompt<'_> { | |||
| 361 | } | 421 | } |
| 362 | } | 422 | } |
| 363 | 423 | ||
| 424 | fn shorten_string(s: &str) -> String { | ||
| 425 | if s.len() < 15 { | ||
| 426 | s.to_string() | ||
| 427 | } else { | ||
| 428 | format!("{}...", &s[..15]) | ||
| 429 | } | ||
| 430 | } | ||
| 431 | |||
| 364 | pub struct CliTesterPasswordPrompt<'a> { | 432 | pub struct CliTesterPasswordPrompt<'a> { |
| 365 | tester: &'a mut CliTester, | 433 | tester: &'a mut CliTester, |
| 366 | prompt: String, | 434 | prompt: String, |