summaryrefslogtreecommitdiffstats
path: root/vendor/pulldown-cmark/tests/serde.rs
blob: 7098bfbe8d0bc2f96f0b3095e6e5340d0fb7958c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#[cfg(feature = "serde")]
mod tests {
    use std::convert::TryInto;

    use pulldown_cmark::CowStr;

    #[test]
    fn cow_str_to_str_round_trip_bincode() {
        for i in &[
            CowStr::Borrowed("dssa"),
            CowStr::Borrowed(""),
            CowStr::Boxed("hello".to_owned().into_boxed_str()),
            CowStr::Boxed("".to_owned().into_boxed_str()),
            CowStr::Inlined("inline".try_into().unwrap()),
            CowStr::Inlined("".try_into().unwrap()),
        ] {
            let encoded = bincode::serialize(i).unwrap();
            let decoded1: CowStr = bincode::deserialize(&encoded).unwrap();
            let decoded2: String = bincode::deserialize(&encoded).unwrap();
            let decoded3: &str = bincode::deserialize(&encoded).unwrap();

            assert_eq!(&decoded1, i);
            assert_eq!(decoded2, i.as_ref());
            assert_eq!(decoded3, i.as_ref());
        }
    }

    #[test]
    fn cow_str_to_str_round_trip_json() {
        for i in &[
            CowStr::Borrowed("dssa"),
            CowStr::Borrowed(""),
            CowStr::Boxed("hello".to_owned().into_boxed_str()),
            CowStr::Boxed("".to_owned().into_boxed_str()),
            CowStr::Inlined("inline".try_into().unwrap()),
            CowStr::Inlined("".try_into().unwrap()),
        ] {
            let encoded = serde_json::to_string(i).unwrap();
            let decoded1: CowStr = serde_json::from_str(&encoded).unwrap();
            let decoded2: String = serde_json::from_str(&encoded).unwrap();
            let decoded3: &str = serde_json::from_str(&encoded).unwrap();

            assert_eq!(&decoded1, i);
            assert_eq!(decoded2, i.as_ref());
            assert_eq!(decoded3, i.as_ref());
        }
    }

    #[test]
    fn str_to_cow_str_json() {
        let str = "a borrowed str";
        let string = "a owned str".to_owned();

        let encoded_str = serde_json::to_string(&str).unwrap();
        let encoded_string = serde_json::to_string(&string).unwrap();

        let decoded_str: CowStr = serde_json::from_str(&encoded_str).unwrap();
        let decoded_string: CowStr = serde_json::from_str(&encoded_string).unwrap();

        assert_eq!(decoded_str.as_ref(), str);
        assert_eq!(decoded_string.as_ref(), string);
    }

    #[test]
    fn str_to_cow_str_bincode() {
        let str = "a borrowed str";
        let string = "a owned str".to_owned();

        let encoded_str = bincode::serialize(&str).unwrap();
        let encoded_string = bincode::serialize(&string).unwrap();

        let decoded_str: CowStr = bincode::deserialize(&encoded_str).unwrap();
        let decoded_string: CowStr = bincode::deserialize(&encoded_string).unwrap();

        assert_eq!(decoded_str.as_ref(), str);
        assert_eq!(decoded_string.as_ref(), string);
    }
}