example unit test

This commit is contained in:
Fernando Herrera
2021-10-09 14:10:10 +01:00
parent 5021d61800
commit e3e4ae0591
10 changed files with 285 additions and 25 deletions

View File

@ -1,6 +1,6 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{IntoValueStream, ShellError, Signature, Span, Value};
use nu_protocol::{Example, IntoValueStream, ShellError, Signature, Span, Value};
pub struct FromJson;
@ -21,6 +21,50 @@ impl Command for FromJson {
)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
example: "'{ a:1 }' | from json",
description: "Converts json formatted string to table",
result: Some(Value::Record {
cols: vec!["a".to_string()],
vals: vec![Value::Int {
val: 1,
span: Span::unknown(),
}],
span: Span::unknown(),
}),
},
Example {
example: "'{ a:1, b: [1, 2] }' | from json",
description: "Converts json formatted string to table",
result: Some(Value::Record {
cols: vec!["a".to_string(), "b".to_string()],
vals: vec![
Value::Int {
val: 1,
span: Span::unknown(),
},
Value::List {
vals: vec![
Value::Int {
val: 1,
span: Span::unknown(),
},
Value::Int {
val: 2,
span: Span::unknown(),
},
],
span: Span::unknown(),
},
],
span: Span::unknown(),
}),
},
]
}
fn run(
&self,
_context: &EvaluationContext,
@ -109,3 +153,15 @@ fn convert_string_to_value(string_input: String, span: Span) -> Result<Value, Sh
)),
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(FromJson {})
}
}