| 1 | leaf! { uuid::Uuid } |
| 2 | |
| 3 | #[cfg(test)] |
| 4 | #[cfg(feature = "alloc")] |
| 5 | mod tests { |
| 6 | use crate::{BTreeMapDiff, Diffable, Leaf}; |
| 7 | use alloc::{collections::BTreeMap, vec}; |
| 8 | |
| 9 | #[test] |
| 10 | fn example_struct() { |
| 11 | use uuid::Uuid; |
| 12 | |
| 13 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 14 | enum SledState { |
| 15 | Active, |
| 16 | Decommissioned, |
| 17 | } |
| 18 | leaf!(SledState); |
| 19 | |
| 20 | #[derive(Debug, Clone)] |
| 21 | struct TestStruct { |
| 22 | id: Uuid, |
| 23 | sled_state: BTreeMap<Uuid, SledState>, |
| 24 | } |
| 25 | |
| 26 | // This is what daft-derive should generate |
| 27 | // for `TestStruct` |
| 28 | #[derive(Debug)] |
| 29 | struct TestStructDiff<'daft> { |
| 30 | id: Leaf<&'daft Uuid>, |
| 31 | sled_state: BTreeMapDiff<'daft, Uuid, SledState>, |
| 32 | } |
| 33 | |
| 34 | let sled_states = vec![ |
| 35 | (Uuid::new_v4(), SledState::Active), |
| 36 | (Uuid::new_v4(), SledState::Active), |
| 37 | (Uuid::new_v4(), SledState::Decommissioned), |
| 38 | ]; |
| 39 | |
| 40 | let a = TestStruct { |
| 41 | id: Uuid::new_v4(), |
| 42 | sled_state: sled_states.clone().into_iter().collect(), |
| 43 | }; |
| 44 | let mut b = a.clone(); |
| 45 | b.id = Uuid::new_v4(); |
| 46 | *(b.sled_state.get_mut(&sled_states[0].0).unwrap()) = |
| 47 | SledState::Decommissioned; |
| 48 | b.sled_state.insert(Uuid::new_v4(), SledState::Active); |
| 49 | |
| 50 | let diff = TestStructDiff { |
| 51 | id: a.id.diff(&b.id), |
| 52 | sled_state: a.sled_state.diff(&b.sled_state), |
| 53 | }; |
| 54 | |
| 55 | assert_ne!(diff.id.before, diff.id.after); |
| 56 | assert_eq!(diff.sled_state.unchanged().count(), 2); |
| 57 | assert_eq!(diff.sled_state.added.len(), 1); |
| 58 | assert_eq!(diff.sled_state.removed.len(), 0); |
| 59 | assert_eq!(diff.sled_state.modified().count(), 1); |
| 60 | } |
| 61 | } |