forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
191 lines (179 loc) · 4.86 KB
/
client_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package minecraft
import (
"testing"
"github.com/stretchr/testify/require"
)
type MockConnection struct {
commands map[string]string
}
func (c *MockConnection) Execute(command string) (string, error) {
return c.commands[command], nil
}
type MockConnector struct {
conn *MockConnection
}
func (c *MockConnector) Connect() (Connection, error) {
return c.conn, nil
}
func TestClient_Player(t *testing.T) {
tests := []struct {
name string
commands map[string]string
expected []string
}{
{
name: "minecraft 1.12 no players",
commands: map[string]string{
"scoreboard players list": "There are no tracked players on the scoreboard",
},
expected: []string{},
},
{
name: "minecraft 1.12 single player",
commands: map[string]string{
"scoreboard players list": "Showing 1 tracked players on the scoreboard:Etho",
},
expected: []string{"Etho"},
},
{
name: "minecraft 1.12 two players",
commands: map[string]string{
"scoreboard players list": "Showing 2 tracked players on the scoreboard:Etho and torham",
},
expected: []string{"Etho", "torham"},
},
{
name: "minecraft 1.12 three players",
commands: map[string]string{
"scoreboard players list": "Showing 3 tracked players on the scoreboard:Etho, notch and torham",
},
expected: []string{"Etho", "notch", "torham"},
},
{
name: "minecraft 1.12 players space in username",
commands: map[string]string{
"scoreboard players list": "Showing 4 tracked players on the scoreboard:with space, Etho, notch and torham",
},
expected: []string{"with space", "Etho", "notch", "torham"},
},
{
name: "minecraft 1.12 players and in username",
commands: map[string]string{
"scoreboard players list": "Showing 5 tracked players on the scoreboard:left and right, with space,Etho, notch and torham",
},
expected: []string{"left and right", "with space", "Etho", "notch", "torham"},
},
{
name: "minecraft 1.13 no players",
commands: map[string]string{
"scoreboard players list": "There are no tracked entities",
},
expected: []string{},
},
{
name: "minecraft 1.13 single player",
commands: map[string]string{
"scoreboard players list": "There are 1 tracked entities: torham",
},
expected: []string{"torham"},
},
{
name: "minecraft 1.13 multiple player",
commands: map[string]string{
"scoreboard players list": "There are 3 tracked entities: Etho, notch, torham",
},
expected: []string{"Etho", "notch", "torham"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
connector := &MockConnector{
conn: &MockConnection{commands: tt.commands},
}
client := newClient(connector)
actual, err := client.Players()
require.NoError(t, err)
require.Equal(t, tt.expected, actual)
})
}
}
func TestClient_Scores(t *testing.T) {
tests := []struct {
name string
player string
commands map[string]string
expected []Score
}{
{
name: "minecraft 1.12 player with no scores",
player: "Etho",
commands: map[string]string{
"scoreboard players list Etho": "Player Etho has no scores recorded",
},
expected: []Score{},
},
{
name: "minecraft 1.12 player with one score",
player: "Etho",
commands: map[string]string{
"scoreboard players list Etho": "Showing 1 tracked objective(s) for Etho:- jump: 2 (jump)",
},
expected: []Score{
{Name: "jump", Value: 2},
},
},
{
name: "minecraft 1.12 player with many scores",
player: "Etho",
commands: map[string]string{
"scoreboard players list Etho": "Showing 3 tracked objective(s) for Etho:- hopper: 2 (hopper)- dropper: 2 (dropper)- redstone: 1 (redstone)",
},
expected: []Score{
{Name: "hopper", Value: 2},
{Name: "dropper", Value: 2},
{Name: "redstone", Value: 1},
},
},
{
name: "minecraft 1.13 player with no scores",
player: "Etho",
commands: map[string]string{
"scoreboard players list Etho": "Etho has no scores to show",
},
expected: []Score{},
},
{
name: "minecraft 1.13 player with one score",
player: "Etho",
commands: map[string]string{
"scoreboard players list Etho": "Etho has 1 scores:[jumps]: 1",
},
expected: []Score{
{Name: "jumps", Value: 1},
},
},
{
name: "minecraft 1.13 player with many scores",
player: "Etho",
commands: map[string]string{
"scoreboard players list Etho": "Etho has 3 scores:[hopper]: 2[dropper]: 2[redstone]: 1",
},
expected: []Score{
{Name: "hopper", Value: 2},
{Name: "dropper", Value: 2},
{Name: "redstone", Value: 1},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
connector := &MockConnector{
conn: &MockConnection{commands: tt.commands},
}
client := newClient(connector)
actual, err := client.Scores(tt.player)
require.NoError(t, err)
require.Equal(t, tt.expected, actual)
})
}
}