| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/go-test/deep" |
| 8 | ) |
| 9 | |
| 10 | const ( |
| 11 | dummyPayload = `{ |
| 12 | "type":"rich_text", |
| 13 | "block_id":"FaYCD", |
| 14 | "elements": [ |
| 15 | { |
| 16 | "type":"rich_text_section", |
| 17 | "elements": [ |
| 18 | { |
| 19 | "type":"channel", |
| 20 | "channel_id":"C012345678" |
| 21 | }, |
| 22 | { |
| 23 | "type":"text", |
| 24 | "text":"dummy_text" |
| 25 | } |
| 26 | ] |
| 27 | } |
| 28 | ] |
| 29 | }` |
| 30 | ) |
| 31 | |
| 32 | func TestRichTextBlock_UnmarshalJSON(t *testing.T) { |
| 33 | cases := []struct { |
| 34 | raw []byte |
| 35 | expected RichTextBlock |
| 36 | err error |
| 37 | }{ |
| 38 | { |
| 39 | []byte(`{"elements":[{"type":"rich_text_unknown"},{"type":"rich_text_section"}]}`), |
| 40 | RichTextBlock{ |
| 41 | Elements: []RichTextElement{ |
| 42 | &RichTextUnknown{Type: RTEUnknown, Raw: `{"type":"rich_text_unknown"}`}, |
| 43 | &RichTextSection{Type: RTESection, Elements: []RichTextSectionElement{}}, |
| 44 | }, |
| 45 | }, |
| 46 | nil, |
| 47 | }, |
| 48 | { |
| 49 | []byte(`{"type": "rich_text","block_id":"blk","elements":[]}`), |
| 50 | RichTextBlock{ |
| 51 | Type: MBTRichText, |
| 52 | BlockID: "blk", |
| 53 | Elements: []RichTextElement{}, |
| 54 | }, |
| 55 | nil, |
| 56 | }, |
| 57 | } |
| 58 | for _, tc := range cases { |
| 59 | var actual RichTextBlock |
| 60 | err := json.Unmarshal(tc.raw, &actual) |
| 61 | if err != nil { |
| 62 | if tc.err == nil { |
| 63 | t.Errorf("unexpected error: %s", err) |
| 64 | } |
| 65 | t.Errorf("expected error is %s, but got %s", tc.err, err) |
| 66 | } |
| 67 | if tc.err != nil { |
| 68 | t.Errorf("expected to raise an error %s", tc.err) |
| 69 | } |
| 70 | if diff := deep.Equal(actual, tc.expected); diff != nil { |
| 71 | t.Errorf("actual value does not match expected one\n%s", diff) |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func TestRichTextSection_UnmarshalJSON(t *testing.T) { |
| 77 | cases := []struct { |
| 78 | raw []byte |
| 79 | expected RichTextSection |
| 80 | err error |
| 81 | }{ |
| 82 | { |
| 83 | []byte(`{"elements":[{"type":"unknown","value":10},{"type":"text","text":"hi"}]}`), |
| 84 | RichTextSection{ |
| 85 | Type: RTESection, |
| 86 | Elements: []RichTextSectionElement{ |
| 87 | &RichTextSectionUnknownElement{Type: RTSEUnknown, Raw: `{"type":"unknown","value":10}`}, |
| 88 | &RichTextSectionTextElement{Type: RTSEText, Text: "hi"}, |
| 89 | }, |
| 90 | }, |
| 91 | nil, |
| 92 | }, |
| 93 | { |
| 94 | []byte(`{"type": "rich_text_section","elements":[]}`), |
| 95 | RichTextSection{ |
| 96 | Type: RTESection, |
| 97 | Elements: []RichTextSectionElement{}, |
| 98 | }, |
| 99 | nil, |
| 100 | }, |
| 101 | } |
| 102 | for _, tc := range cases { |
| 103 | var actual RichTextSection |
| 104 | err := json.Unmarshal(tc.raw, &actual) |
| 105 | if err != nil { |
| 106 | if tc.err == nil { |
| 107 | t.Errorf("unexpected error: %s", err) |
| 108 | } |
| 109 | t.Errorf("expected error is %s, but got %s", tc.err, err) |
| 110 | } |
| 111 | if tc.err != nil { |
| 112 | t.Errorf("expected to raise an error %s", tc.err) |
| 113 | } |
| 114 | if diff := deep.Equal(actual, tc.expected); diff != nil { |
| 115 | t.Errorf("actual value does not match expected one\n%s", diff) |
| 116 | } |
| 117 | } |
| 118 | } |