Procházet zdrojové kódy

darkwikid: add more tests and fix bugs

ghassmo před 4 roky
rodič
revize
a3527db24b
2 změnil soubory, kde provedl 31 přidání a 7 odebrání
  1. 8 7
      bin/darkwikid/src/main.rs
  2. 23 0
      bin/darkwikid/src/patch.rs

+ 8 - 7
bin/darkwikid/src/main.rs

@@ -173,7 +173,6 @@ fn on_receive_update(settings: &DarkWikiSettings, dry: bool) -> DarkWikiResult<P
 
         // create new patch
         let mut new_patch = Patch::new(doc_title, &doc_id, &settings.author);
-        let mut b_patch;
 
         // check for any changes found with local doc and darkwiki doc
         if let Ok(local_patch) = load_json_file::<Patch>(&local_path.join(&doc_id)) {
@@ -194,6 +193,10 @@ fn on_receive_update(settings: &DarkWikiSettings, dry: bool) -> DarkWikiResult<P
 
             local_patches.push(new_patch.clone());
 
+            let mut b_patch = new_patch.clone();
+            b_patch.base = "".to_string();
+            patches.push(b_patch);
+
             // check if the same doc has received patch from the network
             if let Ok(sync_patch) = load_json_file::<Patch>(&sync_path.join(&doc_id)) {
                 if sync_patch.timestamp != local_patch.timestamp {
@@ -207,20 +210,16 @@ fn on_receive_update(settings: &DarkWikiSettings, dry: bool) -> DarkWikiResult<P
                     merge_patches.push(new_patch.clone());
                 }
             }
-
-            b_patch = new_patch.clone();
-            b_patch.base = "".to_string();
         } else {
             new_patch.base = edit.to_string();
             local_patches.push(new_patch.clone());
-            b_patch = new_patch.clone();
+            patches.push(new_patch.clone());
         };
 
         if !dry {
             save_json_file(&local_path.join(&doc_id), &new_patch)?;
             save_json_file(&sync_path.join(doc_id), &new_patch)?;
         }
-        patches.push(b_patch);
     }
 
     // check if a new patch received
@@ -243,7 +242,9 @@ fn on_receive_update(settings: &DarkWikiSettings, dry: bool) -> DarkWikiResult<P
             save_json_file(&local_path.join(file_id), &sync_patch)?;
         }
 
-        sync_patches.push(sync_patch);
+        if !sync_patches.contains(&sync_patch) {
+            sync_patches.push(sync_patch);
+        }
     }
 
     Ok((patches, local_patches, sync_patches, merge_patches))

+ 23 - 0
bin/darkwikid/src/patch.rs

@@ -553,6 +553,29 @@ mod tests {
         assert_eq!(patch4.to_string(), "new ex\n hello");
     }
 
+    #[test]
+    fn test_transform2() {
+        let mut patch_init = Patch::new("", &gen_id(30), "");
+        let base = "#hello\n hello";
+        patch_init.base = base.to_string();
+
+        let mut patch1 = patch_init.clone();
+        patch1.retain(13);
+        patch1.insert(" world");
+
+        let mut patch2 = patch_init.clone();
+        patch2.retain(1);
+        patch2.delete(5);
+        patch2.insert("this is the title");
+        patch2.retain(7);
+        patch2.insert("\n this is the content");
+
+        let patch3 = patch1.transform(&patch2);
+        let patch4 = patch1.merge(&patch3);
+
+        assert_eq!(patch4.to_string(), "#this is the title\n hello world\n this is the content");
+    }
+
     #[test]
     fn test_serialize() {
         // serialize & deserialize OpMethod