mirror of
https://github.com/gay-pizza/jaarg.git
synced 2025-12-19 07:20:18 +00:00
Tests for Opts
This commit is contained in:
@@ -201,17 +201,17 @@ impl<ID: 'static> Opt<ID> {
|
|||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod opt_tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic(expected = "Option names cannot be an empty slice")]
|
#[should_panic(expected = "Option names cannot be an empty slice")]
|
||||||
fn test_opt_new_empty_names_disallowed() {
|
fn test_new_empty_names_disallowed() {
|
||||||
Opt::new((), OptIdentifier::Multi(&[]), None, OptType::Positional);
|
Opt::new((), OptIdentifier::Multi(&[]), None, OptType::Positional);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_opt_public_initialisers() {
|
fn test_public_initialisers() {
|
||||||
assert_eq!(Opt::positional((), "name"), Opt { id: (),
|
assert_eq!(Opt::positional((), "name"), Opt { id: (),
|
||||||
names: OptIdentifier::Single("name"), value_name: None, help_string: None,
|
names: OptIdentifier::Single("name"), value_name: None, help_string: None,
|
||||||
r#type: OptType::Positional, flags: OptFlag::NONE,
|
r#type: OptType::Positional, flags: OptFlag::NONE,
|
||||||
@@ -231,7 +231,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_opt_valid_with_chains() {
|
fn test_valid_with_chains() {
|
||||||
assert_eq!(Opt::positional((), "").required(), Opt { id: (),
|
assert_eq!(Opt::positional((), "").required(), Opt { id: (),
|
||||||
names: OptIdentifier::Single(""), value_name: None, help_string: None,
|
names: OptIdentifier::Single(""), value_name: None, help_string: None,
|
||||||
r#type: OptType::Positional, flags: OptFlag::REQUIRED,
|
r#type: OptType::Positional, flags: OptFlag::REQUIRED,
|
||||||
@@ -248,24 +248,24 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic(expected = "Help flag cannot be made required")]
|
#[should_panic(expected = "Help flag cannot be made required")]
|
||||||
fn test_opt_required_help_disallowed() {
|
fn test_required_help_disallowed() {
|
||||||
Opt::help_flag((), &["-h", "--help"]).required();
|
Opt::help_flag((), &["-h", "--help"]).required();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic(expected = "Only flags are allowed to be help options")]
|
#[should_panic(expected = "Only flags are allowed to be help options")]
|
||||||
fn test_opt_positional_with_help_flag_disallowed() {
|
fn test_positional_with_help_flag_disallowed() {
|
||||||
Opt::positional((), "").with_help_flag();
|
Opt::positional((), "").with_help_flag();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic(expected = "Only flags are allowed to be help options")]
|
#[should_panic(expected = "Only flags are allowed to be help options")]
|
||||||
fn test_opt_value_with_help_flag_disallowed() {
|
fn test_value_with_help_flag_disallowed() {
|
||||||
Opt::value((), &[""], "").with_help_flag();
|
Opt::value((), &[""], "").with_help_flag();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_opt_flag_getters() {
|
fn test_flag_getters() {
|
||||||
const HELP: Opt<()> = Opt::help_flag((), &[""]);
|
const HELP: Opt<()> = Opt::help_flag((), &[""]);
|
||||||
const REQUIRED: Opt<()> = Opt::positional((), "").required();
|
const REQUIRED: Opt<()> = Opt::positional((), "").required();
|
||||||
assert!(HELP.is_help());
|
assert!(HELP.is_help());
|
||||||
@@ -275,13 +275,13 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_opt_first_name() {
|
fn test_first_name() {
|
||||||
assert_eq!(Opt::positional((), "first").first_name(), "first");
|
assert_eq!(Opt::positional((), "first").first_name(), "first");
|
||||||
assert_eq!(Opt::flag((), &["first", "second"]).first_name(), "first");
|
assert_eq!(Opt::flag((), &["first", "second"]).first_name(), "first");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_opt_first_long_name() {
|
fn test_first_long_name() {
|
||||||
assert_eq!(Opt::positional((), "--long").first_long_name(), Some("--long"));
|
assert_eq!(Opt::positional((), "--long").first_long_name(), Some("--long"));
|
||||||
assert_eq!(Opt::positional((), "-long").first_long_name(), Some("-long"));
|
assert_eq!(Opt::positional((), "-long").first_long_name(), Some("-long"));
|
||||||
assert_eq!(Opt::positional((), "--l").first_long_name(), Some("--l"));
|
assert_eq!(Opt::positional((), "--l").first_long_name(), Some("--l"));
|
||||||
@@ -290,7 +290,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_opt_first_short_name() {
|
fn test_first_short_name() {
|
||||||
assert_eq!(Opt::positional((), "-s").first_short_name(), Some("-s"));
|
assert_eq!(Opt::positional((), "-s").first_short_name(), Some("-s"));
|
||||||
assert_eq!(Opt::positional((), "--long").first_short_name(), None);
|
assert_eq!(Opt::positional((), "--long").first_short_name(), None);
|
||||||
assert_eq!(Opt::positional((), "--").first_short_name(), None);
|
assert_eq!(Opt::positional((), "--").first_short_name(), None);
|
||||||
@@ -300,7 +300,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_opt_first_short_name_char() {
|
fn test_first_short_name_char() {
|
||||||
assert_eq!(Opt::positional((), "-s").first_short_name_char(), Some('s'));
|
assert_eq!(Opt::positional((), "-s").first_short_name_char(), Some('s'));
|
||||||
assert_eq!(Opt::positional((), "--long").first_short_name_char(), None);
|
assert_eq!(Opt::positional((), "--long").first_short_name_char(), None);
|
||||||
assert_eq!(Opt::positional((), "--").first_short_name_char(), None);
|
assert_eq!(Opt::positional((), "--").first_short_name_char(), None);
|
||||||
@@ -310,7 +310,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_opt_match_name() {
|
fn test_match_name() {
|
||||||
assert_eq!(Opt::flag((), &["--one", "--two", "--threee", "--three"])
|
assert_eq!(Opt::flag((), &["--one", "--two", "--threee", "--three"])
|
||||||
.match_name("--three", 0), Some("--three"));
|
.match_name("--three", 0), Some("--three"));
|
||||||
assert_eq!(Opt::flag((), &["--one", "--two", "--threee"])
|
assert_eq!(Opt::flag((), &["--one", "--two", "--threee"])
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/// Static structure that contains instructions for parsing command-line arguments.
|
/// Static structure that contains instructions for parsing command-line arguments.
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct Opts<ID: 'static> {
|
pub struct Opts<ID: 'static> {
|
||||||
/// List of options
|
/// List of options
|
||||||
options: &'static[Opt<ID>],
|
options: &'static[Opt<ID>],
|
||||||
@@ -72,3 +73,51 @@ impl<ID: 'static> Opts<ID> {
|
|||||||
self.options.iter()
|
self.options.iter()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod opts_tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[allow(unused)]
|
||||||
|
fn test_required_opt_limit() {
|
||||||
|
const NUM_OPTS: usize = MAX_REQUIRED_OPTIONS + 2;
|
||||||
|
const OPT_LIST: [Opt<()>; NUM_OPTS] = {
|
||||||
|
const REQUIRED: Opt<()> = Opt::flag((), &[""]).required();
|
||||||
|
let mut array: [Opt<()>; NUM_OPTS] = [REQUIRED; NUM_OPTS];
|
||||||
|
array[0] = Opt::help_flag((), &[""]);
|
||||||
|
array[NUM_OPTS - 1] = Opt::positional((), "");
|
||||||
|
array
|
||||||
|
};
|
||||||
|
const OPTIONS: Opts<()> = Opts::new(&OPT_LIST);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_chains() {
|
||||||
|
assert_eq!(Opts::<()>::new(&[]).with_flag_chars("-/"),
|
||||||
|
Opts { options: &[], flag_chars: "-/", description: None });
|
||||||
|
assert_eq!(Opts::<()>::new(&[]).with_description("test description"),
|
||||||
|
Opts { options: &[], flag_chars: "-", description: Some("test description") });
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_help_option() {
|
||||||
|
const OPTS1: Opts<()> = Opts::new(&[
|
||||||
|
Opt::flag((), &[""]),
|
||||||
|
Opt::flag((), &[""]),
|
||||||
|
Opt::positional((), ""),
|
||||||
|
Opt::positional((), ""),
|
||||||
|
Opt::help_flag((), &["--help"]),
|
||||||
|
Opt::value((), &[""], ""),
|
||||||
|
Opt::help_flag((), &[""]),
|
||||||
|
]);
|
||||||
|
const OPTS2: Opts<()> = Opts::new(&[
|
||||||
|
Opt::flag((), &[""]),
|
||||||
|
Opt::positional((), ""),
|
||||||
|
Opt::value((), &[""], ""),
|
||||||
|
]);
|
||||||
|
assert_eq!(OPTS1.help_option(), Some(&Opt::help_flag((), &["--help"])));
|
||||||
|
assert_eq!(OPTS2.help_option(), None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user