Add (near) automatic testing for command examples (#1777)

This commit is contained in:
Jason Gedge 2020-05-18 08:56:01 -04:00 committed by GitHub
parent 3fc4a9f142
commit acf13a6fcf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
119 changed files with 1935 additions and 282 deletions

View file

@ -376,9 +376,7 @@ pub fn create_default_context(
#[cfg(feature = "clipboard")] #[cfg(feature = "clipboard")]
{ {
context.add_commands(vec![whole_stream_command( context.add_commands(vec![whole_stream_command(crate::commands::clip::Clip)]);
crate::commands::clip::clipboard::Clip,
)]);
} }
} }

View file

@ -12,6 +12,7 @@ pub(crate) mod cal;
pub(crate) mod calc; pub(crate) mod calc;
pub(crate) mod cd; pub(crate) mod cd;
pub(crate) mod classified; pub(crate) mod classified;
#[cfg(feature = "clipboard")]
pub(crate) mod clip; pub(crate) mod clip;
pub(crate) mod command; pub(crate) mod command;
pub(crate) mod compact; pub(crate) mod compact;

View file

@ -43,15 +43,17 @@ impl WholeStreamCommand for Alias {
alias(args, registry) alias(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "An alias without parameters", description: "An alias without parameters",
example: "alias say-hi [] { echo 'Hello!' }", example: "alias say-hi [] { echo 'Hello!' }",
result: None,
}, },
Example { Example {
description: "An alias with a single parameter", description: "An alias with a single parameter",
example: "alias l [x] { ls $x }", example: "alias l [x] { ls $x }",
result: None,
}, },
] ]
} }
@ -74,3 +76,15 @@ pub fn alias(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStre
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Alias;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Alias {})
}
}

View file

@ -2,7 +2,7 @@ use crate::commands::WholeStreamCommand;
use crate::context::CommandRegistry; use crate::context::CommandRegistry;
use crate::prelude::*; use crate::prelude::*;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, Value}; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
#[derive(Deserialize)] #[derive(Deserialize)]
struct AppendArgs { struct AppendArgs {
@ -36,10 +36,16 @@ impl WholeStreamCommand for Append {
append(args, registry) append(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[Example { vec![Example {
description: "Add something to the end of a list or table", description: "Add something to the end of a list or table",
example: "echo [1 2 3] | append 4", example: "echo [1 2 3] | append 4",
result: Some(vec![
UntaggedValue::int(1).into(),
UntaggedValue::int(2).into(),
UntaggedValue::int(3).into(),
UntaggedValue::int(4).into(),
]),
}] }]
} }
} }
@ -58,3 +64,15 @@ fn append(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream,
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Append;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Append {})
}
}

View file

@ -38,15 +38,17 @@ impl WholeStreamCommand for Autoview {
}) })
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "Automatically view the results", description: "Automatically view the results",
example: "ls | autoview", example: "ls | autoview",
result: None,
}, },
Example { Example {
description: "Autoview is also implied. The above can be written as", description: "Autoview is also implied. The above can be written as",
example: "ls", example: "ls",
result: None,
}, },
] ]
} }
@ -336,3 +338,15 @@ fn create_default_command_args(context: &RunnableContextWithoutInput) -> RawComm
}, },
} }
} }
#[cfg(test)]
mod tests {
use super::Autoview;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Autoview {})
}
}

View file

@ -44,15 +44,17 @@ impl WholeStreamCommand for Cal {
cal(args, registry) cal(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "This month's calendar", description: "This month's calendar",
example: "cal", example: "cal",
result: None,
}, },
Example { Example {
description: "The calendar for all of 2012", description: "The calendar for all of 2012",
example: "cal --full-year 2012", example: "cal --full-year 2012",
result: None,
}, },
] ]
} }
@ -340,3 +342,15 @@ fn add_month_to_table(
Ok(()) Ok(())
} }
#[cfg(test)]
mod tests {
use super::Cal;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Cal {})
}
}

View file

@ -22,10 +22,11 @@ impl WholeStreamCommand for Calc {
calc(args, registry) calc(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[Example { vec![Example {
description: "Calculate math in the pipeline", description: "Calculate math in the pipeline",
example: "echo '10 / 4' | calc", example: "echo '10 / 4' | calc",
result: Some(vec![UntaggedValue::decimal(2.5).into()]),
}] }]
} }
} }
@ -70,3 +71,15 @@ pub fn parse(math_expression: &str, tag: impl Into<Tag>) -> Result<Value, String
Err(error) => Err(error.to_string()), Err(error) => Err(error.to_string()),
} }
} }
#[cfg(test)]
mod tests {
use super::Calc;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Calc {})
}
}

View file

@ -39,23 +39,27 @@ impl WholeStreamCommand for Cd {
cd(args, registry) cd(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "Change to a new directory called 'dirname'", description: "Change to a new directory called 'dirname'",
example: "cd dirname", example: "cd dirname",
result: None,
}, },
Example { Example {
description: "Change to your home directory", description: "Change to your home directory",
example: "cd", example: "cd",
result: None,
}, },
Example { Example {
description: "Change to your home directory (alternate version)", description: "Change to your home directory (alternate version)",
example: "cd ~", example: "cd ~",
result: None,
}, },
Example { Example {
description: "Change to the previous directory", description: "Change to the previous directory",
example: "cd -", example: "cd -",
result: None,
}, },
] ]
} }
@ -76,3 +80,15 @@ fn cd(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, She
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Cd;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Cd {})
}
}

View file

@ -10,12 +10,15 @@ impl WholeStreamCommand for Clear {
fn name(&self) -> &str { fn name(&self) -> &str {
"clear" "clear"
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("clear") Signature::build("clear")
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
"clears the terminal" "clears the terminal"
} }
fn run( fn run(
&self, &self,
args: CommandArgs, args: CommandArgs,
@ -23,13 +26,16 @@ impl WholeStreamCommand for Clear {
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
clear(args, registry) clear(args, registry)
} }
fn examples(&self) -> &[Example] {
&[Example { fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Clear the screen", description: "Clear the screen",
example: "clear", example: "clear",
result: None,
}] }]
} }
} }
fn clear(_args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> { fn clear(_args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
if cfg!(windows) { if cfg!(windows) {
Command::new("cmd") Command::new("cmd")
@ -44,3 +50,15 @@ fn clear(_args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream
} }
Ok(OutputStream::empty()) Ok(OutputStream::empty())
} }
#[cfg(test)]
mod tests {
use super::Clear;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Clear {})
}
}

View file

