use serde_json::{json, Value};
use flowcore::errors::Result;
use flowcore::flow_output;
use flowcore::RunAgain;
use flowmacro::flow_function;
#[flow_function]
fn inner_to_string(input: &Value) -> Result<(Option<Value>, RunAgain)> {
flow_output!(json!(input.to_string()))
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod test {
use std::collections::HashMap;
use serde_json::{json, Value};
use flowcore::model::datatype::NULL_TYPE;
use super::inner_to_string;
fn test_to_string(value: &Value, string: &str) {
let (result, _) = inner_to_string(value).expect("_to_string() failed");
match result {
Some(value) => assert_eq!(value.as_str(), Some(string)),
None => panic!("No Result returned"),
}
}
#[test]
fn test_null_input() {
test_to_string(&serde_json::Value::Null, NULL_TYPE);
}
#[test]
fn test_string_input() {
test_to_string(&json!("Hello World"), "\"Hello World\"");
}
#[test]
fn test_bool_input() {
test_to_string(&json!(true), "true");
test_to_string(&json!(false), "false");
}
#[test]
fn test_number_input() {
test_to_string(&json!(42), "42");
}
#[test]
fn test_array_input() {
test_to_string(&json!([1, 2, 3, 4]), "[1,2,3,4]");
}
#[test]
fn test_map_input() {
let mut map: HashMap<&str, u32> = HashMap::new();
map.insert("Meaning", 42);
map.insert("Size", 9);
test_to_string(&json!(map), "{\"Meaning\":42,\"Size\":9}");
}
}