working for comparing filepath to string (#2906)

* working for comparing filepath to string

* added tests
This commit is contained in:
Darren Schroeder 2021-01-10 21:41:19 -06:00 committed by GitHub
parent 93e8f6c05e
commit 231a445809
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 15 deletions

12
Cargo.lock generated
View File

@ -3156,18 +3156,6 @@ dependencies = [
"zip",
]
[[package]]
name = "nu-core-commands"
version = "0.25.1"
dependencies = [
"async-trait",
"eml-parser",
"nu-errors",
"nu-protocol",
"nu-source",
"serde 1.0.118",
]
[[package]]
name = "nu-data"
version = "0.25.1"

View File

@ -1,4 +1,4 @@
use nu_data::value;
use nu_data::{value, value::compare_values};
use nu_errors::ShellError;
use nu_protocol::hir::Operator;
use nu_protocol::{Primitive, ShellTypeName, UntaggedValue, Value};
@ -79,9 +79,15 @@ fn table_contains(
left: &UntaggedValue,
right: &UntaggedValue,
) -> Result<bool, (&'static str, &'static str)> {
let left = left.clone();
match right {
UntaggedValue::Table(values) => Ok(values.iter().any(|x| x.value == left)),
UntaggedValue::Table(values) => {
Ok(values
.iter()
.any(|x| match compare_values(Operator::Equal, &left, &x.value) {
Ok(coerced) => coerced,
_ => false,
}))
}
_ => Err((left.type_name(), right.type_name())),
}
}

View File

@ -0,0 +1 @@
mod operator;

View File

@ -0,0 +1,38 @@
use nu_test_support::fs::Stub::EmptyFile;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};
#[test]
fn filter_ls_by_in_array() {
Playground::setup("filter_ls_by_1", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("jean-luc.cap"),
EmptyFile("riker.cmdr"),
EmptyFile("data.ltcmdr"),
EmptyFile("troi.ltcmdr"),
EmptyFile("worf.lt"),
EmptyFile("geordi.lt"),
]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
ls | where name in ['data.ltcmdr', 'riker.cmdr'] | get name | to json
"#
));
assert_eq!(actual.out, "[\"data.ltcmdr\",\"riker.cmdr\"]");
})
}
#[test]
fn filter_json_with_in_array() {
let actual = nu!(
cwd: ".", pipeline(
r#"
echo '[{"name": "foo", "size": 3}, {"name": "foo", "size": 2}, {"name": "bar", "size": 4}]' | from json | where size in [2] | get name
"#
));
assert_eq!(actual.out, "foo");
}

View File

@ -0,0 +1,3 @@
extern crate nu_test_support;
mod evaluate;