-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_message_test.go
128 lines (119 loc) · 3.46 KB
/
chat_message_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package arbor_test
import (
"testing"
"time"
arbor "github.com/arborchat/arbor-go"
)
const testContent = "Test message"
const testContent2 = "another test message"
// TestNewChatMessage ensures that the chat message is created with its
// `Content` and `Timestamp` fields populated and its `Parent` field cleared.
func TestNewChatMessage(t *testing.T) {
m, err := arbor.NewChatMessage(testContent)
if err != nil {
t.Fatalf("Unable to create ChatMessage with valid parameters, error: %v", err)
}
if m == nil {
t.Fatal("Recieved nil ChatMessage from valid call to NewChatMessage")
}
if m.Parent != "" {
t.Errorf("Expected \"\" as Parent, found %v", m.Parent)
}
if m.Content != testContent {
t.Errorf("Expected \"%s\" as Content, found \"%s\"", testContent, m.Content)
}
if unreasonableTimestamp(m.Timestamp) {
t.Errorf("Timestamp invalid (either in the future or before the epoch): %d", m.Timestamp)
}
}
func newMessageOrSkip(t *testing.T, content string) *arbor.ChatMessage {
m, err := arbor.NewChatMessage(testContent)
if err != nil || m == nil {
t.Skip("Unable to create message")
}
return m
}
// TestAssignID ensures that the AssignID function actually populates the UUID field
// of the ChatMessage
func TestAssignID(t *testing.T) {
m := newMessageOrSkip(t, testContent)
err := m.AssignID()
if err != nil {
t.Error("Failed to assign UUID", err)
return
}
if m.UUID == "" {
t.Error("No UUID assigned", m)
}
}
func unreasonableTimestamp(timestamp int64) bool {
return timestamp > time.Now().Unix() || timestamp < 0
}
// TestReply ensures that the Reply method creates a new ChatMessage with the correct
// Parent and Content as well as a reasonable Timestamp.
func TestReply(t *testing.T) {
m := newMessageOrSkip(t, testContent)
err := m.AssignID()
if err != nil || m.UUID == "" {
t.Skip("Failed to assign UUID", err)
return
}
m2, err := m.Reply(testContent2)
if err != nil {
t.Error("Failed create reply", err)
return
}
if m2.Parent == "" {
t.Error("No parent assigned", m)
} else if m2.Parent != m.UUID {
t.Errorf("Expected Parent to be %s, found %s", m.UUID, m2.Parent)
}
if m2.Content != testContent2 {
t.Errorf("Expected Content to be \"%s\", found \"%s\"", testContent2, m2.Content)
}
if m == m2 {
t.Error("Reply reused original structure")
}
if unreasonableTimestamp(m2.Timestamp) {
t.Errorf("Timestamp invalid (either in the future or before the epoch): %d", m2.Timestamp)
}
}
// TestEquals checks that the Equals method correctly identifies identical messages.
func TestEquals(t *testing.T) {
m := newMessageOrSkip(t, testContent)
if !m.Equals(m) {
t.Error("Message reports that it is not equal to itself")
}
if m.Equals(nil) {
t.Error("Non-nil message reports that it is equal to nil")
}
m2 := *m
if !m.Equals(&m2) {
t.Error("Message reports that it is not equal to a copy of itself")
}
m2.Content += "foo"
if m.Equals(&m2) {
t.Error("Messages with different Content reported as same")
}
m2.Content = m.Content
m2.Parent += "foo"
if m.Equals(&m2) {
t.Error("Messages with different Parent reported as same")
}
m2.Parent = m.Parent
m2.UUID += "foo"
if m.Equals(&m2) {
t.Error("Messages with different UUID reported as same")
}
m2.UUID = m.UUID
m2.Username += "foo"
if m.Equals(&m2) {
t.Error("Messages with different Username reported as same")
}
m2.Username = m.Username
m2.Timestamp++
if m.Equals(&m2) {
t.Error("Messages with different Timestamp reported as same")
}
m2.Timestamp = m.Timestamp
}