parser_test.rs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. use collab_document::blocks::json_str_to_hashmap;
  2. use flowy_document2::parser::json::parser::JsonToDocumentParser;
  3. use serde_json::json;
  4. #[test]
  5. fn test_parser_children_in_order() {
  6. let json = json!({
  7. "type": "page",
  8. "children": [
  9. {
  10. "type": "paragraph1",
  11. },
  12. {
  13. "type": "paragraph2",
  14. },
  15. {
  16. "type": "paragraph3",
  17. },
  18. {
  19. "type": "paragraph4",
  20. }
  21. ]
  22. });
  23. let document = JsonToDocumentParser::json_str_to_document(json.to_string().as_str()).unwrap();
  24. // root + 4 paragraphs
  25. assert_eq!(document.blocks.len(), 5);
  26. // root + 4 paragraphs
  27. assert_eq!(document.meta.children_map.len(), 5);
  28. let (page_id, page_block) = document
  29. .blocks
  30. .iter()
  31. .find(|(_, block)| block.ty == "page")
  32. .unwrap();
  33. // the children should be in order
  34. let page_children = document
  35. .meta
  36. .children_map
  37. .get(page_block.children_id.as_str())
  38. .unwrap();
  39. assert_eq!(page_children.children.len(), 4);
  40. for (i, child_id) in page_children.children.iter().enumerate() {
  41. let child = document.blocks.get(child_id).unwrap();
  42. assert_eq!(child.ty, format!("paragraph{}", i + 1));
  43. assert_eq!(child.parent_id, page_id.to_owned());
  44. }
  45. }
  46. #[test]
  47. fn test_parser_nested_children() {
  48. let json = json!({
  49. "type": "page",
  50. "children": [
  51. {
  52. "type": "paragraph",
  53. "children": [
  54. {
  55. "type": "paragraph",
  56. "children": [
  57. {
  58. "type": "paragraph",
  59. "children": [
  60. {
  61. "type": "paragraph"
  62. }
  63. ]
  64. }
  65. ]
  66. }
  67. ]
  68. }
  69. ]
  70. });
  71. let document = JsonToDocumentParser::json_str_to_document(json.to_string().as_str()).unwrap();
  72. // root + 4 paragraphs
  73. assert_eq!(document.blocks.len(), 5);
  74. // root + 4 paragraphs
  75. assert_eq!(document.meta.children_map.len(), 5);
  76. let (page_id, page_block) = document
  77. .blocks
  78. .iter()
  79. .find(|(_, block)| block.ty == "page")
  80. .unwrap();
  81. // first child of root is a paragraph
  82. let page_children = document
  83. .meta
  84. .children_map
  85. .get(page_block.children_id.as_str())
  86. .unwrap();
  87. assert_eq!(page_children.children.len(), 1);
  88. let page_first_child_id = page_children.children.first().unwrap();
  89. let page_first_child = document.blocks.get(page_first_child_id).unwrap();
  90. assert_eq!(page_first_child.ty, "paragraph");
  91. assert_eq!(page_first_child.parent_id, page_id.to_owned());
  92. }
  93. #[tokio::test]
  94. async fn parse_readme_test() {
  95. let json = include_str!("../../../../flowy-core/assets/read_me.json");
  96. let document = JsonToDocumentParser::json_str_to_document(json).unwrap();
  97. document.blocks.iter().for_each(|(_, block)| {
  98. let data = json_str_to_hashmap(&block.data).ok();
  99. assert!(data.is_some());
  100. if let Some(data) = data {
  101. assert!(data.get("delta").is_none());
  102. }
  103. if let Some(external_id) = &block.external_id {
  104. let text = document.meta.text_map.get(external_id);
  105. assert!(text.is_some());
  106. }
  107. });
  108. }