-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathuser_comment_record_test.go
118 lines (99 loc) · 3.22 KB
/
user_comment_record_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
package bbs
import (
"strings"
"testing"
"time"
)
func TestNewUserCommentRecord(t *testing.T) {
perfectData := "→ lex: 快一點 05/15 01:06"
expectedOrder := uint32(1)
expectedOwner := "lex"
expectedTime := time.Date(0, 5, 15, 1, 6, 0, 0, time.UTC)
expectedBoardID := "SYSOP"
expectedComment := "快一點"
expectedArticleRecord := &MockArticleRecord{}
got, err := NewUserCommentRecord(1, perfectData, expectedBoardID, expectedArticleRecord)
if got.CommentOrder() != expectedOrder {
t.Errorf("comment order = %v, expected %v\n", got.CommentOrder(), expectedOrder)
}
if strings.Compare(got.Owner(), expectedOwner) != 0 {
t.Errorf("comment owner = %v, expected %v\n", got.Owner(), expectedOwner)
}
if !got.CommentTime().Equal(expectedTime) {
t.Errorf("comment time = %v, expected %v\n", got.CommentTime(), expectedTime)
}
if got.BoardID() != expectedBoardID {
t.Errorf("boardID = %v, expected %v\n", got.BoardID(), expectedBoardID)
}
if got.Filename() != expectedArticleRecord.Filename() {
t.Errorf("boardID = %v, expected %v\n", got.Filename(), expectedArticleRecord.Filename())
}
if strings.Compare(got.Comment(), expectedComment) != 0 {
t.Errorf("comment = %v, expected %v\n", got.Comment(), expectedComment)
}
if err != nil {
t.Errorf("err = %v, should be nil\n", err)
}
}
func TestParseUserComment(t *testing.T) {
expectedTime := time.Date(0, 5, 15, 1, 6, 0, 0, time.UTC)
expectedComment := "快一點,lex:"
emptyTime := time.Time{}
emptyComment := ""
perfectData := "→ lex: 快一點,lex: 05/15 01:06"
dataWithoutOwner := ": 快一點 05/15 01:06"
dataWithInvalidTime := "→ lex: 快一點 5/15 01:06"
emptyData := ""
type args struct {
data string
}
type expected struct {
owner string
ctime time.Time
comment string
hasErr bool
}
tests := []struct {
name string
args args
expected expected
}{
{
name: "parse perfect data",
args: args{perfectData},
expected: expected{"lex", expectedTime, expectedComment, false},
},
{
name: "parse data without owner should return error",
args: args{dataWithoutOwner},
expected: expected{"", emptyTime, emptyComment, true},
},
{
name: "parse data with invalid time should return error",
args: args{dataWithInvalidTime},
expected: expected{"", emptyTime, emptyComment, true},
},
{
name: "parse empty data should return error",
args: args{emptyData},
expected: expected{"", emptyTime, emptyComment, true},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotOwner, gotTime, gotComment, gotErr := parseUserComment(tt.args.data)
if strings.Compare(gotOwner, tt.expected.owner) != 0 {
t.Errorf("comment owner = %v, expected %v\n", gotOwner, tt.expected.owner)
}
if !gotTime.Equal(tt.expected.ctime) {
t.Errorf("comment time = %v, expected %v\n", gotTime, tt.expected.ctime)
}
if strings.Compare(gotComment, tt.expected.comment) != 0 {
t.Errorf("comment time = %v, expected %v\n", gotTime, tt.expected.ctime)
}
if (gotErr != nil) != tt.expected.hasErr {
t.Errorf("err = %v, should be nil\n", gotErr)
}
})
}
}