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