1
use ron::{extensions::Extensions, ser::PrettyConfig, Options};
2
use serde::{Deserialize, Serialize};
3

            
4
#[derive(Serialize, Deserialize)]
5
struct Newtype(f64);
6

            
7
#[derive(Serialize, Deserialize)]
8
struct Struct(Option<u32>, Newtype);
9

            
10
#[test]
11
4
fn default_options() {
12
4
    let ron = Options::default();
13
4

            
14
4
    let de: Struct = ron.from_str("(Some(42),(4.2))").unwrap();
15
4
    let ser = ron.to_string(&de).unwrap();
16
4

            
17
4
    assert_eq!(ser, "(Some(42),(4.2))");
18
4
}
19

            
20
#[test]
21
4
fn without_any_options() {
22
4
    let mut ron = Options::default().with_default_extension(Extensions::all());
23
16
    for extension in Extensions::all().iter() {
24
16
        ron = ron.without_default_extension(extension);
25
16
    }
26

            
27
4
    let de: Struct = ron.from_str("(Some(42),(4.2))").unwrap();
28
4
    let ser = ron.to_string(&de).unwrap();
29
4

            
30
4
    assert_eq!(ser, "(Some(42),(4.2))");
31
4
}
32

            
33
#[test]
34
4
fn single_default_extension() {
35
4
    let ron = Options::default().with_default_extension(Extensions::IMPLICIT_SOME);
36
4

            
37
4
    let de: Struct = ron.from_str("(42,(4.2))").unwrap();
38
4
    let ser = ron.to_string(&de).unwrap();
39
4

            
40
4
    assert_eq!(ser, "(42,(4.2))");
41

            
42
4
    let de: Struct = ron.from_str("#![enable(implicit_some)](42,(4.2))").unwrap();
43
4
    let ser = ron.to_string(&de).unwrap();
44
4

            
45
4
    assert_eq!(ser, "(42,(4.2))");
46

            
47
4
    let de: Struct = ron
48
4
        .from_str("#![enable(implicit_some)]#![enable(unwrap_newtypes)](42,4.2)")
49
4
        .unwrap();
50
4
    let ser = ron.to_string(&de).unwrap();
51
4

            
52
4
    assert_eq!(ser, "(42,(4.2))");
53

            
54
4
    let de: Struct = ron
55
4
        .from_str("#![enable(implicit_some)]#![enable(unwrap_newtypes)](42,4.2)")
56
4
        .unwrap();
57
4
    let ser = ron
58
4
        .to_string_pretty(
59
4
            &de,
60
4
            PrettyConfig::default().extensions(Extensions::UNWRAP_NEWTYPES),
61
4
        )
62
4
        .unwrap();
63
4

            
64
4
    assert_eq!(ser, "#![enable(unwrap_newtypes)]\n(42, 4.2)");
65
4
}
66

            
67
#[cfg(feature = "std")]
68
#[test]
69
4
fn reader_io_error() {
70
    struct Reader<'a> {
71
        buf: &'a [u8],
72
    }
73

            
74
    impl<'a> std::io::Read for Reader<'a> {
75
20
        fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
76
20
            let written = self.buf.read(buf)?;
77
20
            if written == 0 {
78
12
                Err(std::io::Error::new(std::io::ErrorKind::BrokenPipe, "oh no"))
79
            } else {
80
8
                Ok(written)
81
            }
82
20
        }
83
    }
84

            
85
4
    assert_eq!(
86
4
        ron::de::from_reader::<Reader, ()>(Reader { buf: b"" }).unwrap_err(),
87
4
        ron::error::SpannedError {
88
4
            code: ron::Error::Io(String::from("oh no")),
89
4
            position: ron::error::Position { line: 1, col: 1 },
90
4
        }
91
4
    );
92
4
    assert_eq!(
93
4
        ron::de::from_reader::<Reader, ()>(Reader { buf: b"hello" }).unwrap_err(),
94
4
        ron::error::SpannedError {
95
4
            code: ron::Error::Io(String::from("oh no")),
96
4
            position: ron::error::Position { line: 1, col: 6 },
97
4
        }
98
4
    );
99
4
    assert_eq!(
100
4
        ron::de::from_reader::<Reader, ()>(Reader {
101
4
            buf: b"hello\nmy \xff"
102
4
        })
103
4
        .unwrap_err(),
104
4
        ron::error::SpannedError {
105
4
            code: ron::Error::Io(String::from("oh no")),
106
4
            position: ron::error::Position { line: 2, col: 4 },
107
4
        }
108
4
    );
109
4
}