-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathblock_object_test.go
256 lines (235 loc) · 6.45 KB
/
block_object_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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package slack
import (
"encoding/json"
"errors"
"strings"
"testing"
"github.com/go-test/deep"
"github.com/stretchr/testify/assert"
)
func TestNewImageBlockObject(t *testing.T) {
imageObject := NewImageBlockElement("https://api.slack.com/img/blocks/bkb_template_images/beagle.png", "Beagle")
assert.Equal(t, string(imageObject.Type), "image")
assert.Equal(t, imageObject.AltText, "Beagle")
assert.Contains(t, imageObject.ImageURL, "beagle.png")
}
func TestNewTextBlockObject(t *testing.T) {
textObject := NewTextBlockObject("plain_text", "test", true, false)
assert.Equal(t, textObject.Type, "plain_text")
assert.Equal(t, textObject.Text, "test")
assert.True(t, *textObject.Emoji, "Emoji property should be true")
assert.False(t, textObject.Verbatim, "Verbatim should be false")
}
func TestNewConfirmationBlockObject(t *testing.T) {
titleObj := NewTextBlockObject("plain_text", "testTitle", false, false)
textObj := NewTextBlockObject("plain_text", "testText", false, false)
confirmObj := NewTextBlockObject("plain_text", "testConfirm", false, false)
confirmation := NewConfirmationBlockObject(titleObj, textObj, confirmObj, nil)
assert.Equal(t, confirmation.Title.Text, "testTitle")
assert.Equal(t, confirmation.Text.Text, "testText")
assert.Equal(t, confirmation.Confirm.Text, "testConfirm")
assert.Nil(t, confirmation.Deny, "Deny should be nil")
}
func TestWithStyleForConfirmation(t *testing.T) {
// these values are irrelevant in this test
titleObj := NewTextBlockObject("plain_text", "testTitle", false, false)
textObj := NewTextBlockObject("plain_text", "testText", false, false)
confirmObj := NewTextBlockObject("plain_text", "testConfirm", false, false)
confirmation := NewConfirmationBlockObject(titleObj, textObj, confirmObj, nil)
confirmation.WithStyle(StyleDefault)
assert.Equal(t, confirmation.Style, Style(""))
confirmation.WithStyle(StylePrimary)
assert.Equal(t, confirmation.Style, Style("primary"))
confirmation.WithStyle(StyleDanger)
assert.Equal(t, confirmation.Style, Style("danger"))
}
func TestNewOptionBlockObject(t *testing.T) {
valTextObj := NewTextBlockObject("plain_text", "testText", false, false)
valDescriptionObj := NewTextBlockObject("plain_text", "testDescription", false, false)
optObj := NewOptionBlockObject("testOpt", valTextObj, valDescriptionObj)
assert.Equal(t, optObj.Text.Text, "testText")
assert.Equal(t, optObj.Description.Text, "testDescription")
assert.Equal(t, optObj.Value, "testOpt")
}
func TestNewOptionGroupBlockElement(t *testing.T) {
labelObj := NewTextBlockObject("plain_text", "testLabel", false, false)
valTextObj := NewTextBlockObject("plain_text", "testText", false, false)
optObj := NewOptionBlockObject("testOpt", valTextObj, nil)
optGroup := NewOptionGroupBlockElement(labelObj, optObj)
assert.Equal(t, optGroup.Label.Text, "testLabel")
assert.Len(t, optGroup.Options, 1, "Options should contain one element")
}
func TestValidateTextBlockObject(t *testing.T) {
emojiTrue := new(bool)
emojiFalse := new(bool)
*emojiTrue = true
*emojiFalse = false
tests := []struct {
input TextBlockObject
expected error
}{
{
input: TextBlockObject{
Type: "plain_text",
Text: "testText",
Emoji: emojiFalse,
Verbatim: false,
},
expected: nil,
},
{
input: TextBlockObject{
Type: "plain_text",
Text: "testText",
Emoji: emojiTrue,
Verbatim: false,
},
expected: nil,
},
{
input: TextBlockObject{
Type: "plain_text",
Text: "testText",
Emoji: nil,
Verbatim: false,
},
expected: nil,
},
{
input: TextBlockObject{
Type: "mrkdwn",
Text: "testText",
Emoji: emojiFalse,
Verbatim: false,
},
expected: nil,
},
{
input: TextBlockObject{
Type: "mrkdwn",
Text: "testText",
Emoji: nil,
Verbatim: false,
},
expected: nil,
},
{
input: TextBlockObject{
Type: "invalid",
Text: "testText",
Emoji: emojiFalse,
Verbatim: false,
},
expected: errors.New("type must be either of plain_text or mrkdwn"),
},
{
input: TextBlockObject{
Type: "mrkdwn",
Text: "testText",
Emoji: emojiTrue,
Verbatim: false,
},
expected: errors.New("emoji cannot be true in mrkdown"),
},
{
input: TextBlockObject{
Type: "mrkdwn",
Text: "",
Emoji: emojiFalse,
Verbatim: false,
},
expected: errors.New("text must have a minimum length of 1"),
},
{
input: TextBlockObject{
Type: "mrkdwn",
Text: strings.Repeat("a", 3001),
Emoji: emojiFalse,
Verbatim: false,
},
expected: errors.New("text cannot be longer than 3000 characters"),
},
}
for _, test := range tests {
err := test.input.Validate()
assert.Equal(t, err, test.expected)
}
}
func TestTextBlockObject_UnmarshalJSON(t *testing.T) {
emojiTrue := new(bool)
emojiFalse := new(bool)
*emojiTrue = true
*emojiFalse = false
cases := []struct {
raw []byte
expected TextBlockObject
err error
}{
{
[]byte(`{"type":"plain_text","text":"testText"}`),
TextBlockObject{
Type: "plain_text",
Text: "testText",
Emoji: nil,
Verbatim: false,
},
nil,
},
{
[]byte(`{"type":"plain_text","text":":+1:","emoji":true}`),
TextBlockObject{
Type: "plain_text",
Text: ":+1:",
Emoji: emojiTrue,
Verbatim: false,
},
nil,
},
{
[]byte(`{"type":"plain_text","text":"No emojis allowed :(","emoji":false}`),
TextBlockObject{
Type: "plain_text",
Text: "No emojis allowed :(",
Emoji: emojiFalse,
Verbatim: false,
},
nil,
},
{
[]byte(`{"type":"mrkdwn","text":"testText"}`),
TextBlockObject{
Type: "mrkdwn",
Text: "testText",
Emoji: nil,
Verbatim: false,
},
nil,
},
{
[]byte(`{"type":"mrkdwn","text":"No emojis allowed :(","emoji":false}`),
TextBlockObject{
Type: "mrkdwn",
Text: "No emojis allowed :(",
Emoji: emojiFalse,
Verbatim: false,
},
nil,
},
}
for _, tc := range cases {
var actual TextBlockObject
err := json.Unmarshal(tc.raw, &actual)
if err != nil {
if tc.err == nil {
t.Errorf("unexpected error: %s", err)
}
t.Errorf("expected error is %v, but got %v", tc.err, err)
}
if tc.err != nil {
t.Errorf("expected to raise an error %v", tc.err)
}
if diff := deep.Equal(actual, tc.expected); diff != nil {
t.Errorf("actual value does not match expected one\n%s", diff)
}
}
}