chore: fix fmt
This commit is contained in:
parent
2c217c5261
commit
7a21db58bd
185
src/tests.rs
185
src/tests.rs
@ -1,103 +1,102 @@
|
|||||||
// use super::*;
|
// use super::*;
|
||||||
use nu_protocol::{Record, Span, Value};
|
use crate::helpers::{unwrap_value_key, value_to_serde_json, wrap_top_level_if_needed};
|
||||||
use tera::Tera;
|
use crate::{Render, TeraPlugin};
|
||||||
use crate::helpers::{unwrap_value_key, value_to_serde_json, wrap_top_level_if_needed};
|
use nu_protocol::{Record, Span, Value};
|
||||||
use crate::{Render, TeraPlugin};
|
use tera::Tera;
|
||||||
|
|
||||||
|
/// Runs the plugin test examples using nu_plugin_test_support.
|
||||||
|
#[test]
|
||||||
|
fn test_examples() -> Result<(), nu_protocol::ShellError> {
|
||||||
|
use nu_plugin_test_support::PluginTest;
|
||||||
|
|
||||||
/// Runs the plugin test examples using nu_plugin_test_support.
|
// This will automatically run the examples specified in your command and compare their actual
|
||||||
#[test]
|
// output against what was specified in the example. You can remove this test if the examples
|
||||||
fn test_examples() -> Result<(), nu_protocol::ShellError> {
|
// can't be tested this way, but we recommend including it if possible.
|
||||||
use nu_plugin_test_support::PluginTest;
|
|
||||||
|
|
||||||
// This will automatically run the examples specified in your command and compare their actual
|
PluginTest::new("tera", TeraPlugin.into())?.test_command_examples(&Render)
|
||||||
// output against what was specified in the example. You can remove this test if the examples
|
}
|
||||||
// can't be tested this way, but we recommend including it if possible.
|
#[test]
|
||||||
|
fn test_value_to_serde_json_record() {
|
||||||
|
let record = Record::from_raw_cols_vals(
|
||||||
|
vec!["name".to_string(), "age".to_string()],
|
||||||
|
vec![
|
||||||
|
Value::string("Akasha", Span::test_data()),
|
||||||
|
Value::int(42, Span::test_data()),
|
||||||
|
],
|
||||||
|
Span::test_data(),
|
||||||
|
Span::test_data(),
|
||||||
|
)
|
||||||
|
.expect("failed to create test record");
|
||||||
|
let val = Value::record(record, Span::test_data());
|
||||||
|
let json = value_to_serde_json(val).unwrap();
|
||||||
|
assert_eq!(json["name"], "Akasha");
|
||||||
|
assert_eq!(json["age"], 42);
|
||||||
|
}
|
||||||
|
|
||||||
PluginTest::new("tera", TeraPlugin.into())?.test_command_examples(&Render)
|
#[test]
|
||||||
}
|
fn test_value_to_serde_json_list() {
|
||||||
#[test]
|
let val = Value::list(
|
||||||
fn test_value_to_serde_json_record() {
|
vec![
|
||||||
let record = Record::from_raw_cols_vals(
|
Value::int(1, Span::test_data()),
|
||||||
vec!["name".to_string(), "age".to_string()],
|
Value::int(2, Span::test_data()),
|
||||||
vec![
|
],
|
||||||
Value::string("Akasha", Span::test_data()),
|
Span::test_data(),
|
||||||
Value::int(42, Span::test_data()),
|
);
|
||||||
],
|
let json = value_to_serde_json(val).unwrap();
|
||||||
Span::test_data(),
|
assert_eq!(json, serde_json::json!([1, 2]));
|
||||||
Span::test_data(),
|
}
|
||||||
)
|
|
||||||
.expect("failed to create test record");
|
|
||||||
let val = Value::record(record, Span::test_data());
|
|
||||||
let json = value_to_serde_json(val).unwrap();
|
|
||||||
assert_eq!(json["name"], "Akasha");
|
|
||||||
assert_eq!(json["age"], 42);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_value_to_serde_json_list() {
|
fn test_value_to_serde_json_string() {
|
||||||
let val = Value::list(
|
let val = Value::string("hello", Span::test_data());
|
||||||
vec![
|
let json = value_to_serde_json(val).unwrap();
|
||||||
Value::int(1, Span::test_data()),
|
assert_eq!(json, serde_json::json!("hello"));
|
||||||
Value::int(2, Span::test_data()),
|
}
|
||||||
],
|
|
||||||
Span::test_data(),
|
|
||||||
);
|
|
||||||
let json = value_to_serde_json(val).unwrap();
|
|
||||||
assert_eq!(json, serde_json::json!([1, 2]));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_value_to_serde_json_string() {
|
fn test_unwrap_value_key_simple() {
|
||||||
let val = Value::string("hello", Span::test_data());
|
let json = serde_json::json!({"value": {"name": "Akasha"}});
|
||||||
let json = value_to_serde_json(val).unwrap();
|
let unwrapped = unwrap_value_key(json);
|
||||||
assert_eq!(json, serde_json::json!("hello"));
|
assert_eq!(unwrapped["name"], "Akasha");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_unwrap_value_key_simple() {
|
fn test_unwrap_value_key_nested() {
|
||||||
let json = serde_json::json!({"value": {"name": "Akasha"}});
|
let json = serde_json::json!({"value": {"value": {"name": "Akasha"}}});
|
||||||
let unwrapped = unwrap_value_key(json);
|
let unwrapped = unwrap_value_key(json);
|
||||||
assert_eq!(unwrapped["name"], "Akasha");
|
assert_eq!(unwrapped["name"], "Akasha");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_unwrap_value_key_nested() {
|
fn test_unwrap_value_key_non_object() {
|
||||||
let json = serde_json::json!({"value": {"value": {"name": "Akasha"}}});
|
let json = serde_json::json!(42);
|
||||||
let unwrapped = unwrap_value_key(json);
|
let unwrapped = unwrap_value_key(json);
|
||||||
assert_eq!(unwrapped["name"], "Akasha");
|
assert_eq!(unwrapped["value"], 42);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_unwrap_value_key_non_object() {
|
fn test_unwrap_value_key_object() {
|
||||||
let json = serde_json::json!(42);
|
let json = serde_json::json!({"name": "Akasha"});
|
||||||
let unwrapped = unwrap_value_key(json);
|
let unwrapped = unwrap_value_key(json);
|
||||||
assert_eq!(unwrapped["value"], 42);
|
assert_eq!(unwrapped["name"], "Akasha");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_unwrap_value_key_object() {
|
fn test_render_pipeline() {
|
||||||
let json = serde_json::json!({"name": "Akasha"});
|
let template = "Hello, {{ name }}!";
|
||||||
let unwrapped = unwrap_value_key(json);
|
let mut tera = Tera::default();
|
||||||
assert_eq!(unwrapped["name"], "Akasha");
|
tera.add_raw_template("test", template).unwrap();
|
||||||
}
|
let record = Record::from_raw_cols_vals(
|
||||||
|
vec!["name".to_string()],
|
||||||
#[test]
|
vec![Value::string("Akasha", Span::test_data())],
|
||||||
fn test_render_pipeline() {
|
Span::test_data(),
|
||||||
let template = "Hello, {{ name }}!";
|
Span::test_data(),
|
||||||
let mut tera = Tera::default();
|
)
|
||||||
tera.add_raw_template("test", template).unwrap();
|
.expect("failed to create test record");
|
||||||
let record = Record::from_raw_cols_vals(
|
let val = Value::record(record, Span::test_data());
|
||||||
vec!["name".to_string()],
|
let context_json =
|
||||||
vec![Value::string("Akasha", Span::test_data())],
|
unwrap_value_key(wrap_top_level_if_needed(value_to_serde_json(val).unwrap()));
|
||||||
Span::test_data(),
|
let context = tera::Context::from_serialize(context_json).unwrap();
|
||||||
Span::test_data(),
|
let output = tera.render("test", &context).unwrap();
|
||||||
)
|
assert_eq!(output, "Hello, Akasha!");
|
||||||
.expect("failed to create test record");
|
}
|
||||||
let val = Value::record(record, Span::test_data());
|
|
||||||
let context_json =
|
|
||||||
unwrap_value_key(wrap_top_level_if_needed(value_to_serde_json(val).unwrap()));
|
|
||||||
let context = tera::Context::from_serialize(context_json).unwrap();
|
|
||||||
let output = tera.render("test", &context).unwrap();
|
|
||||||
assert_eq!(output, "Hello, Akasha!");
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user