github.com/Ptt-official-app/go-bbs@v0.12.0/user_comment_record_test.go (about) 1 package bbs 2 3 import ( 4 "strings" 5 "testing" 6 "time" 7 ) 8 9 func TestNewUserCommentRecord(t *testing.T) { 10 perfectData := "→ lex: 快一點 05/15 01:06" 11 expectedOrder := uint32(1) 12 expectedOwner := "lex" 13 expectedTime := time.Date(0, 5, 15, 1, 6, 0, 0, time.UTC) 14 expectedBoardID := "SYSOP" 15 expectedComment := "快一點" 16 expectedArticleRecord := &MockArticleRecord{} 17 18 got, err := NewUserCommentRecord(1, perfectData, expectedBoardID, expectedArticleRecord) 19 20 if got.CommentOrder() != expectedOrder { 21 t.Errorf("comment order = %v, expected %v\n", got.CommentOrder(), expectedOrder) 22 } 23 24 if strings.Compare(got.Owner(), expectedOwner) != 0 { 25 t.Errorf("comment owner = %v, expected %v\n", got.Owner(), expectedOwner) 26 } 27 28 if !got.CommentTime().Equal(expectedTime) { 29 t.Errorf("comment time = %v, expected %v\n", got.CommentTime(), expectedTime) 30 } 31 32 if got.BoardID() != expectedBoardID { 33 t.Errorf("boardID = %v, expected %v\n", got.BoardID(), expectedBoardID) 34 } 35 36 if got.Filename() != expectedArticleRecord.Filename() { 37 t.Errorf("boardID = %v, expected %v\n", got.Filename(), expectedArticleRecord.Filename()) 38 } 39 40 if strings.Compare(got.Comment(), expectedComment) != 0 { 41 t.Errorf("comment = %v, expected %v\n", got.Comment(), expectedComment) 42 } 43 44 if err != nil { 45 t.Errorf("err = %v, should be nil\n", err) 46 } 47 } 48 49 func TestParseUserComment(t *testing.T) { 50 51 expectedTime := time.Date(0, 5, 15, 1, 6, 0, 0, time.UTC) 52 expectedComment := "快一點,lex:" 53 emptyTime := time.Time{} 54 emptyComment := "" 55 56 perfectData := "→ lex: 快一點,lex: 05/15 01:06" 57 dataWithoutOwner := ": 快一點 05/15 01:06" 58 dataWithInvalidTime := "→ lex: 快一點 5/15 01:06" 59 emptyData := "" 60 61 type args struct { 62 data string 63 } 64 type expected struct { 65 owner string 66 ctime time.Time 67 comment string 68 hasErr bool 69 } 70 tests := []struct { 71 name string 72 args args 73 expected expected 74 }{ 75 { 76 name: "parse perfect data", 77 args: args{perfectData}, 78 expected: expected{"lex", expectedTime, expectedComment, false}, 79 }, 80 { 81 name: "parse data without owner should return error", 82 args: args{dataWithoutOwner}, 83 expected: expected{"", emptyTime, emptyComment, true}, 84 }, 85 { 86 name: "parse data with invalid time should return error", 87 args: args{dataWithInvalidTime}, 88 expected: expected{"", emptyTime, emptyComment, true}, 89 }, 90 { 91 name: "parse empty data should return error", 92 args: args{emptyData}, 93 expected: expected{"", emptyTime, emptyComment, true}, 94 }, 95 } 96 97 for _, tt := range tests { 98 t.Run(tt.name, func(t *testing.T) { 99 gotOwner, gotTime, gotComment, gotErr := parseUserComment(tt.args.data) 100 101 if strings.Compare(gotOwner, tt.expected.owner) != 0 { 102 t.Errorf("comment owner = %v, expected %v\n", gotOwner, tt.expected.owner) 103 } 104 105 if !gotTime.Equal(tt.expected.ctime) { 106 t.Errorf("comment time = %v, expected %v\n", gotTime, tt.expected.ctime) 107 } 108 109 if strings.Compare(gotComment, tt.expected.comment) != 0 { 110 t.Errorf("comment time = %v, expected %v\n", gotTime, tt.expected.ctime) 111 } 112 113 if (gotErr != nil) != tt.expected.hasErr { 114 t.Errorf("err = %v, should be nil\n", gotErr) 115 } 116 }) 117 } 118 }