1use clap::{error::ErrorKind, Arg, ArgAction, Command}; 2 3#[cfg(feature = "error-context")] 4use super::utils; 5 6#[test] 7fn empty_values() { 8 let m = Command::new("config") 9 .arg(Arg::new("config").long("config").action(ArgAction::Set)) 10 .try_get_matches_from(["config", "--config", ""]) 11 .unwrap(); 12 assert_eq!(m.get_one::<String>("config").map(|v| v.as_str()), Some("")); 13} 14 15#[test] 16fn empty_values_with_equals() { 17 let m = Command::new("config") 18 .arg(Arg::new("config").long("config").action(ArgAction::Set)) 19 .try_get_matches_from(["config", "--config="]) 20 .unwrap(); 21 assert_eq!(m.get_one::<String>("config").map(|v| v.as_str()), Some("")); 22 23 let m = Command::new("config") 24 .arg(Arg::new("config").short('c').action(ArgAction::Set)) 25 .try_get_matches_from(["config", "-c="]) 26 .unwrap(); 27 assert_eq!(m.get_one::<String>("config").map(|v| v.as_str()), Some("")) 28} 29 30#[test] 31fn no_empty_values() { 32 let m = Command::new("config") 33 .arg( 34 Arg::new("config") 35 .long("config") 36 .action(ArgAction::Set) 37 .value_parser(clap::builder::NonEmptyStringValueParser::new()), 38 ) 39 .try_get_matches_from(["config", "--config", ""]); 40 assert!(m.is_err()); 41 assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue); 42 43 let m = Command::new("config") 44 .arg( 45 Arg::new("config") 46 .short('c') 47 .action(ArgAction::Set) 48 .value_parser(clap::builder::NonEmptyStringValueParser::new()), 49 ) 50 .try_get_matches_from(["config", "-c", ""]); 51 assert!(m.is_err()); 52 assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue) 53} 54 55#[test] 56fn no_empty_values_with_equals() { 57 let m = Command::new("config") 58 .arg( 59 Arg::new("config") 60 .long("config") 61 .action(ArgAction::Set) 62 .value_parser(clap::builder::NonEmptyStringValueParser::new()), 63 ) 64 .try_get_matches_from(["config", "--config="]); 65 assert!(m.is_err()); 66 assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue); 67 68 let m = Command::new("config") 69 .arg( 70 Arg::new("config") 71 .short('c') 72 .action(ArgAction::Set) 73 .value_parser(clap::builder::NonEmptyStringValueParser::new()), 74 ) 75 .try_get_matches_from(["config", "-c="]); 76 assert!(m.is_err()); 77 assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue); 78} 79 80#[test] 81fn no_empty_values_without_equals() { 82 let m = Command::new("config") 83 .arg( 84 Arg::new("config") 85 .long("config") 86 .action(ArgAction::Set) 87 .value_parser(clap::builder::NonEmptyStringValueParser::new()), 88 ) 89 .try_get_matches_from(["config", "--config"]); 90 assert!(m.is_err()); 91 assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue); 92 93 let m = Command::new("config") 94 .arg( 95 Arg::new("config") 96 .short('c') 97 .action(ArgAction::Set) 98 .value_parser(clap::builder::NonEmptyStringValueParser::new()), 99 ) 100 .try_get_matches_from(["config", "-c"]); 101 assert!(m.is_err()); 102 assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidValue) 103} 104 105#[test] 106#[cfg(feature = "error-context")] 107fn no_empty_values_without_equals_but_requires_equals() { 108 let cmd = Command::new("config").arg( 109 Arg::new("config") 110 .long("config") 111 .action(ArgAction::Set) 112 .value_parser(clap::builder::NonEmptyStringValueParser::new()) 113 .require_equals(true), 114 ); 115 let m = cmd.clone().try_get_matches_from(["config", "--config"]); 116 // Should error on no equals rather than empty value. 117 assert!(m.is_err()); 118 assert_eq!(m.unwrap_err().kind(), ErrorKind::NoEquals); 119 120 static NO_EUQALS_ERROR: &str = 121 "error: equal sign is needed when assigning values to '--config=<config>' 122 123Usage: config [OPTIONS] 124 125For more information, try '--help'. 126"; 127 128 utils::assert_output(cmd, "config --config", NO_EUQALS_ERROR, true); 129} 130