mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 04:45:04 +02:00
Simplify parse plugin code. (#1904)
Primarily, instead of building a parse pattern enum, we just build the regex directly, with the appropriate capture group names so that the column name codepaths can be shared between simple and `--regex` patterns. Also removed capture group count compared to column name count. I don't think this codepath can possibly be reached with the regex we now use for the simplified capture form.
This commit is contained in:
@ -38,10 +38,10 @@ mod simple {
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
echo "{abc}123"
|
||||
| parse "{{abc}{name}"
|
||||
| echo $it.name
|
||||
"#
|
||||
echo "{abc}123"
|
||||
| parse "{{abc}{name}"
|
||||
| echo $it.name
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "123");
|
||||
@ -54,15 +54,48 @@ mod simple {
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
echo "(abc)123"
|
||||
| parse "(abc){name}"
|
||||
| echo $it.name
|
||||
"#
|
||||
echo "(abc)123"
|
||||
| parse "(abc){name}"
|
||||
| echo $it.name
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "123");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn properly_captures_empty_column() {
|
||||
Playground::setup("parse_test_regex_4", |dirs, _sandbox| {
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
echo ["1:INFO:component:all is well" "2:ERROR::something bad happened"]
|
||||
| parse "{timestamp}:{level}:{tag}:{entry}"
|
||||
| get entry
|
||||
| nth 1
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "something bad happened");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn errors_when_missing_closing_brace() {
|
||||
Playground::setup("parse_test_regex_5", |dirs, _sandbox| {
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
echo "(abc)123"
|
||||
| parse "(abc){name"
|
||||
| echo $it.name
|
||||
"#
|
||||
));
|
||||
|
||||
assert!(actual.err.contains("invalid parse pattern"));
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
mod regex {
|
||||
@ -86,12 +119,12 @@ mod regex {
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open nushell_git_log_oneline.txt
|
||||
| parse --regex "(?P<Hash>\w+) (?P<Message>.+) \(#(?P<PR>\d+)\)"
|
||||
| nth 1
|
||||
| get PR
|
||||
| echo $it
|
||||
"#
|
||||
open nushell_git_log_oneline.txt
|
||||
| parse --regex "(?P<Hash>\w+) (?P<Message>.+) \(#(?P<PR>\d+)\)"
|
||||
| nth 1
|
||||
| get PR
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "1842");
|
||||
@ -106,12 +139,12 @@ mod regex {
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open nushell_git_log_oneline.txt
|
||||
| parse --regex "(\w+) (.+) \(#(\d+)\)"
|
||||
| nth 1
|
||||
| get Capture1
|
||||
| echo $it
|
||||
"#
|
||||
open nushell_git_log_oneline.txt
|
||||
| parse --regex "(\w+) (.+) \(#(\d+)\)"
|
||||
| nth 1
|
||||
| get Capture1
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "b89976da");
|
||||
@ -126,15 +159,33 @@ mod regex {
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open nushell_git_log_oneline.txt
|
||||
| parse --regex "(?P<Hash>\w+) (.+) \(#(?P<PR>\d+)\)"
|
||||
| nth 1
|
||||
| get Capture2
|
||||
| echo $it
|
||||
"#
|
||||
open nushell_git_log_oneline.txt
|
||||
| parse --regex "(?P<Hash>\w+) (.+) \(#(?P<PR>\d+)\)"
|
||||
| nth 1
|
||||
| get Capture2
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "let format access variables also");
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn errors_with_invalid_regex() {
|
||||
Playground::setup("parse_test_regex_1", |dirs, sandbox| {
|
||||
sandbox.with_files(nushell_git_log_oneline());
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open nushell_git_log_oneline.txt
|
||||
| parse --regex "(?P<Hash>\w+ unfinished capture group"
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert!(actual.err.contains("invalid regex"));
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user