2020-05-22 16:13:58 +02:00
|
|
|
use nu_test_support::fs::Stub;
|
2020-01-10 16:44:24 +01:00
|
|
|
use nu_test_support::playground::Playground;
|
2019-12-17 19:54:39 +01:00
|
|
|
use nu_test_support::{nu, pipeline};
|
2019-12-15 17:15:06 +01:00
|
|
|
|
2020-05-25 20:19:49 +02:00
|
|
|
mod simple {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extracts_fields_from_the_given_the_pattern() {
|
|
|
|
Playground::setup("parse_test_1", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![Stub::FileWithContentToBeTrimmed(
|
|
|
|
"key_value_separated_arepa_ingredients.txt",
|
|
|
|
r#"
|
|
|
|
VAR1=Cheese
|
2023-03-15 06:54:55 +01:00
|
|
|
VAR2=JTParsed
|
2020-05-25 20:19:49 +02:00
|
|
|
VAR3=NushellSecretIngredient
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
|
|
|
open key_value_separated_arepa_ingredients.txt
|
|
|
|
| lines
|
2022-02-17 12:40:24 +01:00
|
|
|
| each { |it| echo $it | parse "{Name}={Value}" }
|
2022-02-16 19:24:45 +01:00
|
|
|
| flatten
|
|
|
|
| get 1
|
2020-10-26 07:55:52 +01:00
|
|
|
| get Value
|
2020-05-25 20:19:49 +02:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2023-03-15 06:54:55 +01:00
|
|
|
assert_eq!(actual.out, "JTParsed");
|
2020-05-25 20:19:49 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-01-15 03:03:32 +01:00
|
|
|
fn double_open_curly_evaluates_to_a_single_curly() {
|
2020-05-25 20:19:49 +02:00
|
|
|
Playground::setup("parse_test_regex_2", |dirs, _sandbox| {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
2020-05-28 15:58:06 +02:00
|
|
|
echo "{abc}123"
|
|
|
|
| parse "{{abc}{name}"
|
2022-02-09 11:58:54 +01:00
|
|
|
| get name.0
|
2020-05-28 15:58:06 +02:00
|
|
|
"#
|
2020-05-25 20:19:49 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "123");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn properly_escapes_text() {
|
|
|
|
Playground::setup("parse_test_regex_3", |dirs, _sandbox| {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(), pipeline(
|
|
|
|
r#"
|
2020-05-28 15:58:06 +02:00
|
|
|
echo "(abc)123"
|
|
|
|
| parse "(abc){name}"
|
2022-02-09 11:58:54 +01:00
|
|
|
| get name.0
|
2020-05-28 15:58:06 +02:00
|
|
|
"#
|
2020-05-25 20:19:49 +02:00
|
|
|
));
|
2020-01-10 16:44:24 +01:00
|
|
|
|
2020-05-25 20:19:49 +02:00
|
|
|
assert_eq!(actual.out, "123");
|
|
|
|
})
|
|
|
|
}
|
2020-05-28 15:58:06 +02:00
|
|
|
|
|
|
|
#[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
|
2022-02-09 11:58:54 +01:00
|
|
|
| get 1
|
2020-05-28 15:58:06 +02:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
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"
|
2020-10-26 07:55:52 +01:00
|
|
|
| get name
|
2020-05-28 15:58:06 +02:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2022-02-16 19:24:45 +01:00
|
|
|
assert!(actual
|
|
|
|
.err
|
|
|
|
.contains("Found opening `{` without an associated closing `}`"));
|
2020-05-28 15:58:06 +02:00
|
|
|
})
|
|
|
|
}
|
2019-12-15 17:15:06 +01:00
|
|
|
}
|
2020-05-22 16:13:58 +02: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#"
|
2020-05-28 15:58:06 +02:00
|
|
|
open nushell_git_log_oneline.txt
|
2022-03-03 19:14:03 +01:00
|
|
|
| parse --regex "(?P<Hash>\\w+) (?P<Message>.+) \\(#(?P<PR>\\d+)\\)"
|
2022-02-09 11:58:54 +01:00
|
|
|
| get 1
|
2020-05-28 15:58:06 +02:00
|
|
|
| get PR
|
|
|
|
"#
|
2020-05-22 16:13:58 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
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#"
|
2020-05-28 15:58:06 +02:00
|
|
|
open nushell_git_log_oneline.txt
|
2022-03-03 19:14:03 +01:00
|
|
|
| parse --regex "(\\w+) (.+) \\(#(\\d+)\\)"
|
2022-02-09 11:58:54 +01:00
|
|
|
| get 1
|
2023-01-29 14:34:34 +01:00
|
|
|
| get capture0
|
2020-05-28 15:58:06 +02:00
|
|
|
"#
|
2020-05-22 16:13:58 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
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#"
|
2020-05-28 15:58:06 +02:00
|
|
|
open nushell_git_log_oneline.txt
|
2022-03-03 19:14:03 +01:00
|
|
|
| parse --regex "(?P<Hash>\\w+) (.+) \\(#(?P<PR>\\d+)\\)"
|
2022-02-09 11:58:54 +01:00
|
|
|
| get 1
|
2023-01-29 14:34:34 +01:00
|
|
|
| get capture1
|
2020-05-28 15:58:06 +02:00
|
|
|
"#
|
2020-05-22 16:13:58 +02:00
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "let format access variables also");
|
|
|
|
})
|
|
|
|
}
|
2020-05-28 15:58:06 +02:00
|
|
|
|
|
|
|
#[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
|
2022-03-03 19:14:03 +01:00
|
|
|
| parse --regex "(?P<Hash>\\w+ unfinished capture group"
|
2020-05-28 15:58:06 +02:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2022-08-04 21:51:02 +02:00
|
|
|
assert!(actual
|
|
|
|
.err
|
|
|
|
.contains("Opening parenthesis without closing parenthesis"));
|
2020-05-28 15:58:06 +02:00
|
|
|
})
|
|
|
|
}
|
2023-02-09 03:59:02 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_works_with_streaming() {
|
2023-07-17 18:43:51 +02:00
|
|
|
let actual =
|
|
|
|
nu!(r#"seq char a z | each {|c| $c + " a"} | parse '{letter} {a}' | describe"#);
|
2023-02-09 03:59:02 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "table<letter: string, a: string> (stream)")
|
|
|
|
}
|
2023-08-06 13:17:03 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_does_not_truncate_list_streams() {
|
|
|
|
let actual = nu!(pipeline(
|
|
|
|
r#"
|
|
|
|
[a b c]
|
|
|
|
| each {|x| $x}
|
|
|
|
| parse --regex "[ac]"
|
|
|
|
| length
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "2");
|
|
|
|
}
|
2023-08-08 13:48:13 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_handles_external_stream_chunking() {
|
2023-09-13 09:52:04 +02:00
|
|
|
Playground::setup("parse_test_streaming_1", |dirs, sandbox| {
|
2023-09-16 21:49:10 +02:00
|
|
|
let data: String = "abcdefghijklmnopqrstuvwxyz".repeat(1000);
|
2023-09-13 09:52:04 +02:00
|
|
|
sandbox.with_files(vec![Stub::FileWithContent("data.txt", &data)]);
|
|
|
|
|
2023-08-08 13:48:13 +02:00
|
|
|
let actual = nu!(
|
2023-09-13 09:52:04 +02:00
|
|
|
cwd: dirs.test(),
|
|
|
|
r#"open data.txt | parse --regex "(abcdefghijklmnopqrstuvwxyz)" | length"#
|
|
|
|
);
|
2023-08-08 13:48:13 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "1000");
|
|
|
|
})
|
|
|
|
}
|
2020-05-22 16:13:58 +02:00
|
|
|
}
|