aboutsummaryrefslogtreecommitdiffstats
path: root/src/document/testing.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-10-31 19:03:15 +0100
committertv <tv@krebsco.de>2026-10-31 19:03:15 +0100
commitc83604d8f82b6c67a64e3cee6f5c012ee4a10fc1 (patch)
tree556b834c79561e2090b06cf2477117083e05afc5 /src/document/testing.rs
parent2cba03fa6af4aeca176d69514603bce26b9c934a (diff)
tests write through document::testing helpers
Diffstat (limited to 'src/document/testing.rs')
-rw-r--r--src/document/testing.rs29
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
+}