serde_test.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. use lib_ot::core::{
  2. AttributeBuilder, Changeset, NodeData, NodeDataBuilder, NodeOperation, NodeTree, NodeTreeContext, Path,
  3. };
  4. use lib_ot::text_delta::DeltaTextOperationBuilder;
  5. #[test]
  6. fn operation_insert_node_serde_test() {
  7. let insert = NodeOperation::Insert {
  8. path: Path(vec![0, 1]),
  9. nodes: vec![NodeData::new("text".to_owned())],
  10. };
  11. let result = serde_json::to_string(&insert).unwrap();
  12. assert_eq!(result, r#"{"op":"insert","path":[0,1],"nodes":[{"type":"text"}]}"#);
  13. }
  14. #[test]
  15. fn operation_insert_node_with_children_serde_test() {
  16. let node = NodeDataBuilder::new("text")
  17. .add_node_data(NodeData::new("sub_text".to_owned()))
  18. .build();
  19. let insert = NodeOperation::Insert {
  20. path: Path(vec![0, 1]),
  21. nodes: vec![node],
  22. };
  23. assert_eq!(
  24. serde_json::to_string(&insert).unwrap(),
  25. r#"{"op":"insert","path":[0,1],"nodes":[{"type":"text","children":[{"type":"sub_text"}]}]}"#
  26. );
  27. }
  28. #[test]
  29. fn operation_update_node_attributes_serde_test() {
  30. let operation = NodeOperation::Update {
  31. path: Path(vec![0, 1]),
  32. changeset: Changeset::Attributes {
  33. new: AttributeBuilder::new().insert("bold", true).build(),
  34. old: AttributeBuilder::new().insert("bold", false).build(),
  35. },
  36. };
  37. let result = serde_json::to_string(&operation).unwrap();
  38. assert_eq!(
  39. result,
  40. r#"{"op":"update","path":[0,1],"changeset":{"attributes":{"new":{"bold":true},"old":{"bold":false}}}}"#
  41. );
  42. }
  43. #[test]
  44. fn operation_update_node_body_serialize_test() {
  45. let delta = DeltaTextOperationBuilder::new().insert("AppFlowy...").build();
  46. let inverted = delta.invert_str("");
  47. let changeset = Changeset::Delta { delta, inverted };
  48. let insert = NodeOperation::Update {
  49. path: Path(vec![0, 1]),
  50. changeset,
  51. };
  52. let result = serde_json::to_string(&insert).unwrap();
  53. assert_eq!(
  54. result,
  55. r#"{"op":"update","path":[0,1],"changeset":{"delta":{"delta":[{"insert":"AppFlowy..."}],"inverted":[{"delete":11}]}}}"#
  56. );
  57. }
  58. #[test]
  59. fn operation_update_node_body_deserialize_test() {
  60. let json_1 = r#"{"op":"update","path":[0,1],"changeset":{"delta":{"delta":[{"insert":"AppFlowy..."}],"inverted":[{"delete":11}]}}}"#;
  61. let operation: NodeOperation = serde_json::from_str(json_1).unwrap();
  62. let json_2 = serde_json::to_string(&operation).unwrap();
  63. assert_eq!(json_1, json_2);
  64. }
  65. // #[test]
  66. // fn transaction_serialize_test() {
  67. // let insert = NodeOperation::Insert {
  68. // path: Path(vec![0, 1]),
  69. // nodes: vec![NodeData::new("text".to_owned())],
  70. // };
  71. // let transaction = Transaction::from_operations(vec![insert]);
  72. // let json = serde_json::to_string(&transaction).unwrap();
  73. // assert_eq!(
  74. // json,
  75. // r#"{"operations":[{"op":"insert","path":[0,1],"nodes":[{"type":"text"}]}]}"#
  76. // );
  77. // }
  78. //
  79. // #[test]
  80. // fn transaction_deserialize_test() {
  81. // let json = r#"{"operations":[{"op":"insert","path":[0,1],"nodes":[{"type":"text"}]}],"TextSelection":{"before_selection":{"start":{"path":[],"offset":0},"end":{"path":[],"offset":0}},"after_selection":{"start":{"path":[],"offset":0},"end":{"path":[],"offset":0}}}}"#;
  82. //
  83. // let transaction: Transaction = serde_json::from_str(json).unwrap();
  84. // assert_eq!(transaction.operations.len(), 1);
  85. // }
  86. //
  87. // #[test]
  88. // fn node_tree_deserialize_test() {
  89. // let tree: NodeTree = serde_json::from_str(TREE_JSON).unwrap();
  90. // assert_eq!(tree.number_of_children(None), 1);
  91. // }
  92. #[test]
  93. fn node_tree_serialize_test() {
  94. let tree: NodeTree = serde_json::from_str(TREE_JSON).unwrap();
  95. let json = serde_json::to_string_pretty(&tree).unwrap();
  96. assert_eq!(json, TREE_JSON);
  97. }
  98. #[test]
  99. fn node_tree_serde_test() {
  100. let tree: NodeTree = serde_json::from_str(TREE_JSON).unwrap();
  101. let bytes = tree.to_bytes();
  102. let tree = NodeTree::from_bytes(&bytes).unwrap();
  103. assert_eq!(bytes, tree.to_bytes());
  104. }
  105. #[allow(dead_code)]
  106. const TREE_JSON: &str = r#"{
  107. "type": "editor",
  108. "children": [
  109. {
  110. "type": "image",
  111. "attributes": {
  112. "image_src": "https://s1.ax1x.com/2022/08/26/v2sSbR.jpg"
  113. }
  114. },
  115. {
  116. "type": "text",
  117. "attributes": {
  118. "heading": "h1"
  119. },
  120. "body": {
  121. "delta": [
  122. {
  123. "insert": "👋 "
  124. },
  125. {
  126. "insert": "Welcome to ",
  127. "attributes": {
  128. "href": "appflowy.io"
  129. }
  130. },
  131. {
  132. "insert": "AppFlowy Editor",
  133. "attributes": {
  134. "italic": true
  135. }
  136. }
  137. ]
  138. }
  139. }
  140. ]
  141. }"#;