From 4307478718896f45620f9f9a4c9339cd1e13a7fb Mon Sep 17 00:00:00 2001 From: Alexis Sellier Date: Thu, 15 Dec 2022 14:34:15 +0100 Subject: [PATCH] Test comment editing Signed-off-by: Alexis Sellier --- radicle/src/cob/thread.rs | 52 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/radicle/src/cob/thread.rs b/radicle/src/cob/thread.rs index fbe429b5..ba4eb225 100644 --- a/radicle/src/cob/thread.rs +++ b/radicle/src/cob/thread.rs @@ -297,6 +297,14 @@ impl Actor { pub fn redact(&mut self, id: OpId) -> Op { self.op(Action::Redact { id }) } + + /// Edit a comment. + pub fn edit(&mut self, id: OpId, body: &str) -> Op { + self.op(Action::Edit { + id, + body: body.to_owned(), + }) + } } impl Deref for Actor { @@ -463,12 +471,24 @@ mod tests { } } + mod setup { + use super::*; + use crate::storage::git as storage; + use cob::store::Store; + + pub fn store<'a, G: Signer>( + signer: &G, + repo: &'a storage::Repository, + ) -> Store<'a, Thread> { + Store::::open(*signer.public_key(), repo).unwrap() + } + } + #[test] fn test_redact_comment() { let tmp = tempfile::tempdir().unwrap(); let (_, signer, repository) = radicle::test::setup::context(&tmp); - let store = - radicle::cob::store::Store::::open(*signer.public_key(), &repository).unwrap(); + let store = setup::store(&signer, &repository); let mut alice = Actor::new(signer); let a0 = alice.comment("First comment", None); @@ -500,12 +520,36 @@ mod tests { assert_eq!(comment1.body(), "Third comment"); // Second comment was redacted. } + #[test] + fn test_edit_comment() { + let mut alice = Actor::::default(); + + let c0 = alice.comment("Hello world!", None); + let c1 = alice.edit(c0.id(), "Goodbye world."); + let c2 = alice.edit(c0.id(), "Goodbye world!"); + + let mut t1 = Thread::default(); + t1.apply([c0.clone(), c1.clone(), c2.clone()]).unwrap(); + + let comment = t1.comment(&c0.id()); + let edits = comment.unwrap().edits().collect::>(); + + assert_eq!(edits[0].body.as_str(), "Hello world!"); + assert_eq!(edits[1].body.as_str(), "Goodbye world."); + assert_eq!(edits[2].body.as_str(), "Goodbye world!"); + assert_eq!(t1.comment(&c0.id()).unwrap().body(), "Goodbye world!"); + + let mut t2 = Thread::default(); + t2.apply([c0, c2, c1]).unwrap(); // Apply in different order. + + assert_eq!(t1, t2); + } + #[test] fn test_storage() { let tmp = tempfile::tempdir().unwrap(); let (_, signer, repository) = radicle::test::setup::context(&tmp); - let store = - radicle::cob::store::Store::::open(*signer.public_key(), &repository).unwrap(); + let store = setup::store(&signer, &repository); let mut alice = Actor::new(signer);