Skip to content

Commit

Permalink
test: Add test of derive functionality
Browse files Browse the repository at this point in the history
It was noticed that between clap-rs#4443 and clap-rs#4432, an issue in the behavior
was that the derive api handles tasks with values slightly differently
than the declarative api. Added a test to show parity between
declaritive and derive api.
  • Loading branch information
Calder-Ty committed Dec 16, 2022
1 parent b0f6289 commit d9e5473
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 19 deletions.
2 changes: 1 addition & 1 deletion clap_mangen/Cargo.toml
Expand Up @@ -45,7 +45,7 @@ clap = { path = "../", version = "4.0.0", default-features = false, features = [

[dev-dependencies]
snapbox = { version = "0.4", features = ["diff"] }
clap = { path = "../", version = "4.0.0", default-features = false, features = ["std", "help"] }
clap = { path = "../", version = "4.0.0", default-features = false, features = ["std", "help", "derive"] }

[features]
default = []
Expand Down
14 changes: 0 additions & 14 deletions clap_mangen/tests/common.rs
Expand Up @@ -312,17 +312,3 @@ pub fn possible_values_command(name: &'static str) -> clap::Command {
]),
)
}

/// Checks to make sure boolean valued "Flag options" do not generate
/// suggestions for a parameter. i.e:
/// --boolean_flag=BOOLEAN_FLAG
///
/// This is both confusing and suggest erroneous behavior as clap will fail if you
/// pass a value to a boolean flag
pub fn flag_without_value(name: &'static str) -> clap::Command {
clap::Command::new(name).arg(
clap::Arg::new("is_bool")
.long("is_bool")
.action(clap::ArgAction::SetTrue),
)
}
36 changes: 36 additions & 0 deletions clap_mangen/tests/derive.rs
@@ -0,0 +1,36 @@
use clap::{Parser, Subcommand};

#[derive(Parser, Debug)]
#[command(name = "my-app")]
pub struct BasicCommand {
#[clap(short, global = true)]
config: bool,

#[clap(short, conflicts_with("config"))]
v: bool,

#[command(subcommand)]
test: Option<BasicCommandSubCommand>,
}

#[derive(Subcommand, Debug)]
pub enum BasicCommandSubCommand {
/// Subcommand
Test {
#[clap(short, action(clap::ArgAction::Count))]
debug: u8,
},
}

// Checks to make sure boolean valued "Flag options" do not generate
// suggestions for a parameter. i.e:
// --boolean_flag=BOOLEAN_FLAG
//
// This is both confusing and suggest erroneous behavior as clap will fail if you
// pass a value to a boolean flag
#[derive(Parser, Debug)]
#[command(name = "my-app")]
pub struct FlagWithoutValue {
#[clap(long)]
boolean_flag: bool,
}
12 changes: 10 additions & 2 deletions clap_mangen/tests/roff.rs
@@ -1,4 +1,7 @@
mod common;
mod derive;

use clap::CommandFactory;

#[test]
fn basic() {
Expand Down Expand Up @@ -72,11 +75,16 @@ fn possible_values() {

#[test]
fn flag_without_value() {
let name = "my-app";
let cmd = common::flag_without_value(name);
let cmd = derive::FlagWithoutValue::command();
common::assert_matches_path("tests/snapshots/flag_without_value.bash.roff", cmd);
}

#[test]
fn derive_basic_command() {
let cmd = derive::BasicCommand::command();
common::assert_matches_path("tests/snapshots/basic.bash.roff", cmd);
}

#[test]
fn sub_subcommands_help() {
let name = "my-app";
Expand Down
4 changes: 2 additions & 2 deletions clap_mangen/tests/snapshots/flag_without_value.bash.roff
Expand Up @@ -4,11 +4,11 @@
.SH NAME
my/-app
.SH SYNOPSIS
/fBmy/-app/fR [/fB/-/-is_bool/fR] [/fB/-h/fR|/fB/-/-help/fR]
/fBmy/-app/fR [/fB/-/-boolean/-flag/fR] [/fB/-h/fR|/fB/-/-help/fR]
.SH DESCRIPTION
.SH OPTIONS
.TP
/fB/-/-is_bool/fR
/fB/-/-boolean/-flag/fR

.TP
/fB/-h/fR, /fB/-/-help/fR
Expand Down

0 comments on commit d9e5473

Please sign in to comment.