diff options
Diffstat (limited to 'test_utils/src')
| -rw-r--r-- | test_utils/src/lib.rs | 85 |
1 files changed, 84 insertions, 1 deletions
diff --git a/test_utils/src/lib.rs b/test_utils/src/lib.rs index b4f0360..2edbc60 100644 --- a/test_utils/src/lib.rs +++ b/test_utils/src/lib.rs | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | use std::{ffi::OsStr, path::PathBuf}; | 1 | use std::{ffi::OsStr, path::PathBuf}; |
| 2 | 2 | ||
| 3 | use anyhow::{ensure, Context, Result}; | 3 | use anyhow::{bail, ensure, Context, Result}; |
| 4 | use dialoguer::theme::{ColorfulTheme, Theme}; | 4 | use dialoguer::theme::{ColorfulTheme, Theme}; |
| 5 | use directories::ProjectDirs; | 5 | use directories::ProjectDirs; |
| 6 | use nostr::{self, prelude::FromSkStr, Kind, Tag}; | 6 | use nostr::{self, prelude::FromSkStr, Kind, Tag}; |
| @@ -259,6 +259,20 @@ impl CliTester { | |||
| 259 | i.prompt(false).context("initial confirm prompt")?; | 259 | i.prompt(false).context("initial confirm prompt")?; |
| 260 | Ok(i) | 260 | Ok(i) |
| 261 | } | 261 | } |
| 262 | |||
| 263 | pub fn expect_multi_select( | ||
| 264 | &mut self, | ||
| 265 | prompt: &str, | ||
| 266 | choices: Vec<String>, | ||
| 267 | ) -> Result<CliTesterMultiSelectPrompt> { | ||
| 268 | let mut i = CliTesterMultiSelectPrompt { | ||
| 269 | tester: self, | ||
| 270 | prompt: prompt.to_string(), | ||
| 271 | choices, | ||
| 272 | }; | ||
| 273 | i.prompt(false).context("initial confirm prompt")?; | ||
| 274 | Ok(i) | ||
| 275 | } | ||
| 262 | } | 276 | } |
| 263 | 277 | ||
| 264 | pub struct CliTesterInputPrompt<'a> { | 278 | pub struct CliTesterInputPrompt<'a> { |
| @@ -448,6 +462,75 @@ impl CliTesterConfirmPrompt<'_> { | |||
| 448 | } | 462 | } |
| 449 | } | 463 | } |
| 450 | 464 | ||
| 465 | pub struct CliTesterMultiSelectPrompt<'a> { | ||
| 466 | tester: &'a mut CliTester, | ||
| 467 | prompt: String, | ||
| 468 | choices: Vec<String>, | ||
| 469 | } | ||
| 470 | |||
| 471 | impl CliTesterMultiSelectPrompt<'_> { | ||
| 472 | fn prompt(&mut self, eventually: bool) -> Result<&mut Self> { | ||
| 473 | if eventually { | ||
| 474 | self.tester | ||
| 475 | .expect_eventually(format!("{}:\r\n", self.prompt)) | ||
| 476 | .context("expect multi-select prompt eventually")?; | ||
| 477 | } else { | ||
| 478 | self.tester | ||
| 479 | .expect(format!("{}:\r\n", self.prompt)) | ||
| 480 | .context("expect multi-select prompt")?; | ||
| 481 | } | ||
| 482 | Ok(self) | ||
| 483 | } | ||
| 484 | |||
| 485 | pub fn succeeds_with( | ||
| 486 | &mut self, | ||
| 487 | chosen_indexes: Vec<usize>, | ||
| 488 | report: bool, | ||
| 489 | default_indexes: Vec<usize>, | ||
| 490 | ) -> Result<&mut Self> { | ||
| 491 | if report { | ||
| 492 | bail!("TODO: add support for report") | ||
| 493 | } | ||
| 494 | |||
| 495 | fn show_options( | ||
| 496 | tester: &mut CliTester, | ||
| 497 | choices: &[String], | ||
| 498 | active_index: usize, | ||
| 499 | selected_indexes: &[usize], | ||
| 500 | ) -> Result<()> { | ||
| 501 | for (index, item) in choices.iter().enumerate() { | ||
| 502 | tester.expect(format!( | ||
| 503 | "{}{}{}\r\n", | ||
| 504 | if active_index.eq(&index) { "> " } else { " " }, | ||
| 505 | if selected_indexes.iter().any(|i| i.eq(&index)) { | ||
| 506 | "[x] " | ||
| 507 | } else { | ||
| 508 | "[ ] " | ||
| 509 | }, | ||
| 510 | item, | ||
| 511 | ))?; | ||
| 512 | } | ||
| 513 | Ok(()) | ||
| 514 | } | ||
| 515 | |||
| 516 | show_options(self.tester, &self.choices, 0, &default_indexes)?; | ||
| 517 | |||
| 518 | if default_indexes.eq(&chosen_indexes) { | ||
| 519 | self.tester.send("\r\n")?; | ||
| 520 | } else { | ||
| 521 | bail!("TODO: add support changing options"); | ||
| 522 | } | ||
| 523 | |||
| 524 | for _ in self.choices.iter() { | ||
| 525 | self.tester.expect("\r")?; | ||
| 526 | } | ||
| 527 | // one for removing prompt maybe? | ||
| 528 | self.tester.expect("\r")?; | ||
| 529 | |||
| 530 | Ok(self) | ||
| 531 | } | ||
| 532 | } | ||
| 533 | |||
| 451 | pub struct CliTesterChoicePrompt<'a> { | 534 | pub struct CliTesterChoicePrompt<'a> { |
| 452 | tester: &'a mut CliTester, | 535 | tester: &'a mut CliTester, |
| 453 | prompt: String, | 536 | prompt: String, |