add final test and apply pre-commit hooks

This commit is contained in:
northernSage 2021-03-26 15:46:39 -03:00
parent 0977769472
commit be2a64b6f8
1 changed files with 50 additions and 44 deletions

View File

@ -1,10 +1,10 @@
package gomatrix package gomatrix
import ( import (
"encoding/json" "encoding/json"
"testing" "reflect"
"strings" "strings"
"reflect" "testing"
) )
// example events from docs // example events from docs
@ -12,10 +12,11 @@ var testEvent = `{
"content": { "content": {
"body": "eventbody123", "body": "eventbody123",
"msgtype": "m.text", "msgtype": "m.text",
"membership": "join",
"format": "org.matrix.custom.html", "format": "org.matrix.custom.html",
"formatted_body": "<b>This is an example text message</b>" "formatted_body": "<b>This is an example text message</b>"
}, },
"type": "m.room.message", "type": "m.room.member",
"event_id": "$143273582443PhrSn:example.org", "event_id": "$143273582443PhrSn:example.org",
"room_id": "!726s6s6q:example.com", "room_id": "!726s6s6q:example.com",
"sender": "@example:example.org", "sender": "@example:example.org",
@ -26,64 +27,69 @@ var testEvent = `{
} }
}` }`
// IsRoom checks if a given variable is of type *Room // IsRoom checks if a given variable is of type *Room
func IsRoom(t interface{}) bool { func IsRoom(t interface{}) bool {
switch t.(type) { switch t.(type) {
case *Room: case *Room:
return true return true
default: default:
return false return false
} }
} }
func TestNewRoom (t *testing.T) { func TestNewRoom(t *testing.T) {
var testID = "test-id-001" var testID = "test-id-001"
r := NewRoom(testID) r := NewRoom(testID)
if r.ID != testID { if r.ID != testID {
t.Fatalf("TestNewRoom: Expected ID '%s', but got '%s'", testID, r.ID) t.Fatalf("TestNewRoom: Expected ID '%s', but got '%s'", testID, r.ID)
} }
if !IsRoom(r) { if !IsRoom(r) {
t.Fatalf("TestNewRoom: Wrong data type returned, expected 'Room'") t.Fatalf("TestNewRoom: Wrong data type returned, expected 'Room'")
} }
} }
func TestUpdateState (t *testing.T) { func TestUpdateState(t *testing.T) {
var e Event var e Event
err := json.NewDecoder(strings.NewReader(testEvent)).Decode(&e) err := json.NewDecoder(strings.NewReader(testEvent)).Decode(&e)
if err != nil { if err != nil {
t.Fatalf("TestUpdateState: Something went wrong while parsing: %s", testEvent) t.Fatalf("TestUpdateState: Something went wrong while parsing: %s", testEvent)
} }
r := NewRoom("test_id_001") r := NewRoom("test_id_001")
r.UpdateState(&e) r.UpdateState(&e)
// check mapping here first // check mapping here first
if r.State[e.Type][*e.StateKey] != &e { if r.State[e.Type][*e.StateKey] != &e {
t.Fatalf("TestUpdateState: Wrong object reference. The reference is not pointing to the expected event object") t.Fatalf("TestUpdateState: Wrong object reference. The reference is not pointing to the expected event object")
} }
} }
func TestGetStateEvent (t *testing.T) { func TestGetStateEvent(t *testing.T) {
var e Event var e Event
err := json.NewDecoder(strings.NewReader(testEvent)).Decode(&e) err := json.NewDecoder(strings.NewReader(testEvent)).Decode(&e)
if err != nil { if err != nil {
t.Fatalf("TestUpdateState: Something went wrong while parsing: %s", testEvent) t.Fatalf("TestUpdateState: Something went wrong while parsing: %s", testEvent)
} }
r := NewRoom("test_id_001") r := NewRoom("test_id_001")
r.UpdateState(&e) r.UpdateState(&e)
e2 := *r.GetStateEvent(e.Type, *e.StateKey) e2 := *r.GetStateEvent(e.Type, *e.StateKey)
if !reflect.DeepEqual(e, e2) { if !reflect.DeepEqual(e, e2) {
t.Fatalf("TestGetStateEvent: Wrong event object returned, expected object at address %p, got %p", &e, &e2) t.Fatalf("TestGetStateEvent: Wrong event object returned, expected object at address %p, got %p", &e, &e2)
} }
} }
func TestGetMembershipState (t *testing.T) { func TestGetMembershipState(t *testing.T) {
var e Event
var e Event
err := json.NewDecoder(strings.NewReader(testEvent)).Decode(&e) err := json.NewDecoder(strings.NewReader(testEvent)).Decode(&e)
if err != nil { if err != nil {
t.Fatalf("TestUpdateState: Something went wrong while parsing: %s", testEvent) t.Fatalf("TestUpdateState: Something went wrong while parsing: %s", testEvent)
} }
r := NewRoom("test_id_001") r := NewRoom("test_id_001")
r.UpdateState(&e) r.UpdateState(&e)
state := r.GetMembershipState("@alice:example.org") membState := r.GetMembershipState("@alice:example.org")
if membState != e.Content["membership"] {
t.Fatalf("TestGetMembershipState: Wrong membership state, expected '%s', got '%s'", e.Content["membership"], membState)
}
membState = r.GetMembershipState("@unkown:example.org")
if membState != "leave" {
t.Fatalf("TestGetMembershipState: Wrong membership state, expected 'leave', got '%s'", membState)
}
} }