serde_test.rs 4.5 KB

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