Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tests to verify initial rendering #25

Merged
merged 4 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
34 changes: 30 additions & 4 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 @@ -133,12 +133,11 @@ impl<'a> Confirm<'a> {
let mut out = Buffer::ansi();

out.set_color(&self.theme.title)?;
write!(out, " {}", self.title)?;
writeln!(out, " {}", self.title)?;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you want to remove the 1 column prefix?

I looked at huh and it looks like they actually put a solid line on the side which I don't really like the look of. I don't have a strong preference here, but whatever we do should be consistent across our components I think

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess in theory this is something that could be handled via themes if we want to go that route (not sure if huh does that or not)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK the line on the side in huh marks the active field in a form/group.


if !self.description.is_empty() {
out.set_color(&self.theme.description)?;
write!(out, " {}", self.description)?;
writeln!(out)?;
}
writeln!(out, "\n")?;

Expand Down Expand Up @@ -201,3 +200,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

roele marked this conversation as resolved.
Show resolved Hide resolved
↑/↓/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)
}