@ -1,112 +1,119 @@
#[cfg(feature = "clipboard")] use crate::commands::WholeStreamCommand;
pub mod clipboard { use crate::context::CommandRegistry;
use crate::commands::WholeStreamCommand; use crate::prelude::*;
use crate::context::CommandRegistry; use futures::stream::StreamExt;
use crate::prelude::*; use nu_errors::ShellError;
use futures::stream::StreamExt; use nu_protocol::{ReturnValue, Signature, Value};
use nu_errors::ShellError;
use nu_protocol::{ReturnValue, Signature, Value};
use clipboard::{ClipboardContext, ClipboardProvider}; use clipboard::{ClipboardContext, ClipboardProvider};
pub struct Clip; pub struct Clip;
impl WholeStreamCommand for Clip { impl WholeStreamCommand for Clip {
fn name(&self) -> &str { fn name(&self) -> &str {
"clip" "clip"
}
fn signature(&self) -> Signature {
Signature::build("clip")
}
fn usage(&self) -> &str {
"Copy the contents of the pipeline to the copy/paste buffer"
}
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
clip(args, registry)
}
fn examples(&self) -> &[Example] {
&[Example {
description: "Save text to the clipboard",
example: "echo 'secret value' | clip",
}]
}
} }
pub fn clip( fn signature(&self) -> Signature {
Signature::build("clip")
}
fn usage(&self) -> &str {
"Copy the contents of the pipeline to the copy/paste buffer"
}
fn run(
&self,
args: CommandArgs, args: CommandArgs,
_registry: &CommandRegistry, registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
let stream = async_stream! { clip(args, registry)
let mut input = args.input;
let name = args.call_info.name_tag.clone();
let values: Vec<Value> = input.collect().await;
let mut clip_stream = inner_clip(values, name).await;
while let Some(value) = clip_stream.next().await {
yield value;
}
};
let stream: BoxStream<'static, ReturnValue> = stream.boxed();
Ok(OutputStream::from(stream))
} }
async fn inner_clip(input: Vec<Value>, name: Tag) -> OutputStream { fn examples(&self) -> Vec<Example> {
if let Ok(clip_context) = ClipboardProvider::new() { vec![Example {
let mut clip_context: ClipboardContext = clip_context; description: "Save text to the clipboard",
let mut new_copy_data = String::new(); example: "echo 'secret value' | clip",
result: None,
if !input.is_empty() { }]
let mut first = true; }
for i in input.iter() { }
if !first {
new_copy_data.push_str("\n"); pub fn clip(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
} else { let stream = async_stream! {
first = false; let mut input = args.input;
} let name = args.call_info.name_tag.clone();
let values: Vec<Value> = input.collect().await;
let string: String = match i.as_string() {
Ok(string) => string.to_string(), let mut clip_stream = inner_clip(values, name).await;
Err(_) => { while let Some(value) = clip_stream.next().await {
return OutputStream::one(Err(ShellError::labeled_error( yield value;
"Given non-string data", }
"expected strings from pipeline", };
name,
))) let stream: BoxStream<'static, ReturnValue> = stream.boxed();
}
}; Ok(OutputStream::from(stream))
}
new_copy_data.push_str(&string);
} async fn inner_clip(input: Vec<Value>, name: Tag) -> OutputStream {
} if let Ok(clip_context) = ClipboardProvider::new() {
let mut clip_context: ClipboardContext = clip_context;
match clip_context.set_contents(new_copy_data) { let mut new_copy_data = String::new();
Ok(_) => {}
Err(_) => { if !input.is_empty() {
return OutputStream::one(Err(ShellError::labeled_error( let mut first = true;
"Could not set contents of clipboard", for i in input.iter() {
"could not set contents of clipboard", if !first {
name, new_copy_data.push_str("\n");
))); } else {
} first = false;
} }
OutputStream::empty() let string: String = match i.as_string() {
} else { Ok(string) => string.to_string(),
OutputStream::one(Err(ShellError::labeled_error( Err(_) => {
"Could not open clipboard", return OutputStream::one(Err(ShellError::labeled_error(
"could not open clipboard", "Given non-string data",
name, "expected strings from pipeline",
))) name,
} )))
}
};
new_copy_data.push_str(&string);
}
}
match clip_context.set_contents(new_copy_data) {
Ok(_) => {}
Err(_) => {
return OutputStream::one(Err(ShellError::labeled_error(
"Could not set contents of clipboard",
"could not set contents of clipboard",
name,
)));
}
}
OutputStream::empty()
} else {
OutputStream::one(Err(ShellError::labeled_error(
"Could not open clipboard",
"could not open clipboard",
name,
)))
}
}
#[cfg(test)]
mod tests {
use super::Clip;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Clip {})
} }
} }

View file

@ -272,6 +272,7 @@ impl EvaluatedCommandArgs {
pub struct Example { pub struct Example {
pub example: &'static str, pub example: &'static str,
pub description: &'static str, pub description: &'static str,
pub result: Option<Vec<Value>>,
} }
pub trait WholeStreamCommand: Send + Sync { pub trait WholeStreamCommand: Send + Sync {
@ -293,8 +294,8 @@ pub trait WholeStreamCommand: Send + Sync {
false false
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[] Vec::new()
} }
} }

View file

@ -34,11 +34,23 @@ impl WholeStreamCommand for Compact {
compact(args, registry) compact(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[Example { vec![
description: "Remove all directory entries, except those with a 'target'", Example {
example: "ls -af | compact target", description: "Filter out all null entries in a list",
}] example: "echo [1 2 $null 3 $null $null] | compact target",
result: Some(vec![
UntaggedValue::int(1).into(),
UntaggedValue::int(2).into(),
UntaggedValue::int(3).into(),
]),
},
Example {
description: "Filter out all directory entries having no 'target'",
example: "ls -af | compact target",
result: None,
},
]
} }
} }
@ -68,3 +80,15 @@ pub fn compact(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputSt
}; };
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Compact;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Compact {})
}
}

View file

