forked from winston-stripe/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_rich_text_test.go
118 lines (113 loc) · 2.6 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
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"}]}`),
RichTextBlock{
Elements: []RichTextElement{
&RichTextUnknown{Type: RTEUnknown, Raw: `{"type":"rich_text_unknown"}`},
&RichTextSection{Type: RTESection, Elements: []RichTextSectionElement{}},
},
},
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"}]}`),
RichTextSection{
Type: RTESection,
Elements: []RichTextSectionElement{
&RichTextSectionUnknownElement{Type: RTSEUnknown, Raw: `{"type":"unknown","value":10}`},
&RichTextSectionTextElement{Type: RTSEText, Text: "hi"},
},
},
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)
}
}
}