Skip to content

Commit

Permalink
Implement PartialEq for DispatchError. (paritytech#8407)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunxw authored and hirschenberger committed Apr 14, 2021
1 parent d221939 commit e56f759
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ pub type DispatchResult = sp_std::result::Result<(), DispatchError>;
pub type DispatchResultWithInfo<T> = sp_std::result::Result<T, DispatchErrorWithPostInfo<T>>;

/// Reason why a dispatch call failed.
#[derive(Eq, PartialEq, Clone, Copy, Encode, Decode, RuntimeDebug)]
#[derive(Eq, Clone, Copy, Encode, Decode, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum DispatchError {
/// Some error occurred.
Expand Down Expand Up @@ -589,6 +589,27 @@ impl<T> traits::Printable for DispatchErrorWithPostInfo<T> where
}
}

impl PartialEq for DispatchError {
fn eq(&self, other: &Self) -> bool {
use DispatchError::*;

match (self, other) {
(CannotLookup, CannotLookup) |
(BadOrigin, BadOrigin) |
(ConsumerRemaining, ConsumerRemaining) |
(NoProviders, NoProviders) => true,

(Other(l), Other(r)) => l == r,
(
Module { index: index_l, error: error_l, .. },
Module { index: index_r, error: error_r, .. },
) => (index_l == index_r) && (error_l == error_r),

_ => false,
}
}
}

/// This type specifies the outcome of dispatching a call to a module.
///
/// In case of failure an error specific to the module is returned.
Expand Down Expand Up @@ -826,6 +847,38 @@ mod tests {
);
}

#[test]
fn dispatch_error_equality() {
use DispatchError::*;

let variants = vec![
Other("foo"),
Other("bar"),
CannotLookup,
BadOrigin,
Module { index: 1, error: 1, message: None },
Module { index: 1, error: 2, message: None },
Module { index: 2, error: 1, message: None },
ConsumerRemaining,
NoProviders,
];
for (i, variant) in variants.iter().enumerate() {
for (j, other_variant) in variants.iter().enumerate() {
if i == j {
assert_eq!(variant, other_variant);
} else {
assert_ne!(variant, other_variant);
}
}
}

// Ignores `message` field in `Module` variant.
assert_eq!(
Module { index: 1, error: 1, message: Some("foo") },
Module { index: 1, error: 1, message: None},
);
}

#[test]
fn multi_signature_ecdsa_verify_works() {
let msg = &b"test-message"[..];
Expand Down

0 comments on commit e56f759

Please sign in to comment.