From 8756e88e3c9a5eb53d5c6c8f8536fd9acc4205eb Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Sat, 9 Oct 2021 14:28:09 +0100 Subject: [PATCH] command split --- crates/nu-command/src/example_test.rs | 5 +- crates/nu-command/src/strings/split/chars.rs | 80 ++++++++++---------- 2 files changed, 44 insertions(+), 41 deletions(-) diff --git a/crates/nu-command/src/example_test.rs b/crates/nu-command/src/example_test.rs index 9d546407a1..366ab69cb7 100644 --- a/crates/nu-command/src/example_test.rs +++ b/crates/nu-command/src/example_test.rs @@ -7,7 +7,7 @@ use nu_protocol::{ Value, }; -use super::From; +use super::{From, Split}; pub fn test_examples(cmd: impl Command + 'static) { let examples = cmd.examples(); @@ -15,10 +15,11 @@ pub fn test_examples(cmd: impl Command + 'static) { let delta = { // Base functions that are needed for testing - // Try to keep this working set as small to keep tests running as fast as possible + // Try to keep this working set small to keep tests running as fast as possible let engine_state = engine_state.borrow(); let mut working_set = StateWorkingSet::new(&*engine_state); working_set.add_decl(Box::new(From)); + working_set.add_decl(Box::new(Split)); // Adding the command that is being tested to the working set working_set.add_decl(Box::new(cmd)); diff --git a/crates/nu-command/src/strings/split/chars.rs b/crates/nu-command/src/strings/split/chars.rs index 39df74ab9f..382b50ee9d 100644 --- a/crates/nu-command/src/strings/split/chars.rs +++ b/crates/nu-command/src/strings/split/chars.rs @@ -19,6 +19,38 @@ impl Command for SubCommand { "splits a string's characters into separate rows" } + fn examples(&self) -> Vec { + vec![Example { + description: "Split the string's characters into separate rows", + example: "'hello' | split chars", + result: Some(Value::List { + vals: vec![ + Value::String { + val: "h".into(), + span: Span::unknown(), + }, + Value::String { + val: "e".into(), + span: Span::unknown(), + }, + Value::String { + val: "l".into(), + span: Span::unknown(), + }, + Value::String { + val: "l".into(), + span: Span::unknown(), + }, + Value::String { + val: "o".into(), + span: Span::unknown(), + }, + ], + span: Span::unknown(), + }), + }] + } + fn run( &self, _context: &EvaluationContext, @@ -27,35 +59,6 @@ impl Command for SubCommand { ) -> Result { split_chars(call, input) } - - fn examples(&self) -> Vec { - vec![Example { - description: "Split the string's characters into separate rows", - example: "echo 'hello' | split chars", - result: Some(vec![ - Value::String { - val: "h".into(), - span: Span::unknown(), - }, - Value::String { - val: "e".into(), - span: Span::unknown(), - }, - Value::String { - val: "l".into(), - span: Span::unknown(), - }, - Value::String { - val: "l".into(), - span: Span::unknown(), - }, - Value::String { - val: "o".into(), - span: Span::unknown(), - }, - ]), - }] - } } fn split_chars(call: &Call, input: Value) -> Result { @@ -86,15 +89,14 @@ fn split_chars_helper(v: &Value, name: Span) -> Vec { } } -// #[cfg(test)] -// mod tests { -// use super::ShellError; -// use super::SubCommand; +#[cfg(test)] +mod test { + use super::*; -// #[test] -// fn examples_work_as_expected() -> Result<(), ShellError> { -// use crate::examples::test as test_examples; + #[test] + fn test_examples() { + use crate::test_examples; -// test_examples(SubCommand {}) -// } -// } + test_examples(SubCommand {}) + } +}