aboutsummaryrefslogtreecommitdiffstats
path: root/src/document/testing.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/document/testing.rs')
-rw-r--r--src/document/testing.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/document/testing.rs b/src/document/testing.rs
new file mode 100644
index 0000000..7b75a96
--- /dev/null
+++ b/src/document/testing.rs
@@ -0,0 +1,31 @@
+//! 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, Transaction};
+
+/// The writes `f` makes to `doc`, as one change; what `f` returns.
+pub(crate) fn change<T>(doc: &mut Document, f: impl FnOnce(&mut Transaction<'_>) -> T) -> T {
+ let mut transaction = doc.transaction();
+ let out = f(&mut transaction);
+ transaction.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 Transaction<'_>) -> Result<T, E>,
+) -> Result<T, E> {
+ let mut transaction = doc.transaction();
+ let out = f(&mut transaction);
+ match &out {
+ Ok(_) => {
+ transaction.commit_with(automerge::transaction::CommitOptions::default().with_time(0));
+ }
+ Err(_) => {
+ transaction.rollback();
+ }
+ }
+ out
+}