Skip to content

Commit

Permalink
feat: add tests to verify initial rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
roele committed Jan 23, 2024
1 parent 12ee914 commit c57e6ab
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 8 deletions.
11 changes: 10 additions & 1 deletion .mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,14 @@ done
description = "Run tests"
run = [
"cargo clippy -- -D warnings",
"cargo fmt -- --check"
"cargo fmt -- --check",
"cargo test --lib --tests"
]

[tasks.coverage]
description = "Run tests with coverage"
run = [
"cargo clippy -- -D warnings",
"cargo fmt -- --check",
"cargo tarpaulin --lib --tests"
]
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ keywords = ["cli", "prompt", "console"]
console = "0.15.8"
once_cell = "1.19.0"
termcolor = "1.4.1"

[dev-dependencies]
ctor = "0.2.6"
indoc = "2.0.4"
1 change: 1 addition & 0 deletions examples/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use demand::Confirm;

fn main() {
let ms = Confirm::new("Are you sure?")
.description("This will do a thing.")
.affirmative("Yes!")
.negative("No.");
let yes = ms.run().expect("error running confirm");
Expand Down
31 changes: 29 additions & 2 deletions src/confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use crate::theme::Theme;
/// ```rust
/// use demand::Confirm;
///
/// let ms = Confirm::new("Are you sure?")
/// let confirm = Confirm::new("Are you sure?")
/// .affirmative("Yes!")
/// .negative("No.");
/// let yes = ms.run().expect("error running confirm");
/// let yes = confirm.run().expect("error running confirm");
/// println!("yes: {}", yes);
/// ```
pub struct Confirm<'a> {
Expand Down Expand Up @@ -201,3 +201,30 @@ impl<'a> Confirm<'a> {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::test::without_ansi;
use indoc::indoc;

#[test]
fn test_render() {
let confirm = Confirm::new("Are you sure?")
.description("This will do a thing.")
.affirmative("Yes!")
.negative("No.");

assert_eq!(
indoc! {
" Are you sure? This will do a thing.
Yes! No.
←/→ toggle • y/n/enter submit"
},
without_ansi(confirm.render().unwrap().as_str())
);
}
}
61 changes: 61 additions & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{theme, Theme};
/// .description("We'll use this to personalize your experience.")
/// .placeholder("Enter your name");
/// let name = input.run().expect("error running input");
/// ````
pub struct Input<'a> {
/// The title of the input
pub title: String,
Expand Down Expand Up @@ -257,3 +258,63 @@ impl<'a> Input<'a> {
Ok(())
}
}