@ -73,35 +73,42 @@ impl WholeStreamCommand for Config {
config(args, registry) config(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "See all config values", description: "See all config values",
example: "config", example: "config",
result: None,
}, },
Example { Example {
description: "Set completion_mode to circular", description: "Set completion_mode to circular",
example: "config --set [completion_mode circular]", example: "config --set [completion_mode circular]",
result: None,
}, },
Example { Example {
description: "Store the contents of the pipeline as a path", description: "Store the contents of the pipeline as a path",
example: "echo ['/usr/bin' '/bin'] | config --set_into path", example: "echo ['/usr/bin' '/bin'] | config --set_into path",
result: None,
}, },
Example { Example {
description: "Get the current startup commands", description: "Get the current startup commands",
example: "config --get startup", example: "config --get startup",
result: None,
}, },
Example { Example {
description: "Remove the startup commands", description: "Remove the startup commands",
example: "config --remove startup", example: "config --remove startup",
result: None,
}, },
Example { Example {
description: "Clear the config (be careful!)", description: "Clear the config (be careful!)",
example: "config --clear", example: "config --clear",
result: None,
}, },
Example { Example {
description: "Get the path to the current config file", description: "Get the path to the current config file",
example: "config --path", example: "config --path",
result: None,
}, },
] ]
} }
@ -220,3 +227,15 @@ pub fn config(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStr
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Config;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Config {})
}
}

View file

@ -28,10 +28,11 @@ impl WholeStreamCommand for Count {
count(args, registry) count(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[Example { vec![Example {
description: "Count the number of files/directories in the current directory", description: "Count the number of entries in a list",
example: "ls | count", example: "echo [1 2 3 4 5] | count",
result: Some(vec![UntaggedValue::int(5).into()]),
}] }]
} }
} }
@ -46,3 +47,15 @@ pub fn count(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStr
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Count;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Count {})
}
}

View file

@ -43,15 +43,17 @@ impl WholeStreamCommand for Cpy {
cp(args, registry) cp(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "Copy myfile to dir_b", description: "Copy myfile to dir_b",
example: "cp myfile dir_b", example: "cp myfile dir_b",
result: None,
}, },
Example { Example {
description: "Recursively copy dir_a to dir_b", description: "Recursively copy dir_a to dir_b",
example: "cp -r dir_a dir_b", example: "cp -r dir_a dir_b",
result: None,
}, },
] ]
} }
@ -72,3 +74,15 @@ pub fn cp(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream,
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Cpy;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Cpy {})
}
}

View file

@ -34,15 +34,17 @@ impl WholeStreamCommand for Date {
date(args, registry) date(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "Get the current local time and date", description: "Get the current local time and date",
example: "date", example: "date",
result: None,
}, },
Example { Example {
description: "Get the current UTC time and date", description: "Get the current UTC time and date",
example: "date --utc", example: "date --utc",
result: None,
}, },
] ]
} }
@ -108,3 +110,15 @@ pub fn date(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStrea
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Date;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Date {})
}
}

View file

@ -49,3 +49,15 @@ fn debug_value(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputSt
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Debug;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Debug {})
}
}

View file

@ -41,10 +41,11 @@ impl WholeStreamCommand for Default {
default(args, registry) default(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[Example { vec![Example {
description: "Give a default 'target' to all file entries", description: "Give a default 'target' to all file entries",
example: "ls -af | default target 'nothing'", example: "ls -af | default target 'nothing'",
result: None,
}] }]
} }
} }
@ -76,3 +77,15 @@ fn default(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Default;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Default {})
}
}

View file

@ -2,7 +2,7 @@ use crate::commands::WholeStreamCommand;
use crate::context::CommandRegistry; use crate::context::CommandRegistry;
use crate::prelude::*; use crate::prelude::*;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, Value}; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::Tagged; use nu_source::Tagged;
pub struct Drop; pub struct Drop;
@ -37,15 +37,20 @@ impl WholeStreamCommand for Drop {
drop(args, registry) drop(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "Remove the last item of a list/table", description: "Remove the last item of a list/table",
example: "echo [1 2 3] | drop", example: "echo [1 2 3] | drop",
result: Some(vec![
UntaggedValue::int(1).into(),
UntaggedValue::int(2).into(),
]),
}, },
Example { Example {
description: "Remove the last 2 items of a list/table", description: "Remove the last 2 items of a list/table",
example: "echo [1 2 3] | drop 2", example: "echo [1 2 3] | drop 2",
result: Some(vec![UntaggedValue::int(1).into()]),
}, },
] ]
} }
@ -73,3 +78,15 @@ fn drop(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, S
}; };
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Drop;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Drop {})
}
}

View file

@ -79,10 +79,11 @@ impl WholeStreamCommand for Du {
du(args, registry) du(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[Example { vec![Example {
description: "Disk usage of the current directory", description: "Disk usage of the current directory",
example: "du *", example: "du",
result: None,
}] }]
} }
} }
@ -404,3 +405,15 @@ impl From<FileInfo> for Value {
UntaggedValue::row(r).retag(&f.tag) UntaggedValue::row(r).retag(&f.tag)
} }
} }
#[cfg(test)]
mod tests {
use super::Du;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Du {})
}
}

View file

@ -7,7 +7,7 @@ use futures::stream::once;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ use nu_protocol::{
hir::Block, hir::Expression, hir::SpannedExpression, hir::Synthetic, ReturnSuccess, Signature, hir::Block, hir::Expression, hir::SpannedExpression, hir::Synthetic, ReturnSuccess, Signature,
SyntaxShape, SyntaxShape, UntaggedValue,
}; };
pub struct Each; pub struct Each;
@ -42,11 +42,26 @@ impl WholeStreamCommand for Each {
each(args, registry) each(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[Example { vec![
description: "Print the name of each file", Example {
example: "ls | each { echo $it.name }", description: "Echo the square of each integer",
}] example: "echo [1 2 3] | each { echo $(= $it * $it) }",
result: Some(vec![
UntaggedValue::int(1).into(),
UntaggedValue::int(4).into(),
UntaggedValue::int(9).into(),
]),
},
Example {
description: "Echo the sum of each row",
example: "echo [[1 2] [3 4]] | each { echo $it | sum }",
result: Some(vec![
UntaggedValue::int(3).into(),
UntaggedValue::int(7).into(),
]),
},
]
} }
} }
@ -104,3 +119,15 @@ fn each(raw_args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStrea
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Each;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Each {})
}
}

View file

@ -31,15 +31,17 @@ impl WholeStreamCommand for Echo {
echo(args, registry) echo(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "Put a hello message in the pipeline", description: "Put a hello message in the pipeline",
example: "echo 'hello'", example: "echo 'hello'",
result: Some(vec![Value::from("hello")]),
}, },
Example { Example {
description: "Print the value of the special '$nu' variable", description: "Print the value of the special '$nu' variable",
example: "echo $nu", example: "echo $nu",
result: None,
}, },
] ]
} }
@ -76,3 +78,15 @@ fn echo(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, S
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Echo;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Echo {})
}
}

View file

