Add tests
Fix tests
@@ -264,3 +264,78 @@ fn invalid_utf8_option_long_equals() {
&*OsString::from_vec(vec![0xe9])
);
}
+
+#[test]
+fn refuse_invalid_utf8_subcommand_with_allow_external_subcommands() {
+ let m = App::new("bad_utf8")
+ .setting(AppSettings::StrictUtf8)
+ .setting(AppSettings::AllowExternalSubcommands)
+ .try_get_matches_from(vec![
+ OsString::from(""),
+ OsString::from_vec(vec![0xe9]),
+ OsString::from("normal"),
+ ]);
+ assert!(m.is_err());
+ assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8);
+}
+
+#[test]
+fn allow_invalid_utf8_subcommand_with_allow_external_subcommands() {
+ let m = App::new("bad_utf8")
+ .setting(AppSettings::AllowExternalSubcommands)
+ .try_get_matches_from(vec![
+ OsString::from(""),
+ OsString::from_vec(vec![0xe9]),
+ OsString::from("normal"),
+ ]);
+ assert!(m.is_ok());
+ let m = m.unwrap();
+ let (_subcommand, args) = m.subcommand().unwrap();
+ let args = args.values_of_os("").unwrap().collect::<Vec<_>>();
+ assert_eq!(args, vec![OsString::from("normal"),]);
+ // TODO: currently subcommand can only be &str, which means following code
+ // will panic.
+ // assert_eq!(_subcommand, OsString::from_vec(vec![0xe9]));
+}
+
+#[test]
+fn refuse_invalid_utf8_subcommand_args_with_allow_external_subcommands() {
+ let m = App::new("bad_utf8")
+ .setting(AppSettings::StrictUtf8)
+ .setting(AppSettings::AllowExternalSubcommands)
+ .try_get_matches_from(vec![
+ OsString::from(""),
+ OsString::from("subcommand"),
+ OsString::from("normal"),
+ OsString::from_vec(vec![0xe9]),
+ OsString::from("--another_normal"),
+ ]);
+ assert!(m.is_err());
+ assert_eq!(m.unwrap_err().kind, ErrorKind::InvalidUtf8);
+}
+
+#[test]
+fn allow_invalid_utf8_subcommand_args_with_allow_external_subcommands() {
+ let m = App::new("bad_utf8")
+ .setting(AppSettings::AllowExternalSubcommands)
+ .try_get_matches_from(vec![
+ OsString::from(""),
+ OsString::from("subcommand"),
+ OsString::from("normal"),
+ OsString::from_vec(vec![0xe9]),
+ OsString::from("--another_normal"),
+ ]);
+ assert!(m.is_ok());
+ let m = m.unwrap();
+ let (subcommand, args) = m.subcommand().unwrap();
+ let args = args.values_of_os("").unwrap().collect::<Vec<_>>();
+ assert_eq!(subcommand, OsString::from("subcommand"));
+ assert_eq!(
+ args,
+ vec![
+ OsString::from("normal"),
+ OsString::from_vec(vec![0xe9]),
+ OsString::from("--another_normal"),
+ ]
+ );
+}
