Add debug and describe (#351)

* Add debug and describe

* Fix test
This commit is contained in:
JT 2021-11-19 18:00:29 +13:00 committed by GitHub
parent ff43ca4d24
commit e01e73cb67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 122 additions and 2 deletions

View File

@ -0,0 +1,57 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, Value};
#[derive(Clone)]
pub struct Debug;
impl Command for Debug {
fn name(&self) -> &str {
"debug"
}
fn usage(&self) -> &str {
"Debug print the value(s) piped in."
}
fn signature(&self) -> Signature {
Signature::build("describe").category(Category::Core)
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let config = stack.get_config()?;
input.map(
move |x| Value::String {
val: x.debug_string(", ", &config),
span: head,
},
engine_state.ctrlc.clone(),
)
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Describe the type of a string",
example: "'hello' | debug",
result: Some(Value::test_string("hello")),
}]
}
}
#[cfg(test)]
mod test {
#[test]
fn test_examples() {
use super::Debug;
use crate::test_examples;
test_examples(Debug {})
}
}

View File

@ -0,0 +1,55 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, Value};
#[derive(Clone)]
pub struct Describe;
impl Command for Describe {
fn name(&self) -> &str {
"describe"
}
fn usage(&self) -> &str {
"Describe the value(s) piped in."
}
fn signature(&self) -> Signature {
Signature::build("describe").category(Category::Core)
}
fn run(
&self,
engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
input.map(
move |x| Value::String {
val: x.get_type().to_string(),
span: head,
},
engine_state.ctrlc.clone(),
)
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Describe the type of a string",
example: "'hello' | describe",
result: Some(Value::test_string("string")),
}]
}
}
#[cfg(test)]
mod test {
#[test]
fn test_examples() {
use super::Describe;
use crate::test_examples;
test_examples(Describe {})
}
}

View File

@ -1,5 +1,7 @@
mod alias; mod alias;
mod debug;
mod def; mod def;
mod describe;
mod do_; mod do_;
mod echo; mod echo;
mod export; mod export;
@ -15,7 +17,9 @@ mod source;
mod use_; mod use_;
pub use alias::Alias; pub use alias::Alias;
pub use debug::Debug;
pub use def::Def; pub use def::Def;
pub use describe::Describe;
pub use do_::Do; pub use do_::Do;
pub use echo::Echo; pub use echo::Echo;
pub use export::ExportCommand; pub use export::ExportCommand;

View File

@ -36,7 +36,9 @@ pub fn create_default_context() -> EngineState {
DateNow, DateNow,
DateToTable, DateToTable,
DateToTimezone, DateToTimezone,
Debug,
Def, Def,
Describe,
Do, Do,
Each, Each,
Echo, Echo,

View File

@ -1120,7 +1120,7 @@ mod engine_state_tests {
} }
#[test] #[test]
fn merge_states() { fn merge_states() -> Result<(), ShellError> {
let mut engine_state = EngineState::new(); let mut engine_state = EngineState::new();
engine_state.add_file("test.nu".into(), vec![]); engine_state.add_file("test.nu".into(), vec![]);
@ -1130,10 +1130,12 @@ mod engine_state_tests {
working_set.render() working_set.render()
}; };
engine_state.merge_delta(delta); engine_state.merge_delta(delta)?;
assert_eq!(engine_state.num_files(), 2); assert_eq!(engine_state.num_files(), 2);
assert_eq!(&engine_state.files[0].0, "test.nu"); assert_eq!(&engine_state.files[0].0, "test.nu");
assert_eq!(&engine_state.files[1].0, "child.nu"); assert_eq!(&engine_state.files[1].0, "child.nu");
Ok(())
} }
} }