summaryrefslogtreecommitdiff
path: root/argparse/src/test_optional.rs
diff options
context:
space:
mode:
Diffstat (limited to 'argparse/src/test_optional.rs')
-rw-r--r--argparse/src/test_optional.rs54
1 files changed, 54 insertions, 0 deletions
diff --git a/argparse/src/test_optional.rs b/argparse/src/test_optional.rs
new file mode 100644
index 0000000..5f4c35e
--- /dev/null
+++ b/argparse/src/test_optional.rs
@@ -0,0 +1,54 @@
+use parser::ArgumentParser;
+use super::StoreOption;
+use test_parser::{check_ok};
+
+fn opt(args: &[&str]) -> Option<isize> {
+ let mut val = None;
+ {
+ let mut ap = ArgumentParser::new();
+ ap.refer(&mut val)
+ .add_option(&["-s", "--set"], StoreOption,
+ "Set int value");
+ check_ok(&ap, args);
+ }
+ return val;
+}
+
+#[test]
+fn test_opt() {
+ assert_eq!(opt(&["./argparse_test"]), None);
+ assert_eq!(opt(&["./argparse_test", "-s", "10"]), Some(10));
+ assert_eq!(opt(&["./argparse_test", "--set", "11"]), Some(11));
+}
+
+#[test]
+#[should_panic]
+fn test_opt_no_arg() {
+ opt(&["./argparse_test", "--set"]);
+}
+
+fn optstr(args: &[&str]) -> Option<String> {
+ let mut val = None;
+ {
+ let mut ap = ArgumentParser::new();
+ ap.refer(&mut val)
+ .add_option(&["-s", "--set"], StoreOption,
+ "Set string value");
+ check_ok(&ap, args);
+ }
+ return val;
+}
+
+#[test]
+fn test_str() {
+ assert_eq!(optstr(&["./argparse_test"]), None);
+ assert_eq!(optstr(&["./argparse_test", "-s", "10"]), Some(10.to_string()));
+ assert_eq!(optstr(&["./argparse_test", "--set", "11"]),
+ Some(11.to_string()));
+}
+
+#[test]
+#[should_panic]
+fn test_str_no_art() {
+ optstr(&["./argparse_test", "--set"]);
+}