1
use ron::error::{Error, Position, Span, SpannedError};
2

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

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

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

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

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

            
112
#[test]
113
4
fn test_implicit_some_requires_parens_after_some() {
114
    #[derive(Debug, PartialEq, serde::Deserialize)]
115
    struct Config {
116
        name: Option<String>,
117
        limit: Option<Option<u32>>,
118
    }
119

            
120
    // whitespace and comments are still allowed between `Some` and `(`
121
4
    assert_eq!(
122
4
        ron::from_str::<Config>(
123
4
            "#![enable(implicit_some)]\n(name: Some (\"a\"), limit: Some /* x */ (None))"
124
        ),
125
4
        Ok(Config {
126
4
            name: Some(String::from("a")),
127
4
            limit: Some(None),
128
4
        })
129
    );
130

            
131
    // `Some` without `(` is rejected like without implicit_some,
132
    //  instead of being dropped before the implicit `Some`
133
4
    assert_eq!(
134
4
        ron::from_str::<Config>("(name: Some \"a\", limit: None)"),
135
        Err(SpannedError {
136
            code: Error::ExpectedOption,
137
            span: Span {
138
                start: Position { line: 1, col: 12 },
139
                end: Position { line: 1, col: 13 },
140
            }
141
        })
142
    );
143
4
    assert_eq!(
144
4
        ron::from_str::<Config>("#![enable(implicit_some)]\n(name: Some \"a\", limit: None)"),
145
        Err(SpannedError {
146
            code: Error::ExpectedOption,
147
            span: Span {
148
                start: Position { line: 2, col: 12 },
149
                end: Position { line: 2, col: 13 },
150
            }
151
        })
152
    );
153
4
    assert_eq!(
154
4
        ron::from_str::<Config>("#![enable(implicit_some)]\n(name: \"a\", limit: Some None)"),
155
        Err(SpannedError {
156
            code: Error::ExpectedOption,
157
            span: Span {
158
                start: Position { line: 2, col: 24 },
159
                end: Position { line: 2, col: 25 },
160
            }
161
        })
162
    );
163
4
    assert_eq!(
164
4
        ron::from_str::<ron::Value>("#![enable(implicit_some)]\n[Some 5]"),
165
        Err(SpannedError {
166
            code: Error::ExpectedOption,
167
            span: Span {
168
                start: Position { line: 2, col: 6 },
169
                end: Position { line: 2, col: 7 },
170
            }
171
        })
172
    );
173
4
}
174

            
175
#[test]
176
4
fn fuzzer_found_issues() {
177
    #[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
178
    struct Some_();
179

            
180
    #[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
181
    struct None_();
182

            
183
4
    let ser_some = ron::ser::to_string_pretty(
184
4
        &Some(Some_()),
185
4
        ron::ser::PrettyConfig::default()
186
4
            .struct_names(true)
187
4
            .extensions(ron::extensions::Extensions::IMPLICIT_SOME),
188
    )
189
4
    .unwrap();
190
4
    assert_eq!(ser_some, "#![enable(implicit_some)]\nSome_()");
191

            
192
4
    let ser_none = ron::ser::to_string_pretty(
193
4
        &Some(None_()),
194
4
        ron::ser::PrettyConfig::default()
195
4
            .struct_names(true)
196
4
            .extensions(ron::extensions::Extensions::IMPLICIT_SOME),
197
    )
198
4
    .unwrap();
199
4
    assert_eq!(ser_none, "#![enable(implicit_some)]\nNone_()");
200

            
201
4
    assert_eq!(
202
4
        ron::from_str::<Option<Some_>>(&ser_some).unwrap(),
203
        Some(Some_())
204
    );
205
4
    assert_eq!(
206
4
        ron::from_str::<Option<None_>>(&ser_none).unwrap(),
207
        Some(None_())
208
    );
209
4
}