forked from markus-wa/demoinfocs-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatatables_test.go
142 lines (115 loc) · 3.27 KB
/
datatables_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package demoinfocs
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/markus-wa/demoinfocs-golang/common"
"github.com/markus-wa/demoinfocs-golang/events"
st "github.com/markus-wa/demoinfocs-golang/sendtables"
fakest "github.com/markus-wa/demoinfocs-golang/sendtables/fake"
)
type DevNullReader struct {
}
func (DevNullReader) Read(p []byte) (n int, err error) {
return len(p), nil
}
func TestParser_BindNewPlayer_Issue98(t *testing.T) {
p := newParser()
p.rawPlayers = map[int]*playerInfo{
0: {
userID: 1,
name: "Zim",
guid: "BOT",
},
1: {
userID: 2,
name: "The Suspect",
guid: "123",
},
}
bot := fakePlayerEntity(1)
p.bindNewPlayer(bot)
bot.Destroy()
player := fakePlayerEntity(2)
p.bindNewPlayer(player)
assert.Len(t, p.GameState().Participants().Connected(), 1)
}
func TestParser_BindNewPlayer_Issue98_Reconnect(t *testing.T) {
p := newParser()
p.rawPlayers = map[int]*playerInfo{
0: {
userID: 2,
name: "The Suspect",
guid: "123",
xuid: 1,
},
}
player := fakePlayerEntity(1)
p.bindNewPlayer(player)
player.Destroy()
p.RegisterEventHandler(func(events.PlayerConnect) {
t.Error("expected no more PlayerConnect events but got one")
})
p.bindNewPlayer(player)
assert.Len(t, p.GameState().Participants().All(), 1)
}
func TestParser_BindNewPlayer_PlayerSpotted_Under32(t *testing.T) {
testPlayerSpotted(t, "m_bSpottedByMask.000")
}
func TestParser_BindNewPlayer_PlayerSpotted_Over32(t *testing.T) {
testPlayerSpotted(t, "m_bSpottedByMask.001")
}
func testPlayerSpotted(t *testing.T, propName string) {
p := newParser()
p.rawPlayers = map[int]*playerInfo{
0: {
userID: 2,
name: "Spotter",
guid: "123",
xuid: 1,
},
}
// TODO: Player interface so we don't have to mock all this
spotted := new(fakest.Entity)
spottedByProp0 := new(fakest.Property)
var spottedByUpdateHandler st.PropertyUpdateHandler
spottedByProp0.On("OnUpdate", mock.Anything).Run(func(args mock.Arguments) {
spottedByUpdateHandler = args.Get(0).(st.PropertyUpdateHandler)
})
spotted.On("FindPropertyI", propName).Return(spottedByProp0)
configurePlayerEntityMock(1, spotted)
p.bindNewPlayer(spotted)
var actual events.PlayerSpottersChanged
p.RegisterEventHandler(func(e events.PlayerSpottersChanged) {
actual = e
})
spottedByUpdateHandler(st.PropertyValue{IntVal: 1})
expected := events.PlayerSpottersChanged{
Spotted: p.gameState.playersByEntityID[1],
}
assert.NotNil(t, expected.Spotted)
assert.Equal(t, expected, actual)
}
func newParser() *Parser {
p := NewParser(new(DevNullReader))
p.header = &common.DemoHeader{}
return p
}
func fakePlayerEntity(id int) *fakest.Entity {
entity := new(fakest.Entity)
configurePlayerEntityMock(id, entity)
return entity
}
func configurePlayerEntityMock(id int, entity *fakest.Entity) {
entity.On("ID").Return(id)
var destroyCallback func()
entity.On("OnDestroy", mock.Anything).Run(func(args mock.Arguments) {
destroyCallback = args.Get(0).(func())
})
entity.On("OnPositionUpdate", mock.Anything).Return()
entity.On("FindPropertyI", mock.Anything).Return(new(st.Property))
entity.On("BindProperty", mock.Anything, mock.Anything, mock.Anything)
entity.On("Destroy").Run(func(mock.Arguments) {
destroyCallback()
})
}