1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// ==================================================
// Event Sourcing Aggregate Test Case
// 
// allows to test with Given->When->Then
// ==================================================

pub struct AfterWhen<E, DomainErr> {
    decided: Result<Vec<E>, DomainErr>
}

impl<
    DomainErr:std::cmp::PartialEq + std::fmt::Debug,
    E:std::cmp::PartialEq + std::fmt::Debug> AfterWhen<E, DomainErr> { 
            

        pub fn then_ok(self, expected_events: Vec<E>) -> () {
            match self.decided {
                Ok (outcome_events) =>  
                    assert_eq!(outcome_events, expected_events),
    
                Err (error) => 
                    assert!(false, "command result was error, but ok was expected. Returned error is : {:?}", error)
            }
        }
        pub fn then_error(self, expected_error: DomainErr) -> () {
            match self.decided {
                Ok (outcome_events) =>  
                    assert!(false, "command result was ok, but error was expected. Returned events: {:?}", outcome_events),
                    
                Err (outcome_error) => 
                    assert_eq!(outcome_error, expected_error)
            }
        }
    }

// =======================================
// Test case state after given
// now it can call when([E])
// =======================================
pub struct AfterGiven<S, C,  E, DomainErr> {
    decider: fn(S, C) -> Result<Vec<E>, DomainErr>,
    state: S,
}

impl<S, C,
    DomainErr:std::cmp::PartialEq + std::fmt::Debug,
    E:std::cmp::PartialEq + std::fmt::Debug> AfterGiven<S, C,  E, DomainErr> { 
            
        pub fn when(self, command: C) -> AfterWhen<E,DomainErr> {
            
            let outcome_decide = (self.decider)(self.state, command);
            AfterWhen { decided: outcome_decide }
        }
    }

pub struct AggTestCase<S, C,  E, DomainErr> {
    pub initial: fn() -> S,
    pub decider: fn(S, C) -> Result<Vec<E>, DomainErr>,
    pub evolve: fn(S, &E) -> S    
}
    

impl<S,
    C,
    DomainErr:std::cmp::PartialEq + std::fmt::Debug,
    E:std::cmp::PartialEq + std::fmt::Debug> AggTestCase<S, C, E,DomainErr> {


    
    pub fn given(self, events: Vec<E>) -> AfterGiven<S,C,E,DomainErr> { 
        let initial = (self.initial)();                
        let outcome_state = 
            events
                .iter()
                .fold( initial, self.evolve );
        
        AfterGiven {
            state: outcome_state,
            decider: self.decider,
        }                
     }
    
    pub fn given_none(self) -> AfterGiven<S,C,E,DomainErr> { 
        self.given( vec![])
    }

   

}