123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- use flowy_document2::parser::json::parser::JsonToDocumentParser;
- use serde_json::json;
- #[test]
- fn test_parser_children_in_order() {
- let json = json!({
- "type": "page",
- "children": [
- {
- "type": "paragraph1",
- },
- {
- "type": "paragraph2",
- },
- {
- "type": "paragraph3",
- },
- {
- "type": "paragraph4",
- }
- ]
- });
- let document = JsonToDocumentParser::json_str_to_document(json.to_string().as_str()).unwrap();
- // root + 4 paragraphs
- assert_eq!(document.blocks.len(), 5);
- // root + 4 paragraphs
- assert_eq!(document.meta.children_map.len(), 5);
- let (page_id, page_block) = document
- .blocks
- .iter()
- .find(|(_, block)| block.ty == "page")
- .unwrap();
- // the children should be in order
- let page_children = document
- .meta
- .children_map
- .get(page_block.children_id.as_str())
- .unwrap();
- assert_eq!(page_children.children.len(), 4);
- for (i, child_id) in page_children.children.iter().enumerate() {
- let child = document.blocks.get(child_id).unwrap();
- assert_eq!(child.ty, format!("paragraph{}", i + 1));
- assert_eq!(child.parent_id, page_id.to_owned());
- }
- }
- #[test]
- fn test_parser_nested_children() {
- let json = json!({
- "type": "page",
- "children": [
- {
- "type": "paragraph",
- "children": [
- {
- "type": "paragraph",
- "children": [
- {
- "type": "paragraph",
- "children": [
- {
- "type": "paragraph"
- }
- ]
- }
- ]
- }
- ]
- }
- ]
- });
- let document = JsonToDocumentParser::json_str_to_document(json.to_string().as_str()).unwrap();
- // root + 4 paragraphs
- assert_eq!(document.blocks.len(), 5);
- // root + 4 paragraphs
- assert_eq!(document.meta.children_map.len(), 5);
- let (page_id, page_block) = document
- .blocks
- .iter()
- .find(|(_, block)| block.ty == "page")
- .unwrap();
- // first child of root is a paragraph
- let page_children = document
- .meta
- .children_map
- .get(page_block.children_id.as_str())
- .unwrap();
- assert_eq!(page_children.children.len(), 1);
- let page_first_child_id = page_children.children.first().unwrap();
- let page_first_child = document.blocks.get(page_first_child_id).unwrap();
- assert_eq!(page_first_child.ty, "paragraph");
- assert_eq!(page_first_child.parent_id, page_id.to_owned());
- }
|