package mirclike_test import ( "fmt" "testing" "time" "github.com/stretchr/testify/assert" "git.aiterp.net/rpdata/api/internal/importers/mirclike" "git.aiterp.net/rpdata/api/models" ) func TestParsePost(t *testing.T) { table := []struct { Input string TS string Kind string Nick string Text string }{ { "[12:34] * Stuff does things.", "12:34:00", "action", "Stuff", "does things.", }, { "[12:34] Things said.", "12:34:00", "text", "Stuff", "Things said.", }, { "[13:36:59] Things said.", "13:36:59", "text", "Stuff", "Things said.", }, { "[23:59] <=Scene=> Scenery and such.", "23:59:00", "scene", "=Scene=", "Scenery and such.", }, { "[01:10:11] * =Scene= Scenery and such from the forum or mIRC using my old script.", "01:10:11", "scene", "=Scene=", "Scenery and such from the forum or mIRC using my old script.", }, } for i, row := range table { t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) { post, err := mirclike.ParsePost(row.Input, time.Now(), models.Post{}) if err != nil { t.Fatal("Could not parse post:", err) } assert.Equal(t, row.TS, post.Time.Format("15:04:05"), "Timestamps should match.") assert.Equal(t, row.Kind, post.Kind, "Kinds should match.") assert.Equal(t, row.Nick, post.Nick, "Kinds should match.") assert.Equal(t, row.Text, post.Text, "Kinds should match.") }) } } func TestParsePostErrors(t *testing.T) { table := []struct { Input string Err error }{ {"[12:34] Things said.", nil}, {"[12:34] >Stuff> Things said.", mirclike.ErrNotPost}, {"12:34] Things said.", mirclike.ErrNotPost}, {"* Stuff Things said.", mirclike.ErrNotPost}, {"", mirclike.ErrNotPost}, {"[12:34 Things said.", mirclike.ErrNotPost}, {"[TE:XT] Things said.", mirclike.ErrNotPost}, {"[10] Things said.", mirclike.ErrNotPost}, {"[12:34:56:789] Things said.", nil}, {"[12:34:56.789] Things said.", mirclike.ErrNotPost}, } for i, row := range table { t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) { _, err := mirclike.ParsePost(row.Input, time.Now(), models.Post{}) assert.Equal(t, row.Err, err, "Error should match") }) } } func TestParseNextDay(t *testing.T) { table := []struct { Prev time.Time TS string Time time.Time }{ {Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "12:01", Time: parseDate(t, "2019-01-12 12:01:00")}, {Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "11:53:13", Time: parseDate(t, "2019-01-12 11:53:13")}, {Prev: parseDate(t, "2019-04-08 23:51:59"), TS: "00:09", Time: parseDate(t, "2019-04-09 00:09:00")}, {Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "11:29:59", Time: parseDate(t, "2019-01-13 11:29:59")}, } for i, row := range table { t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) { input := fmt.Sprintf("[%s] * Stuff does things.", row.TS) post, err := mirclike.ParsePost(input, row.Prev, models.Post{Time: row.Prev}) if err != nil { t.Fatal("Could not parse post:", err) } assert.Equal(t, row.Time, post.Time) }) } } func parseDate(t *testing.T, date string) time.Time { result, err := time.Parse("2006-01-02 15:04:05", date) if err != nil { if t != nil { t.Fatal("Could not parse date", date, err) } else { panic("Could not parse date: " + err.Error()) } } return result } func formatDate(date time.Time) string { return date.UTC().Format("2006-01-02 15:04:05") }