parser_test.rs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. use flowy_document2::parser::json::parser::JsonToDocumentParser;
  2. use serde_json::json;
  3. #[test]
  4. fn test_parser_children_in_order() {
  5. let json = json!({
  6. "type": "page",
  7. "children": [
  8. {
  9. "type": "paragraph1",
  10. },
  11. {
  12. "type": "paragraph2",
  13. },
  14. {
  15. "type": "paragraph3",
  16. },
  17. {
  18. "type": "paragraph4",
  19. }
  20. ]
  21. });
  22. let document = JsonToDocumentParser::json_str_to_document(json.to_string().as_str()).unwrap();
  23. // root + 4 paragraphs
  24. assert_eq!(document.blocks.len(), 5);
  25. // root + 4 paragraphs
  26. assert_eq!(document.meta.children_map.len(), 5);
  27. let (page_id, page_block) = document
  28. .blocks
  29. .iter()
  30. .find(|(_, block)| block.ty == "page")
  31. .unwrap();
  32. // the children should be in order
  33. let page_children = document
  34. .meta
  35. .children_map
  36. .get(page_block.children_id.as_str())
  37. .unwrap();
  38. assert_eq!(page_children.children.len(), 4);
  39. for (i, child_id) in page_children.children.iter().enumerate() {
  40. let child = document.blocks.get(child_id).unwrap();
  41. assert_eq!(child.ty, format!("paragraph{}", i + 1));
  42. assert_eq!(child.parent_id, page_id.to_owned());
  43. }
  44. }
  45. #[test]
  46. fn test_parser_nested_children() {
  47. let json = json!({
  48. "type": "page",
  49. "children": [
  50. {
  51. "type": "paragraph",
  52. "children": [
  53. {
  54. "type": "paragraph",
  55. "children": [
  56. {
  57. "type": "paragraph",
  58. "children": [
  59. {
  60. "type": "paragraph"
  61. }
  62. ]
  63. }
  64. ]
  65. }
  66. ]
  67. }
  68. ]
  69. });
  70. let document = JsonToDocumentParser::json_str_to_document(json.to_string().as_str()).unwrap();
  71. // root + 4 paragraphs
  72. assert_eq!(document.blocks.len(), 5);
  73. // root + 4 paragraphs
  74. assert_eq!(document.meta.children_map.len(), 5);
  75. let (page_id, page_block) = document
  76. .blocks
  77. .iter()
  78. .find(|(_, block)| block.ty == "page")
  79. .unwrap();
  80. // first child of root is a paragraph
  81. let page_children = document
  82. .meta
  83. .children_map
  84. .get(page_block.children_id.as_str())
  85. .unwrap();
  86. assert_eq!(page_children.children.len(), 1);
  87. let page_first_child_id = page_children.children.first().unwrap();
  88. let page_first_child = document.blocks.get(page_first_child_id).unwrap();
  89. assert_eq!(page_first_child.ty, "paragraph");
  90. assert_eq!(page_first_child.parent_id, page_id.to_owned());
  91. }