Skip to content

Commit a3c9597

Browse files
committed
Convert test helper into assert_hash_works
1 parent b4fc7f6 commit a3c9597

File tree

7 files changed

+68
-33
lines changed

7 files changed

+68
-33
lines changed

packages/std/src/addresses.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ fn hash(ty: &str, key: &[u8]) -> Vec<u8> {
399399
#[cfg(test)]
400400
mod tests {
401401
use super::*;
402-
use crate::HexBinary;
402+
use crate::{assert_hash_works, HexBinary};
403403
use hex_literal::hex;
404404
use std::collections::HashSet;
405405

@@ -651,7 +651,7 @@ mod tests {
651651
fn canonical_addr_implements_hash() {
652652
let alice = CanonicalAddr::from([0, 187, 61, 11, 250, 0]);
653653
let bob = CanonicalAddr::from([16, 21, 33, 0, 255, 9]);
654-
crate::test_utils::check_hash_impl(alice, bob)
654+
assert_hash_works!(alice, bob);
655655
}
656656

657657
/// This requires Hash and Eq to be implemented

packages/std/src/binary.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ impl<'de> de::Visitor<'de> for Base64Visitor {
238238
#[cfg(test)]
239239
mod tests {
240240
use super::*;
241+
use crate::assert_hash_works;
241242
use crate::errors::StdError;
242243
use crate::serde::{from_slice, to_vec};
243244
use std::collections::HashSet;
@@ -513,7 +514,7 @@ mod tests {
513514
fn binary_implements_hash() {
514515
let a = Binary::from([0, 187, 61, 11, 250, 0]);
515516
let b = Binary::from([16, 21, 33, 0, 255, 9]);
516-
crate::test_utils::check_hash_impl(a, b)
517+
assert_hash_works!(a, b);
517518
}
518519

519520
/// This requires Hash and Eq to be implemented

packages/std/src/hex_binary.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ impl<'de> de::Visitor<'de> for HexVisitor {
247247
mod tests {
248248
use super::*;
249249

250-
use crate::{from_slice, to_vec, StdError};
250+
use crate::{assert_hash_works, from_slice, to_vec, StdError};
251251
use std::collections::HashSet;
252252

253253
#[test]
@@ -577,7 +577,7 @@ mod tests {
577577
fn hex_binary_implements_hash() {
578578
let a = HexBinary::from([0, 187, 61, 11, 250, 0]);
579579
let b = HexBinary::from([16, 21, 33, 0, 255, 9]);
580-
crate::test_utils::check_hash_impl(a, b)
580+
assert_hash_works!(a, b);
581581
}
582582

583583
/// This requires Hash and Eq to be implemented

packages/std/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,6 @@ pub use crate::imports::{ExternalApi, ExternalQuerier, ExternalStorage};
121121
#[cfg(not(target_arch = "wasm32"))]
122122
pub mod testing;
123123

124-
// Internal testing utilities
125-
#[cfg(test)]
126-
mod test_utils;
127-
128124
// Re-exports
129125

130126
pub use cosmwasm_derive::entry_point;

packages/std/src/test_utils.rs

Lines changed: 0 additions & 24 deletions
This file was deleted.

packages/std/src/testing/assertions.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use crate::{Decimal, Uint128};
2+
#[cfg(test)]
3+
use core::hash::{Hash, Hasher};
24
use std::str::FromStr as _;
35

46
/// Asserts that two expressions are approximately equal to each other.
@@ -21,6 +23,25 @@ macro_rules! assert_approx_eq {
2123
}};
2224
}
2325

26+
/// Asserts that type `T` implements `Hash` trait correctly.
27+
///
28+
/// `left` and `right` must be unequal objects.
29+
///
30+
/// Some object pairs may produce the same hash causing test failure.
31+
/// In those cases try different objects. The test uses stable hasher
32+
/// so once working pair is identified, the test’s going to continue
33+
/// passing.
34+
#[macro_export]
35+
#[cfg(test)]
36+
macro_rules! assert_hash_works {
37+
($left:expr, $right:expr $(,)?) => {{
38+
$crate::testing::assert_hash_works_impl($left, $right, None);
39+
}};
40+
($left:expr, $right:expr, $($args:tt)+) => {{
41+
$crate::testing::assert_hash_works_impl($left, $right, Some(format!($($args)*)));
42+
}};
43+
}
44+
2445
/// Implementation for the [`cosmwasm_std::assert_approx_eq`] macro. This does not provide any
2546
/// stability guarantees and may change any time.
2647
#[track_caller]
@@ -50,6 +71,45 @@ pub fn assert_approx_eq_impl<U: Into<Uint128>>(
5071
}
5172
}
5273

74+
/// Tests that type `T` implements `Hash` trait correctly.
75+
///
76+
/// `left` and `right` must be unequal objects.
77+
///
78+
/// Some object pairs may produce the same hash causing test failure.
79+
/// In those cases try different objects. The test uses stable hasher
80+
/// so once working pair is identified, the test’s going to continue
81+
/// passing.
82+
#[track_caller]
83+
#[doc(hidden)]
84+
#[cfg(test)]
85+
pub fn assert_hash_works_impl<T: Clone + Hash>(left: T, right: T, panic_msg: Option<String>) {
86+
fn hash(value: &impl Hash) -> u64 {
87+
let mut hasher = crc32fast::Hasher::default();
88+
value.hash(&mut hasher);
89+
hasher.finish()
90+
}
91+
92+
// Check clone
93+
if hash(&left) != hash(&left.clone()) {
94+
match panic_msg {
95+
Some(panic_msg) => {
96+
panic!("assertion failed: `hash(left) == hash(left.clone())`\n: {panic_msg}")
97+
}
98+
None => panic!("assertion failed: `hash(left) == hash(left.clone())`"),
99+
}
100+
}
101+
102+
// Check different object
103+
if hash(&left) == hash(&right) {
104+
match panic_msg {
105+
Some(panic_msg) => {
106+
panic!("assertion failed: `hash(left) != hash(right)`\n: {panic_msg}")
107+
}
108+
None => panic!("assertion failed: `hash(left) != hash(right)`"),
109+
}
110+
}
111+
}
112+
53113
#[cfg(test)]
54114
mod tests {
55115
#[test]

packages/std/src/testing/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ mod mock;
88
mod shuffle;
99

1010
pub use assertions::assert_approx_eq_impl;
11+
#[cfg(test)]
12+
pub use assertions::assert_hash_works_impl;
1113

1214
#[cfg(feature = "cosmwasm_1_3")]
1315
pub use mock::DistributionQuerier;

0 commit comments

Comments
 (0)