aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAzriel Hoh <azriel91@gmail.com>2018-11-12 10:09:30 +1300
committerAzriel Hoh <azriel91@gmail.com>2018-11-12 10:09:30 +1300
commit2907bd1953b15c81d4e7bfd1b060a9c9d947c2e4 (patch)
treeec3779ffc3c3362ee7203cb851ef829ded91ee86 /tests
parent2c2d62981279721c87128f988148c876c1caf18c (diff)
downloadmilf-rs-2907bd1953b15c81d4e7bfd1b060a9c9d947c2e4.tar.gz
milf-rs-2907bd1953b15c81d4e7bfd1b060a9c9d947c2e4.zip
Error when deserializing struct if encountering unknown fields.
Issue #225
Diffstat (limited to 'tests')
-rw-r--r--tests/enum_external_deserialize.rs40
1 files changed, 40 insertions, 0 deletions
diff --git a/tests/enum_external_deserialize.rs b/tests/enum_external_deserialize.rs
index 3309ac6..e35f06a 100644
--- a/tests/enum_external_deserialize.rs
+++ b/tests/enum_external_deserialize.rs
@@ -20,6 +20,46 @@ struct Multi {
enums: Vec<TheEnum>,
}
+#[test]
+fn invalid_variant_returns_error_with_good_message_string() {
+ let error = toml::from_str::<TheEnum>("\"NonExistent\"")
+ .expect_err("Expected deserialization to fail.");
+
+ assert_eq!(
+ error.to_string(),
+ "unknown variant `NonExistent`, expected one of `Plain`, `Tuple`, `NewType`, `Struct`"
+ );
+}
+
+#[test]
+fn invalid_variant_returns_error_with_good_message_inline_table() {
+ let error = toml::from_str::<TheEnum>("{ NonExistent = {} }")
+ .expect_err("Expected deserialization to fail.");
+ assert_eq!(
+ error.to_string(),
+ "unknown variant `NonExistent`, expected one of `Plain`, `Tuple`, `NewType`, `Struct`"
+ );
+}
+
+#[test]
+fn extra_field_returns_expected_empty_table_error() {
+ let error = toml::from_str::<TheEnum>("{ Plain = { extra_field = 404 } }")
+ .expect_err("Expected deserialization to fail.");
+
+ assert_eq!(error.to_string(), "expected empty table");
+}
+
+#[test]
+fn extra_field_returns_expected_empty_table_error_struct_variant() {
+ let error = toml::from_str::<TheEnum>("{ Struct = { value = 123, extra_0 = 0, extra_1 = 1 } }")
+ .expect_err("Expected deserialization to fail.");
+
+ assert_eq!(
+ error.to_string(),
+ r#"unexpected keys in table: `["extra_0", "extra_1"]`, available keys: `["value"]`"#
+ );
+}
+
mod enum_unit {
use super::*;