midiserde 0.1.1

When mini isn't enough and serde is too much
Documentation
//! Tests for `#[mini(skip)]`, `#[mini(skip_serializing)]`, `#[mini(skip_deserializing)]`.
//!
//! Run: `cargo test -p midiserde skip`

use midiserde::{from_value, json, to_value, Deserialize, Serialize};

// ── skip_serializing ──

#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct WithSkipSer {
    a: String,
    #[mini(skip_serializing)]
    b: String,
}

#[test]
fn skip_serializing_omit_from_output() {
    let v = WithSkipSer {
        a: "x".into(),
        b: "secret".into(),
    };
    let j = json::to_string(&v);
    assert!(j.contains(r#""a":"x""#));
    assert!(!j.contains(r#""b""#));
}

#[test]
fn skip_serializing_deserialize_both() {
    let json_str = r#"{"a": "x", "b": "y"}"#;
    let value: miniserde::json::Value = json::from_str(json_str).unwrap();
    let v: WithSkipSer = from_value(&value).unwrap();
    assert_eq!(v.a, "x");
    assert_eq!(v.b, "y");
}

// ── skip_deserializing ──

#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct WithSkipDe {
    a: String,
    #[mini(skip_deserializing)]
    b: u32,
}

#[test]
fn skip_deserializing_use_default_when_absent() {
    let json_str = r#"{"a": "x"}"#;
    let value: miniserde::json::Value = json::from_str(json_str).unwrap();
    let v: WithSkipDe = from_value(&value).unwrap();
    assert_eq!(v.a, "x");
    assert_eq!(v.b, 0);
}

#[test]
fn skip_deserializing_serialize_both() {
    let v = WithSkipDe {
        a: "x".into(),
        b: 42,
    };
    let j = json::to_string(&v);
    assert!(j.contains(r#""a":"x""#));
    assert!(j.contains(r#""b":42"#));
}

// ── skip (both) ──

#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct WithSkip {
    a: String,
    #[mini(skip)]
    b: u32,
}

#[test]
fn skip_never_in_json() {
    let v = WithSkip {
        a: "x".into(),
        b: 99,
    };
    let j = json::to_string(&v);
    assert!(j.contains(r#""a":"x""#));
    assert!(!j.contains(r#""b""#));
}

#[test]
fn skip_always_default_on_deserialize() {
    let json_str = r#"{"a": "x"}"#;
    let value: miniserde::json::Value = json::from_str(json_str).unwrap();
    let v: WithSkip = from_value(&value).unwrap();
    assert_eq!(v.a, "x");
    assert_eq!(v.b, 0);
}

// ── skip_deserializing + default ──

#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct WithSkipDeAndDefault {
    a: String,
    #[mini(skip_deserializing, default)]
    b: Option<String>,
}

#[test]
fn skip_deserializing_with_default() {
    let json_str = r#"{"a": "x"}"#;
    let value: miniserde::json::Value = json::from_str(json_str).unwrap();
    let v: WithSkipDeAndDefault = from_value(&value).unwrap();
    assert_eq!(v.a, "x");
    assert_eq!(v.b, None);
}

// ── roundtrip ──

#[test]
fn skip_serializing_roundtrip() {
    // skip_serializing omits "b" from output, so we use JSON that includes "b" to simulate
    // deserializing from a source that includes the field
    let json_str = r#"{"a": "alpha", "b": "beta"}"#;
    let value: miniserde::json::Value = json::from_str(json_str).unwrap();
    let restored: WithSkipSer = from_value(&value).unwrap();
    assert_eq!(restored.a, "alpha");
    assert_eq!(restored.b, "beta");
}

#[test]
fn skip_deserializing_roundtrip() {
    // skip_deserializing: we serialize both fields, but on deserialize "b" is never
    // read from input and always comes from Default::default()
    let original = WithSkipDe {
        a: "alpha".into(),
        b: 100,
    };
    let value = to_value(&original);
    let restored: WithSkipDe = from_value(&value).unwrap();
    assert_eq!(original.a, restored.a);
    assert_eq!(restored.b, 0); // b is skip_deserializing, so always Default
}