2023-05-11 20:39:21 +02:00
|
|
|
use nu_test_support::nu;
|
2021-08-27 10:48:41 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_with_list_search_with_string() {
|
2023-05-11 20:39:21 +02:00
|
|
|
let actual = nu!("[moe larry curly] | find moe | get 0");
|
2021-08-27 10:48:41 +02:00
|
|
|
|
2024-01-30 15:06:20 +01:00
|
|
|
assert_eq!(
|
|
|
|
actual.out,
|
|
|
|
"\u{1b}[37m\u{1b}[0m\u{1b}[41;37mmoe\u{1b}[0m\u{1b}[37m\u{1b}[0m"
|
|
|
|
);
|
2021-08-27 10:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_with_list_search_with_char() {
|
2023-05-11 20:39:21 +02:00
|
|
|
let actual = nu!("[moe larry curly] | find l | to json -r");
|
2021-08-27 10:48:41 +02:00
|
|
|
|
2024-03-20 22:14:31 +01:00
|
|
|
assert_eq!(actual.out, "[\"\\u001b[37m\\u001b[0m\\u001b[41;37ml\\u001b[0m\\u001b[37marry\\u001b[0m\",\"\\u001b[37mcur\\u001b[0m\\u001b[41;37ml\\u001b[0m\\u001b[37my\\u001b[0m\"]");
|
2021-08-27 10:48:41 +02:00
|
|
|
}
|
|
|
|
|
2024-06-27 16:46:10 +02:00
|
|
|
#[test]
|
|
|
|
fn find_with_bytestream_search_with_char() {
|
|
|
|
let actual =
|
|
|
|
nu!("\"ABC\" | save foo.txt; let res = open foo.txt | find abc; rm foo.txt; $res | get 0");
|
|
|
|
assert_eq!(
|
|
|
|
actual.out,
|
|
|
|
"\u{1b}[37m\u{1b}[0m\u{1b}[41;37mABC\u{1b}[0m\u{1b}[37m\u{1b}[0m"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-08-27 10:48:41 +02:00
|
|
|
#[test]
|
|
|
|
fn find_with_list_search_with_number() {
|
2023-05-11 20:39:21 +02:00
|
|
|
let actual = nu!("[1 2 3 4 5] | find 3 | get 0");
|
2021-08-27 10:48:41 +02:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "3");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_with_string_search_with_string() {
|
2023-05-11 20:39:21 +02:00
|
|
|
let actual = nu!("echo Cargo.toml | find toml");
|
2021-08-27 10:48:41 +02:00
|
|
|
|
2024-01-30 15:06:20 +01:00
|
|
|
assert_eq!(
|
|
|
|
actual.out,
|
|
|
|
"\u{1b}[37mCargo.\u{1b}[0m\u{1b}[41;37mtoml\u{1b}[0m\u{1b}[37m\u{1b}[0m"
|
|
|
|
);
|
2021-08-27 10:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_with_string_search_with_string_not_found() {
|
2023-05-11 20:39:21 +02:00
|
|
|
let actual = nu!("[moe larry curly] | find shemp | is-empty");
|
2021-08-27 10:48:41 +02:00
|
|
|
|
2023-02-22 17:18:33 +01:00
|
|
|
assert_eq!(actual.out, "true");
|
2021-08-27 10:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_with_filepath_search_with_string() {
|
2023-05-11 20:39:21 +02:00
|
|
|
let actual =
|
|
|
|
nu!(r#"["amigos.txt","arepas.clu","los.txt","tres.txt"] | find arep | to json -r"#);
|
|
|
|
|
2024-01-30 15:06:20 +01:00
|
|
|
assert_eq!(
|
|
|
|
actual.out,
|
2024-03-20 22:14:31 +01:00
|
|
|
"[\"\\u001b[37m\\u001b[0m\\u001b[41;37marep\\u001b[0m\\u001b[37mas.clu\\u001b[0m\"]"
|
2024-01-30 15:06:20 +01:00
|
|
|
);
|
2021-08-27 10:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_with_filepath_search_with_multiple_patterns() {
|
2023-05-11 20:39:21 +02:00
|
|
|
let actual =
|
|
|
|
nu!(r#"["amigos.txt","arepas.clu","los.txt","tres.txt"] | find arep ami | to json -r"#);
|
|
|
|
|
2024-03-20 22:14:31 +01:00
|
|
|
assert_eq!(actual.out, "[\"\\u001b[37m\\u001b[0m\\u001b[41;37mami\\u001b[0m\\u001b[37mgos.txt\\u001b[0m\",\"\\u001b[37m\\u001b[0m\\u001b[41;37marep\\u001b[0m\\u001b[37mas.clu\\u001b[0m\"]");
|
2021-08-27 10:48:41 +02:00
|
|
|
}
|
2023-01-20 00:07:34 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_takes_into_account_linebreaks_in_string() {
|
2023-05-11 20:39:21 +02:00
|
|
|
let actual = nu!(r#""atest\nanothertest\nnohit\n" | find a | length"#);
|
2023-01-20 00:07:34 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "2");
|
|
|
|
}
|
2023-05-11 20:39:21 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_with_regex_in_table_keeps_row_if_one_column_matches() {
|
|
|
|
let actual = nu!(
|
|
|
|
"[[name nickname]; [Maurice moe] [Laurence larry]] | find --regex ce | get name | to json -r"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, r#"["Maurice","Laurence"]"#);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inverted_find_with_regex_in_table_keeps_row_if_none_of_the_columns_matches() {
|
|
|
|
let actual = nu!(
|
|
|
|
"[[name nickname]; [Maurice moe] [Laurence larry]] | find --regex moe --invert | get name | to json -r"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, r#"["Laurence"]"#);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_in_table_only_keep_rows_with_matches_on_selected_columns() {
|
|
|
|
let actual = nu!(
|
|
|
|
"[[name nickname]; [Maurice moe] [Laurence larry]] | find r --columns [nickname] | get name | to json -r"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(actual.out.contains("Laurence"));
|
|
|
|
assert!(!actual.out.contains("Maurice"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inverted_find_in_table_keeps_row_if_none_of_the_selected_columns_matches() {
|
|
|
|
let actual = nu!(
|
|
|
|
"[[name nickname]; [Maurice moe] [Laurence larry]] | find r --columns [nickname] --invert | get name | to json -r"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(actual.out, r#"["Maurice"]"#);
|
|
|
|
}
|
2023-06-10 23:57:26 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_in_table_keeps_row_with_single_matched_and_keeps_other_columns() {
|
|
|
|
let actual = nu!("[[name nickname Age]; [Maurice moe 23] [Laurence larry 67] [William will 18]] | find Maurice");
|
|
|
|
|
|
|
|
println!("{:?}", actual.out);
|
|
|
|
assert!(actual.out.contains("moe"));
|
|
|
|
assert!(actual.out.contains("Maurice"));
|
|
|
|
assert!(actual.out.contains("23"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_in_table_keeps_row_with_multiple_matched_and_keeps_other_columns() {
|
|
|
|
let actual = nu!("[[name nickname Age]; [Maurice moe 23] [Laurence larry 67] [William will 18] [William bill 60]] | find moe William");
|
|
|
|
|
|
|
|
println!("{:?}", actual.out);
|
|
|
|
assert!(actual.out.contains("moe"));
|
|
|
|
assert!(actual.out.contains("Maurice"));
|
|
|
|
assert!(actual.out.contains("23"));
|
|
|
|
assert!(actual.out.contains("William"));
|
|
|
|
assert!(actual.out.contains("will"));
|
|
|
|
assert!(actual.out.contains("18"));
|
|
|
|
assert!(actual.out.contains("bill"));
|
|
|
|
assert!(actual.out.contains("60"));
|
|
|
|
}
|
2024-09-06 01:22:03 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_with_string_search_with_special_char_1() {
|
|
|
|
let actual = nu!("[[d]; [a?b] [a*b] [a{1}b] ] | find '?' | to json -r");
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
actual.out,
|
|
|
|
"[{\"d\":\"\\u001b[37ma\\u001b[0m\\u001b[41;37m?\\u001b[0m\\u001b[37mb\\u001b[0m\"}]"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_with_string_search_with_special_char_2() {
|
|
|
|
let actual = nu!("[[d]; [a?b] [a*b] [a{1}b]] | find '*' | to json -r");
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
actual.out,
|
|
|
|
"[{\"d\":\"\\u001b[37ma\\u001b[0m\\u001b[41;37m*\\u001b[0m\\u001b[37mb\\u001b[0m\"}]"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_with_string_search_with_special_char_3() {
|
|
|
|
let actual = nu!("[[d]; [a?b] [a*b] [a{1}b] ] | find '{1}' | to json -r");
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
actual.out,
|
|
|
|
"[{\"d\":\"\\u001b[37ma\\u001b[0m\\u001b[41;37m{1}\\u001b[0m\\u001b[37mb\\u001b[0m\"}]"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_with_string_search_with_special_char_4() {
|
|
|
|
let actual = nu!("[{d: a?b} {d: a*b} {d: a{1}b} {d: a[]b}] | find '[' | to json -r");
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
actual.out,
|
|
|
|
"[{\"d\":\"\\u001b[37ma\\u001b[0m\\u001b[41;37m[\\u001b[0m\\u001b[37m]b\\u001b[0m\"}]"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_with_string_search_with_special_char_5() {
|
|
|
|
let actual = nu!("[{d: a?b} {d: a*b} {d: a{1}b} {d: a[]b}] | find ']' | to json -r");
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
actual.out,
|
|
|
|
"[{\"d\":\"\\u001b[37ma[\\u001b[0m\\u001b[41;37m]\\u001b[0m\\u001b[37mb\\u001b[0m\"}]"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_with_string_search_with_special_char_6() {
|
|
|
|
let actual = nu!("[{d: a?b} {d: a*b} {d: a{1}b} {d: a[]b}] | find '[]' | to json -r");
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
actual.out,
|
|
|
|
"[{\"d\":\"\\u001b[37ma\\u001b[0m\\u001b[41;37m[]\\u001b[0m\\u001b[37mb\\u001b[0m\"}]"
|
|
|
|
);
|
|
|
|
}
|