Skip to content

Commit

Permalink
Make {} variable have the same behavior as {0}.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronriekenberg committed May 27, 2024
1 parent 05e9079 commit b9b9c93
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 9 deletions.
54 changes: 54 additions & 0 deletions src/parser/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ impl CommandLineRegex {
if let (Some(match_value), Some(match_key)) =
(match_option, self.numbered_group_match_keys.get(i))
{
// make {} have the same behavior as {0}
if i == 0 {
update_argument("{}", match_value.as_str());
}
update_argument(match_key, match_value.as_str());
}
}
Expand Down Expand Up @@ -297,6 +301,31 @@ mod test {
);
}

#[test]
fn test_regex_numbered_groups_json_empty_group() {
let command_line_args = CommandLineArgs {
regex: Some("(.*),(.*)".to_string()),
..Default::default()
};

let regex_processor = RegexProcessor::new(&command_line_args).unwrap();

assert_eq!(regex_processor.regex_mode(), true);

let arguments =
vec![r#"{"id": 123, "$zero": "{}", "one": "{1}", "two": "{2}"}"#.to_string()];
assert_eq!(
regex_processor.apply_regex_to_arguments(&arguments, "hello,world",),
Some(ApplyRegexToArgumentsResult {
arguments: vec![
r#"{"id": 123, "$zero": "hello,world", "one": "hello", "two": "world"}"#
.to_string(),
],
modified_arguments: true,
})
);
}

#[test]
fn test_regex_named_groups_json() {
let command_line_args = CommandLineArgs {
Expand All @@ -322,6 +351,31 @@ mod test {
);
}

#[test]
fn test_regex_named_groups_json_empty_group() {
let command_line_args = CommandLineArgs {
regex: Some("(?P<arg1>.*),(?P<arg2>.*)".to_string()),
..Default::default()
};

let regex_processor = RegexProcessor::new(&command_line_args).unwrap();

assert_eq!(regex_processor.regex_mode(), true);

let arguments =
vec![r#"{"id": 123, "$zero": "{}", "one": "{arg1}", "two": "{arg2}"}"#.to_string()];
assert_eq!(
regex_processor.apply_regex_to_arguments(&arguments, "hello,world",),
Some(ApplyRegexToArgumentsResult {
arguments: vec![
r#"{"id": 123, "$zero": "hello,world", "one": "hello", "two": "world"}"#
.to_string()
],
modified_arguments: true,
})
);
}

#[test]
fn test_regex_string_containing_dollar_curly_brace_variable() {
let command_line_args = CommandLineArgs {
Expand Down
21 changes: 12 additions & 9 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,11 @@ fn runs_regex_from_input_file_j1() {
.arg("arg2={arg2}")
.arg("arg3={arg3}")
.arg("dollarzero={0}")
.arg("emptygroup={}")
.assert()
.success()
.stdout(predicate::eq(
"arg1=1 arg2=2 arg3=3 dollarzero=1,2,3\narg1=foo arg2=bar arg3=baz dollarzero=foo,bar,baz\n",
"arg1=1 arg2=2 arg3=3 dollarzero=1,2,3 emptygroup=1,2,3\narg1=foo arg2=bar arg3=baz dollarzero=foo,bar,baz emptygroup=foo,bar,baz\n",
))
.stderr(predicate::str::is_empty());
}
Expand Down Expand Up @@ -315,13 +316,14 @@ fn runs_regex_from_command_line_args_j1() {
.arg("arg2={2}")
.arg("arg3={3}")
.arg("dollarzero={0}")
.arg("emptygroup={}")
.arg(":::")
.arg("a,b,c")
.arg("d,e,f")
.assert()
.success()
.stdout(predicate::eq(
"arg1=a arg2=b arg3=c dollarzero=a,b,c\narg1=d arg2=e arg3=f dollarzero=d,e,f\n",
"arg1=a arg2=b arg3=c dollarzero=a,b,c emptygroup=a,b,c\narg1=d arg2=e arg3=f dollarzero=d,e,f emptygroup=d,e,f\n",
))
.stderr(predicate::str::is_empty());
}
Expand Down Expand Up @@ -375,6 +377,7 @@ fn runs_auto_regex_from_command_line_args_j1() {
.arg("arg1={1}")
.arg("arg2={2}")
.arg("dollarzero={0}")
.arg("emptygroup={}")
.arg(":::")
.arg("a")
.arg("b")
Expand All @@ -384,15 +387,15 @@ fn runs_auto_regex_from_command_line_args_j1() {
.assert()
.success()
.stdout(predicate::eq(
"arg1=a arg2=c dollarzero=a c\narg1=a arg2=d dollarzero=a d\narg1=b arg2=c dollarzero=b c\narg1=b arg2=d dollarzero=b d\n",
"arg1=a arg2=c dollarzero=a c emptygroup=a c\narg1=a arg2=d dollarzero=a d emptygroup=a d\narg1=b arg2=c dollarzero=b c emptygroup=b c\narg1=b arg2=d dollarzero=b d emptygroup=b d\n",
))
.stderr(predicate::str::is_empty());
}

#[test]
fn runs_regex_from_input_file_produce_json_named_groups_j1() {
let expected_stdout = r#"{"id": 123, "zero": "1,2,3", "one": "1", "two": "2", "three": "3"}
{"id": 123, "zero": "foo,bar,baz", "one": "foo", "two": "bar", "three": "baz"}
let expected_stdout = r#"{"id": 123, "zero": "1,2,3", "empty": "1,2,3", "one": "1", "two": "2", "three": "3"}
{"id": 123, "zero": "foo,bar,baz", "empty": "foo,bar,baz", "one": "foo", "two": "bar", "three": "baz"}
"#;

rust_parallel()
Expand All @@ -402,7 +405,7 @@ fn runs_regex_from_input_file_produce_json_named_groups_j1() {
.arg("-r")
.arg("(?P<arg1>.*),(?P<arg2>.*),(?P<arg3>.*)")
.arg("echo")
.arg(r#"{"id": 123, "zero": "{0}", "one": "{arg1}", "two": "{arg2}", "three": "{arg3}"}"#)
.arg(r#"{"id": 123, "zero": "{0}", "empty": "{}", "one": "{arg1}", "two": "{arg2}", "three": "{arg3}"}"#)
.assert()
.success()
.stdout(predicate::eq(expected_stdout))
Expand All @@ -411,8 +414,8 @@ fn runs_regex_from_input_file_produce_json_named_groups_j1() {

#[test]
fn runs_regex_from_input_file_produce_json_numbered_groups_j1() {
let expected_stdout = r#"{"id": 123, "zero": "1,2,3", "three": "3", "two": "2", "one": "1"}
{"id": 123, "zero": "foo,bar,baz", "three": "baz", "two": "bar", "one": "foo"}
let expected_stdout = r#"{"id": 123, "zero": "1,2,3", "empty": "1,2,3", "three": "3", "two": "2", "one": "1"}
{"id": 123, "zero": "foo,bar,baz", "empty": "foo,bar,baz", "three": "baz", "two": "bar", "one": "foo"}
"#;

rust_parallel()
Expand All @@ -422,7 +425,7 @@ fn runs_regex_from_input_file_produce_json_numbered_groups_j1() {
.arg("-r")
.arg("(.*),(.*),(.*)")
.arg("echo")
.arg(r#"{"id": 123, "zero": "{0}", "three": "{3}", "two": "{2}", "one": "{1}"}"#)
.arg(r#"{"id": 123, "zero": "{0}", "empty": "{}", "three": "{3}", "two": "{2}", "one": "{1}"}"#)
.assert()
.success()
.stdout(predicate::eq(expected_stdout))
Expand Down

0 comments on commit b9b9c93

Please sign in to comment.