| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/go-test/deep" |
| 9 | ) |
| 10 | |
| 11 | func TestAttachment_UnmarshalMarshalJSON_WithBlocks(t *testing.T) { |
| 12 | |
| 13 | originalAttachmentJson := `{ |
| 14 | "id": 1, |
| 15 | "blocks": [ |
| 16 | { |
| 17 | "type": "section", |
| 18 | "block_id": "xxxx", |
| 19 | "text": { |
| 20 | "type": "mrkdwn", |
| 21 | "text": "Pick something:", |
| 22 | "verbatim": true |
| 23 | }, |
| 24 | "accessory": { |
| 25 | "type": "static_select", |
| 26 | "action_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", |
| 27 | "placeholder": { |
| 28 | "type": "plain_text", |
| 29 | "text": "Select one item", |
| 30 | "emoji": true |
| 31 | }, |
| 32 | "options": [ |
| 33 | { |
| 34 | "text": { |
| 35 | "type": "plain_text", |
| 36 | "text": "ghi", |
| 37 | "emoji": true |
| 38 | }, |
| 39 | "value": "ghi" |
| 40 | } |
| 41 | ] |
| 42 | } |
| 43 | } |
| 44 | ], |
| 45 | "color": "#13A554", |
| 46 | "fallback": "[no preview available]" |
| 47 | }` |
| 48 | |
| 49 | attachment := new(Attachment) |
| 50 | err := json.Unmarshal([]byte(originalAttachmentJson), attachment) |
| 51 | if err != nil { |
| 52 | t.Fatalf("expected no error unmarshaling attachment with blocks, got: %v", err) |
| 53 | } |
| 54 | |
| 55 | actualAttachmentJson, err := json.Marshal(attachment) |
| 56 | if err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | |
| 60 | var ( |
| 61 | actual interface{} |
| 62 | expected interface{} |
| 63 | ) |
| 64 | if err = json.Unmarshal([]byte(originalAttachmentJson), &expected); err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | if err = json.Unmarshal(actualAttachmentJson, &actual); err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | if diff := deep.Equal(actual, expected); diff != nil { |
| 71 | t.Fatal("actual does not match expected\n", strings.Join(diff, "\n")) |
| 72 | } |
| 73 | } |