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")]
{
context.add_commands(vec![whole_stream_command(
crate::commands::clip::clipboard::Clip,
)]);
context.add_commands(vec![whole_stream_command(crate::commands::clip::Clip)]);
}
}

View File

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

View File

@ -43,15 +43,17 @@ impl WholeStreamCommand for Alias {
alias(args, registry)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "An alias without parameters",
example: "alias say-hi [] { echo 'Hello!' }",
result: None,
},
Example {
description: "An alias with a single parameter",
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())
}
#[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::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, Value};
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
#[derive(Deserialize)]
struct AppendArgs {
@ -36,10 +36,16 @@ impl WholeStreamCommand for Append {
append(args, registry)
}
fn examples(&self) -> &[Example] {
&[Example {
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Add something to the end of a list or table",
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())
}
#[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 {
description: "Automatically view the results",
example: "ls | autoview",
result: None,
},
Example {
description: "Autoview is also implied. The above can be written as",
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)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "This month's calendar",
example: "cal",
result: None,
},
Example {
description: "The calendar for all of 2012",
example: "cal --full-year 2012",
result: None,
},
]
}
@ -340,3 +342,15 @@ fn add_month_to_table(
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)
}
fn examples(&self) -> &[Example] {
&[Example {
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Calculate math in the pipeline",
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()),
}
}
#[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)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Change to a new directory called 'dirname'",
example: "cd dirname",
result: None,
},
Example {
description: "Change to your home directory",
example: "cd",
result: None,
},
Example {
description: "Change to your home directory (alternate version)",
example: "cd ~",
result: None,
},
Example {
description: "Change to the previous directory",
example: "cd -",
result: None,
},
]
}
@ -76,3 +80,15 @@ fn cd(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, She
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 {
"clear"
}
fn signature(&self) -> Signature {
Signature::build("clear")
}
fn usage(&self) -> &str {
"clears the terminal"
}
fn run(
&self,
args: CommandArgs,
@ -23,13 +26,16 @@ impl WholeStreamCommand for Clear {
) -> Result<OutputStream, ShellError> {
clear(args, registry)
}
fn examples(&self) -> &[Example] {
&[Example {
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Clear the screen",
example: "clear",
result: None,
}]
}
}
fn clear(_args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
if cfg!(windows) {
Command::new("cmd")
@ -44,3 +50,15 @@ fn clear(_args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream
}
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,17 +1,15 @@
#[cfg(feature = "clipboard")]
pub mod clipboard {
use crate::commands::WholeStreamCommand;
use crate::context::CommandRegistry;
use crate::prelude::*;
use futures::stream::StreamExt;
use nu_errors::ShellError;
use nu_protocol::{ReturnValue, Signature, Value};
use crate::commands::WholeStreamCommand;
use crate::context::CommandRegistry;
use crate::prelude::*;
use futures::stream::StreamExt;
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 {
"clip"
}
@ -32,18 +30,16 @@ pub mod clipboard {
clip(args, registry)
}
fn examples(&self) -> &[Example] {
&[Example {
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Save text to the clipboard",
example: "echo 'secret value' | clip",
result: None,
}]
}
}
}
pub fn clip(
args: CommandArgs,
_registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
pub fn clip(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let stream = async_stream! {
let mut input = args.input;
let name = args.call_info.name_tag.clone();
@ -58,9 +54,9 @@ pub mod clipboard {
let stream: BoxStream<'static, ReturnValue> = stream.boxed();
Ok(OutputStream::from(stream))
}
}
async fn inner_clip(input: Vec<Value>, name: Tag) -> OutputStream {
async fn inner_clip(input: Vec<Value>, name: Tag) -> OutputStream {
if let Ok(clip_context) = ClipboardProvider::new() {
let mut clip_context: ClipboardContext = clip_context;
let mut new_copy_data = String::new();
@ -108,5 +104,16 @@ pub mod 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 example: &'static str,
pub description: &'static str,
pub result: Option<Vec<Value>>,
}
pub trait WholeStreamCommand: Send + Sync {
@ -293,8 +294,8 @@ pub trait WholeStreamCommand: Send + Sync {
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)
}
fn examples(&self) -> &[Example] {
&[Example {
description: "Remove all directory entries, except those with a 'target'",
fn examples(&self) -> Vec<Example> {
vec![
Example {
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())
}
#[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)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "See all config values",
example: "config",
result: None,
},
Example {
description: "Set completion_mode to circular",
example: "config --set [completion_mode circular]",
result: None,
},
Example {
description: "Store the contents of the pipeline as a path",
example: "echo ['/usr/bin' '/bin'] | config --set_into path",
result: None,
},
Example {
description: "Get the current startup commands",
example: "config --get startup",
result: None,
},
Example {
description: "Remove the startup commands",
example: "config --remove startup",
result: None,
},
Example {
description: "Clear the config (be careful!)",
example: "config --clear",
result: None,
},
Example {
description: "Get the path to the current config file",
example: "config --path",
result: None,
},
]
}
@ -220,3 +227,15 @@ pub fn config(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStr
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)
}
fn examples(&self) -> &[Example] {
&[Example {
description: "Count the number of files/directories in the current directory",
example: "ls | count",
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Count the number of entries in a list",
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())
}
#[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)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Copy myfile to dir_b",
example: "cp myfile dir_b",
result: None,
},
Example {
description: "Recursively copy dir_a to 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())
}
#[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)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Get the current local time and date",
example: "date",
result: None,
},
Example {
description: "Get the current UTC time and date",
example: "date --utc",
result: None,
},
]
}
@ -108,3 +110,15 @@ pub fn date(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStrea
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())
}
#[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)
}
fn examples(&self) -> &[Example] {
&[Example {
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Give a default 'target' to all file entries",
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())
}
#[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::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, Value};
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::Tagged;
pub struct Drop;
@ -37,15 +37,20 @@ impl WholeStreamCommand for Drop {
drop(args, registry)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Remove the last item of a list/table",
example: "echo [1 2 3] | drop",
result: Some(vec![
UntaggedValue::int(1).into(),
UntaggedValue::int(2).into(),
]),
},
Example {
description: "Remove the last 2 items of a list/table",
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())
}
#[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)
}
fn examples(&self) -> &[Example] {
&[Example {
fn examples(&self) -> Vec<Example> {
vec![Example {
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)
}
}
#[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_protocol::{
hir::Block, hir::Expression, hir::SpannedExpression, hir::Synthetic, ReturnSuccess, Signature,
SyntaxShape,
SyntaxShape, UntaggedValue,
};
pub struct Each;
@ -42,11 +42,26 @@ impl WholeStreamCommand for Each {
each(args, registry)
}
fn examples(&self) -> &[Example] {
&[Example {
description: "Print the name of each file",
example: "ls | each { echo $it.name }",
}]
fn examples(&self) -> Vec<Example> {
vec![
Example {
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())
}
#[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)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Put a hello message in the pipeline",
example: "echo 'hello'",
result: Some(vec![Value::from("hello")]),
},
Example {
description: "Print the value of the special '$nu' variable",
example: "echo $nu",
result: None,
},
]
}
@ -76,3 +78,15 @@ fn echo(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, S
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)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Enter a path as a new shell",
example: "enter ../projectB",
result: None,
},
Example {
description: "Enter a file as a new shell",
example: "enter package.json",
result: None,
},
]
}
@ -165,3 +167,15 @@ fn enter(raw_args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStre
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())
}
#[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)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Exit the current shell",
example: "exit",
result: None,
},
Example {
description: "Exit all shells (exiting Nu)",
example: "exit --now",
result: None,
},
]
}
@ -55,3 +57,15 @@ pub fn exit(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStrea
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::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape};
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
use nu_source::Tagged;
pub struct First;
@ -37,15 +37,20 @@ impl WholeStreamCommand for First {
first(args, registry)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Return the first item of a list/table",
example: "echo [1 2 3] | first",
result: Some(vec![UntaggedValue::int(1).into()]),
},
Example {
description: "Return the first 2 items of a list/table",
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())
}
#[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)
}
fn examples(&self) -> &[Example] {
&[Example {
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Print filenames with their sizes",
example: "ls | format '{name}: {size}'",
result: None,
}]
}
}
@ -172,3 +173,15 @@ fn to_column_path(
.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())
}
}
#[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)
}
fn examples(&self) -> &[Example] {
&[Example {
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Convert bson data to a table",
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())
}
#[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)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Convert comma-separated data to a table",
example: "open data.txt | from csv",
result: None,
},
Example {
description: "Convert comma-separated data to a table, ignoring headers",
example: "open data.txt | from csv --headerless",
result: None,
},
Example {
description: "Convert semicolon-separated data to a table",
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())
}
#[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())
}
#[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()
}
#[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())
}
#[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())
}
#[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())
}
#[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())
}
#[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_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())
}
#[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())
}
#[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())
}
#[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()
}
#[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())
}
#[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(())
}
#[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())
}
#[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)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Extract the name of files as a list",
example: "ls | get name",
result: None,
},
Example {
description: "Extract the cpu list from the sys information",
example: "sys | get cpu",
result: None,
},
]
}
@ -240,3 +242,15 @@ pub fn get(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream
};
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::prelude::*;
use indexmap::indexmap;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, Value};
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::Tagged;
pub struct GroupBy;
@ -36,11 +37,36 @@ impl WholeStreamCommand for GroupBy {
group_by(args, registry)
}
fn examples(&self) -> &[Example] {
&[Example {
description: "Group files by type",
example: "ls | group-by type",
}]
fn examples(&self) -> Vec<Example> {
vec![
Example {
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(())
}
#[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)
}
fn examples(&self) -> &[Example] {
&[Example {
fn examples(&self) -> Vec<Example> {
vec![Example {
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())
}
#[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)
}
fn examples(&self) -> &[Example] {
&[Example {
fn examples(&self) -> Vec<Example> {
vec![Example {
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())
}
#[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
}
#[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)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Get a histogram for the types of files",
example: "ls | histogram type",
result: None,
},
Example {
description:
"Get a histogram for the types of files, with frequency column named count",
example: "ls | histogram type count",
result: None,
},
Example {
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)
}
#[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())
}
#[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())
}
#[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())
}
#[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::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape};
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
use nu_source::Tagged;
pub struct Keep;
@ -37,15 +37,22 @@ impl WholeStreamCommand for Keep {
keep(args, registry)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Keep the first row",
example: "ls | keep",
example: "echo [1 2 3] | keep",
result: Some(vec![UntaggedValue::int(1).into()]),
},
Example {
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())
}
#[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())
}
}
#[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())
}
}
#[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)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Kill the pid using the most memory",
example: "ps | sort-by mem | last | kill $it.pid",
result: None,
},
Example {
description: "Force kill a given pid",
example: "kill --force 12345",
result: None,
},
]
}
@ -117,3 +119,15 @@ fn kill(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, S
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::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, Value};
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::Tagged;
pub struct Last;
@ -37,15 +37,21 @@ impl WholeStreamCommand for Last {
last(args, registry)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
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 {
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())
}
#[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)
}
fn examples(&self) -> &[Example] {
&[Example {
description: "Split output from an external command into lines",
example: "^ls -l | lines",
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Split multi-line string into 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())
}
#[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)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "List all files in the current directory",
example: "ls",
result: None,
},
Example {
description: "List all files in a subdirectory",
example: "ls subdir",
result: None,
},
Example {
description: "List all rust files",
example: "ls *.rs",
result: None,
},
]
}
@ -101,3 +104,15 @@ fn ls(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, She
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())
}
#[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)
}
fn examples(&self) -> &[Example] {
&[Example {
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Merge a 1-based index column with some ls output",
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())
}
#[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)
}
fn examples(&self) -> &[Example] {
&[Example {
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Make a directory named foo",
example: "mkdir foo",
result: None,
}]
}
}
@ -57,3 +58,15 @@ fn mkdir(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream,
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)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Rename a file",
example: "mv before.txt after.txt",
result: None,
},
Example {
description: "Move a file into a directory",
example: "mv test.txt my/subdirectory",
result: None,
},
Example {
description: "Move many files into a directory",
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())
}
#[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> {
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::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape};
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, Value};
use nu_source::Tagged;
#[derive(Deserialize)]
@ -40,15 +40,17 @@ impl WholeStreamCommand for Nth {
nth(args, registry)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Get the second row",
example: "echo [first second third] | nth 1",
result: Some(vec![Value::from("second")]),
},
Example {
description: "Get the first and third rows",
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())
}
#[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)
}
}
#[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))
}
#[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::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, Value};
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
#[derive(Deserialize)]
struct PrependArgs {
@ -36,10 +36,16 @@ impl WholeStreamCommand for Prepend {
prepend(args, registry)
}
fn examples(&self) -> &[Example] {
&[Example {
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Add something to the beginning of a list or table",
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())
}
#[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> {
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)
}
fn examples(&self) -> &[Example] {
&[Example {
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Print the current working directory",
example: "pwd",
result: None,
}]
}
}
@ -49,3 +50,15 @@ pub fn pwd(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream
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())
}
#[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())
}
#[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())
}
#[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)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
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 {
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())
}
#[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::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature};
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
pub struct Reverse;
@ -27,10 +27,17 @@ impl WholeStreamCommand for Reverse {
reverse(args, registry)
}
fn examples(&self) -> &[Example] {
&[Example {
description: "Sort files in descending file size",
example: "ls | sort-by size | reverse",
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Sort list of numbers in descending file size",
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())
}
#[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)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Delete a file",
example: "rm file.txt",
result: None,
},
Example {
description: "Move a file to the system trash",
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())
}
#[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
}
#[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)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Select just the name column",
example: "ls | select name",
result: None,
},
Example {
description: "Select the name and size columns",
example: "ls | select name size",
result: None,
},
]
}
@ -172,3 +174,15 @@ fn select(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream,
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())
}
#[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())
}
#[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::prelude::*;
use indexmap::indexmap;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value};
@ -26,10 +27,17 @@ impl WholeStreamCommand for Size {
size(args, registry)
}
fn examples(&self) -> &[Example] {
&[Example {
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Count the number of words in a string",
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()
}
#[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::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape};
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
use nu_source::Tagged;
pub struct Skip;
@ -33,10 +33,14 @@ impl WholeStreamCommand for Skip {
skip(args, registry)
}
fn examples(&self) -> &[Example] {
&[Example {
fn examples(&self) -> Vec<Example> {
vec![Example {
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())
}
#[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())
}
}
#[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())
}
}
#[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)
}
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 {
description: "Sort output by increasing file size",
example: "ls | sort-by size",
result: None,
},
Example {
description: "Sort output by type, and then by file size for each type",
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())
}
#[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());
}
#[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())
}
#[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())
}
#[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::utils::data_processing::{reducer_for, Reduce};
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, ReturnValue, Signature, Value};
use nu_protocol::{ReturnSuccess, ReturnValue, Signature, UntaggedValue, Value};
use num_traits::identities::Zero;
pub struct Sum;
@ -35,15 +35,17 @@ impl WholeStreamCommand for Sum {
})
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Sum a list of numbers",
example: "echo [1 2 3] | sum",
result: Some(vec![UntaggedValue::int(6).into()]),
},
Example {
description: "Get the disk usage for the current directory",
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())
}
#[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())
}
#[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))
}
#[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())
}
#[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())
}
}
#[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())
}
#[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())
}
#[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())
}
#[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)
}
fn examples(&self) -> &[Example] {
&[
fn examples(&self) -> Vec<Example> {
vec![
Example {
description:
"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 {
description:
"Outputs a formatted JSON string representing the contents of this table with an indentation setting of 4 spaces",
example: "to json --pretty 4",
"Outputs a formatted JSON string representing the contents of this table with an indentation setting of 2 spaces",
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())
}
#[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())
}
#[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())
}
#[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