]> uap-core.de Git - note.git/commitdiff
use markdown compare func for editor markdown tests
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Mon, 9 Mar 2026 19:39:09 +0000 (20:39 +0100)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Mon, 9 Mar 2026 19:39:09 +0000 (20:39 +0100)
application/editor.c
application/tests/test-editor.c

index 2f7bb3772d96d858cc9635558bb916a3afb17a95..2eba2123f5790fcff7511fac5f4516c2c687147a 100644 (file)
@@ -142,6 +142,7 @@ static int md_enter_block(MD_BLOCKTYPE type, void* detail, void* userdata) {
     p->content = NULL;
     p->next = NULL;
     p->type = type;
+    p->heading = 0;
     
     if(type == MD_BLOCK_H) {
         MD_BLOCK_H_DETAIL *heading = detail;
@@ -309,7 +310,9 @@ MDDoc* parse_markdown(cxstring markdown) {
 }
 
 void mddoc_free(MDDoc *doc) {
-    cxMempoolFree(doc->mp);
+    if(doc) {
+        cxMempoolFree(doc->mp);
+    }
 }
 
 cxstring mdnode_get_text(MDNode *node) {
index ce322f0463c4c5216c5e6f3d8eeb23457661b13a..dda0752f31c360e460f1cb4153873825067eccb7 100644 (file)
@@ -374,6 +374,75 @@ CX_TEST(test_editor_load_markdown_simple) {
     }
 }
 
+static int md_node_equal(MDNode *node0, MDNode *node1) {
+    while(node0) {
+        if(node1 == NULL) {
+            return 0;
+        }
+        if(node0->type != node1->type ||
+           node0->type0 != node1->type0) {
+            return 0;
+        }
+        if(cx_strcmp(node0->text, node1->text)) {
+           return 0; 
+        }
+        if(cx_strcmp(node0->link, node1->link)) {
+           return 0; 
+        }
+        
+        if(node0->children_begin) {
+            if(!node1->children_begin) {
+                return 0;
+            }
+            if(!md_node_equal(node0->children_begin, node1->children_begin)) {
+                return 0;
+            }
+        }
+        
+        node0 = node0->next;
+        node1 = node1->next;
+    }
+    
+    return node1 == NULL;
+}
+
+static int md_equal(cxmutstr md0, cxmutstr md1) {
+    MDDoc *doc0 = parse_markdown(cx_strcast(md0));
+    MDDoc *doc1 = parse_markdown(cx_strcast(md1));
+    
+    if(!doc0 || !doc1) {
+        mddoc_free(doc0);
+        mddoc_free(doc1);
+        return 0;
+    }
+    
+    MDPara *para0 = doc0->content;
+    MDPara *para1 = doc1->content;
+    
+    while(para0) {
+        if(!para1) {
+            return 0;
+        }
+        if(para0->type != para1->type) {
+            return 0;
+        }
+        if(para0->heading != para1->heading) {
+            return 0;
+        }
+        if(!md_node_equal(para0->content, para1->content)) {
+            return 0;
+        }        
+        
+        para0 = para0->next;
+        para1 = para1->next;
+    }
+    
+    mddoc_free(doc0);
+    mddoc_free(doc1);
+    
+    return para1 == NULL;
+}
+
 CX_TEST(test_editor_load_markdown_para1) {
     CX_TEST_DO {
         UiObject *obj = ui_simple_window("test");
@@ -393,7 +462,7 @@ CX_TEST(test_editor_load_markdown_para1) {
         
         editor_load_markdown(&note, textview, md0);
         cxmutstr mdtext = editor_get_markdown(text, a);
-        CX_TEST_ASSERT(!cx_strcmp(cx_strtrim(mdtext), md0));
+        CX_TEST_ASSERT(md_equal(md0, mdtext));
         
         // TODO: cleanup
         ui_close(obj);