From 36e440bcce88658e5a44cac2852646f3f7dc12ed Mon Sep 17 00:00:00 2001 From: northernSage Date: Fri, 26 Feb 2021 03:40:12 -0300 Subject: [PATCH] add tests for events.go --- events_test.go | 110 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 events_test.go diff --git a/events_test.go b/events_test.go new file mode 100644 index 0000000..3ac5cca --- /dev/null +++ b/events_test.go @@ -0,0 +1,110 @@ +package gomatrix + +import ( + "encoding/json" + "strings" + "testing" +) + +// example events from docs +var testEvents = map[string]string{ + "withFields": `{ + "content": { + "body": "eventbody123", + "msgtype": "m.text", + "format": "org.matrix.custom.html", + "formatted_body": "This is an example text message" + }, + "type": "m.room.message", + "event_id": "$143273582443PhrSn:example.org", + "room_id": "!726s6s6q:example.com", + "sender": "@example:example.org", + "origin_server_ts": 1432735824653, + "unsigned": { + "age": 1234 + } + }`, + + "withoutFields": `{ + "content": { + "avatar_url": "mxc://example.org/SEsfnsuifSDFSSEF", + "displayname": "Alice Margatroid", + "membership": "join" + }, + "event_id": "$143273582443PhrSn:example.org", + "origin_server_ts": 1432735824653, + "room_id": "!jEsUZKDJdhlrceRyVU:example.org", + "sender": "@example:example.org", + "state_key": "@alice:example.org", + "type": "m.room.member", + "unsigned": { + "age": 1234 + } + }`, +} + +func TestEventWithBody(t *testing.T) { + var e Event + err := json.NewDecoder(strings.NewReader(testEvents["withFields"])).Decode(&e) + if err != nil { + t.Fatalf("TestFetchEventBody: Something went wrong while parsing: %s", testEvents["withFields"]) + } + body, ok := e.Body() + if !ok || body != "eventbody123" { + t.Fatal("TestEventWithBody: Failed to fetch value of 'body' key in event content") + } +} + +func TestEventWithoutBody(t *testing.T) { + var e Event + err := json.NewDecoder(strings.NewReader(testEvents["withoutFields"])).Decode(&e) + if err != nil { + t.Fatalf("TestEventWithoutBody: Something went wrong while parsing: %s", testEvents["withFields"]) + } + body, ok := e.Body() + if ok || body != "" { + t.Fatal("TestEventWithoutBody: Failed on 'Event.Body' call for event without a 'body' key") + } +} + +func TestEventWithMessageType(t *testing.T) { + var e Event + err := json.NewDecoder(strings.NewReader(testEvents["withFields"])).Decode(&e) + if err != nil { + t.Fatalf("TestEventWithMessageType: Something went wrong while parsing: %s", testEvents["withFields"]) + } + msgtype, ok := e.MessageType() + if !ok || msgtype != "m.text" { + t.Fatal("TestEventWithMessageType: Failed to fetch value of 'msgtype' key in event content") + } +} + +func TestEventWithoutMessageType(t *testing.T) { + var e Event + err := json.NewDecoder(strings.NewReader(testEvents["withoutFields"])).Decode(&e) + if err != nil { + t.Fatalf("TestEventWithMessageType: Something went wrong while parsing: %s", testEvents["withFields"]) + } + msgtype, ok := e.MessageType() + if ok || msgtype != "" { + t.Fatal("TestEventWithoutBody: Failed on 'Event.Body' call for event without a 'msgtype' key") + } +} + +var testHTML = `
a

bc

d

efghi

j

k
lmno

pqrs
` + +func TestGetHTMLMessage(t *testing.T) { + msg := GetHTMLMessage("m.text", testHTML) + if expected := "abcdefghijklmnopqrs"; msg.Body != expected { + t.Fatalf("TestGetHTMLMessage: got '%s', expected '%s'", msg.Body, expected) + } + if msg.FormattedBody != testHTML { + t.Fatalf("TestGetHTMLMessage: got '%s', expected '%s'", msg.FormattedBody, testHTML) + } + if msg.MsgType != "m.text" { + t.Fatalf("TestGetHTMLMessage: got '%s', expected 'm.text'", msg.FormattedBody) + } + if expected := "org.matrix.custom.html"; msg.Format != expected { + t.Fatalf("TestGetHTMLMessage: got '%s', expected '%s'", msg.Format, expected) + } +}