serde_test.rs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. use flowy_ot::{
  2. attributes::{Attributes, AttributesBuilder},
  3. delta::Delta,
  4. operation::{OpBuilder, Operation, Retain},
  5. };
  6. #[test]
  7. fn operation_insert_serialize_test() {
  8. let attributes = AttributesBuilder::new().bold().italic().build();
  9. let operation = OpBuilder::insert("123")
  10. .attributes(Some(attributes))
  11. .build();
  12. let json = serde_json::to_string(&operation).unwrap();
  13. eprintln!("{}", json);
  14. let insert_op: Operation = serde_json::from_str(&json).unwrap();
  15. assert_eq!(insert_op, operation);
  16. }
  17. #[test]
  18. fn operation_retain_serialize_test() {
  19. let operation = Operation::Retain(12.into());
  20. let json = serde_json::to_string(&operation).unwrap();
  21. eprintln!("{}", json);
  22. let insert_op: Operation = serde_json::from_str(&json).unwrap();
  23. assert_eq!(insert_op, operation);
  24. }
  25. #[test]
  26. fn operation_delete_serialize_test() {
  27. let operation = Operation::Delete(2);
  28. let json = serde_json::to_string(&operation).unwrap();
  29. let insert_op: Operation = serde_json::from_str(&json).unwrap();
  30. assert_eq!(insert_op, operation);
  31. }
  32. #[test]
  33. fn delta_serialize_test() {
  34. let mut delta = Delta::default();
  35. let attributes = AttributesBuilder::new().bold().italic().build();
  36. let retain = OpBuilder::insert("123")
  37. .attributes(Some(attributes))
  38. .build();
  39. delta.add(retain);
  40. delta.add(Operation::Retain(5.into()));
  41. delta.add(Operation::Delete(3));
  42. let json = serde_json::to_string(&delta).unwrap();
  43. eprintln!("{}", json);
  44. let delta_from_json: Delta = serde_json::from_str(&json).unwrap();
  45. assert_eq!(delta_from_json, delta);
  46. }