mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 08:55:40 +02:00
Change string contains operators to regex (#5117)
This commit is contained in:
@ -9,6 +9,7 @@ mod test_math;
|
||||
mod test_modules;
|
||||
mod test_parser;
|
||||
mod test_ranges;
|
||||
mod test_regex;
|
||||
mod test_strings;
|
||||
mod test_table_operations;
|
||||
mod test_type_check;
|
||||
|
82
src/tests/test_regex.rs
Normal file
82
src/tests/test_regex.rs
Normal file
@ -0,0 +1,82 @@
|
||||
use crate::tests::{fail_test, run_test, TestResult};
|
||||
|
||||
#[test]
|
||||
fn contains() -> TestResult {
|
||||
run_test(r#"'foobarbaz' =~ bar"#, "true")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn contains_case_insensitive() -> TestResult {
|
||||
run_test(r#"'foobarbaz' =~ '(?i)BaR'"#, "true")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn not_contains() -> TestResult {
|
||||
run_test(r#"'foobarbaz' !~ asdf"#, "true")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn match_full_line() -> TestResult {
|
||||
run_test(r#"'foobarbaz' =~ '^foobarbaz$'"#, "true")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn not_match_full_line() -> TestResult {
|
||||
run_test(r#"'foobarbaz' !~ '^foobarbaz$'"#, "false")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn starts_with() -> TestResult {
|
||||
run_test(r#"'foobarbaz' =~ '^foo'"#, "true")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn not_starts_with() -> TestResult {
|
||||
run_test(r#"'foobarbaz' !~ '^foo'"#, "false")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ends_with() -> TestResult {
|
||||
run_test(r#"'foobarbaz' =~ 'baz$'"#, "true")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn not_ends_with() -> TestResult {
|
||||
run_test(r#"'foobarbaz' !~ 'baz$'"#, "false")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn where_works() -> TestResult {
|
||||
run_test(
|
||||
r#"[{name: somefile.txt} {name: anotherfile.csv }] | where name =~ ^s | get name.0"#,
|
||||
"somefile.txt",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn where_not_works() -> TestResult {
|
||||
run_test(
|
||||
r#"[{name: somefile.txt} {name: anotherfile.csv }] | where name !~ ^s | get name.0"#,
|
||||
"anotherfile.csv",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_regex_fails() -> TestResult {
|
||||
fail_test(r#"'foo' =~ '['"#, "regex parse error")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_not_regex_fails() -> TestResult {
|
||||
fail_test(r#"'foo' !~ '['"#, "regex parse error")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn regex_on_int_fails() -> TestResult {
|
||||
fail_test(r#"33 =~ foo"#, "Types mismatched")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn not_regex_on_int_fails() -> TestResult {
|
||||
fail_test(r#"33 !~ foo"#, "Types mismatched")
|
||||
}
|
Reference in New Issue
Block a user