first and get coverage.

This commit is contained in:
Andrés N. Robalino 2019-09-12 05:22:58 -05:00
parent 39489a75aa
commit 7838dac689
2 changed files with 129 additions and 2 deletions

View File

@ -7,6 +7,7 @@ pub struct Get;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct GetArgs { pub struct GetArgs {
member: Tagged<String>,
rest: Vec<Tagged<String>>, rest: Vec<Tagged<String>>,
} }
@ -16,7 +17,9 @@ impl WholeStreamCommand for Get {
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("get").rest(SyntaxType::Member) Signature::build("get")
.required("member", SyntaxType::Member)
.rest(SyntaxType::Member)
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@ -63,13 +66,24 @@ fn get_member(path: &Tagged<String>, obj: &Tagged<Value>) -> Result<Tagged<Value
} }
pub fn get( pub fn get(
GetArgs { rest: fields }: GetArgs, GetArgs {
member: member,
rest: fields,
}: GetArgs,
RunnableContext { input, .. }: RunnableContext, RunnableContext { input, .. }: RunnableContext,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
let stream = input let stream = input
.values .values
.map(move |item| { .map(move |item| {
let mut result = VecDeque::new(); let mut result = VecDeque::new();
let member = vec![member.clone()];
let fields = vec![&member, &fields]
.into_iter()
.flatten()
.collect::<Vec<&Tagged<String>>>();
for field in &fields { for field in &fields {
match get_member(field, &item) { match get_member(field, &item) {
Ok(Tagged { Ok(Tagged {

View File

@ -3,6 +3,119 @@ mod helpers;
use helpers as h; use helpers as h;
use helpers::{Playground, Stub::*}; use helpers::{Playground, Stub::*};
#[test]
fn first_gets_first_rows_by_amount() {
Playground::setup("first_test_1", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("los.1.txt"),
EmptyFile("tres.1.txt"),
EmptyFile("amigos.1.txt"),
EmptyFile("arepas.1.clu"),
]);
let actual = nu!(
cwd: dirs.test(), h::pipeline(
r#"
ls
| get name
| first 2
| split-column "."
| get Column2
| str --to-int
| sum
| echo $it
"#
));
assert_eq!(actual, "2");
})
}
#[test]
fn first_requires_an_amount() {
Playground::setup("first_test_2", |dirs, _| {
let actual = nu_error!(
cwd: dirs.test(), "ls | first"
);
assert!(actual.contains("requires amount parameter"));
})
}
#[test]
fn get() {
Playground::setup("get_test_1", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
nu_party_venue = "zion"
"#,
)]);
let actual = nu!(
cwd: dirs.test(), h::pipeline(
r#"
open sample.toml
| get nu_party_venue
| echo $it
"#
));
assert_eq!(actual, "zion");
})
}
#[test]
fn get_more_than_one_member() {
Playground::setup("get_test_2", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"sample.toml",
r#"
[[fortune_tellers]]
name = "Andrés N. Robalino"
arepas = 1
broken_builds = 0
[[fortune_tellers]]
name = "Jonathan Turner"
arepas = 1
broken_builds = 1
[[fortune_tellers]]
name = "Yehuda Katz"
arepas = 1
broken_builds = 1
"#,
)]);
let actual = nu!(
cwd: dirs.test(), h::pipeline(
r#"
open sample.toml
| get fortune_tellers
| get arepas broken_builds
| sum
| echo $it
"#
));
assert_eq!(actual, "5");
})
}
#[test]
fn get_requires_at_least_one_member() {
Playground::setup("first_test_3", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("andres.txt")]);
let actual = nu_error!(
cwd: dirs.test(), "ls | get"
);
assert!(actual.contains("requires member parameter"));
})
}
#[test] #[test]
fn lines() { fn lines() {
let actual = nu!( let actual = nu!(