Files

553 lines
12 KiB
Rust
Raw Permalink Normal View History

2023-12-02 17:40:52 +01:00
use uutils_args::{Arguments, Options};
2022-11-27 14:22:27 +01:00
#[test]
fn one_flag() {
#[derive(Arguments)]
2022-12-02 00:41:35 +01:00
enum Arg {
#[arg("-f", "--foo")]
2022-12-02 00:41:35 +01:00
Foo,
2022-11-27 14:22:27 +01:00
}
2023-12-02 17:40:52 +01:00
#[derive(Default)]
2022-12-02 00:41:35 +01:00
struct Settings {
foo: bool,
}
impl Options<Arg> for Settings {
2024-03-29 03:32:30 +01:00
fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
match arg {
Arg::Foo => self.foo = true,
}
2024-03-29 03:32:30 +01:00
Ok(())
}
}
2023-12-19 15:13:12 +01:00
let (settings, _) = Settings::default().parse(["test", "-f"]).unwrap();
2022-12-02 00:41:35 +01:00
assert!(settings.foo);
2022-11-27 14:22:27 +01:00
}
#[test]
fn two_flags() {
2022-12-03 18:50:46 +01:00
#[derive(Arguments, Clone)]
2022-12-02 00:41:35 +01:00
enum Arg {
#[arg("-a")]
2022-12-02 00:41:35 +01:00
A,
#[arg("-b")]
2022-12-02 00:41:35 +01:00
B,
}
2023-12-02 17:40:52 +01:00
#[derive(Default, PartialEq, Eq, Debug)]
2022-11-27 14:22:27 +01:00
struct Settings {
a: bool,
b: bool,
}
impl Options<Arg> for Settings {
2024-03-29 03:32:30 +01:00
fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
match arg {
Arg::A => self.a = true,
Arg::B => self.b = true,
}
2024-03-29 03:32:30 +01:00
Ok(())
}
}
2022-11-27 16:33:09 +01:00
assert_eq!(
2023-12-19 15:13:12 +01:00
Settings::default().parse(["test", "-a"]).unwrap().0,
2022-11-27 16:33:09 +01:00
Settings { a: true, b: false }
);
assert_eq!(
2023-12-19 15:13:12 +01:00
Settings::default().parse(["test"]).unwrap().0,
2023-12-02 17:40:52 +01:00
Settings { a: false, b: false }
);
assert_eq!(
2023-12-19 15:13:12 +01:00
Settings::default().parse(["test", "-b"]).unwrap().0,
2022-11-27 16:33:09 +01:00
Settings { a: false, b: true }
);
assert_eq!(
2023-12-19 15:13:12 +01:00
Settings::default().parse(["test", "-a", "-b"]).unwrap().0,
2022-11-27 16:33:09 +01:00
Settings { a: true, b: true }
);
}
#[test]
fn long_and_short_flag() {
#[derive(Arguments)]
2022-12-02 00:41:35 +01:00
enum Arg {
#[arg("-f", "--foo")]
2022-12-02 00:41:35 +01:00
Foo,
}
2023-12-02 17:40:52 +01:00
#[derive(Default)]
2022-12-02 00:41:35 +01:00
struct Settings {
2022-11-27 16:33:09 +01:00
foo: bool,
}
impl Options<Arg> for Settings {
2024-03-29 03:32:30 +01:00
fn apply(&mut self, Arg::Foo: Arg) -> Result<(), uutils_args::Error> {
self.foo = true;
2024-03-29 03:32:30 +01:00
Ok(())
}
}
2023-12-19 15:13:12 +01:00
assert!(!Settings::default().parse(["test"]).unwrap().0.foo);
assert!(Settings::default().parse(["test", "--foo"]).unwrap().0.foo);
assert!(Settings::default().parse(["test", "-f"]).unwrap().0.foo);
2022-11-27 16:33:09 +01:00
}
#[test]
fn short_alias() {
#[derive(Arguments)]
2022-12-02 00:41:35 +01:00
enum Arg {
#[arg("-b")]
2022-12-02 00:41:35 +01:00
Foo,
}
2023-12-02 17:40:52 +01:00
#[derive(Default)]
2022-12-02 00:41:35 +01:00
struct Settings {
2022-11-27 16:33:09 +01:00
foo: bool,
}
impl Options<Arg> for Settings {
2024-03-29 03:32:30 +01:00
fn apply(&mut self, Arg::Foo: Arg) -> Result<(), uutils_args::Error> {
self.foo = true;
2024-03-29 03:32:30 +01:00
Ok(())
}
}
2023-12-19 15:13:12 +01:00
assert!(Settings::default().parse(["test", "-b"]).unwrap().0.foo);
2022-11-27 16:33:09 +01:00
}
#[test]
fn long_alias() {
#[derive(Arguments)]
2022-12-02 00:41:35 +01:00
enum Arg {
#[arg("--bar")]
2022-12-02 00:41:35 +01:00
Foo,
}
2023-12-02 17:40:52 +01:00
#[derive(Default)]
2022-12-02 00:41:35 +01:00
struct Settings {
2022-11-27 16:33:09 +01:00
foo: bool,
}
impl Options<Arg> for Settings {
2024-03-29 03:32:30 +01:00
fn apply(&mut self, Arg::Foo: Arg) -> Result<(), uutils_args::Error> {
self.foo = true;
2024-03-29 03:32:30 +01:00
Ok(())
}
}
2023-12-19 15:13:12 +01:00
assert!(Settings::default().parse(["test", "--bar"]).unwrap().0.foo);
2022-11-27 16:33:09 +01:00
}
#[test]
fn short_and_long_alias() {
#[derive(Arguments)]
2022-12-02 00:41:35 +01:00
enum Arg {
#[arg("-b", "--bar")]
2022-12-02 00:41:35 +01:00
Foo,
#[arg("-f", "--foo")]
2022-12-02 00:41:35 +01:00
Bar,
}
2023-12-02 17:40:52 +01:00
#[derive(Default, PartialEq, Eq, Debug)]
2022-12-02 00:41:35 +01:00
struct Settings {
foo: bool,
2022-11-27 16:33:09 +01:00
bar: bool,
}
impl Options<Arg> for Settings {
2024-03-29 03:32:30 +01:00
fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
match arg {
Arg::Foo => self.foo = true,
Arg::Bar => self.bar = true,
}
2024-03-29 03:32:30 +01:00
Ok(())
}
}
2022-11-27 16:33:09 +01:00
let foo_true = Settings {
foo: true,
bar: false,
};
let bar_true = Settings {
foo: false,
bar: true,
};
2023-12-19 15:13:12 +01:00
assert_eq!(
Settings::default().parse(["test", "--bar"]).unwrap().0,
foo_true
);
assert_eq!(
Settings::default().parse(["test", "-b"]).unwrap().0,
foo_true
);
assert_eq!(
Settings::default().parse(["test", "--foo"]).unwrap().0,
bar_true
);
assert_eq!(
Settings::default().parse(["test", "-f"]).unwrap().0,
bar_true
);
2022-11-27 16:33:09 +01:00
}
#[test]
fn xyz_map_to_abc() {
#[derive(Arguments)]
2022-12-02 00:41:35 +01:00
enum Arg {
#[arg("-x")]
2022-12-02 00:41:35 +01:00
X,
#[arg("-y")]
2022-12-02 00:41:35 +01:00
Y,
#[arg("-z")]
2022-12-02 00:41:35 +01:00
Z,
}
2023-12-02 17:40:52 +01:00
#[derive(Default, PartialEq, Eq, Debug)]
2022-11-27 16:33:09 +01:00
struct Settings {
a: bool,
b: bool,
c: bool,
}
impl Options<Arg> for Settings {
2024-03-29 03:32:30 +01:00
fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
match arg {
Arg::X => {
self.a = true;
self.b = true;
}
Arg::Y => {
self.b = true;
self.c = true;
}
Arg::Z => {
self.a = true;
self.b = true;
self.c = true;
}
}
2024-03-29 03:32:30 +01:00
Ok(())
}
}
2022-11-27 16:33:09 +01:00
assert_eq!(
2023-12-19 15:13:12 +01:00
Settings::default().parse(["test", "-x"]).unwrap().0,
2022-11-27 16:33:09 +01:00
Settings {
a: true,
b: true,
c: false,
},
);
assert_eq!(
2023-12-19 15:13:12 +01:00
Settings::default().parse(["test", "-y"]).unwrap().0,
2022-11-27 16:33:09 +01:00
Settings {
a: false,
b: true,
c: true,
},
);
assert_eq!(
2023-12-19 15:13:12 +01:00
Settings::default().parse(["test", "-xy"]).unwrap().0,
2022-11-27 16:33:09 +01:00
Settings {
a: true,
b: true,
c: true,
},
);
assert_eq!(
2023-12-19 15:13:12 +01:00
Settings::default().parse(["test", "-z"]).unwrap().0,
2022-11-27 16:33:09 +01:00
Settings {
a: true,
b: true,
c: true,
},
);
2022-11-27 14:22:27 +01:00
}
#[test]
fn non_rust_ident() {
#[derive(Arguments)]
2022-12-02 00:41:35 +01:00
enum Arg {
#[arg("--foo-bar")]
2022-12-02 00:41:35 +01:00
FooBar,
#[arg("--super")]
2022-12-02 00:41:35 +01:00
Super,
}
2023-12-02 17:40:52 +01:00
#[derive(Default, PartialEq, Eq, Debug)]
2022-12-02 00:41:35 +01:00
struct Settings {
a: bool,
b: bool,
}
impl Options<Arg> for Settings {
2024-03-29 03:32:30 +01:00
fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
match arg {
Arg::FooBar => self.a = true,
Arg::Super => self.b = true,
}
2024-03-29 03:32:30 +01:00
Ok(())
}
}
assert_eq!(
Settings::default()
.parse(["test", "--foo-bar", "--super"])
2023-12-19 15:13:12 +01:00
.unwrap()
.0,
Settings { a: true, b: true }
)
}
#[test]
fn number_flag() {
2022-12-03 18:50:46 +01:00
#[derive(Arguments, Clone)]
2022-12-02 00:41:35 +01:00
enum Arg {
#[arg("-1")]
2022-12-02 00:41:35 +01:00
One,
}
2023-12-02 17:40:52 +01:00
#[derive(Default, PartialEq, Eq, Debug)]
2022-12-02 00:41:35 +01:00
struct Settings {
one: bool,
}
impl Options<Arg> for Settings {
2024-03-29 03:32:30 +01:00
fn apply(&mut self, Arg::One: Arg) -> Result<(), uutils_args::Error> {
self.one = true;
2024-03-29 03:32:30 +01:00
Ok(())
}
}
2023-12-19 15:13:12 +01:00
assert!(Settings::default().parse(["test", "-1"]).unwrap().0.one)
}
2022-11-27 18:41:14 +01:00
#[test]
fn false_bool() {
#[derive(Arguments)]
2022-12-02 00:41:35 +01:00
enum Arg {
#[arg("-a")]
2022-12-02 00:41:35 +01:00
A,
#[arg("-b")]
2022-12-02 00:41:35 +01:00
B,
}
2023-12-02 17:40:52 +01:00
#[derive(Default)]
2022-11-27 18:41:14 +01:00
struct Settings {
foo: bool,
}
impl Options<Arg> for Settings {
2024-03-29 03:32:30 +01:00
fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
self.foo = match arg {
Arg::A => true,
Arg::B => false,
2024-03-29 03:32:30 +01:00
};
Ok(())
}
2022-12-02 13:49:12 +01:00
}
2023-12-19 15:13:12 +01:00
assert!(Settings::default().parse(["test", "-a"]).unwrap().0.foo);
assert!(!Settings::default().parse(["test", "-b"]).unwrap().0.foo);
assert!(!Settings::default().parse(["test", "-ab"]).unwrap().0.foo);
assert!(Settings::default().parse(["test", "-ba"]).unwrap().0.foo);
assert!(
!Settings::default()
.parse(["test", "-a", "-b"])
.unwrap()
.0
.foo
);
assert!(
Settings::default()
.parse(["test", "-b", "-a"])
.unwrap()
.0
.foo
);
2022-11-27 18:41:14 +01:00
}
#[test]
fn verbosity() {
#[derive(Arguments)]
2022-12-02 00:41:35 +01:00
enum Arg {
#[arg("-v")]
2022-12-02 00:41:35 +01:00
Verbosity,
}
2023-12-02 17:40:52 +01:00
#[derive(Default)]
2022-11-27 18:41:14 +01:00
struct Settings {
verbosity: u8,
}
impl Options<Arg> for Settings {
2024-03-29 03:32:30 +01:00
fn apply(&mut self, Arg::Verbosity: Arg) -> Result<(), uutils_args::Error> {
self.verbosity += 1;
2024-03-29 03:32:30 +01:00
Ok(())
}
}
2023-12-19 15:13:12 +01:00
assert_eq!(
Settings::default()
.parse(["test", "-v"])
.unwrap()
.0
.verbosity,
1
);
assert_eq!(
Settings::default()
.parse(["test", "-vv"])
.unwrap()
.0
.verbosity,
2
);
assert_eq!(
Settings::default()
.parse(["test", "-vvv"])
.unwrap()
.0
.verbosity,
3
);
2022-11-27 18:41:14 +01:00
}
2022-12-03 16:17:21 +01:00
#[test]
fn infer_long_args() {
#[derive(Arguments)]
2022-12-03 16:17:21 +01:00
enum Arg {
#[arg("--all")]
2022-12-03 16:17:21 +01:00
All,
#[arg("--almost-all")]
2022-12-03 16:17:21 +01:00
AlmostAll,
#[arg("--author")]
2022-12-03 18:50:46 +01:00
Author,
2022-12-03 16:17:21 +01:00
}
2023-12-02 17:40:52 +01:00
#[derive(Default)]
2022-12-03 16:17:21 +01:00
struct Settings {
all: bool,
almost_all: bool,
author: bool,
}
impl Options<Arg> for Settings {
2024-03-29 03:32:30 +01:00
fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
match arg {
Arg::All => self.all = true,
Arg::AlmostAll => self.almost_all = true,
Arg::Author => self.author = true,
}
2024-03-29 03:32:30 +01:00
Ok(())
}
}
2023-12-19 15:13:12 +01:00
assert!(Settings::default().parse(["test", "--all"]).unwrap().0.all);
assert!(
Settings::default()
.parse(["test", "--alm"])
.unwrap()
.0
.almost_all
);
assert!(
Settings::default()
.parse(["test", "--au"])
.unwrap()
.0
.author
);
assert!(Settings::default().parse(["test", "--a"]).is_err());
2022-12-03 18:50:46 +01:00
}
#[test]
fn enum_flag() {
#[derive(Default, PartialEq, Eq, Debug)]
enum SomeEnum {
#[default]
Foo,
Bar,
Baz,
}
#[derive(Arguments)]
enum Arg {
#[arg("--foo")]
Foo,
#[arg("--bar")]
Bar,
#[arg("--baz")]
Baz,
}
2023-12-02 17:40:52 +01:00
#[derive(Default)]
struct Settings {
foo: SomeEnum,
}
impl Options<Arg> for Settings {
2024-03-29 03:32:30 +01:00
fn apply(&mut self, arg: Arg) -> Result<(), uutils_args::Error> {
self.foo = match arg {
Arg::Foo => SomeEnum::Foo,
Arg::Bar => SomeEnum::Bar,
Arg::Baz => SomeEnum::Baz,
};
2024-03-29 03:32:30 +01:00
Ok(())
}
}
2023-12-02 17:40:52 +01:00
assert_eq!(
2023-12-19 15:13:12 +01:00
Settings::default().parse(["test"]).unwrap().0.foo,
SomeEnum::Foo
);
assert_eq!(
Settings::default().parse(["test", "--bar"]).unwrap().0.foo,
2023-12-02 17:40:52 +01:00
SomeEnum::Bar
);
assert_eq!(
2023-12-19 15:13:12 +01:00
Settings::default().parse(["test", "--baz"]).unwrap().0.foo,
2023-12-02 17:40:52 +01:00
SomeEnum::Baz,
);
}
2024-03-29 03:51:00 +01:00
#[test]
fn simple_error() {
#[derive(Arguments)]
enum Arg {
#[arg("-f", "--foo")]
Foo,
}
#[derive(Debug, Default)]
struct Settings {}
impl Options<Arg> for Settings {
fn apply(&mut self, _arg: Arg) -> Result<(), uutils_args::Error> {
Err(uutils_args::Error {
exit_code: 42,
kind: uutils_args::ErrorKind::UnexpectedArgument(
"This is an example error".to_owned(),
),
})
}
}
let settings_or_error = Settings::default().parse(["test", "-f"]);
let the_error = settings_or_error.expect_err("should have propagated error");
assert_eq!(the_error.exit_code, 42);
match the_error.kind {
uutils_args::ErrorKind::UnexpectedArgument(err_str) => {
assert_eq!(err_str, "This is an example error")
}
_ => panic!("wrong error kind: {:?}", the_error.kind),
}
}