Repository: toml-rs/toml
Pull request #448 (2023-01-17 19:59:19)

test(toml): Leverage snapbox


Patch:
diff --git a/Cargo.lock b/Cargo.lock
index 440bf4e9..5e212963 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -894,6 +894,7 @@ dependencies = [
  "serde",
  "serde_json",
  "serde_spanned",
+ "snapbox",
  "toml-test-harness",
  "toml_datetime",
 ]
diff --git a/crates/toml/Cargo.toml b/crates/toml/Cargo.toml
index b91b726c..267b2d51 100644
--- a/crates/toml/Cargo.toml
+++ b/crates/toml/Cargo.toml
@@ -53,6 +53,7 @@ serde_spanned = { version = "0.5.0", path = "../serde_spanned", features = ["ser
 serde = { version = "1.0.152", features = ["derive"] }
 serde_json = "1.0.91"
 toml-test-harness = "0.4.2"
+snapbox = "0.4.3"
 
 [[test]]
 name = "decoder_compliance"


Test patch:
diff --git a/crates/toml/tests/testsuite/datetime.rs b/crates/toml/tests/testsuite/datetime.rs
index 0b5cac55..780f1f0d 100644
--- a/crates/toml/tests/testsuite/datetime.rs
+++ b/crates/toml/tests/testsuite/datetime.rs
@@ -4,7 +4,7 @@ macro_rules! bad {
     ($toml:expr, $msg:expr) => {
         match $toml.parse::<toml::Value>() {
             Ok(s) => panic!("parsed to: {:#?}", s),
-            Err(e) => assert_eq!(e.to_string(), $msg),
+            Err(e) => snapbox::assert_eq($msg, e.to_string()),
         }
     };
 }
diff --git a/crates/toml/tests/testsuite/de_errors.rs b/crates/toml/tests/testsuite/de_errors.rs
index 77db7a60..f8ff07b5 100644
--- a/crates/toml/tests/testsuite/de_errors.rs
+++ b/crates/toml/tests/testsuite/de_errors.rs
@@ -5,7 +5,7 @@ macro_rules! bad {
     ($toml:expr, $ty:ty, $msg:expr) => {
         match toml::from_str::<$ty>($toml) {
             Ok(s) => panic!("parsed to: {:#?}", s),
-            Err(e) => assert_eq!(e.to_string(), $msg),
+            Err(e) => snapbox::assert_eq($msg, e.to_string()),
         }
     };
 }
diff --git a/crates/toml/tests/testsuite/enum_external_deserialize.rs b/crates/toml/tests/testsuite/enum_external_deserialize.rs
index c3bbcb36..e3450c8f 100644
--- a/crates/toml/tests/testsuite/enum_external_deserialize.rs
+++ b/crates/toml/tests/testsuite/enum_external_deserialize.rs
@@ -27,18 +27,18 @@ struct Multi {
 fn invalid_variant_returns_error_with_good_message_string() {
     let error = toml::from_str::<TheEnum>("\"NonExistent\"").unwrap_err();
 
-    assert_eq!(
+    snapbox::assert_eq(
+        "unknown variant `NonExistent`, expected one of `Plain`, `Tuple`, `NewType`, `Struct`",
         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 = {} }").unwrap_err();
-    assert_eq!(
+    snapbox::assert_eq(
+        "unknown variant `NonExistent`, expected one of `Plain`, `Tuple`, `NewType`, `Struct`",
         error.to_string(),
-        "unknown variant `NonExistent`, expected one of `Plain`, `Tuple`, `NewType`, `Struct`"
     );
 }
 
@@ -46,7 +46,7 @@ fn invalid_variant_returns_error_with_good_message_inline_table() {
 fn extra_field_returns_expected_empty_table_error() {
     let error = toml::from_str::<TheEnum>("{ Plain = { extra_field = 404 } }").unwrap_err();
 
-    assert_eq!(error.to_string(), "expected empty table");
+    snapbox::assert_eq("expected empty table", error.to_string());
 }
 
 #[test]
@@ -54,9 +54,9 @@ 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 } }")
         .unwrap_err();
 
-    assert_eq!(
+    snapbox::assert_eq(
+        r#"unexpected keys in table: `["extra_0", "extra_1"]`, available keys: `["value"]`"#,
         error.to_string(),
-        r#"unexpected keys in table: `["extra_0", "extra_1"]`, available keys: `["value"]`"#
     );
 }
 
diff --git a/crates/toml/tests/testsuite/invalid_misc.rs b/crates/toml/tests/testsuite/invalid_misc.rs
index e89455ab..f5d67cf4 100644
--- a/crates/toml/tests/testsuite/invalid_misc.rs
+++ b/crates/toml/tests/testsuite/invalid_misc.rs
@@ -2,7 +2,7 @@ macro_rules! bad {
     ($toml:expr, $msg:expr) => {
         match $toml.parse::<toml::Value>() {
             Ok(s) => panic!("parsed to: {:#?}", s),
-            Err(e) => assert_eq!(e.to_string(), $msg),
+            Err(e) => snapbox::assert_eq($msg, e.to_string()),
         }
     };
 }
diff --git a/crates/toml/tests/testsuite/parser.rs b/crates/toml/tests/testsuite/parser.rs
index e4fa1977..ce77b5cb 100644
--- a/crates/toml/tests/testsuite/parser.rs
+++ b/crates/toml/tests/testsuite/parser.rs
@@ -4,7 +4,7 @@ macro_rules! bad {
     ($toml:expr, $msg:expr) => {
         match $toml.parse::<toml::Value>() {
             Ok(s) => panic!("parsed to: {:#?}", s),
-            Err(e) => assert_eq!(e.to_string(), $msg),
+            Err(e) => snapbox::assert_eq($msg, e.to_string()),
         }
     };
 }
diff --git a/crates/toml/tests/testsuite/pretty.rs b/crates/toml/tests/testsuite/pretty.rs
index 6d182e0a..dab72c8a 100644
--- a/crates/toml/tests/testsuite/pretty.rs
+++ b/crates/toml/tests/testsuite/pretty.rs
@@ -1,4 +1,5 @@
 use serde::ser::Serialize;
+use snapbox::assert_eq;
 
 const NO_PRETTY: &str = "\
 [example]
@@ -62,7 +63,7 @@ fn pretty_std() {
         .unwrap();
     println!("EXPECTED:\n{}", toml);
     println!("\nRESULT:\n{}", result);
-    assert_eq!(toml, &result);
+    assert_eq(toml, &result);
 }
 
 const PRETTY_INDENT_2: &str = "\
@@ -261,7 +262,7 @@ fn table_array() {
         .unwrap();
     println!("EXPECTED:\n{}", toml);
     println!("\nRESULT:\n{}", result);
-    assert_eq!(toml, &result);
+    assert_eq(toml, &result);
 }
 
 const PRETTY_TRICKY_NON_LITERAL: &str = r##"[example]
@@ -307,5 +308,5 @@ fn pretty_tricky_non_literal() {
     }
     println!("EXPECTED:\n{}", toml);
     println!("\nRESULT:\n{}", result);
-    assert_eq!(toml, &result);
+    assert_eq(toml, &result);
 }
diff --git a/crates/toml/tests/testsuite/serde.rs b/crates/toml/tests/testsuite/serde.rs
index 280954ee..309f7e54 100644
--- a/crates/toml/tests/testsuite/serde.rs
+++ b/crates/toml/tests/testsuite/serde.rs
@@ -1,7 +1,7 @@
 use serde::Deserialize;
 use serde::Deserializer;
 use serde::Serialize;
-use std::collections::{BTreeMap, HashSet};
+use std::collections::BTreeMap;
 
 use toml::map::Map;
 use toml::Value;
@@ -29,9 +29,9 @@ macro_rules! equivalent {
 
         // Through a string equivalent
         println!("to_string(literal)");
-        assert_eq!(t!(toml::to_string(&literal)), toml.to_string());
+        snapbox::assert_eq(toml.to_string(), t!(toml::to_string(&literal)));
         println!("to_string(toml)");
-        assert_eq!(t!(toml::to_string(&toml)), toml.to_string());
+        snapbox::assert_eq(toml.to_string(), t!(toml::to_string(&toml)));
         println!("literal, from_str(toml)");
         assert_eq!(literal, t!(toml::from_str(&toml.to_string())));
         println!("toml, from_str(toml)");
@@ -44,13 +44,13 @@ macro_rules! error {
         println!("attempting parsing");
         match toml::from_str::<$ty>(&$toml.to_string()) {
             Ok(_) => panic!("successful"),
-            Err(e) => assert_eq!(e.to_string(), $msg_parse),
+            Err(e) => snapbox::assert_eq($msg_parse, e.to_string()),
         }
 
         println!("attempting toml decoding");
         match $toml.try_into::<$ty>() {
             Ok(_) => panic!("successful"),
-            Err(e) => assert_eq!(e.to_string(), $msg_decode),
+            Err(e) => snapbox::assert_eq($msg_decode, e.to_string()),
         }
     }};
 }
@@ -205,6 +205,8 @@ fn inner_structs_with_options() {
 #[test]
 #[cfg(feature = "preserve_order")]
 fn hashmap() {
+    use std::collections::HashSet;
+
     #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
     struct Foo {
         set: HashSet<char>,
@@ -600,7 +602,7 @@ fn table_structs_empty() {
     );
     expected.insert("foo".to_string(), CanBeEmpty::default());
     assert_eq!(value, expected);
-    assert_eq!(toml::to_string(&value).unwrap(), text);
+    snapbox::assert_eq(text, toml::to_string(&value).unwrap());
 }
 
 #[test]