#[cfg(test)]
mod tests {
use crate::test::without_ansi;

use super::*;

#[test]
fn test_render_title() {
let mut input = Input::new("Title");

assert_eq!(
" Title\n > ",
without_ansi(input.render().unwrap().as_str())
);
}

#[test]
fn test_render_description() {
let mut input = Input::new("Title").description("Description");

assert_eq!(
" Title\n Description\n > ",
without_ansi(input.render().unwrap().as_str())
);
}

#[test]
fn test_render_prompt() {
let mut input = Input::new("Title").prompt("$ ");

assert_eq!(
" Title\n $ ",
without_ansi(input.render().unwrap().as_str())
);
}

#[test]
fn test_render_placeholder() {
let mut input = Input::new("Title").placeholder("Placeholder");

assert_eq!(
" Title\n > Placeholder",
without_ansi(input.render().unwrap().as_str())
);
}

#[test]
fn test_render_all() {
let mut input = Input::new("Title")
.description("Description")
.prompt("$ ")
.placeholder("Placeholder");

assert_eq!(
" Title\n Description\n $ Placeholder",
without_ansi(input.render().unwrap().as_str())
);
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ mod option;
mod select;
mod spinner;
mod theme;

#[cfg(test)]
mod test;
37 changes: 34 additions & 3 deletions src/multiselect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{theme, DemandOption};
/// ```rust
/// use demand::{DemandOption, MultiSelect};
///
/// let ms = MultiSelect::new("Toppings")
/// let multiselect = MultiSelect::new("Toppings")
/// .description("Select your toppings")
/// .min(1)
/// .max(4)
Expand All @@ -26,8 +26,8 @@ use crate::{theme, DemandOption};
/// .option(DemandOption::new("Jalapenos").label("Jalapeños"))
/// .option(DemandOption::new("Cheese"))
/// .option(DemandOption::new("Vegan Cheese"))
/// .option(DemandOption::new("Nutella"));///
/// let toppings = ms.run().expect("error running multi select");
/// .option(DemandOption::new("Nutella"));
/// let toppings = multiselect.run().expect("error running multi select");
/// ```
pub struct MultiSelect<'a, T: Display> {
/// The title of the selector
Expand Down Expand Up @@ -422,3 +422,34 @@ impl<'a, T: Display> MultiSelect<'a, T> {
Ok(())
}
}

#[cfg(test)]
mod tests {
use crate::test::without_ansi;

use super::*;
use indoc::indoc;

#[test]
fn test_render() {
let select = MultiSelect::new("Toppings")
.description("Select your toppings")
.option(DemandOption::new("Lettuce").selected(true))
.option(DemandOption::new("Tomatoes").selected(true))
.option(DemandOption::new("Charm Sauce"))
.option(DemandOption::new("Jalapenos").label("Jalapeños"))
.option(DemandOption::new("Cheese"))
.option(DemandOption::new("Vegan Cheese"))
.option(DemandOption::new("Nutella"));

assert_eq!(
indoc! {
" Toppings
Select your toppings
↑/↓/k/j up/down • x/space toggle • a toggle all • enter confirm"
},
without_ansi(select.render().unwrap().as_str())
);
}
}
33 changes: 31 additions & 2 deletions src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{theme, DemandOption};
/// ```rust
/// use demand::{DemandOption, Select};
///
/// let ms = Select::new("Toppings")
/// let select = Select::new("Toppings")
/// .description("Select your topping")
/// .filterable(true)
/// .option(DemandOption::new("Lettuce"))
Expand All @@ -24,7 +24,7 @@ use crate::{theme, DemandOption};
/// .option(DemandOption::new("Cheese"))
/// .option(DemandOption::new("Vegan Cheese"))
/// .option(DemandOption::new("Nutella"));
/// let topping = ms.run().expect("error running multi select");
/// let topping = select.run().expect("error running multi select");
/// ```
pub struct Select<'a, T: Display> {
/// The title of the selector
Expand Down Expand Up @@ -316,3 +316,32 @@ impl<'a, T: Display> Select<'a, T> {
Ok(())
}
}

#[cfg(test)]
mod tests {
use crate::test::without_ansi;

use super::*;
use indoc::indoc;

#[test]
fn test_render() {
let select = Select::new("Country")
.description("Select your Country")
.option(DemandOption::new("United States").selected(true))
.option(DemandOption::new("Germany"))
.option(DemandOption::new("Brazil"))
.option(DemandOption::new("Canada"))
.option(DemandOption::new("Mexico"));

assert_eq!(
indoc! {
" Country
Select your Country
↑/↓/k/j up/down • enter confirm"
},
without_ansi(select.render().unwrap().as_str())
);
}
}
28 changes: 28 additions & 0 deletions src/spinner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,31 @@ impl SpinnerStyle {
}
}
}

#[cfg(test)]
mod test {
use crate::test::without_ansi;

use super::*;

#[test]
fn test_render() {
for t in vec![
SpinnerStyle::dots(),
SpinnerStyle::jump(),
SpinnerStyle::line(),
SpinnerStyle::points(),
SpinnerStyle::meter(),
SpinnerStyle::minidots(),
SpinnerStyle::ellipsis(),
] {
let mut spinner = Spinner::new("Loading data...").style(t);
for f in spinner.style.chars.clone().iter() {
assert_eq!(
format!("{} Loading data...", f),
without_ansi(spinner.render().unwrap().as_str())
);
}
}
}
}
11 changes: 11 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::borrow::Cow;

#[ctor::ctor]
fn init() {
console::set_colors_enabled(false);
console::set_colors_enabled_stderr(false);
}

pub fn without_ansi(s: &str) -> Cow<str> {
console::strip_ansi_codes(s)
}

0 comments on commit c57e6ab

Please sign in to comment.