aboutsummaryrefslogtreecommitdiff
path: root/src/agent.rs
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2020-10-01 17:04:55 +0200
committerDaniel Schadt <kingdread@gmx.de>2020-10-01 17:04:55 +0200
commit60f8e6a1e547f7059a69cb71714f34aed5797dd5 (patch)
tree9b8351bf510e40326d974c70dacc4620ab9be143 /src/agent.rs
parent62249e950c0c5dee0b09b962842543fd60c9ccb4 (diff)
downloadevtclib-60f8e6a1e547f7059a69cb71714f34aed5797dd5.tar.gz
evtclib-60f8e6a1e547f7059a69cb71714f34aed5797dd5.tar.bz2
evtclib-60f8e6a1e547f7059a69cb71714f34aed5797dd5.zip
add Agent serialization tests
Diffstat (limited to 'src/agent.rs')
-rw-r--r--src/agent.rs53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/agent.rs b/src/agent.rs
index 11f81d1..bb5acd6 100644
--- a/src/agent.rs
+++ b/src/agent.rs
@@ -730,3 +730,56 @@ impl Agent<Character> {
self.character().name()
}
}
+
+#[cfg(all(feature = "serde", test))]
+mod tests {
+ use super::*;
+
+ fn agent() -> Agent {
+ Agent {
+ addr: 0xdeadbeef,
+ kind: AgentKind::Character(Character {
+ id: 0xf00,
+ name: "Foo Bar".into(),
+ }),
+ toughness: -13,
+ concentration: -14,
+ healing: -15,
+ condition: -16,
+ instance_id: 1337,
+ first_aware: 0,
+ last_aware: 0xffffff,
+ master_agent: None,
+ phantom_data: PhantomData,
+ }
+ }
+
+ #[test]
+ fn serialization() {
+ let agent = agent();
+ let json = serde_json::to_string(&agent).unwrap();
+ let expected = r#"{"addr":3735928559,"kind":{"Character":{"id":3840,"name":"Foo Bar"}},"toughness":-13,"concentration":-14,"healing":-15,"condition":-16,"instance_id":1337,"first_aware":0,"last_aware":16777215,"master_agent":null}"#;
+ assert_eq!(json, expected);
+ }
+
+ #[test]
+ fn deserialization() {
+ let json = r#"{"addr":3735928559,"kind":{"Character":{"id":3840,"name":"Foo Bar"}},"toughness":-13,"concentration":-14,"healing":-15,"condition":-16,"instance_id":1337,"first_aware":0,"last_aware":16777215,"master_agent":null}"#;
+ let deserialized: Agent = serde_json::from_str(json).unwrap();
+ assert_eq!(deserialized, agent());
+ }
+
+ #[test]
+ #[should_panic(expected = "missing field `master_agent`")]
+ fn deserialization_missing_field() {
+ let json = r#"{"addr":3735928559,"kind":{"Character":{"id":3840,"name":"Foo Bar"}},"toughness":-13,"concentration":-14,"healing":-15,"condition":-16,"instance_id":1337,"first_aware":0,"last_aware":16777215}"#;
+ serde_json::from_str::<Agent>(json).unwrap();
+ }
+
+ #[test]
+ #[should_panic(expected = "duplicate field `master_agent`")]
+ fn deserialization_duplicated_field() {
+ let json = r#"{"addr":3735928559,"kind":{"Character":{"id":3840,"name":"Foo Bar"}},"toughness":-13,"concentration":-14,"healing":-15,"condition":-16,"instance_id":1337,"first_aware":0,"last_aware":16777215,"master_agent":null,"master_agent":null}"#;
+ serde_json::from_str::<Agent>(json).unwrap();
+ }
+}