message: Add some basic serde tests
diff: @@ -20,3 +20,84 @@ where
  {
  serde_json::to_vec(data).map_err(|e| VmError::serialize_err(type_name::<T>(), e))
  }
  +
  +#[cfg(test)]
  +mod tests {
  + use super::*;
  + use serde::Deserialize;
  +
  + #[derive(Serialize, Deserialize, Debug, PartialEq)]
  + #[serde(rename_all = "snake_case")]
  + enum SomeMsg {
  + Refund {},
  + ReleaseAll {
  + image: String,
  + amount: u32,
  + time: u64,
  + karma: i32,
  + },
  + Cowsay {
  + text: String,
  + },
  + }
  +
  + #[test]
  + fn from_slice_works() {
  + let deserialized: SomeMsg = from_slice(br#"{"refund":{}}"#).unwrap();
  + assert_eq!(deserialized, SomeMsg::Refund {});
  +
  + let deserialized: SomeMsg = from_slice(
  + br#"{"release_all":{"image":"foo","amount":42,"time":18446744073709551615,"karma":-17}}"#,
  + )
  + .unwrap();
  + assert_eq!(
  + deserialized,
  + SomeMsg::ReleaseAll {
  + image: "foo".to_string(),
  + amount: 42,
  + time: 18446744073709551615,
  + karma: -17
  + }
  + );
  + }
  +
  + #[test]
  + fn from_slice_works_for_special_chars() {
  + let deserialized: SomeMsg =
  + from_slice(br#"{"cowsay":{"text":"foo\"bar\\\"bla"}}"#).unwrap();
  + assert_eq!(
  + deserialized,
  + SomeMsg::Cowsay {
  + text: "foo\"bar\\\"bla".to_string(),
  + }
  + );
  + }
  +
  + #[test]
  + fn to_vec_works() {
  + let msg = SomeMsg::Refund {};
  + let serialized = to_vec(&msg).unwrap();
  + assert_eq!(serialized, br#"{"refund":{}}"#);
  +
  + let msg = SomeMsg::ReleaseAll {
  + image: "foo".to_string(),
  + amount: 42,
  + time: 9007199254740999, // Number.MAX_SAFE_INTEGER + 7
  + karma: -17,
  + };
  + let serialized = String::from_utf8(to_vec(&msg).unwrap()).unwrap();
  + assert_eq!(
  + serialized,
  + r#"{"release_all":{"image":"foo","amount":42,"time":9007199254740999,"karma":-17}}"#
  + );
  + }
  +
  + #[test]
  + fn to_vec_works_for_special_chars() {
  + let msg = SomeMsg::Cowsay {
  + text: "foo\"bar\\\"bla".to_string(),
  + };
  + let serialized = String::from_utf8(to_vec(&msg).unwrap()).unwrap();
  + assert_eq!(serialized, r#"{"cowsay":{"text":"foo\"bar\\\"bla"}}"#);
  + }
  +}
  