diff --git a/src/entity/aggregator/feature.rs b/src/entity/aggregator/feature.rs index b4be24f8..1f953e1c 100644 --- a/src/entity/aggregator/feature.rs +++ b/src/entity/aggregator/feature.rs @@ -4,6 +4,7 @@ use std::collections::HashSet; #[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, Hash)] pub enum Feature { TokenHash, + #[serde(untagged)] Unknown(String), } @@ -27,3 +28,70 @@ impl FromIterator for Features { Self(iter.into_iter().collect()) } } + +#[cfg(test)] +mod tests { + use serde_json::json; + + use crate::entity::aggregator::{Feature, Features}; + + #[test] + fn features_none() { + assert_eq!( + serde_json::from_value::(json!([])).unwrap(), + Features::from_iter([]) + ) + } + + #[test] + fn features_token_hash() { + assert_eq!( + serde_json::from_value::(json!(["TokenHash"])).unwrap(), + Features::from_iter([Feature::TokenHash]) + ); + assert_eq!( + serde_json::from_value::(json!(["TokenHash", "TokenHash", "TokenHash"])) + .unwrap(), + Features::from_iter([Feature::TokenHash]) + ); + } + + #[test] + fn features_unknown() { + assert_eq!( + serde_json::from_value::(json!(["UnspecifiedUnknownFeature"])).unwrap(), + Features::from_iter([Feature::Unknown("UnspecifiedUnknownFeature".to_string())]) + ); + + assert_eq!( + serde_json::from_value::(json!([ + "UnspecifiedUnknownFeature", + "UnspecifiedUnknownFeature", + "UnspecifiedUnknownFeature" + ])) + .unwrap(), + Features::from_iter([Feature::Unknown("UnspecifiedUnknownFeature".to_string())]) + ); + + assert_eq!( + serde_json::from_value::(json!([ + "UnspecifiedUnknownFeature", + "AnotherUnspecifiedUnknownFeature" + ])) + .unwrap(), + Features::from_iter([ + Feature::Unknown("UnspecifiedUnknownFeature".to_string()), + Feature::Unknown("AnotherUnspecifiedUnknownFeature".to_string()) + ]) + ); + + assert_eq!( + serde_json::from_value::(json!(["UnspecifiedUnknownFeature", "TokenHash"])) + .unwrap(), + Features::from_iter([ + Feature::TokenHash, + Feature::Unknown("UnspecifiedUnknownFeature".to_string()) + ]) + ); + } +}