mod common;
use common::{Basic, WithDefaults};
#[cfg(feature = "base64")]
use common::WithBase64;
use midiserde::{from_value, json, to_value};
#[test]
fn from_value_basic() {
let json_str = r#"{"name": "hello", "count": 42}"#;
let value: miniserde::json::Value = json::from_str(json_str).unwrap();
let v: Basic = from_value(&value).unwrap();
assert_eq!(v.name, "hello");
assert_eq!(v.count, 42);
}
#[test]
fn from_value_with_defaults() {
let json_str = r#"{"name": "partial"}"#;
let value: miniserde::json::Value = json::from_str(json_str).unwrap();
let v: WithDefaults = from_value(&value).unwrap();
assert_eq!(v.name, "partial");
assert_eq!(v.retries, 0);
assert_eq!(v.timeout_ms, 0);
}
#[cfg(feature = "base64")]
#[test]
fn from_value_with_base64() {
let json_str = r#"{"name": "test", "payload": "SGVsbG8="}"#;
let value: miniserde::json::Value = json::from_str(json_str).unwrap();
let v: WithBase64 = from_value(&value).unwrap();
assert_eq!(v.name, "test");
assert_eq!(v.payload, b"Hello");
}
#[test]
fn roundtrip_through_value() {
let original = Basic {
name: "roundtrip".into(),
count: 99,
};
let j = json::to_string(&original);
let value: miniserde::json::Value = json::from_str(&j).unwrap();
let restored: Basic = from_value(&value).unwrap();
assert_eq!(original, restored);
}
#[test]
fn to_value_basic() {
let v = Basic {
name: "test".into(),
count: 42,
};
let value = to_value(&v);
let j = json::to_string(&value);
assert!(j.contains(r#""name":"test""#));
assert!(j.contains(r#""count":42"#));
}
#[test]
fn to_value_roundtrip() {
let original = Basic {
name: "roundtrip".into(),
count: 99,
};
let value = to_value(&original);
let restored: Basic = from_value(&value).unwrap();
assert_eq!(original, restored);
}
#[cfg(feature = "base64")]
#[test]
fn to_value_with_base64() {
let v = WithBase64 {
name: "encoded".into(),
payload: b"Hello".to_vec(),
};
let value = to_value(&v);
let j = json::to_string(&value);
assert!(j.contains(r#""payload":"SGVsbG8=""#));
}
#[cfg(feature = "base64")]
#[test]
fn from_value_invalid_base64() {
let json_str = r#"{"name": "test", "payload": "!!invalid!!"}"#;
let value: miniserde::json::Value = json::from_str(json_str).unwrap();
let result: Result<WithBase64, _> = from_value(&value);
assert!(result.is_err());
}