| 1 |
#[macro_use] |
| 2 |
extern crate serde_derive; |
| 3 |
extern crate toml; |
| 4 |
|
| 5 |
#[derive(Debug, Deserialize, PartialEq)] |
| 6 |
struct OuterStruct { |
| 7 |
inner: TheEnum, |
| 8 |
} |
| 9 |
|
| 10 |
#[derive(Debug, Deserialize, PartialEq)] |
| 11 |
enum TheEnum { |
| 12 |
Plain, |
| 13 |
Tuple(i64, bool), |
| 14 |
NewType(String), |
| 15 |
Struct { value: i64 }, |
| 16 |
} |
| 17 |
|
| 18 |
#[derive(Debug, Deserialize, PartialEq)] |
| 19 |
struct Val { |
| 20 |
val: TheEnum, |
| 21 |
} |
| 22 |
|
| 23 |
#[derive(Debug, Deserialize, PartialEq)] |
| 24 |
struct Multi { |
| 25 |
enums: Vec<TheEnum>, |
| 26 |
} |
| 27 |
|
| 28 |
#[test] |
| 29 |
fn invalid_variant_returns_error_with_good_message_string() { |
| 30 |
let error = toml::from_str::<TheEnum>("\"NonExistent\"").unwrap_err(); |
| 31 |
|
| 32 |
assert_eq!( |
| 33 |
error.to_string(), |
| 34 |
"unknown variant `NonExistent`, expected one of `Plain`, `Tuple`, `NewType`, `Struct`" |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
#[test] |
| 39 |
fn invalid_variant_returns_error_with_good_message_inline_table() { |
| 40 |
let error = toml::from_str::<TheEnum>("{ NonExistent = {} }").unwrap_err(); |
| 41 |
assert_eq!( |
| 42 |
error.to_string(), |
| 43 |
"unknown variant `NonExistent`, expected one of `Plain`, `Tuple`, `NewType`, `Struct`" |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
#[test] |
| 48 |
fn extra_field_returns_expected_empty_table_error() { |
| 49 |
let error = toml::from_str::<TheEnum>("{ Plain = { extra_field = 404 } }").unwrap_err(); |
| 50 |
|
| 51 |
assert_eq!(error.to_string(), "expected empty table"); |
| 52 |
} |
| 53 |
|
| 54 |
#[test] |
| 55 |
fn extra_field_returns_expected_empty_table_error_struct_variant() { |
| 56 |
let error = toml::from_str::<TheEnum>("{ Struct = { value = 123, extra_0 = 0, extra_1 = 1 } }") |
| 57 |
.unwrap_err(); |
| 58 |
|
| 59 |
assert_eq!( |
| 60 |
error.to_string(), |
| 61 |
r#"unexpected keys in table: `["extra_0", "extra_1"]`, available keys: `["value"]`"# |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
mod enum_unit { |
| 66 |
use super::*; |
| 67 |
|
| 68 |
#[test] |
| 69 |
fn from_str() { |
| 70 |
assert_eq!(TheEnum::Plain, toml::from_str("\"Plain\"").unwrap()); |
| 71 |
} |
| 72 |
|
| 73 |
#[test] |
| 74 |
fn from_inline_table() { |
| 75 |
assert_eq!(TheEnum::Plain, toml::from_str("{ Plain = {} }").unwrap()); |
| 76 |
assert_eq!( |
| 77 |
Val { |
| 78 |
val: TheEnum::Plain |
| 79 |
}, |
| 80 |
toml::from_str("val = { Plain = {} }").unwrap() |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
#[test] |
| 85 |
fn from_dotted_table() { |
| 86 |
assert_eq!(TheEnum::Plain, toml::from_str("[Plain]\n").unwrap()); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
mod enum_tuple { |
| 91 |
use super::*; |
| 92 |
|
| 93 |
#[test] |
| 94 |
fn from_inline_table() { |
| 95 |
assert_eq!( |
| 96 |
TheEnum::Tuple(-123, true), |
| 97 |
toml::from_str("{ Tuple = { 0 = -123, 1 = true } }").unwrap() |
| 98 |
); |
| 99 |
assert_eq!( |
| 100 |
Val { |
| 101 |
val: TheEnum::Tuple(-123, true) |
| 102 |
}, |
| 103 |
toml::from_str("val = { Tuple = { 0 = -123, 1 = true } }").unwrap() |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
#[test] |
| 108 |
fn from_dotted_table() { |
| 109 |
assert_eq!( |
| 110 |
TheEnum::Tuple(-123, true), |
| 111 |
toml::from_str( |
| 112 |
r#"[Tuple] |
| 113 |
0 = -123 |
| 114 |
1 = true |
| 115 |
"# |
| 116 |
) |
| 117 |
.unwrap() |
| 118 |
); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
mod enum_newtype { |
| 123 |
use super::*; |
| 124 |
|
| 125 |
#[test] |
| 126 |
fn from_inline_table() { |
| 127 |
assert_eq!( |
| 128 |
TheEnum::NewType("value".to_string()), |
| 129 |
toml::from_str(r#"{ NewType = "value" }"#).unwrap() |
| 130 |
); |
| 131 |
assert_eq!( |
| 132 |
Val { |
| 133 |
val: TheEnum::NewType("value".to_string()), |
| 134 |
}, |
| 135 |
toml::from_str(r#"val = { NewType = "value" }"#).unwrap() |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
#[test] |
| 140 |
#[ignore = "Unimplemented: https://github.com/alexcrichton/toml-rs/pull/264#issuecomment-431707209"] |
| 141 |
fn from_dotted_table() { |
| 142 |
assert_eq!( |
| 143 |
TheEnum::NewType("value".to_string()), |
| 144 |
toml::from_str(r#"NewType = "value""#).unwrap() |
| 145 |
); |
| 146 |
assert_eq!( |
| 147 |
Val { |
| 148 |
val: TheEnum::NewType("value".to_string()), |
| 149 |
}, |
| 150 |
toml::from_str( |
| 151 |
r#"[val] |
| 152 |
NewType = "value" |
| 153 |
"# |
| 154 |
) |
| 155 |
.unwrap() |
| 156 |
); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
mod enum_struct { |
| 161 |
use super::*; |
| 162 |
|
| 163 |
#[test] |
| 164 |
fn from_inline_table() { |
| 165 |
assert_eq!( |
| 166 |
TheEnum::Struct { value: -123 }, |
| 167 |
toml::from_str("{ Struct = { value = -123 } }").unwrap() |
| 168 |
); |
| 169 |
assert_eq!( |
| 170 |
Val { |
| 171 |
val: TheEnum::Struct { value: -123 } |
| 172 |
}, |
| 173 |
toml::from_str("val = { Struct = { value = -123 } }").unwrap() |
| 174 |
); |
| 175 |
} |
| 176 |
|
| 177 |
#[test] |
| 178 |
fn from_dotted_table() { |
| 179 |
assert_eq!( |
| 180 |
TheEnum::Struct { value: -123 }, |
| 181 |
toml::from_str( |
| 182 |
r#"[Struct] |
| 183 |
value = -123 |
| 184 |
"# |
| 185 |
) |
| 186 |
.unwrap() |
| 187 |
); |
| 188 |
} |
| 189 |
|
| 190 |
#[test] |
| 191 |
fn from_nested_dotted_table() { |
| 192 |
assert_eq!( |
| 193 |
OuterStruct { |
| 194 |
inner: TheEnum::Struct { value: -123 } |
| 195 |
}, |
| 196 |
toml::from_str( |
| 197 |
r#"[inner.Struct] |
| 198 |
value = -123 |
| 199 |
"# |
| 200 |
) |
| 201 |
.unwrap() |
| 202 |
); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
mod enum_array { |
| 207 |
use super::*; |
| 208 |
|
| 209 |
#[test] |
| 210 |
fn from_inline_tables() { |
| 211 |
let toml_str = r#" |
| 212 |
enums = [ |
| 213 |
{ Plain = {} }, |
| 214 |
{ Tuple = { 0 = -123, 1 = true } }, |
| 215 |
{ NewType = "value" }, |
| 216 |
{ Struct = { value = -123 } } |
| 217 |
]"#; |
| 218 |
assert_eq!( |
| 219 |
Multi { |
| 220 |
enums: vec![ |
| 221 |
TheEnum::Plain, |
| 222 |
TheEnum::Tuple(-123, true), |
| 223 |
TheEnum::NewType("value".to_string()), |
| 224 |
TheEnum::Struct { value: -123 }, |
| 225 |
] |
| 226 |
}, |
| 227 |
toml::from_str(toml_str).unwrap() |
| 228 |
); |
| 229 |
} |
| 230 |
|
| 231 |
#[test] |
| 232 |
#[ignore = "Unimplemented: https://github.com/alexcrichton/toml-rs/pull/264#issuecomment-431707209"] |
| 233 |
fn from_dotted_table() { |
| 234 |
let toml_str = r#"[[enums]] |
| 235 |
Plain = {} |
| 236 |
|
| 237 |
[[enums]] |
| 238 |
Tuple = { 0 = -123, 1 = true } |
| 239 |
|
| 240 |
[[enums]] |
| 241 |
NewType = "value" |
| 242 |
|
| 243 |
[[enums]] |
| 244 |
Struct = { value = -123 } |
| 245 |
"#; |
| 246 |
assert_eq!( |
| 247 |
Multi { |
| 248 |
enums: vec![ |
| 249 |
TheEnum::Plain, |
| 250 |
TheEnum::Tuple(-123, true), |
| 251 |
TheEnum::NewType("value".to_string()), |
| 252 |
TheEnum::Struct { value: -123 }, |
| 253 |
] |
| 254 |
}, |
| 255 |
toml::from_str(toml_str).unwrap() |
| 256 |
); |
| 257 |
} |
| 258 |
} |