@ -41,15 +41,17 @@ impl WholeStreamCommand for Enter {
enter(args, registry) enter(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "Enter a path as a new shell", description: "Enter a path as a new shell",
example: "enter ../projectB", example: "enter ../projectB",
result: None,
}, },
Example { Example {
description: "Enter a file as a new shell", description: "Enter a file as a new shell",
example: "enter package.json", example: "enter package.json",
result: None,
}, },
] ]
} }
@ -165,3 +167,15 @@ fn enter(raw_args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStre
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Enter;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Enter {})
}
}

View file

@ -73,3 +73,15 @@ pub fn evaluate_by(
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::EvaluateBy;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(EvaluateBy {})
}
}

View file

@ -27,15 +27,17 @@ impl WholeStreamCommand for Exit {
exit(args, registry) exit(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "Exit the current shell", description: "Exit the current shell",
example: "exit", example: "exit",
result: None,
}, },
Example { Example {
description: "Exit all shells (exiting Nu)", description: "Exit all shells (exiting Nu)",
example: "exit --now", example: "exit --now",
result: None,
}, },
] ]
} }
@ -55,3 +57,15 @@ pub fn exit(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStrea
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Exit;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Exit {})
}
}

View file

@ -2,7 +2,7 @@ use crate::commands::WholeStreamCommand;
use crate::context::CommandRegistry; use crate::context::CommandRegistry;
use crate::prelude::*; use crate::prelude::*;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape}; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
use nu_source::Tagged; use nu_source::Tagged;
pub struct First; pub struct First;
@ -37,15 +37,20 @@ impl WholeStreamCommand for First {
first(args, registry) first(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "Return the first item of a list/table", description: "Return the first item of a list/table",
example: "echo [1 2 3] | first", example: "echo [1 2 3] | first",
result: Some(vec![UntaggedValue::int(1).into()]),
}, },
Example { Example {
description: "Return the first 2 items of a list/table", description: "Return the first 2 items of a list/table",
example: "echo [1 2 3] | first 2", example: "echo [1 2 3] | first 2",
result: Some(vec![
UntaggedValue::int(1).into(),
UntaggedValue::int(2).into(),
]),
}, },
] ]
} }
@ -73,3 +78,15 @@ fn first(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream,
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::First;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(First {})
}
}

View file

@ -39,10 +39,11 @@ impl WholeStreamCommand for Format {
format_command(args, registry) format_command(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[Example { vec![Example {
description: "Print filenames with their sizes", description: "Print filenames with their sizes",
example: "ls | format '{name}: {size}'", example: "ls | format '{name}: {size}'",
result: None,
}] }]
} }
} }
@ -172,3 +173,15 @@ fn to_column_path(
.into_value(&tag), .into_value(&tag),
) )
} }
#[cfg(test)]
mod tests {
use super::Format;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Format {})
}
}

View file

@ -34,3 +34,15 @@ impl WholeStreamCommand for From {
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
} }
#[cfg(test)]
mod tests {
use super::From;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(From {})
}
}

View file

@ -29,10 +29,11 @@ impl WholeStreamCommand for FromBSON {
from_bson(args, registry) from_bson(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[Example { vec![Example {
description: "Convert bson data to a table", description: "Convert bson data to a table",
example: "open file.bin | from bson", example: "open file.bin | from bson",
result: None,
}] }]
} }
} }
@ -231,3 +232,15 @@ fn from_bson(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStre
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::FromBSON;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(FromBSON {})
}
}

View file

@ -44,19 +44,22 @@ impl WholeStreamCommand for FromCSV {
from_csv(args, registry) from_csv(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "Convert comma-separated data to a table", description: "Convert comma-separated data to a table",
example: "open data.txt | from csv", example: "open data.txt | from csv",
result: None,
}, },
Example { Example {
description: "Convert comma-separated data to a table, ignoring headers", description: "Convert comma-separated data to a table, ignoring headers",
example: "open data.txt | from csv --headerless", example: "open data.txt | from csv --headerless",
result: None,
}, },
Example { Example {
description: "Convert semicolon-separated data to a table", description: "Convert semicolon-separated data to a table",
example: "open data.txt | from csv --separator ';'", example: "open data.txt | from csv --separator ';'",
result: None,
}, },
] ]
} }
@ -100,3 +103,15 @@ fn from_csv(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStrea
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::FromCSV;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(FromCSV {})
}
}

View file

@ -117,3 +117,15 @@ fn from_eml(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStrea
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::FromEML;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(FromEML {})
}
}

View file

@ -239,3 +239,15 @@ fn params_to_value(params: Vec<(String, Vec<String>)>, tag: Tag) -> Value {
row.into_value() row.into_value()
} }
#[cfg(test)]
mod tests {
use super::FromIcs;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(FromIcs {})
}
}

View file

@ -94,3 +94,15 @@ fn from_ini(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStrea
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::FromINI;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(FromINI {})
}
}

View file

@ -130,3 +130,15 @@ fn from_json(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStre
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::FromJSON;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(FromJSON {})
}
}

View file

@ -92,3 +92,15 @@ fn from_ods(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStrea
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::FromODS;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(FromODS {})
}
}

View file

@ -164,3 +164,15 @@ fn from_sqlite(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputSt
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::FromSQLite;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(FromSQLite {})
}
}

View file

@ -485,4 +485,12 @@ mod tests {
assert_eq!(aligned_columns_headerless, separator_headerless); assert_eq!(aligned_columns_headerless, separator_headerless);
assert_eq!(aligned_columns_with_headers, separator_with_headers); assert_eq!(aligned_columns_with_headers, separator_with_headers);
} }
#[test]
fn examples_work_as_expected() {
use super::FromSSV;
use crate::examples::test as test_examples;
test_examples(FromSSV {})
}
} }

View file

@ -97,3 +97,15 @@ pub fn from_toml(
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::FromTOML;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(FromTOML {})
}
}

View file

@ -51,3 +51,15 @@ fn from_tsv(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStrea
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::FromTSV;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(FromTSV {})
}
}

View file

@ -62,3 +62,15 @@ fn from_url(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStrea
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::FromURL;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(FromURL {})
}
}

View file

@ -101,3 +101,15 @@ fn params_to_value(params: Vec<(String, Vec<String>)>, tag: Tag) -> Value {
row.into_value() row.into_value()
} }
#[cfg(test)]
mod tests {
use super::FromVcf;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(FromVcf {})
}
}

View file

@ -92,3 +92,15 @@ fn from_xlsx(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStre
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::FromXLSX;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(FromXLSX {})
}
}

View file

@ -301,4 +301,12 @@ mod tests {
Ok(()) Ok(())
} }
#[test]
fn examples_work_as_expected() {
use super::FromXML;
use crate::examples::test as test_examples;
test_examples(FromXML {})
}
} }

