1
88
#[derive(Debug, PartialEq, serde::Deserialize, serde::Serialize)]
2
struct MaybeFields {
3
    f1: i64,
4
    f2: Option<i64>,
5
    f3: Option<Option<i64>>,
6
}
7

            
8
#[test]
9
4
fn test_recursive_implicit_some() {
10
4
    // Test case provided by d86leader in
11
4
    //  https://github.com/ron-rs/ron/issues/367#issue-1147920589
12
4

            
13
4
    let x1: std::result::Result<MaybeFields, _> =
14
4
        ron::from_str("#![enable(implicit_some)]\n(f1: 1)");
15
4
    let x2: std::result::Result<MaybeFields, _> =
16
4
        ron::from_str("#![enable(implicit_some)]\n(f1: 1, f2: None, f3: None)");
17
4
    let x3: std::result::Result<MaybeFields, _> =
18
4
        ron::from_str("#![enable(implicit_some)]\n(f1: 1, f2: 2, f3: 3)");
19
4
    let x4: std::result::Result<MaybeFields, _> =
20
4
        ron::from_str("#![enable(implicit_some)]\n(f1: 1, f2: 2, f3: Some(3))");
21
4
    let x5: std::result::Result<MaybeFields, _> =
22
4
        ron::from_str("#![enable(implicit_some)]\n(f1: 1, f2: 2, f3: Some(Some(3)))");
23
4
    let x6: std::result::Result<MaybeFields, _> =
24
4
        ron::from_str("#![enable(implicit_some)]\n(f1: 1, f2: 2, f3: Some(None))");
25
4

            
26
4
    assert_eq!(
27
4
        x1,
28
4
        Ok(MaybeFields {
29
4
            f1: 1,
30
4
            f2: None,
31
4
            f3: None
32
4
        })
33
4
    );
34
4
    assert_eq!(
35
4
        x2,
36
4
        Ok(MaybeFields {
37
4
            f1: 1,
38
4
            f2: None,
39
4
            f3: None
40
4
        })
41
4
    );
42
4
    assert_eq!(
43
4
        x3,
44
4
        Ok(MaybeFields {
45
4
            f1: 1,
46
4
            f2: Some(2),
47
4
            f3: Some(Some(3))
48
4
        })
49
4
    );
50
4
    assert_eq!(
51
4
        x4,
52
4
        Ok(MaybeFields {
53
4
            f1: 1,
54
4
            f2: Some(2),
55
4
            f3: Some(Some(3))
56
4
        })
57
4
    );
58
4
    assert_eq!(
59
4
        x5,
60
4
        Ok(MaybeFields {
61
4
            f1: 1,
62
4
            f2: Some(2),
63
4
            f3: Some(Some(3))
64
4
        })
65
4
    );
66
4
    assert_eq!(
67
4
        x6,
68
4
        Ok(MaybeFields {
69
4
            f1: 1,
70
4
            f2: Some(2),
71
4
            f3: Some(None)
72
4
        })
73
4
    );
74
4
}
75

            
76
#[test]
77
4
fn test_nested_implicit_some() {
78
4
    assert_eq!(
79
4
        ron::from_str::<Option<Option<Option<u32>>>>("#![enable(implicit_some)]\n5"),
80
4
        Ok(Some(Some(Some(5))))
81
4
    );
82
4
    assert_eq!(
83
4
        ron::from_str::<Option<Option<Option<u32>>>>("#![enable(implicit_some)]\nNone"),
84
4
        Ok(None)
85
4
    );
86
4
    assert_eq!(
87
4
        ron::from_str::<Option<Option<Option<u32>>>>("#![enable(implicit_some)]\nSome(5)"),
88
4
        Ok(Some(Some(Some(5))))
89
4
    );
90
4
    assert_eq!(
91
4
        ron::from_str::<Option<Option<Option<u32>>>>("#![enable(implicit_some)]\nSome(None)"),
92
4
        Ok(Some(None))
93
4
    );
94
4
    assert_eq!(
95
4
        ron::from_str::<Option<Option<Option<u32>>>>("#![enable(implicit_some)]\nSome(Some(5))"),
96
4
        Ok(Some(Some(Some(5))))
97
4
    );
98
4
    assert_eq!(
99
4
        ron::from_str::<Option<Option<Option<u32>>>>("#![enable(implicit_some)]\nSome(Some(None))"),
100
4
        Ok(Some(Some(None)))
101
4
    );
102
4
    assert_eq!(
103
4
        ron::from_str::<Option<Option<Option<u32>>>>(
104
4
            "#![enable(implicit_some)]\nSome(Some(Some(5)))"
105
4
        ),
106
4
        Ok(Some(Some(Some(5))))
107
4
    );
108
4
}
109

            
110
#[test]
111
4
fn fuzzer_found_issues() {
112
4
    #[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
113
    struct Some_();
114

            
115
4
    #[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
116
    struct None_();
117

            
118
4
    let ser_some = ron::ser::to_string_pretty(
119
4
        &Some(Some_()),
120
4
        ron::ser::PrettyConfig::default()
121
4
            .struct_names(true)
122
4
            .extensions(ron::extensions::Extensions::IMPLICIT_SOME),
123
4
    )
124
4
    .unwrap();
125
4
    assert_eq!(ser_some, "#![enable(implicit_some)]\nSome_()");
126

            
127
4
    let ser_none = ron::ser::to_string_pretty(
128
4
        &Some(None_()),
129
4
        ron::ser::PrettyConfig::default()
130
4
            .struct_names(true)
131
4
            .extensions(ron::extensions::Extensions::IMPLICIT_SOME),
132
4
    )
133
4
    .unwrap();
134
4
    assert_eq!(ser_none, "#![enable(implicit_some)]\nNone_()");
135

            
136
4
    assert_eq!(
137
4
        ron::from_str::<Option<Some_>>(&ser_some).unwrap(),
138
4
        Some(Some_())
139
4
    );
140
4
    assert_eq!(
141
4
        ron::from_str::<Option<None_>>(&ser_none).unwrap(),
142
4
        Some(None_())
143
4
    );
144
4
}