| 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 |
fn default_options() { |
| 12 |
let ron = Options::default(); |
| 13 |
|
| 14 |
let de: Struct = ron.from_str("(Some(42),(4.2))").unwrap(); |
| 15 |
let ser = ron.to_string(&de).unwrap(); |
| 16 |
|
| 17 |
assert_eq!(ser, "(Some(42),(4.2))"); |
| 18 |
} |
| 19 |
|
| 20 |
#[test] |
| 21 |
fn without_any_options() { |
| 22 |
let mut ron = Options::default().with_default_extension(Extensions::all()); |
| 23 |
for extension in Extensions::all().iter() { |
| 24 |
ron = ron.without_default_extension(extension); |
| 25 |
} |
| 26 |
|
| 27 |
let de: Struct = ron.from_str("(Some(42),(4.2))").unwrap(); |
| 28 |
let ser = ron.to_string(&de).unwrap(); |
| 29 |
|
| 30 |
assert_eq!(ser, "(Some(42),(4.2))"); |
| 31 |
} |
| 32 |
|
| 33 |
#[test] |
| 34 |
fn single_default_extension() { |
| 35 |
let ron = Options::default().with_default_extension(Extensions::IMPLICIT_SOME); |
| 36 |
|
| 37 |
let de: Struct = ron.from_str("(42,(4.2))").unwrap(); |
| 38 |
let ser = ron.to_string(&de).unwrap(); |
| 39 |
|
| 40 |
assert_eq!(ser, "(42,(4.2))"); |
| 41 |
|
| 42 |
let de: Struct = ron.from_str("#)").unwrap(); |
| 43 |
let ser = ron.to_string(&de).unwrap(); |
| 44 |
|
| 45 |
assert_eq!(ser, "(42,(4.2))"); |
| 46 |
|
| 47 |
let de: Struct = ron |
| 48 |
.from_str("#![enable(implicit_some)]#") |
| 49 |
.unwrap(); |
| 50 |
let ser = ron.to_string(&de).unwrap(); |
| 51 |
|
| 52 |
assert_eq!(ser, "(42,(4.2))"); |
| 53 |
|
| 54 |
let de: Struct = ron |
| 55 |
.from_str("#![enable(implicit_some)]#") |
| 56 |
.unwrap(); |
| 57 |
let ser = ron |
| 58 |
.to_string_pretty( |
| 59 |
&de, |
| 60 |
PrettyConfig::default().extensions(Extensions::UNWRAP_NEWTYPES), |
| 61 |
) |
| 62 |
.unwrap(); |
| 63 |
|
| 64 |
assert_eq!(ser, "#![enable(unwrap_newtypes)]\n(42, 4.2)"); |
| 65 |
} |
| 66 |
|
| 67 |
#[test] |
| 68 |
fn reader_io_error() { |
| 69 |
struct Reader<'a> { |
| 70 |
buf: &'a [u8], |
| 71 |
} |
| 72 |
|
| 73 |
impl<'a> std::io::Read for Reader<'a> { |
| 74 |
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { |
| 75 |
let written = self.buf.read(buf)?; |
| 76 |
if written == 0 { |
| 77 |
Err(std::io::Error::new(std::io::ErrorKind::BrokenPipe, "oh no")) |
| 78 |
} else { |
| 79 |
Ok(written) |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
assert_eq!( |
| 85 |
ron::de::from_reader::<Reader, ()>(Reader { buf: b"" }).unwrap_err(), |
| 86 |
ron::error::SpannedError { |
| 87 |
code: ron::Error::Io(String::from("oh no")), |
| 88 |
position: ron::error::Position { line: 1, col: 1 }, |
| 89 |
} |
| 90 |
); |
| 91 |
assert_eq!( |
| 92 |
ron::de::from_reader::<Reader, ()>(Reader { buf: b"hello" }).unwrap_err(), |
| 93 |
ron::error::SpannedError { |
| 94 |
code: ron::Error::Io(String::from("oh no")), |
| 95 |
position: ron::error::Position { line: 1, col: 6 }, |
| 96 |
} |
| 97 |
); |
| 98 |
assert_eq!( |
| 99 |
ron::de::from_reader::<Reader, ()>(Reader { |
| 100 |
buf: b"hello\nmy \xff" |
| 101 |
}) |
| 102 |
.unwrap_err(), |
| 103 |
ron::error::SpannedError { |
| 104 |
code: ron::Error::Io(String::from("oh no")), |
| 105 |
position: ron::error::Position { line: 2, col: 4 }, |
| 106 |
} |
| 107 |
); |
| 108 |
} |