-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogparser_test.go
168 lines (152 loc) · 3.67 KB
/
logparser_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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package wrapper
import (
"bufio"
"os"
"testing"
"github.com/wlwanpan/minecraft-wrapper/events"
)
func testParsedEvents(t *testing.T, evs []events.Event, testfilename string) {
testfile, err := os.Open(testfilename)
if err != nil {
t.Errorf("failed to load test file: %s", err)
}
actualEvents := []events.Event{}
scanner := bufio.NewScanner(testfile)
for scanner.Scan() {
ev, t := logParserFunc(scanner.Text(), 0)
if t == events.TypeNil {
continue
}
actualEvents = append(actualEvents, ev)
}
if len(evs) != len(actualEvents) {
t.Errorf("wrong event count detected: actual=%d, expected=%d", len(actualEvents), len(evs))
return
}
for i, ev := range evs {
aEv := actualEvents[i]
if !ev.Is(aEv) {
t.Errorf("event mismatched at %d: actual=%s, expected=%s", i, aEv.String(), ev.String())
}
}
}
func testParsedGameEvents(t *testing.T, gevs []events.GameEvent, testfilename string) {
testfile, err := os.Open(testfilename)
if err != nil {
t.Errorf("failed to load test file: %s", err)
}
actualEvents := []events.GameEvent{}
scanner := bufio.NewScanner(testfile)
for scanner.Scan() {
ev, t := logParserFunc(scanner.Text(), 0)
if t == events.TypeNil {
continue
}
actualEvents = append(actualEvents, ev.(events.GameEvent))
}
if len(gevs) != len(actualEvents) {
t.Logf("actual : %v", actualEvents)
t.Logf("expected: %v", gevs)
t.Errorf("wrong event count detected: actual=%d, expected=%d", len(actualEvents), len(gevs))
return
}
for i, ev := range gevs {
aEv := actualEvents[i]
if !ev.Is(aEv) {
t.Errorf("event mismatched at %d: actual=%s, expected=%s", i, aEv.String(), ev.String())
}
for k, v := range ev.Data {
actualV, ok := aEv.Data[k]
if !ok {
t.Errorf("missing data '%s' for event '%s'", v, ev.String())
}
if v != actualV {
t.Errorf("data '%s' mismatch for event '%s': actual=%s, expected=%s ", k, ev.String(), actualV, v)
}
}
}
}
func TestServerStartLog(t *testing.T) {
evs := []events.Event{
events.VersionEvent,
events.StartingEvent,
events.StartedEvent,
}
testParsedEvents(t, evs, "testdata/server_start_log")
}
func TestServerOverloadedLog(t *testing.T) {
testLagTimes := [][]string{
{"1", "1"},
{"2004", "0"},
{"1000000", "1000000"},
}
gevs := []events.GameEvent{}
for _, times := range testLagTimes {
gev := events.NewGameEvent(events.ServerOverloaded)
gev.Data = map[string]string{
"lag_time": times[0],
"lag_tick": times[1],
}
gevs = append(gevs, gev)
}
testParsedGameEvents(t, gevs, "testdata/server_overloaded_log")
}
func TestPlayerBasicLog(t *testing.T) {
gevs := []events.GameEvent{
{
Name: events.PlayerUUID,
Data: map[string]string{
"player_name": "player1",
"player_uuid": "player-1-uuid",
},
},
{
Name: events.PlayerJoined,
Data: map[string]string{
"player_name": "player1",
},
},
{
Name: events.PlayerUUID,
Data: map[string]string{
"player_name": "player2",
"player_uuid": "player-2-uuid",
},
},
{
Name: events.PlayerJoined,
Data: map[string]string{
"player_name": "player2",
},
},
{
Name: events.PlayerDied,
Data: map[string]string{
"player_name": "player2",
"death_by": "fell",
"death_details": " from a high place",
},
},
{
Name: events.PlayerDied,
Data: map[string]string{
"player_name": "player1",
"death_by": "was killed by",
"death_details": " Witch using magic",
},
},
{
Name: events.PlayerLeft,
Data: map[string]string{
"player_name": "player1",
},
},
{
Name: events.PlayerLeft,
Data: map[string]string{
"player_name": "player2",
},
},
}
testParsedGameEvents(t, gevs, "testdata/player_basic_log")
}