Repository navigation
Expand file tree
/
Copy pathstate_test.go
More file actions
90 lines (68 loc) 路 1.83 KB
/
Copy pathstate_test.go
File metadata and controls
90 lines (68 loc) 路 1.83 KB
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
90
package mo
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewState(t *testing.T) {
is := assert.New(t)
// A computation that returns the current state doubled as the result and
// increments the state.
s := NewState(func(state int) (int, int) {
return state * 2, state + 1
})
result, newState := s.Run(10)
is.Equal(20, result)
is.Equal(11, newState)
}
func TestReturnState(t *testing.T) {
is := assert.New(t)
// ReturnState yields the given value and leaves the state untouched.
s := ReturnState[string](42)
result, newState := s.Run("unchanged")
is.Equal(42, result)
is.Equal("unchanged", newState)
}
func TestStateRun(t *testing.T) {
is := assert.New(t)
s := NewState(func(state int) (string, int) {
return "value", state + 100
})
result, newState := s.Run(1)
is.Equal("value", result)
is.Equal(101, newState)
}
func TestStateGet(t *testing.T) {
is := assert.New(t)
s := NewState(func(state int) (string, int) {
return "ignored", state
})
// Get returns the current state as both the result and the state.
getter := s.Get()
result, newState := getter.Run(7)
is.Equal(7, result)
is.Equal(7, newState)
}
func TestStateModify(t *testing.T) {
is := assert.New(t)
s := NewState(func(state int) (string, int) {
return "ignored", state
})
// Modify applies f to the state and yields the zero value of A.
modified := s.Modify(func(state int) int {
return state * 10
})
result, newState := modified.Run(5)
is.Equal("", result) // zero value of string
is.Equal(50, newState)
}
func TestStatePut(t *testing.T) {
is := assert.New(t)
s := NewState(func(state int) (string, int) {
return "ignored", state
})
// Put replaces the state and yields the zero value of A.
put := s.Put(99)
result, newState := put.Run(1)
is.Equal("", result) // zero value of string
is.Equal(99, newState)
}