2021-09-04 08:10:31 +00:00
|
|
|
use nu_protocol::{Flag, PositionalArg, Signature, SyntaxShape};
|
2021-09-04 07:45:49 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_signature() {
|
|
|
|
let signature = Signature::new("new_signature");
|
|
|
|
let from_build = Signature::build("new_signature");
|
|
|
|
|
|
|
|
// asserting partial eq implementation
|
|
|
|
assert_eq!(signature, from_build);
|
|
|
|
|
|
|
|
// constructing signature with description
|
2022-03-27 19:25:30 +00:00
|
|
|
let signature = Signature::new("signature").usage("example usage");
|
2021-09-04 07:45:49 +00:00
|
|
|
assert_eq!(signature.usage, "example usage".to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_signature_chained() {
|
|
|
|
let signature = Signature::new("new_signature")
|
2022-03-27 19:25:30 +00:00
|
|
|
.usage("description")
|
2021-09-04 07:45:49 +00:00
|
|
|
.required("required", SyntaxShape::String, "required description")
|
|
|
|
.optional("optional", SyntaxShape::String, "optional description")
|
|
|
|
.required_named(
|
2022-02-17 14:55:17 +00:00
|
|
|
"req-named",
|
2021-09-04 07:45:49 +00:00
|
|
|
SyntaxShape::String,
|
|
|
|
"required named description",
|
|
|
|
Some('r'),
|
|
|
|
)
|
|
|
|
.named("named", SyntaxShape::String, "named description", Some('n'))
|
|
|
|
.switch("switch", "switch description", None)
|
2021-09-07 03:37:02 +00:00
|
|
|
.rest("rest", SyntaxShape::String, "rest description");
|
2021-09-04 07:45:49 +00:00
|
|
|
|
|
|
|
assert_eq!(signature.required_positional.len(), 1);
|
|
|
|
assert_eq!(signature.optional_positional.len(), 1);
|
2022-05-29 13:14:15 +00:00
|
|
|
assert_eq!(signature.named.len(), 3);
|
2021-09-04 07:45:49 +00:00
|
|
|
assert!(signature.rest_positional.is_some());
|
2022-05-29 13:14:15 +00:00
|
|
|
assert_eq!(signature.get_shorts(), vec!['r', 'n']);
|
|
|
|
assert_eq!(signature.get_names(), vec!["req-named", "named", "switch"]);
|
2021-09-04 07:45:49 +00:00
|
|
|
assert_eq!(signature.num_positionals(), 2);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
signature.get_positional(0),
|
|
|
|
Some(PositionalArg {
|
|
|
|
name: "required".to_string(),
|
|
|
|
desc: "required description".to_string(),
|
|
|
|
shape: SyntaxShape::String,
|
2022-03-07 20:08:56 +00:00
|
|
|
var_id: None,
|
|
|
|
default_value: None,
|
2021-09-04 07:45:49 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
signature.get_positional(1),
|
|
|
|
Some(PositionalArg {
|
|
|
|
name: "optional".to_string(),
|
|
|
|
desc: "optional description".to_string(),
|
|
|
|
shape: SyntaxShape::String,
|
2022-03-07 20:08:56 +00:00
|
|
|
var_id: None,
|
|
|
|
default_value: None,
|
2021-09-04 07:45:49 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
signature.get_positional(2),
|
|
|
|
Some(PositionalArg {
|
|
|
|
name: "rest".to_string(),
|
|
|
|
desc: "rest description".to_string(),
|
|
|
|
shape: SyntaxShape::String,
|
2022-03-07 20:08:56 +00:00
|
|
|
var_id: None,
|
|
|
|
default_value: None,
|
2021-09-04 07:45:49 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2022-02-17 14:55:17 +00:00
|
|
|
signature.get_long_flag("req-named"),
|
2021-09-04 07:45:49 +00:00
|
|
|
Some(Flag {
|
2022-02-17 14:55:17 +00:00
|
|
|
long: "req-named".to_string(),
|
2021-09-04 07:45:49 +00:00
|
|
|
short: Some('r'),
|
|
|
|
arg: Some(SyntaxShape::String),
|
|
|
|
required: true,
|
|
|
|
desc: "required named description".to_string(),
|
2022-03-07 20:08:56 +00:00
|
|
|
var_id: None,
|
|
|
|
default_value: None,
|
2021-09-04 07:45:49 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
signature.get_short_flag('r'),
|
|
|
|
Some(Flag {
|
2022-02-17 14:55:17 +00:00
|
|
|
long: "req-named".to_string(),
|
2021-09-04 07:45:49 +00:00
|
|
|
short: Some('r'),
|
|
|
|
arg: Some(SyntaxShape::String),
|
|
|
|
required: true,
|
|
|
|
desc: "required named description".to_string(),
|
2022-03-07 20:08:56 +00:00
|
|
|
var_id: None,
|
|
|
|
default_value: None,
|
2021-09-04 07:45:49 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-12-04 14:06:27 +00:00
|
|
|
#[should_panic(expected = "There may be duplicate short flags for '-n'")]
|
2021-09-04 07:45:49 +00:00
|
|
|
fn test_signature_same_short() {
|
|
|
|
// Creating signature with same short name should panic
|
|
|
|
Signature::new("new_signature")
|
|
|
|
.required_named(
|
2022-02-17 14:55:17 +00:00
|
|
|
"required-named",
|
2021-09-04 07:45:49 +00:00
|
|
|
SyntaxShape::String,
|
|
|
|
"required named description",
|
|
|
|
Some('n'),
|
|
|
|
)
|
|
|
|
.named("named", SyntaxShape::String, "named description", Some('n'));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-12-04 14:06:27 +00:00
|
|
|
#[should_panic(expected = "There may be duplicate name flags for '--name'")]
|
2021-09-04 07:45:49 +00:00
|
|
|
fn test_signature_same_name() {
|
|
|
|
// Creating signature with same short name should panic
|
2022-02-17 14:55:17 +00:00
|
|
|
Signature::new("new-signature")
|
2021-09-04 07:45:49 +00:00
|
|
|
.required_named(
|
|
|
|
"name",
|
|
|
|
SyntaxShape::String,
|
|
|
|
"required named description",
|
|
|
|
Some('r'),
|
|
|
|
)
|
|
|
|
.named("name", SyntaxShape::String, "named description", Some('n'));
|
|
|
|
}
|
2021-11-19 02:51:42 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_signature_round_trip() {
|
|
|
|
let signature = Signature::new("new_signature")
|
2022-03-27 19:25:30 +00:00
|
|
|
.usage("description")
|
2021-11-19 02:51:42 +00:00
|
|
|
.required("first", SyntaxShape::String, "first required")
|
|
|
|
.required("second", SyntaxShape::Int, "second required")
|
|
|
|
.optional("optional", SyntaxShape::String, "optional description")
|
|
|
|
.required_named(
|
2022-02-17 14:55:17 +00:00
|
|
|
"req-named",
|
2021-11-19 02:51:42 +00:00
|
|
|
SyntaxShape::String,
|
|
|
|
"required named description",
|
|
|
|
Some('r'),
|
|
|
|
)
|
|
|
|
.named("named", SyntaxShape::String, "named description", Some('n'))
|
|
|
|
.switch("switch", "switch description", None)
|
|
|
|
.rest("rest", SyntaxShape::String, "rest description")
|
|
|
|
.category(nu_protocol::Category::Conversions);
|
|
|
|
|
|
|
|
let string = serde_json::to_string_pretty(&signature).unwrap();
|
|
|
|
let returned: Signature = serde_json::from_str(&string).unwrap();
|
|
|
|
|
|
|
|
assert_eq!(signature.name, returned.name);
|
|
|
|
assert_eq!(signature.usage, returned.usage);
|
|
|
|
assert_eq!(signature.extra_usage, returned.extra_usage);
|
|
|
|
assert_eq!(signature.is_filter, returned.is_filter);
|
|
|
|
assert_eq!(signature.category, returned.category);
|
|
|
|
|
|
|
|
signature
|
|
|
|
.required_positional
|
|
|
|
.iter()
|
|
|
|
.zip(returned.required_positional.iter())
|
|
|
|
.for_each(|(lhs, rhs)| assert_eq!(lhs, rhs));
|
|
|
|
|
|
|
|
signature
|
|
|
|
.optional_positional
|
|
|
|
.iter()
|
|
|
|
.zip(returned.optional_positional.iter())
|
|
|
|
.for_each(|(lhs, rhs)| assert_eq!(lhs, rhs));
|
|
|
|
|
|
|
|
signature
|
|
|
|
.named
|
|
|
|
.iter()
|
|
|
|
.zip(returned.named.iter())
|
|
|
|
.for_each(|(lhs, rhs)| assert_eq!(lhs, rhs));
|
|
|
|
|
|
|
|
assert_eq!(signature.rest_positional, returned.rest_positional,);
|
|
|
|
}
|