forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_rich_text_test.go
201 lines (195 loc) · 4.84 KB
/
block_rich_text_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
package slack
import (
"encoding/json"
"testing"
"github.com/go-test/deep"
)
const (
dummyPayload = `{
"type":"rich_text",
"block_id":"FaYCD",
"elements": [
{
"type":"rich_text_section",
"elements": [
{
"type":"channel",
"channel_id":"C012345678"
},
{
"type":"text",
"text":"dummy_text"
}
]
}
]
}`
)
func TestRichTextBlock_UnmarshalJSON(t *testing.T) {
cases := []struct {
raw []byte
expected RichTextBlock
err error
}{
{
[]byte(`{"elements":[{"type":"rich_text_unknown"},{"type":"rich_text_section"},{"type":"rich_text_list"}]}`),
RichTextBlock{
Elements: []RichTextElement{
&RichTextUnknown{Type: RTEUnknown, Raw: `{"type":"rich_text_unknown"}`},
&RichTextSection{Type: RTESection, Elements: []RichTextSectionElement{}},
&RichTextList{Type: RTEList, Elements: []RichTextElement{}},
},
},
nil,
},
{
[]byte(`{"type": "rich_text","block_id":"blk","elements":[]}`),
RichTextBlock{
Type: MBTRichText,
BlockID: "blk",
Elements: []RichTextElement{},
},
nil,
},
}
for _, tc := range cases {
var actual RichTextBlock
err := json.Unmarshal(tc.raw, &actual)
if err != nil {
if tc.err == nil {
t.Errorf("unexpected error: %s", err)
}
t.Errorf("expected error is %s, but got %s", tc.err, err)
}
if tc.err != nil {
t.Errorf("expected to raise an error %s", tc.err)
}
if diff := deep.Equal(actual, tc.expected); diff != nil {
t.Errorf("actual value does not match expected one\n%s", diff)
}
}
}
func TestRichTextSection_UnmarshalJSON(t *testing.T) {
cases := []struct {
raw []byte
expected RichTextSection
err error
}{
{
[]byte(`{"elements":[{"type":"unknown","value":10},{"type":"text","text":"hi"},{"type":"date","timestamp":1636961629}]}`),
RichTextSection{
Type: RTESection,
Elements: []RichTextSectionElement{
&RichTextSectionUnknownElement{Type: RTSEUnknown, Raw: `{"type":"unknown","value":10}`},
&RichTextSectionTextElement{Type: RTSEText, Text: "hi"},
&RichTextSectionDateElement{Type: RTSEDate, Timestamp: JSONTime(1636961629)},
},
},
nil,
},
{
[]byte(`{"type": "rich_text_section","elements":[]}`),
RichTextSection{
Type: RTESection,
Elements: []RichTextSectionElement{},
},
nil,
},
}
for _, tc := range cases {
var actual RichTextSection
err := json.Unmarshal(tc.raw, &actual)
if err != nil {
if tc.err == nil {
t.Errorf("unexpected error: %s", err)
}
t.Errorf("expected error is %s, but got %s", tc.err, err)
}
if tc.err != nil {
t.Errorf("expected to raise an error %s", tc.err)
}
if diff := deep.Equal(actual, tc.expected); diff != nil {
t.Errorf("actual value does not match expected one\n%s", diff)
}
}
}
func TestRichTextList_UnmarshalJSON(t *testing.T) {
cases := []struct {
raw []byte
expected RichTextList
err error
}{
{
[]byte(`{"style":"ordered","elements":[{"type":"rich_text_unknown","value":10},{"type":"rich_text_section","elements":[{"type":"text","text":"hi"}]}]}`),
RichTextList{
Type: RTEList,
Style: RTEListOrdered,
Elements: []RichTextElement{
&RichTextUnknown{Type: RTEUnknown, Raw: `{"type":"rich_text_unknown","value":10}`},
&RichTextSection{
Type: RTESection,
Elements: []RichTextSectionElement{
&RichTextSectionTextElement{Type: RTSEText, Text: "hi"},
},
},
},
},
nil,
},
{
[]byte(`{"style":"ordered","elements":[{"type":"rich_text_list","style":"bullet","elements":[{"type":"rich_text_section","elements":[{"type":"text","text":"hi"}]}]}]}`),
RichTextList{
Type: RTEList,
Style: RTEListOrdered,
Elements: []RichTextElement{
&RichTextList{
Type: RTEList,
Style: RTEListBullet,
Elements: []RichTextElement{
&RichTextSection{
Type: RTESection,
Elements: []RichTextSectionElement{
&RichTextSectionTextElement{Type: RTSEText, Text: "hi"},
},
},
},
},
},
},
nil,
},
{
[]byte(`{"type": "rich_text_list","elements":[]}`),
RichTextList{
Type: RTEList,
Elements: []RichTextElement{},
},
nil,
},
{
[]byte(`{"type": "rich_text_list","elements":[],"indent":2}`),
RichTextList{
Type: RTEList,
Indent: 2,
Elements: []RichTextElement{},
},
nil,
},
}
for _, tc := range cases {
var actual RichTextList
err := json.Unmarshal(tc.raw, &actual)
if err != nil {
if tc.err == nil {
t.Errorf("unexpected error: %s", err)
}
t.Errorf("expected error is %s, but got %s", tc.err, err)
}
if tc.err != nil {
t.Errorf("expected to raise an error %s", tc.err)
}
if diff := deep.Equal(actual, tc.expected); diff != nil {
t.Errorf("actual value does not match expected one\n%s", diff)
}
}
}