Skip to content

Commit

Permalink
test serde code
Browse files Browse the repository at this point in the history
  • Loading branch information
Aloso committed May 17, 2024
1 parent 7e3ae9c commit 6681890
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pomsky-bin/src/result/serde_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,26 @@ impl Expected for ExpectedCode {
write!(formatter, "diagnostic code")
}
}

#[test]
fn test_serde() {
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Example {
#[serde(with = "self", skip_serializing_if = "Option::is_none", default)]
pub code: Option<DiagnosticCode>,
}

let value = Example { code: Some(DiagnosticCode::CaptureInLet) };
let serialized = serde_json::to_string(&value).unwrap();

assert_eq!(&serialized, r#"{"code":"P0308"}"#);
assert_eq!(serde_json::from_str::<Example>(&serialized).unwrap(), value);

let value_empty = Example { code: None };
let serialized_empty = serde_json::to_string(&value_empty).unwrap();

assert_eq!(&serialized_empty, "{}");
assert_eq!(serde_json::from_str::<Example>(&serialized_empty).unwrap(), value_empty);
}

0 comments on commit 6681890

Please sign in to comment.