Skip to content

Commit

Permalink
Add tests for HandleErrors from transactions.
Browse files Browse the repository at this point in the history
  • Loading branch information
kpreid committed Jun 19, 2024
1 parent 54f8f92 commit 8bb8705
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
18 changes: 18 additions & 0 deletions all-is-cubes/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,24 @@ where
}
}

/// Note: [`ExecuteError::Commit`] never compares equal, because it contains
/// arbitrary errors which may not implement [`PartialEq`].
/// TODO: push this down to `impl PartialEq for CommitError` for more precision.
impl<Txn> PartialEq for ExecuteError<Txn>
where
Txn: Transaction<Mismatch: PartialEq> + Merge<Conflict: PartialEq>,
{
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Merge(a), Self::Merge(b)) => a == b,
(Self::Check(a), Self::Check(b)) => a == b,
(Self::Commit(_), Self::Commit(_)) => false,
(Self::Handle(a), Self::Handle(b)) => a == b,
_ => false,
}
}
}

/// Error type returned by [`Transaction::check`].
///
/// Note: This type is designed to be cheap to construct, as it is expected that game
Expand Down
22 changes: 21 additions & 1 deletion all-is-cubes/src/universe/universe_txn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ pub struct UniverseCommitCheck {
}

/// Transaction conflict error type for [`UniverseTransaction`].
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum UniverseConflict {
/// The two transactions modify members of different [`Universe`]s.
Expand Down Expand Up @@ -843,6 +843,7 @@ mod tests {
use crate::space::SpaceTransaction;
use crate::space::SpaceTransactionConflict;
use crate::transaction::{ExecuteError, MapConflict, TransactionTester};
use crate::universe::HandleError;
use alloc::sync::Arc;
use indoc::indoc;

Expand Down Expand Up @@ -1136,4 +1137,23 @@ mod tests {
})
));
}

#[test]
fn handle_error_from_handle_execute() {
let e = Handle::<Space>::new_gone("foo".into())
.execute(&SpaceTransaction::default())
.unwrap_err();

assert_eq!(e, ExecuteError::Handle(HandleError::Gone("foo".into())));
}

// This is not specifically desirable, but more work will be needed to avoid it
#[test]
#[should_panic = "Attempted to execute transaction with target already borrowed: Gone(Specific(\"foo\"))"]
fn handle_error_from_universe_txn() {
let mut u = Universe::new();
let txn = SpaceTransaction::default().bind(Handle::<Space>::new_gone("foo".into()));

_ = txn.execute(&mut u, &mut transaction::no_outputs);
}
}

0 comments on commit 8bb8705

Please sign in to comment.