From 8bb8705dbeb8a82c17e47ca17bd9d3febdc72dc5 Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Wed, 19 Jun 2024 10:59:42 -0700 Subject: [PATCH] Add tests for `HandleError`s from transactions. --- all-is-cubes/src/transaction.rs | 18 ++++++++++++++++++ all-is-cubes/src/universe/universe_txn.rs | 22 +++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/all-is-cubes/src/transaction.rs b/all-is-cubes/src/transaction.rs index a0cb005f1..a80f39fb7 100644 --- a/all-is-cubes/src/transaction.rs +++ b/all-is-cubes/src/transaction.rs @@ -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 PartialEq for ExecuteError +where + Txn: Transaction + Merge, +{ + 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 diff --git a/all-is-cubes/src/universe/universe_txn.rs b/all-is-cubes/src/universe/universe_txn.rs index edd625eeb..8694c2cc2 100644 --- a/all-is-cubes/src/universe/universe_txn.rs +++ b/all-is-cubes/src/universe/universe_txn.rs @@ -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. @@ -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; @@ -1136,4 +1137,23 @@ mod tests { }) )); } + + #[test] + fn handle_error_from_handle_execute() { + let e = Handle::::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::::new_gone("foo".into())); + + _ = txn.execute(&mut u, &mut transaction::no_outputs); + } }