View file

@ -150,3 +150,15 @@ fn from_yaml(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStre
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::FromYAML;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(FromYAML {})
}
}

View file

@ -41,15 +41,17 @@ impl WholeStreamCommand for Get {
get(args, registry) get(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "Extract the name of files as a list", description: "Extract the name of files as a list",
example: "ls | get name", example: "ls | get name",
result: None,
}, },
Example { Example {
description: "Extract the cpu list from the sys information", description: "Extract the cpu list from the sys information",
example: "sys | get cpu", example: "sys | get cpu",
result: None,
}, },
] ]
} }
@ -240,3 +242,15 @@ pub fn get(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream
}; };
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Get;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Get {})
}
}

View file

@ -1,7 +1,8 @@
use crate::commands::WholeStreamCommand; use crate::commands::WholeStreamCommand;
use crate::prelude::*; use crate::prelude::*;
use indexmap::indexmap;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, Value}; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::Tagged; use nu_source::Tagged;
pub struct GroupBy; pub struct GroupBy;
@ -36,11 +37,36 @@ impl WholeStreamCommand for GroupBy {
group_by(args, registry) group_by(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[Example { vec![
description: "Group files by type", Example {
example: "ls | group-by type", description: "Group items by type",
}] example: r#"ls | group-by type"#,
result: None,
},
Example {
description: "Group items by their value",
example: "echo [1 3 1 3 2 1 1] | group-by",
result: Some(vec![UntaggedValue::row(indexmap! {
"1".to_string() => UntaggedValue::Table(vec![
UntaggedValue::int(1).into(),
UntaggedValue::int(1).into(),
UntaggedValue::int(1).into(),
UntaggedValue::int(1).into(),
]).into(),
"3".to_string() => UntaggedValue::Table(vec![
UntaggedValue::int(3).into(),
UntaggedValue::int(3).into(),
]).into(),
"2".to_string() => UntaggedValue::Table(vec![
UntaggedValue::int(2).into(),
]).into(),
})
.into()]),
},
]
} }
} }
@ -185,4 +211,12 @@ mod tests {
Ok(()) Ok(())
} }
#[test]
fn examples_work_as_expected() {
use super::GroupBy;
use crate::examples::test as test_examples;
test_examples(GroupBy {})
}
} }

View file

@ -44,10 +44,11 @@ impl WholeStreamCommand for GroupByDate {
group_by_date(args, registry) group_by_date(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[Example { vec![Example {
description: "Group files by type", description: "Group files by type",
example: "ls | group-by date --fmt '%d/%m/%Y'", example: "ls | group-by date --format '%d/%m/%Y'",
result: None,
}] }]
} }
} }
@ -101,3 +102,15 @@ pub fn group_by_date(
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::GroupByDate;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(GroupByDate {})
}
}

View file

@ -30,10 +30,11 @@ impl WholeStreamCommand for Headers {
headers(args, registry) headers(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[Example { vec![Example {
description: "Create headers for a raw string", description: "Create headers for a raw string",
example: "echo \"a b c|1 2 3\" | split-row \"|\" | split-column \" \" | headers", example: r#"echo "a b c|1 2 3" | split-row "|" | split-column " " | headers"#,
result: None,
}] }]
} }
} }
@ -84,3 +85,15 @@ pub fn headers(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputS
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Headers;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Headers {})
}
}

View file

@ -281,3 +281,15 @@ pub fn get_help(cmd: &dyn WholeStreamCommand, registry: &CommandRegistry) -> Str
long_desc long_desc
} }
#[cfg(test)]
mod tests {
use super::Help;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Help {})
}
}

View file

@ -47,20 +47,23 @@ impl WholeStreamCommand for Histogram {
histogram(args, registry) histogram(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "Get a histogram for the types of files", description: "Get a histogram for the types of files",
example: "ls | histogram type", example: "ls | histogram type",
result: None,
}, },
Example { Example {
description: description:
"Get a histogram for the types of files, with frequency column named count", "Get a histogram for the types of files, with frequency column named count",
example: "ls | histogram type count", example: "ls | histogram type count",
result: None,
}, },
Example { Example {
description: "Get a histogram for a list of numbers", description: "Get a histogram for a list of numbers",
example: "echo [1 2 3 1 2 3 1 1 1 1 3 2 1 1 3] | wrap | histogram Column", example: "echo [1 2 3 1 1 1 2 2 1 1] | histogram",
result: None,
}, },
] ]
} }
@ -179,3 +182,15 @@ fn percentages(values: &Value, max: Value, tag: impl Into<Tag>) -> Result<Value,
Ok(results) Ok(results)
} }
#[cfg(test)]
mod tests {
use super::Histogram;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Histogram {})
}
}

View file

@ -48,3 +48,15 @@ fn history(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStrea
}; };
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::History;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(History {})
}
}

View file

@ -74,3 +74,15 @@ fn insert(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream,
}; };
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Insert;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Insert {})
}
}

View file

@ -196,3 +196,15 @@ fn is_empty(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStrea
}; };
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::IsEmpty;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(IsEmpty {})
}
}

View file

@ -2,7 +2,7 @@ use crate::commands::WholeStreamCommand;
use crate::context::CommandRegistry; use crate::context::CommandRegistry;
use crate::prelude::*; use crate::prelude::*;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape}; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
use nu_source::Tagged; use nu_source::Tagged;
pub struct Keep; pub struct Keep;
@ -37,15 +37,22 @@ impl WholeStreamCommand for Keep {
keep(args, registry) keep(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "Keep the first row", description: "Keep the first row",
example: "ls | keep", example: "echo [1 2 3] | keep",
result: Some(vec![UntaggedValue::int(1).into()]),
}, },
Example { Example {
description: "Keep the first four rows", description: "Keep the first four rows",
example: "ls | keep 4", example: "echo [1 2 3 4 5] | keep 4",
result: Some(vec![
UntaggedValue::int(1).into(),
UntaggedValue::int(2).into(),
UntaggedValue::int(3).into(),
UntaggedValue::int(4).into(),
]),
}, },
] ]
} }
@ -73,3 +80,15 @@ fn keep(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, S
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Keep;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Keep {})
}
}

View file

@ -109,3 +109,15 @@ impl WholeStreamCommand for KeepUntil {
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
} }
#[cfg(test)]
mod tests {
use super::KeepUntil;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(KeepUntil {})
}
}

View file

@ -109,3 +109,15 @@ impl WholeStreamCommand for KeepWhile {
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
} }
#[cfg(test)]
mod tests {
use super::KeepWhile;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(KeepWhile {})
}
}

