use forma_derive::{Deserialize, Serialize};
use forma_json::{from_str, to_string};
#[derive(Debug, PartialEq, Serialize, Deserialize)]
enum Color {
Red,
Green,
Blue,
#[forma(other)]
Unknown,
}
#[test]
fn test_other_known_variant() {
assert_eq!(from_str::<Color>("\"Red\"").unwrap(), Color::Red);
assert_eq!(from_str::<Color>("\"Green\"").unwrap(), Color::Green);
assert_eq!(from_str::<Color>("\"Blue\"").unwrap(), Color::Blue);
}
#[test]
fn test_other_unknown_variant() {
assert_eq!(from_str::<Color>("\"Yellow\"").unwrap(), Color::Unknown);
assert_eq!(from_str::<Color>("\"Purple\"").unwrap(), Color::Unknown);
assert_eq!(from_str::<Color>("\"\"").unwrap(), Color::Unknown);
}
#[test]
fn test_other_serialize() {
assert_eq!(to_string(&Color::Unknown).unwrap(), "\"Unknown\"");
assert_eq!(to_string(&Color::Red).unwrap(), "\"Red\"");
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
enum Status {
Active,
Inactive,
#[forma(other)]
#[forma(rename = "other")]
Other,
}
#[test]
fn test_other_with_rename() {
assert_eq!(from_str::<Status>("\"Active\"").unwrap(), Status::Active);
assert_eq!(from_str::<Status>("\"SomeNewStatus\"").unwrap(), Status::Other);
assert_eq!(to_string(&Status::Other).unwrap(), "\"other\"");
}