2020-05-22 14:13:58 +00:00
|
|
|
use nu_test_support::fs::Stub;
|
2020-01-10 15:44:24 +00:00
|
|
|
use nu_test_support::playground::Playground;
|
2019-12-17 18:54:39 +00:00
|
|
|
use nu_test_support::{nu, pipeline};
|
2019-12-15 16:15:06 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extracts_fields_from_the_given_the_pattern() {
|
2020-01-10 15:44:24 +00:00
|
|
|
Playground::setup("parse_test_1", |dirs, sandbox| {
|
2020-05-22 14:13:58 +00:00
|
|
|
sandbox.with_files(vec![Stub::FileWithContentToBeTrimmed(
|
2020-01-10 15:44:24 +00:00
|
|
|
"key_value_separated_arepa_ingredients.txt",
|
|
|
|
r#"
|
|
|
|
VAR1=Cheese
|
|
|
|
VAR2=JonathanParsed
|
|
|
|
VAR3=NushellSecretIngredient
|
|
|
|
"#,
|
|
|
|
)]);
|
2019-12-15 16:15:06 +00:00
|
|
|
|
2020-01-10 15:44:24 +00:00
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
open key_value_separated_arepa_ingredients.txt
|
|
|
|
| parse "{Name}={Value}"
|
|
|
|
| nth 1
|
|
|
|
| get Value
|
|
|
|
| echo $it
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 11:03:43 +00:00
|
|
|
assert_eq!(actual.out, "JonathanParsed");
|
2020-01-10 15:44:24 +00:00
|
|
|
})
|
2019-12-15 16:15:06 +00:00
|
|
|
}
|
2020-05-22 14:13:58 +00:00
|
|
|
|
|
|
|
mod regex {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
fn nushell_git_log_oneline<'a>() -> Vec<Stub<'a>> {
|
|
|
|
vec![Stub::FileWithContentToBeTrimmed(
|
|
|
|
"nushell_git_log_oneline.txt",
|
|
|
|
r#"
|
|
|
|
ae87582c Fix missing invocation errors (#1846)
|
|
|
|
b89976da let format access variables also (#1842)
|
|
|
|
"#,
|
|
|
|
)]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extracts_fields_with_all_named_groups() {
|
|
|
|
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+) (?P<Message>.+) \(#(?P<PR>\d+)\)"
|
|
|
|
| nth 1
|
|
|
|
| get PR
|
|
|
|
| echo $it
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "1842");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extracts_fields_with_all_unnamed_groups() {
|
|
|
|
Playground::setup("parse_test_regex_2", |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 "(\w+) (.+) \(#(\d+)\)"
|
|
|
|
| nth 1
|
|
|
|
| get Capture1
|
|
|
|
| echo $it
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "b89976da");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extracts_fields_with_named_and_unnamed_groups() {
|
|
|
|
Playground::setup("parse_test_regex_3", |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+) (.+) \(#(?P<PR>\d+)\)"
|
|
|
|
| nth 1
|
|
|
|
| get Capture2
|
|
|
|
| echo $it
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "let format access variables also");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|