View file

@ -45,15 +45,17 @@ impl WholeStreamCommand for Kill {
kill(args, registry) kill(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "Kill the pid using the most memory", description: "Kill the pid using the most memory",
example: "ps | sort-by mem | last | kill $it.pid", example: "ps | sort-by mem | last | kill $it.pid",
result: None,
}, },
Example { Example {
description: "Force kill a given pid", description: "Force kill a given pid",
example: "kill --force 12345", example: "kill --force 12345",
result: None,
}, },
] ]
} }
@ -117,3 +119,15 @@ fn kill(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, S
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Kill;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Kill {})
}
}

View file

@ -2,7 +2,7 @@ use crate::commands::WholeStreamCommand;
use crate::context::CommandRegistry; use crate::context::CommandRegistry;
use crate::prelude::*; use crate::prelude::*;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, Value}; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::Tagged; use nu_source::Tagged;
pub struct Last; pub struct Last;
@ -37,15 +37,21 @@ impl WholeStreamCommand for Last {
last(args, registry) last(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "Get the last row", description: "Get the last row",
example: "ls | last", example: "echo [1 2 3] | last",
result: Some(vec![Value::from(UntaggedValue::from(BigInt::from(3)))]),
}, },
Example { Example {
description: "Get the last three rows", description: "Get the last three rows",
example: "ls | last 3", example: "echo [1 2 3 4 5] | last 3",
result: Some(vec![
UntaggedValue::int(3).into(),
UntaggedValue::int(4).into(),
UntaggedValue::int(5).into(),
]),
}, },
] ]
} }
@ -74,3 +80,15 @@ fn last(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, S
}; };
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Last;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Last {})
}
}

View file

@ -26,10 +26,11 @@ impl WholeStreamCommand for Lines {
lines(args, registry) lines(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[Example { vec![Example {
description: "Split output from an external command into lines", description: "Split multi-line string into lines",
example: "^ls -l | lines", example: r#"^echo "two\nlines" | lines"#,
result: None,
}] }]
} }
} }
@ -121,3 +122,15 @@ fn lines(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream,
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Lines;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Lines {})
}
}

View file

@ -67,19 +67,22 @@ impl WholeStreamCommand for Ls {
ls(args, registry) ls(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "List all files in the current directory", description: "List all files in the current directory",
example: "ls", example: "ls",
result: None,
}, },
Example { Example {
description: "List all files in a subdirectory", description: "List all files in a subdirectory",
example: "ls subdir", example: "ls subdir",
result: None,
}, },
Example { Example {
description: "List all rust files", description: "List all rust files",
example: "ls *.rs", example: "ls *.rs",
result: None,
}, },
] ]
} }
@ -101,3 +104,15 @@ fn ls(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, She
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Ls;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Ls {})
}
}

View file

@ -74,3 +74,15 @@ pub fn map_max_by(
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::MapMaxBy;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(MapMaxBy {})
}
}

View file

@ -39,10 +39,11 @@ impl WholeStreamCommand for Merge {
merge(args, registry) merge(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[Example { vec![Example {
description: "Merge a 1-based index column with some ls output", description: "Merge a 1-based index column with some ls output",
example: "ls | select name | keep 3 | merge { echo [1 2 3] | wrap index }", example: "ls | select name | keep 3 | merge { echo [1 2 3] | wrap index }",
result: None,
}] }]
} }
} }
@ -97,3 +98,15 @@ fn merge(raw_args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStre
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Merge;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Merge {})
}
}

View file

@ -34,10 +34,11 @@ impl WholeStreamCommand for Mkdir {
mkdir(args, registry) mkdir(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[Example { vec![Example {
description: "Make a directory named foo", description: "Make a directory named foo",
example: "mkdir foo", example: "mkdir foo",
result: None,
}] }]
} }
} }
@ -57,3 +58,15 @@ fn mkdir(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream,
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Mkdir;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Mkdir {})
}
}

View file

@ -45,19 +45,22 @@ impl WholeStreamCommand for Move {
mv(args, registry) mv(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "Rename a file", description: "Rename a file",
example: "mv before.txt after.txt", example: "mv before.txt after.txt",
result: None,
}, },
Example { Example {
description: "Move a file into a directory", description: "Move a file into a directory",
example: "mv test.txt my/subdirectory", example: "mv test.txt my/subdirectory",
result: None,
}, },
Example { Example {
description: "Move many files into a directory", description: "Move many files into a directory",
example: "mv *.txt my/subdirectory", example: "mv *.txt my/subdirectory",
result: None,
}, },
] ]
} }
@ -78,3 +81,15 @@ fn mv(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, She
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Move;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Move {})
}
}

View file

@ -30,3 +30,15 @@ impl WholeStreamCommand for Next {
fn next(_args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> { fn next(_args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
Ok(vec![Ok(ReturnSuccess::Action(CommandAction::NextShell))].into()) Ok(vec![Ok(ReturnSuccess::Action(CommandAction::NextShell))].into())
} }
#[cfg(test)]
mod tests {
use super::Next;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Next {})
}
}

View file

@ -2,7 +2,7 @@ use crate::commands::WholeStreamCommand;
use crate::context::CommandRegistry; use crate::context::CommandRegistry;
use crate::prelude::*; use crate::prelude::*;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape}; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, Value};
use nu_source::Tagged; use nu_source::Tagged;
#[derive(Deserialize)] #[derive(Deserialize)]
@ -40,15 +40,17 @@ impl WholeStreamCommand for Nth {
nth(args, registry) nth(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "Get the second row", description: "Get the second row",
example: "echo [first second third] | nth 1", example: "echo [first second third] | nth 1",
result: Some(vec![Value::from("second")]),
}, },
Example { Example {
description: "Get the first and third rows", description: "Get the first and third rows",
example: "echo [first second third] | nth 0 2", example: "echo [first second third] | nth 0 2",
result: Some(vec![Value::from("first"), Value::from("third")]),
}, },
] ]
} }
@ -79,3 +81,15 @@ fn nth(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, Sh
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Nth;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Nth {})
}
}

View file

@ -249,3 +249,15 @@ fn read_be_u16(input: &[u8]) -> Option<Vec<u16>> {
Some(result) Some(result)
} }
} }
#[cfg(test)]
mod tests {
use super::Open;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Open {})
}
}

View file

@ -137,3 +137,15 @@ pub fn pivot(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStre
Ok(OutputStream::new(stream)) Ok(OutputStream::new(stream))
} }
#[cfg(test)]
mod tests {
use super::Pivot;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Pivot {})
}
}

View file

