| 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 | #[cfg(feature = "std")] |
| 68 | #[test] |
| 69 | fn reader_io_error() { |
| 70 | struct Reader<'a> { |
| 71 | buf: &'a [u8], |
| 72 | } |
| 73 | |
| 74 | impl<'a> std::io::Read for Reader<'a> { |
| 75 | fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { |
| 76 | let written = self.buf.read(buf)?; |
| 77 | if written == 0 { |
| 78 | Err(std::io::Error::new(std::io::ErrorKind::BrokenPipe, "oh no")) |
| 79 | } else { |
| 80 | Ok(written) |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | assert_eq!( |
| 86 | ron::de::from_reader::<Reader, ()>(Reader { buf: b"" }).unwrap_err(), |
| 87 | ron::error::SpannedError { |
| 88 | code: ron::Error::Io(String::from("oh no")), |
| 89 | span: ron::error::Span { |
| 90 | start: ron::error::Position { line: 1, col: 1 }, |
| 91 | end: ron::error::Position { line: 1, col: 1 }, |
| 92 | } |
| 93 | } |
| 94 | ); |
| 95 | assert_eq!( |
| 96 | ron::de::from_reader::<Reader, ()>(Reader { buf: b"hello" }).unwrap_err(), |
| 97 | ron::error::SpannedError { |
| 98 | code: ron::Error::Io(String::from("oh no")), |
| 99 | span: ron::error::Span { |
| 100 | start: ron::error::Position { line: 1, col: 1 }, |
| 101 | end: ron::error::Position { line: 1, col: 6 }, |
| 102 | } |
| 103 | } |
| 104 | ); |
| 105 | assert_eq!( |
| 106 | ron::de::from_reader::<Reader, ()>(Reader { |
| 107 | buf: b"hello\nmy \xff" |
| 108 | }) |
| 109 | .unwrap_err(), |
| 110 | ron::error::SpannedError { |
| 111 | code: ron::Error::Io(String::from("oh no")), |
| 112 | span: ron::error::Span { |
| 113 | start: ron::error::Position { line: 1, col: 1 }, |
| 114 | end: ron::error::Position { line: 2, col: 4 }, |
| 115 | } |
| 116 | } |
| 117 | ); |
| 118 | } |