clap/tests/derive/next/utf8.rs

227 lines
5.5 KiB
Rust

#![cfg(not(windows))]
use clap::{ErrorKind, Parser};
use std::ffi::OsString;
use std::os::unix::ffi::OsStringExt;
#[derive(Parser, Debug, PartialEq, Eq)]
struct Positional {
#[clap(value_parser)]
arg: String,
}
#[derive(Parser, Debug, PartialEq, Eq)]
struct Named {
#[clap(short, long, value_parser)]
arg: String,
}
#[test]
fn invalid_utf8_strict_positional() {
let m = Positional::try_parse_from(vec![OsString::from(""), OsString::from_vec(vec![0xe9])]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidUtf8);
}
#[test]
fn invalid_utf8_strict_option_short_space() {
let m = Named::try_parse_from(vec![
OsString::from(""),
OsString::from("-a"),
OsString::from_vec(vec![0xe9]),
]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidUtf8);
}
#[test]
fn invalid_utf8_strict_option_short_equals() {
let m = Named::try_parse_from(vec![
OsString::from(""),
OsString::from_vec(vec![0x2d, 0x61, 0x3d, 0xe9]),
]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidUtf8);
}
#[test]
fn invalid_utf8_strict_option_short_no_space() {
let m = Named::try_parse_from(vec![
OsString::from(""),
OsString::from_vec(vec![0x2d, 0x61, 0xe9]),
]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidUtf8);
}
#[test]
fn invalid_utf8_strict_option_long_space() {
let m = Named::try_parse_from(vec![
OsString::from(""),
OsString::from("--arg"),
OsString::from_vec(vec![0xe9]),
]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidUtf8);
}
#[test]
fn invalid_utf8_strict_option_long_equals() {
let m = Named::try_parse_from(vec![
OsString::from(""),
OsString::from_vec(vec![0x2d, 0x2d, 0x61, 0x72, 0x67, 0x3d, 0xe9]),
]);
assert!(m.is_err());
assert_eq!(m.unwrap_err().kind(), ErrorKind::InvalidUtf8);
}
#[derive(Parser, Debug, PartialEq, Eq)]
struct PositionalOs {
#[clap(value_parser)]
arg: OsString,
}
#[derive(Parser, Debug, PartialEq, Eq)]
struct NamedOs {
#[clap(short, long, value_parser)]
arg: OsString,
}
#[test]
fn invalid_utf8_positional() {
let r = PositionalOs::try_parse_from(vec![OsString::from(""), OsString::from_vec(vec![0xe9])]);
assert_eq!(
r.unwrap(),
PositionalOs {
arg: OsString::from_vec(vec![0xe9])
}
);
}
#[test]
fn invalid_utf8_option_short_space() {
let r = NamedOs::try_parse_from(vec![
OsString::from(""),
OsString::from("-a"),
OsString::from_vec(vec![0xe9]),
]);
assert_eq!(
r.unwrap(),
NamedOs {
arg: OsString::from_vec(vec![0xe9])
}
);
}
#[test]
fn invalid_utf8_option_short_equals() {
let r = NamedOs::try_parse_from(vec![
OsString::from(""),
OsString::from_vec(vec![0x2d, 0x61, 0x3d, 0xe9]),
]);
assert_eq!(
r.unwrap(),
NamedOs {
arg: OsString::from_vec(vec![0xe9])
}
);
}
#[test]
fn invalid_utf8_option_short_no_space() {
let r = NamedOs::try_parse_from(vec![
OsString::from(""),
OsString::from_vec(vec![0x2d, 0x61, 0xe9]),
]);
assert_eq!(
r.unwrap(),
NamedOs {
arg: OsString::from_vec(vec![0xe9])
}
);
}
#[test]
fn invalid_utf8_option_long_space() {
let r = NamedOs::try_parse_from(vec![
OsString::from(""),
OsString::from("--arg"),
OsString::from_vec(vec![0xe9]),
]);
assert_eq!(
r.unwrap(),
NamedOs {
arg: OsString::from_vec(vec![0xe9])
}
);
}
#[test]
fn invalid_utf8_option_long_equals() {
let r = NamedOs::try_parse_from(vec![
OsString::from(""),
OsString::from_vec(vec![0x2d, 0x2d, 0x61, 0x72, 0x67, 0x3d, 0xe9]),
]);
assert_eq!(
r.unwrap(),
NamedOs {
arg: OsString::from_vec(vec![0xe9])
}
);
}
#[derive(Debug, PartialEq, Parser)]
enum External {
#[clap(external_subcommand)]
Other(Vec<String>),
}
#[test]
fn refuse_invalid_utf8_subcommand_with_allow_external_subcommands() {
let m = External::try_parse_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 refuse_invalid_utf8_subcommand_args_with_allow_external_subcommands() {
let m = External::try_parse_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);
}
#[derive(Debug, PartialEq, Parser)]
enum ExternalOs {
#[clap(external_subcommand)]
Other(Vec<OsString>),
}
#[test]
fn allow_invalid_utf8_subcommand_args_with_allow_external_subcommands() {
let m = ExternalOs::try_parse_from(vec![
OsString::from(""),
OsString::from("subcommand"),
OsString::from("normal"),
OsString::from_vec(vec![0xe9]),
OsString::from("--another_normal"),
]);
assert_eq!(
m.unwrap(),
ExternalOs::Other(vec![
OsString::from("subcommand"),
OsString::from("normal"),
OsString::from_vec(vec![0xe9]),
OsString::from("--another_normal"),
])
);
}