Skip to content

Commit 48e1bc9

Browse files
committed
Add test for signed entity type discriminant ordering
1 parent f36ddb2 commit 48e1bc9

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

mithril-common/src/entities/signed_entity_type.rs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const ENTITY_TYPE_CARDANO_TRANSACTIONS: usize = 3;
2626
/// are identified by their discriminant (i.e. index in the enum), thus the
2727
/// modification of this type should only ever consist of appending new
2828
/// variants.
29+
// Important note: The order of the variants is important as it is used for the derived Ord trait.
2930
#[derive(Display, Debug, Clone, PartialEq, Eq, Serialize, Deserialize, EnumDiscriminants)]
3031
#[strum(serialize_all = "PascalCase")]
3132
#[strum_discriminants(derive(EnumString, AsRefStr, Serialize, Deserialize, PartialOrd, Ord))]
@@ -162,4 +163,56 @@ impl SignedEntityTypeDiscriminants {
162163
}
163164

164165
#[cfg(test)]
165-
mod tests {}
166+
mod tests {
167+
use super::*;
168+
169+
// Expected ord:
170+
// MithrilStakeDistribution < CardanoStakeDistribution < CardanoImmutableFilesFull < CardanoTransactions
171+
#[test]
172+
fn ordering_discriminant() {
173+
let mut list = vec![
174+
SignedEntityTypeDiscriminants::CardanoStakeDistribution,
175+
SignedEntityTypeDiscriminants::CardanoTransactions,
176+
SignedEntityTypeDiscriminants::CardanoImmutableFilesFull,
177+
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
178+
];
179+
list.sort();
180+
181+
assert_eq!(
182+
list,
183+
vec![
184+
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
185+
SignedEntityTypeDiscriminants::CardanoStakeDistribution,
186+
SignedEntityTypeDiscriminants::CardanoImmutableFilesFull,
187+
SignedEntityTypeDiscriminants::CardanoTransactions,
188+
]
189+
);
190+
}
191+
192+
#[test]
193+
fn ordering_discriminant_with_duplicate() {
194+
let mut list = vec![
195+
SignedEntityTypeDiscriminants::CardanoStakeDistribution,
196+
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
197+
SignedEntityTypeDiscriminants::CardanoTransactions,
198+
SignedEntityTypeDiscriminants::CardanoStakeDistribution,
199+
SignedEntityTypeDiscriminants::CardanoImmutableFilesFull,
200+
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
201+
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
202+
];
203+
list.sort();
204+
205+
assert_eq!(
206+
list,
207+
vec![
208+
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
209+
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
210+
SignedEntityTypeDiscriminants::MithrilStakeDistribution,
211+
SignedEntityTypeDiscriminants::CardanoStakeDistribution,
212+
SignedEntityTypeDiscriminants::CardanoStakeDistribution,
213+
SignedEntityTypeDiscriminants::CardanoImmutableFilesFull,
214+
SignedEntityTypeDiscriminants::CardanoTransactions,
215+
]
216+
);
217+
}
218+
}

0 commit comments

Comments
 (0)