-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Context_test.go
99 lines (79 loc) · 1.98 KB
/
Context_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
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
91
92
93
94
95
96
97
98
99
package giu
import (
"testing"
"github.com/stretchr/testify/assert"
)
type teststate struct{}
func (s *teststate) Dispose() {
// noop
}
type teststate2 struct{}
func (t *teststate2) Dispose() {
// noop
}
func Test_SetGetState(t *testing.T) {
tests := []struct {
id ID
data *teststate
}{
{"nil", nil},
{"pointer", &teststate{}},
}
for _, tc := range tests {
t.Run(tc.id.String(), func(t *testing.T) {
ctx := CreateContext(nil)
SetState(ctx, tc.id, tc.data)
restored := GetState[teststate](ctx, tc.id)
assert.Equal(t, tc.data, restored, "unexpected state restored")
})
}
}
func Test_SetGetStateGeneric(t *testing.T) {
tests := []struct {
id ID
data *teststate
}{
{"nil", nil},
{"pointer", &teststate{}},
}
for _, tc := range tests {
t.Run(tc.id.String(), func(t *testing.T) {
ctx := CreateContext(nil)
SetState(ctx, tc.id, tc.data)
restored := GetState[teststate](ctx, tc.id)
assert.Equal(t, tc.data, restored, "unexpected state restored")
})
}
}
func Test_SetGetWrongStateGeneric(t *testing.T) {
id := ID("id")
data := &teststate{}
ctx := GIUContext{}
defer func() {
if r := recover(); r == nil {
t.Errorf("expected code to assert to panic, but it didn't")
}
}()
SetState(&ctx, id, data)
GetState[teststate2](&ctx, id)
}
func Test_invalidState(t *testing.T) {
ctx := CreateContext(nil)
state1ID := ID("state1")
state2ID := ID("state2")
states := map[ID]*teststate{
state1ID: {},
state2ID: {},
}
for i, s := range states {
SetState(ctx, i, s)
}
// SetState set "valid=true" so we simulate a first end of frame before tests
ctx.SetDirty()
_ = GetState[teststate](ctx, state2ID)
ctx.SetDirty()
assert.NotNil(t, GetState[teststate](ctx, state2ID),
"although state has been accessed during the frame, it has ben deleted by invalidAllState/cleanState")
assert.Nil(t, GetState[teststate](ctx, state1ID),
"although state hasn't been accessed during the frame, it hasn't ben deleted by invalidAllState/cleanState")
}