Skip to content

Commit 15f335b

Browse files
committed
feat(common): introduce test_utils::Dummy trait
A trait similar to the standard `Default` but dedicated to dummy test double creation.
1 parent 890801c commit 15f335b

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//! Test doubles
2+
//!
3+
//! Enable unit testing with controlled inputs and predictable behavior.
4+
5+
/// A trait for giving a type a dummy value.
6+
///
7+
/// Sometimes in tests you need to provide a value for a type, but the actual value doesn't matter.
8+
/// This trait allows defining a "dummy" value for a type, separated from an eventual default
9+
/// value, that can be reused across multiple tests.
10+
///
11+
/// Note: should not be confused with "fake" values, fake values aim to be believable and contain
12+
/// valid cryptography (if they have some), dummies don't aim for those characteristics.
13+
///
14+
/// # Example
15+
/// ```
16+
/// use mithril_common::test_utils::double::Dummy;
17+
///
18+
/// struct MyType(String);
19+
///
20+
/// impl Dummy for MyType {
21+
/// fn dummy() -> Self {
22+
/// MyType("whatever".to_string())
23+
/// }
24+
/// }
25+
///
26+
/// let instance = MyType::dummy();
27+
/// ```
28+
pub trait Dummy: Sized {
29+
/// Return a dummy value for the type
30+
///
31+
/// Useful for test contexts when the actual value doesn't matter.
32+
fn dummy() -> Self;
33+
}

mithril-common/src/test_utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//! * A builder of [MithrilFixture] to generate signers alongside a stake distribution
77
//!
88
9+
pub mod double;
910
pub mod fake_data;
1011
pub mod fake_keys;
1112
pub mod mock_extensions;

0 commit comments

Comments
 (0)