-
Notifications
You must be signed in to change notification settings - Fork 5
/
memento_test.go
65 lines (59 loc) · 1.59 KB
/
memento_test.go
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
package main
import (
"testing"
)
func TestNewMementoTakeOriginatorState(t *testing.T) {
firstState := originator{}
firstState.state = State{Description: "Idle"}
mem := firstState.NewMemento()
if mem.state.Description != "Idle" {
t.Error("Expected state was not found")
}
}
func TestCareTakerKeepAlMementoInsideAList(t *testing.T) {
firstState := originator{}
firstState.state = State{Description: "Idle"}
mem := firstState.NewMemento()
careTaker := careTaker{}
lenBeforeAddedMemento := len(careTaker.mementoList)
careTaker.Add(mem)
if len(careTaker.mementoList) != lenBeforeAddedMemento+1 {
t.Errorf("No new elements were added on the list")
}
}
func TestCareTaker(t *testing.T) {
originator := originator{}
careTaker := careTaker{}
originator.state = State{"foo"}
careTaker.Add(originator.NewMemento())
mem, err := careTaker.Memento(0)
if err != nil {
t.Fatal(err)
}
if mem.state.Description != "foo" {
t.Error("Unexpected state")
}
}
func TestCareTakerDetectIfNonExistentIndexIsRequested(t *testing.T) {
originator := originator{}
careTaker := careTaker{}
originator.state = State{"foo"}
careTaker.Add(originator.NewMemento())
_, err := careTaker.Memento(0)
if err != nil {
t.Fatal(err)
}
_, err = careTaker.Memento(-1)
if err == nil {
t.Fatal("An error is expected")
}
}
func TestStoresIdleState(t *testing.T) {
originator := originator{state: State{"Idle"}}
idleMemento := originator.NewMemento()
originator.state.Description = "Working"
originator.ExtractAndStoreState(idleMemento)
if originator.state.Description != "Idle" {
t.Error("Unexpected state found")
}
}