Tests for Opts

This commit is contained in:
2025-11-05 07:21:34 +11:00
parent b0072855bc
commit 0098df1252
2 changed files with 62 additions and 13 deletions

View File

@@ -201,17 +201,17 @@ impl<ID: 'static> Opt<ID> {
#[cfg(test)]
mod tests {
mod opt_tests {
use super::*;
#[test]
#[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);
}
#[test]
fn test_opt_public_initialisers() {
fn test_public_initialisers() {
assert_eq!(Opt::positional((), "name"), Opt { id: (),
names: OptIdentifier::Single("name"), value_name: None, help_string: None,
r#type: OptType::Positional, flags: OptFlag::NONE,
@@ -231,7 +231,7 @@ mod tests {
}
#[test]
fn test_opt_valid_with_chains() {
fn test_valid_with_chains() {
assert_eq!(Opt::positional((), "").required(), Opt { id: (),
names: OptIdentifier::Single(""), value_name: None, help_string: None,
r#type: OptType::Positional, flags: OptFlag::REQUIRED,
@@ -248,24 +248,24 @@ mod tests {
#[test]
#[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();
}
#[test]
#[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();
}
#[test]
#[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();
}
#[test]
fn test_opt_flag_getters() {
fn test_flag_getters() {
const HELP: Opt<()> = Opt::help_flag((), &[""]);
const REQUIRED: Opt<()> = Opt::positional((), "").required();
assert!(HELP.is_help());
@@ -275,13 +275,13 @@ mod tests {
}
#[test]
fn test_opt_first_name() {
fn test_first_name() {
assert_eq!(Opt::positional((), "first").first_name(), "first");
assert_eq!(Opt::flag((), &["first", "second"]).first_name(), "first");
}
#[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((), "--l").first_long_name(), Some("--l"));
@@ -290,7 +290,7 @@ mod tests {
}
#[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((), "--long").first_short_name(), None);
assert_eq!(Opt::positional((), "--").first_short_name(), None);
@@ -300,7 +300,7 @@ mod tests {
}
#[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((), "--long").first_short_name_char(), None);
assert_eq!(Opt::positional((), "--").first_short_name_char(), None);
@@ -310,7 +310,7 @@ mod tests {
}
#[test]
fn test_opt_match_name() {
fn test_match_name() {
assert_eq!(Opt::flag((), &["--one", "--two", "--threee", "--three"])
.match_name("--three", 0), Some("--three"));
assert_eq!(Opt::flag((), &["--one", "--two", "--threee"])

View File

@@ -4,6 +4,7 @@
*/
/// Static structure that contains instructions for parsing command-line arguments.
#[derive(Debug, PartialEq)]
pub struct Opts<ID: 'static> {
/// List of options
options: &'static[Opt<ID>],
@@ -72,3 +73,51 @@ impl<ID: 'static> Opts<ID> {
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);
}
}