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

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

            
7
24
#[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
#[test]
68
4
fn reader_io_error() {
69
    struct Reader<'a> {
70
        buf: &'a [u8],
71
    }
72

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

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