@ -2,7 +2,7 @@ use crate::commands::WholeStreamCommand;
use crate::context::CommandRegistry; use crate::context::CommandRegistry;
use crate::prelude::*; use crate::prelude::*;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, Value}; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
#[derive(Deserialize)] #[derive(Deserialize)]
struct PrependArgs { struct PrependArgs {
@ -36,10 +36,16 @@ impl WholeStreamCommand for Prepend {
prepend(args, registry) prepend(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[Example { vec![Example {
description: "Add something to the beginning of a list or table", description: "Add something to the beginning of a list or table",
example: "echo [2 3 4] | prepend 1", example: "echo [2 3 4] | prepend 1",
result: Some(vec![
UntaggedValue::int(1).into(),
UntaggedValue::int(2).into(),
UntaggedValue::int(3).into(),
UntaggedValue::int(4).into(),
]),
}] }]
} }
} }
@ -58,3 +64,15 @@ fn prepend(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Prepend;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Prepend {})
}
}

View file

@ -31,3 +31,15 @@ impl WholeStreamCommand for Previous {
fn previous(_args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> { fn previous(_args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
Ok(vec![Ok(ReturnSuccess::Action(CommandAction::PreviousShell))].into()) Ok(vec![Ok(ReturnSuccess::Action(CommandAction::PreviousShell))].into())
} }
#[cfg(test)]
mod tests {
use super::Previous;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Previous {})
}
}

View file

@ -26,10 +26,11 @@ impl WholeStreamCommand for Pwd {
pwd(args, registry) pwd(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[Example { vec![Example {
description: "Print the current working directory", description: "Print the current working directory",
example: "pwd", example: "pwd",
result: None,
}] }]
} }
} }
@ -49,3 +50,15 @@ pub fn pwd(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Pwd;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Pwd {})
}
}

View file

@ -58,3 +58,15 @@ fn range(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream,
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Range;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Range {})
}
}

View file

@ -73,3 +73,15 @@ pub fn reduce_by(
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::ReduceBy;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(ReduceBy {})
}
}

View file

@ -64,3 +64,15 @@ fn reject(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream,
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Reject;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Reject {})
}
}

View file

@ -40,15 +40,17 @@ impl WholeStreamCommand for Rename {
rename(args, registry) rename(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "Rename a column", description: "Rename a column",
example: "ls | rename my_name", example: r#"echo "{a: 1, b: 2, c: 3}" | from json | rename my_column"#,
result: None,
}, },
Example { Example {
description: "Rename many columns", description: "Rename many columns",
example: "echo \"{a: 1, b: 2, c: 3}\" | from json | rename spam eggs cars", example: r#"echo "{a: 1, b: 2, c: 3}" | from json | rename spam eggs cars"#,
result: None,
}, },
] ]
} }
@ -100,3 +102,15 @@ pub fn rename(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStr
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Rename;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Rename {})
}
}

View file

@ -2,7 +2,7 @@ use crate::commands::WholeStreamCommand;
use crate::context::CommandRegistry; use crate::context::CommandRegistry;
use crate::prelude::*; use crate::prelude::*;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature}; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
pub struct Reverse; pub struct Reverse;
@ -27,10 +27,17 @@ impl WholeStreamCommand for Reverse {
reverse(args, registry) reverse(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[Example { vec![Example {
description: "Sort files in descending file size", description: "Sort list of numbers in descending file size",
example: "ls | sort-by size | reverse", example: "echo [3 1 2 19 0] | reverse",
result: Some(vec![
UntaggedValue::int(0).into(),
UntaggedValue::int(19).into(),
UntaggedValue::int(2).into(),
UntaggedValue::int(1).into(),
UntaggedValue::int(3).into(),
]),
}] }]
} }
} }
@ -49,3 +56,15 @@ fn reverse(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Reverse;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Reverse {})
}
}

View file

@ -44,15 +44,17 @@ impl WholeStreamCommand for Remove {
rm(args, registry) rm(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "Delete a file", description: "Delete a file",
example: "rm file.txt", example: "rm file.txt",
result: None,
}, },
Example { Example {
description: "Move a file to the system trash", description: "Move a file to the system trash",
example: "rm --trash file.txt", example: "rm --trash file.txt",
result: None,
}, },
] ]
} }
@ -72,3 +74,15 @@ fn rm(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, She
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Remove;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Remove {})
}
}

View file

@ -282,3 +282,15 @@ fn string_from(input: &[Value]) -> String {
save_data save_data
} }
#[cfg(test)]
mod tests {
use super::Save;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Save {})
}
}

View file

@ -40,15 +40,17 @@ impl WholeStreamCommand for Select {
select(args, registry) select(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "Select just the name column", description: "Select just the name column",
example: "ls | select name", example: "ls | select name",
result: None,
}, },
Example { Example {
description: "Select the name and size columns", description: "Select the name and size columns",
example: "ls | select name size", example: "ls | select name size",
result: None,
}, },
] ]
} }
@ -172,3 +174,15 @@ fn select(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream,
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Select;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Select {})
}
}

View file

@ -48,3 +48,15 @@ fn shells(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream
Ok(shells_out.into()) Ok(shells_out.into())
} }
#[cfg(test)]
mod tests {
use super::Shells;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Shells {})
}
}

View file

@ -46,3 +46,15 @@ fn shuffle(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStrea
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Shuffle;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Shuffle {})
}
}

View file

@ -1,5 +1,6 @@
use crate::commands::WholeStreamCommand; use crate::commands::WholeStreamCommand;
use crate::prelude::*; use crate::prelude::*;
use indexmap::indexmap;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value}; use nu_protocol::{ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value};
@ -26,10 +27,17 @@ impl WholeStreamCommand for Size {
size(args, registry) size(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[Example { vec![Example {
description: "Count the number of words in a string", description: "Count the number of words in a string",
example: r#"echo "There are seven words in this sentence" | size"#, example: r#"echo "There are seven words in this sentence" | size"#,
result: Some(vec![UntaggedValue::row(indexmap! {
"lines".to_string() => UntaggedValue::int(0).into(),
"words".to_string() => UntaggedValue::int(7).into(),
"chars".to_string() => UntaggedValue::int(38).into(),
"max length".to_string() => UntaggedValue::int(38).into(),
})
.into()]),
}] }]
} }
} }
@ -91,3 +99,15 @@ fn count(contents: &str, tag: impl Into<Tag>) -> Value {
dict.into_value() dict.into_value()
} }
#[cfg(test)]
mod tests {
use super::Size;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Size {})
}
}

View file

