Skip to content

Commit

Permalink
Add test for enum SerializableObjectWithState
Browse files Browse the repository at this point in the history
Signed-off-by: Bhavy Airi <airiragahv@gmail.com>
  • Loading branch information
Bhavy Airi committed Apr 30, 2023
1 parent 036bbf9 commit ec399a7
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions aries_vcx/src/utils/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,55 @@ pub enum SerializableObjectWithState<T, P> {
thread_id: String,
},
}

#[cfg(test)]
mod tests {
use super::*;
use serde_json;

#[test]
fn test_serialize() {
let value = SerializableObjectWithState::V1 {
data: vec!["name".to_string(), "age".to_string()],
state: "1".to_string(),
source_id: "foo".to_string(),
thread_id: "bat".to_string()
};

let serialized = serde_json::to_string(&value).unwrap();

assert!(serialized.contains(r#""data":["name","age"]"#));
assert!(serialized.contains(r#""state":"1""#));
assert!(serialized.contains(r#""source_id":"foo""#));
assert!(serialized.contains(r#""thread_id":"bat""#));

}

#[test]
fn test_deserialize() {
let serialized = r#"
{
"data": [
"name",
"age"
],
"state": "1",
"source_id": "foo",
"thread_id": "bat",
"version": "1.0"
}
"#;

let result = serde_json::from_str(&serialized);
let ans: SerializableObjectWithState<Vec<String>, String> = result.unwrap();

let (data, state, source_id, thread_id) = match ans {
SerializableObjectWithState::V1 { data, state, source_id, thread_id } => (data, state, source_id, thread_id),
};

assert_eq!(data, vec!["name".to_string(), "age".to_string()]);
assert_eq!(state, "1");
assert_eq!(source_id, "foo");
assert_eq!(thread_id, "bat");
}
}

0 comments on commit ec399a7

Please sign in to comment.