diff options
| author | tv <tv@krebsco.de> | 2026-10-31 19:03:15 +0100 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-10-31 19:03:15 +0100 |
| commit | e4f801db5d150fb1bf5540d9ba00f491a6129a5c (patch) | |
| tree | 38a8b5ffd8233df7d24f9bb5effac58cbd92187e /src/document/testing.rs | |
| parent | 4a6403d0242cf730a3f864c8d26e0ba15a98fcf0 (diff) | |
tests write through document::testing helpers
Diffstat (limited to 'src/document/testing.rs')
| -rw-r--r-- | src/document/testing.rs | 29 |
1 files changed, 29 insertions, 0 deletions
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<T>(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<T, E>( + doc: &mut Document, + f: impl FnOnce(&mut Document) -> Result<T, E>, +) -> Result<T, E> { + let out = f(doc); + match &out { + Ok(_) => { + doc.commit_with(automerge::transaction::CommitOptions::default().with_time(0)); + } + Err(_) => { + doc.rollback(); + } + } + out +} |