@ -2,7 +2,7 @@ use crate::commands::WholeStreamCommand;
use crate::context::CommandRegistry; use crate::context::CommandRegistry;
use crate::prelude::*; use crate::prelude::*;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape}; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
use nu_source::Tagged; use nu_source::Tagged;
pub struct Skip; pub struct Skip;
@ -33,10 +33,14 @@ impl WholeStreamCommand for Skip {
skip(args, registry) skip(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[Example { vec![Example {
description: "Skip the first 5 rows", description: "Skip the first 5 rows",
example: "ls | skip 5", example: "echo [1 2 3 4 5 6 7] | skip 5",
result: Some(vec![
UntaggedValue::int(6).into(),
UntaggedValue::int(7).into(),
]),
}] }]
} }
} }
@ -63,3 +67,15 @@ fn skip(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, S
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Skip;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Skip {})
}
}

View file

@ -112,3 +112,15 @@ impl WholeStreamCommand for SkipUntil {
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
} }
#[cfg(test)]
mod tests {
use super::SkipUntil;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(SkipUntil {})
}
}

View file

@ -112,3 +112,15 @@ impl WholeStreamCommand for SkipWhile {
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
} }
#[cfg(test)]
mod tests {
use super::SkipWhile;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(SkipWhile {})
}
}

View file

@ -33,15 +33,27 @@ impl WholeStreamCommand for SortBy {
sort_by(args, registry) sort_by(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example {
description: "Sort list by increasing value",
example: "echo [4 2 3 1] | sort-by",
result: Some(vec![
UntaggedValue::int(1).into(),
UntaggedValue::int(2).into(),
UntaggedValue::int(3).into(),
UntaggedValue::int(4).into(),
]),
},
Example { Example {
description: "Sort output by increasing file size", description: "Sort output by increasing file size",
example: "ls | sort-by size", example: "ls | sort-by size",
result: None,
}, },
Example { Example {
description: "Sort output by type, and then by file size for each type", description: "Sort output by type, and then by file size for each type",
example: "ls | sort-by type size", example: "ls | sort-by type size",
result: None,
}, },
] ]
} }
@ -81,3 +93,15 @@ fn sort_by(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::SortBy;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(SortBy {})
}
}

View file

@ -273,4 +273,12 @@ mod tests {
assert!(split(&for_key, &nu_releases, Tag::from(Span::new(5, 10))).is_err()); assert!(split(&for_key, &nu_releases, Tag::from(Span::new(5, 10))).is_err());
} }
#[test]
fn examples_work_as_expected() {
use super::SplitBy;
use crate::examples::test as test_examples;
test_examples(SplitBy {})
}
} }

View file

@ -103,3 +103,15 @@ fn split_column(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputS
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::SplitColumn;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(SplitColumn {})
}
}

View file

@ -70,3 +70,15 @@ fn split_row(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStre
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::SplitRow;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(SplitRow {})
}
}

View file

@ -2,7 +2,7 @@ use crate::commands::WholeStreamCommand;
use crate::prelude::*; use crate::prelude::*;
use crate::utils::data_processing::{reducer_for, Reduce}; use crate::utils::data_processing::{reducer_for, Reduce};
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, ReturnValue, Signature, Value}; use nu_protocol::{ReturnSuccess, ReturnValue, Signature, UntaggedValue, Value};
use num_traits::identities::Zero; use num_traits::identities::Zero;
pub struct Sum; pub struct Sum;
@ -35,15 +35,17 @@ impl WholeStreamCommand for Sum {
}) })
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: "Sum a list of numbers", description: "Sum a list of numbers",
example: "echo [1 2 3] | sum", example: "echo [1 2 3] | sum",
result: Some(vec![UntaggedValue::int(6).into()]),
}, },
Example { Example {
description: "Get the disk usage for the current directory", description: "Get the disk usage for the current directory",
example: "ls --all --du | get size | sum", example: "ls --all --du | get size | sum",
result: None,
}, },
] ]
} }
@ -65,3 +67,15 @@ fn sum(RunnableContext { mut input, .. }: RunnableContext) -> Result<OutputStrea
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Sum;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Sum {})
}
}

View file

@ -87,3 +87,15 @@ fn t_sort_by(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStre
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::TSortBy;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(TSortBy {})
}
}

View file

@ -128,3 +128,15 @@ fn table(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream,
Ok(OutputStream::new(stream)) Ok(OutputStream::new(stream))
} }
#[cfg(test)]
mod tests {
use super::Table;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Table {})
}
}

View file

@ -55,3 +55,15 @@ fn tags(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream,
}) })
.to_output_stream()) .to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::Tags;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Tags {})
}
}

View file

@ -35,3 +35,15 @@ impl WholeStreamCommand for To {
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
} }
#[cfg(test)]
mod tests {
use super::To;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(To {})
}
}

View file

@ -306,3 +306,15 @@ fn to_bson(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::ToBSON;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(ToBSON {})
}
}

View file

@ -83,3 +83,15 @@ fn to_csv(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream,
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::ToCSV;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(ToCSV {})
}
}

View file

@ -119,3 +119,15 @@ fn to_html(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::ToHTML;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(ToHTML {})
}
}

View file

@ -40,17 +40,19 @@ impl WholeStreamCommand for ToJSON {
to_json(args, registry) to_json(args, registry)
} }
fn examples(&self) -> &[Example] { fn examples(&self) -> Vec<Example> {
&[ vec![
Example { Example {
description: description:
"Outputs an unformatted JSON string representing the contents of this table", "Outputs an unformatted JSON string representing the contents of this table",
example: "to json", example: "echo [1 2 3] | to json",
result: Some(vec![Value::from("[1,2,3]")]),
}, },
Example { Example {
description: description:
"Outputs a formatted JSON string representing the contents of this table with an indentation setting of 4 spaces", "Outputs a formatted JSON string representing the contents of this table with an indentation setting of 2 spaces",
example: "to json --pretty 4", example: "echo [1 2 3] | to json --pretty 2",
result: Some(vec![Value::from("[\n 1,\n 2,\n 3\n]")]),
}, },
] ]
} }
@ -233,3 +235,15 @@ fn to_json(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::ToJSON;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(ToJSON {})
}
}

View file

@ -75,3 +75,15 @@ fn to_html(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::ToMarkdown;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(ToMarkdown {})
}
}

View file

@ -222,3 +222,15 @@ fn to_sqlite(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStre
Ok(stream.to_output_stream()) Ok(stream.to_output_stream())
} }
#[cfg(test)]
mod tests {
use super::ToSQLite;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(ToSQLite {})
}
}

Some files were not shown because too many files have changed in this diff Show more