From e4f801db5d150fb1bf5540d9ba00f491a6129a5c Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 31 Oct 2026 19:03:15 +0100 Subject: tests write through document::testing helpers --- src/document/testing.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/document/testing.rs (limited to 'src/document/testing.rs') diff --git a/src/document/testing.rs b/src/document/testing.rs new file mode 100644 index 0000000..b34d85f --- /dev/null +++ b/src/document/testing.rs @@ -0,0 +1,29 @@ +//! For tests: writing to documents the way the node does, one change at a time, so +//! that tests don't depend on how the engine groups writes into changes. + +use super::Document; + +/// The writes `f` makes to `doc`, as one change; what `f` returns. +pub(crate) fn change(doc: &mut Document, f: impl FnOnce(&mut Document) -> T) -> T { + let out = f(doc); + doc.commit(); + out +} + +/// The writes `f` makes to `doc`, as one change if it succeeds, dropped if not; what +/// `f` returns. Committed at time 0: the same writes make the same change. +pub(crate) fn try_change( + doc: &mut Document, + f: impl FnOnce(&mut Document) -> Result, +) -> Result { + let out = f(doc); + match &out { + Ok(_) => { + doc.commit_with(automerge::transaction::CommitOptions::default().with_time(0)); + } + Err(_) => { + doc.rollback(); + } + } + out +} -- cgit v1.3.1