serde_test.rs 4.3 KB

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