forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel_test.go
190 lines (164 loc) · 4.44 KB
/
channel_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
package disgord
import (
"io/ioutil"
"testing"
"github.com/andersfylling/disgord/httd"
)
func TestChannel_InterfaceImplementations(t *testing.T) {
var c interface{} = &Channel{}
t.Run("DeepCopier", func(t *testing.T) {
if _, ok := c.(DeepCopier); !ok {
t.Error("Channel does not implement DeepCopier")
}
test := NewChannel()
icon1 := "sdljfdsjf"
test.Icon = &icon1
test.PermissionOverwrites = append(test.PermissionOverwrites, PermissionOverwrite{
Type: "first",
})
test.PermissionOverwrites = append(test.PermissionOverwrites, PermissionOverwrite{
Type: "second",
})
copy := test.DeepCopy().(*Channel)
icon2 := "sfkjdsf"
test.Icon = &icon2
if *copy.Icon != icon1 {
t.Error("deep copy failed")
}
test.PermissionOverwrites = append(test.PermissionOverwrites, PermissionOverwrite{
Type: "third",
})
if len(copy.PermissionOverwrites) != 2 {
t.Error("deep copy failed")
}
})
t.Run("Copier", func(t *testing.T) {
if _, ok := c.(Copier); !ok {
t.Error("Channel does not implement Copier")
}
})
t.Run("discordSaver", func(t *testing.T) {
if _, ok := c.(discordSaver); !ok {
t.Error("Channel does not implement discordSaver")
}
})
t.Run("discordDeleter", func(t *testing.T) {
if _, ok := c.(discordDeleter); !ok {
t.Error("Channel does not implement discordDeleter")
}
})
}
func verifyChannelUnmarshal(t *testing.T, data []byte) {
v := Channel{}
err := validateJSONMarshalling(data, &v)
check(err, t)
}
func checkForChannelUnmarshalErr(t *testing.T, data []byte) {
v := Channel{}
if err := unmarshal(data, &v); err != nil {
t.Error(err)
}
}
func TestChannel_UnmarshalJSON(t *testing.T) {
t.Run("create", func(t *testing.T) {
data, err := ioutil.ReadFile("testdata/channel/channel_create.json")
check(err, t)
checkForChannelUnmarshalErr(t, data)
})
t.Run("update", func(t *testing.T) {
files := []string{
"testdata/channel/update_name.json",
"testdata/channel/update_nsfw1.json",
"testdata/channel/update_nsfw2.json",
"testdata/channel/update_ratelimit.json",
"testdata/channel/update_ratelimit_removed.json",
"testdata/channel/update_topic.json",
"testdata/channel/update_topic_removed.json",
}
for _, file := range files {
data, err := ioutil.ReadFile(file)
check(err, t)
checkForChannelUnmarshalErr(t, data)
}
})
t.Run("delete", func(t *testing.T) {
data, err := ioutil.ReadFile("testdata/channel/delete.json")
check(err, t)
checkForChannelUnmarshalErr(t, data)
})
}
func TestChannel_saveToDiscord(t *testing.T) {
}
func TestModifyChannelParams(t *testing.T) {
t.Run("type-all", func(t *testing.T) {
params := ModifyChannelParams{}
var err error
params.SetName("test")
params.SetPosition(2)
params.SetPermissionOverwrites([]PermissionOverwrite{})
if _, exists := params.data["name"]; !exists {
t.Error("missing name key")
}
if _, exists := params.data["position"]; !exists {
t.Error("missing position key")
}
if _, exists := params.data["permission_overwrites"]; !exists {
t.Error("missing permission_overwrites key")
}
// invalid content
err = params.SetName("a")
if err == nil {
t.Error("expected insert to fail")
}
err = params.SetParentID(342)
if err == nil {
t.Error("expected insert to fail")
}
err = params.RemoveParentID()
if err == nil {
t.Error("expected insert to fail")
}
})
t.Run("type-text", func(t *testing.T) {
params := NewModifyTextChannelParams()
var err error
err = params.SetBitrate(9000)
if err == nil {
t.Error("expected change to voice only attribute to fail for text channel")
}
err = params.SetUserLimit(4)
if err == nil {
t.Error("expected change to voice only attribute to fail for text channel")
}
params.SetRateLimitPerUser(0)
if _, exists := params.data["rate_limit_per_user"]; !exists {
t.Error("missing rate_limit_per_user key")
}
})
t.Run("type-voice", func(t *testing.T) {
params := NewModifyVoiceChannelParams()
var err error
err = params.SetBitrate(9000)
if err != nil {
t.Error(err)
}
err = params.SetUserLimit(4)
if err != nil {
t.Error(err)
}
err = params.SetTopic("test")
if err == nil {
t.Error("expected change to voice only attribute to fail for text channel")
}
})
t.Run("empty-marshal", func(t *testing.T) {
params := ModifyChannelParams{}
data, err := httd.Marshal(params)
if err != nil {
t.Fatal(err)
}
if string(data) != "{}" {
t.Error("expected an empty json object")
}
})
}