duesee / imap-codec

Rock-solid and complete codec for IMAP
Apache License 2.0
40 stars 13 forks source link

Replacing `serde_json` by `serde_tests` for tests #549

Open HenningHolmDE opened 4 months ago

HenningHolmDE commented 4 months ago

Currently, the unit tests for Deserialize/Serialize implementation are based on serde_json.

serde_test might to be a more fitting alternative to be used in unit tests.

It should be evaluated and, if reasonable, used for replacing all serde_json-based unit tests.

HenningHolmDE commented 4 months ago

Just to provide this as a strarting point: I did try out serde_test once for test_serialize_fragment:

#[test]
fn test_serialize_fragment() {
let fragments = [
    Fragment::Line {
        data: b"A LOGIN alice {2}\r\n".to_vec(),
    },
    Fragment::Literal {
        data: b"\xCA\xFE".to_vec(),
        mode: LiteralMode::Sync,
    },
    Fragment::Line {
        data: b"\r\n".to_vec(),
    },
];

// using serde_json
assert_eq!(
    serde_json::to_string(&fragments).unwrap(),
    r#"[{"Line":{"data":[65,32,76,79,71,73,78,32,97,108,105,99,101,32,123,50,125,13,10]}},{"Literal":{"data":[202,254],"mode":"Sync"}},{"Line":{"data":[13,10]}}]"#
);

// using serde_test
assert_ser_tokens(
    &fragments,
    &[
        Token::Tuple { len: 3 },
        Token::StructVariant {
            name: "Fragment",
            variant: "Line",
            len: 1,
        },
        Token::Str("data"),
        Token::Seq { len: Some(19) },
        Token::U8(65),
        Token::U8(32),
        Token::U8(76),
        Token::U8(79),
        Token::U8(71),
        Token::U8(73),
        Token::U8(78),
        Token::U8(32),
        Token::U8(97),
        Token::U8(108),
        Token::U8(105),
        Token::U8(99),
        Token::U8(101),
        Token::U8(32),
        Token::U8(123),
        Token::U8(50),
        Token::U8(125),
        Token::U8(13),
        Token::U8(10),
        Token::SeqEnd,
        Token::StructVariantEnd,
        Token::StructVariant {
            name: "Fragment",
            variant: "Literal",
            len: 2,
        },
        Token::Str("data"),
        Token::Seq { len: Some(2) },
        Token::U8(202),
        Token::U8(254),
        Token::SeqEnd,
        Token::Str("mode"),
        Token::UnitVariant {
            name: "LiteralMode",
            variant: "Sync",
        },
        Token::StructVariantEnd,
        Token::StructVariant {
            name: "Fragment",
            variant: "Line",
            len: 1,
        },
        Token::Str("data"),
        Token::Seq { len: Some(2) },
        Token::U8(13),
        Token::U8(10),
        Token::SeqEnd,
        Token::StructVariantEnd,
        Token::TupleEnd,
    ],
);
jonassmedegaard commented 2 months ago

minor nit: title talks about serde_tests (plural) whereas proposal is to use serde_test (singular).