forked from matrix-org/sliding-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
room_test.go
74 lines (70 loc) · 1.8 KB
/
room_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
package sync3
import (
"encoding/json"
"fmt"
"github.com/tidwall/gjson"
"reflect"
"testing"
)
func TestAvatarChangeMarshalling(t *testing.T) {
var url = "mxc://..."
testCases := []struct {
Name string
AvatarChange AvatarChange
Check func(avatar gjson.Result) error
}{
{
Name: "Avatar exists",
AvatarChange: NewAvatarChange(url),
Check: func(avatar gjson.Result) error {
if !(avatar.Exists() && avatar.Type == gjson.String && avatar.Str == url) {
return fmt.Errorf("unexpected marshalled avatar: got %#v want %s", avatar, url)
}
return nil
},
},
{
Name: "Avatar doesn't exist",
AvatarChange: DeletedAvatar,
Check: func(avatar gjson.Result) error {
if !(avatar.Exists() && avatar.Type == gjson.Null) {
return fmt.Errorf("unexpected marshalled Avatar: got %#v want null", avatar)
}
return nil
},
},
{
Name: "Avatar unchanged",
AvatarChange: UnchangedAvatar,
Check: func(avatar gjson.Result) error {
if avatar.Exists() {
return fmt.Errorf("unexpected marshalled Avatar: got %#v want omitted", avatar)
}
return nil
},
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
room := Room{AvatarChange: tc.AvatarChange}
marshalled, err := json.Marshal(room)
t.Logf("Marshalled to %s", string(marshalled))
if err != nil {
t.Fatal(err)
}
avatar := gjson.GetBytes(marshalled, "avatar")
if err = tc.Check(avatar); err != nil {
t.Fatal(err)
}
var unmarshalled Room
err = json.Unmarshal(marshalled, &unmarshalled)
if err != nil {
t.Fatal(err)
}
t.Logf("Unmarshalled to %#v", unmarshalled.AvatarChange)
if !reflect.DeepEqual(unmarshalled, room) {
t.Fatalf("Unmarshalled struct is different from original")
}
})
}
}