GraphQL API and utilities for the rpdata project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

229 lines
7.0 KiB

package parsers_test
import (
"fmt"
"git.aiterp.net/rpdata/api/models"
"git.aiterp.net/rpdata/api/services/parsers"
"github.com/stretchr/testify/assert"
"strings"
"testing"
"time"
)
func TestMircPost(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] <Stuff> Things said.",
"12:34:00", "text", "Stuff", "Things said.",
},
{
"[13:36:59] <Stuff> Things said.",
"13:36:59", "text", "Stuff", "Things said.",
},
{
"[13:36:59] <\u001FStuff\u001F> 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 := parsers.MircPost(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 TestMircPostErrors(t *testing.T) {
table := []struct {
Input string
Err string
}{
{"[12:34] <Stuff> Things said.", ""},
{"[12:34] >Stuff> Things said.", "line is neither action nor text post"},
{"12:34] <Stuff> Things said.", "no timestamp"},
{"* Stuff Things said.", "no timestamp"},
{"", "no timestamp"},
{"[12:34 <Stuff> Things said.", "incomplete timestamp"},
{"[TE:XT] <Stuff> Things said.", "invalid number in timestamp"},
{"[10] <Stuff> Things said.", "invalid timestamp"},
{"[12:34:56:789] <Stuff> Things said.", ""},
{"[12:34:56.789] <Stuff> Things said.", "invalid number in timestamp"},
{"[12:34] <Stuff>", "post is empty"},
{"[12:34] * Stuff", "post is empty"},
{"[12:34] =Scene=", "line is neither action nor text post"},
{"[12:34] <=Scene=>", "post is empty"},
}
for i, row := range table {
t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) {
_, err := parsers.MircPost(row.Input, time.Now(), models.Post{})
errProblem := ""
if err != nil {
if e2, ok := err.(*parsers.ParseError); ok {
errProblem = e2.Problem
} else {
errProblem = err.Error()
}
}
assert.Equal(t, row.Err, errProblem, "Error should match")
})
}
}
func TestMircPostNextDay(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-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 := parsers.MircPost(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 TestMircPostDayPerPostBug(t *testing.T) {
startDate := parseDate(t, "2019-07-20 23:25:00")
table := []struct {
TS string
Time time.Time
}{
{TS: "23:25", Time: parseDate(t, "2019-07-20 23:25:00")},
{TS: "23:45", Time: parseDate(t, "2019-07-20 23:45:00")},
{TS: "23:59", Time: parseDate(t, "2019-07-20 23:59:00")},
{TS: "00:00", Time: parseDate(t, "2019-07-21 00:00:00")},
{TS: "00:03", Time: parseDate(t, "2019-07-21 00:03:00")},
{TS: "00:06", Time: parseDate(t, "2019-07-21 00:06:00")},
{TS: "12:00", Time: parseDate(t, "2019-07-21 12:00:00")},
{TS: "22:00", Time: parseDate(t, "2019-07-21 22:00:00")},
{TS: "23:55", Time: parseDate(t, "2019-07-21 23:55:00")},
{TS: "00:05", Time: parseDate(t, "2019-07-22 00:05:00")},
}
prev := startDate
for i, row := range table {
t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) {
input := fmt.Sprintf("[%s] * Test post content.", row.TS)
post, err := parsers.MircPost(input, startDate, models.Post{Time: prev})
if err != nil {
t.Fatal("Could not parse post:", err)
}
prev = post.Time
assert.Equal(t, row.Time, post.Time)
})
}
}
func TestMircLog(t *testing.T) {
parsed, err := parsers.MircLog(testLog, parseDate(t, "2018-05-11 00:00:00"), false)
if err != nil {
t.Fatal("ParseLog", err)
}
assert.Equal(t, testLogPosts, parsed.Posts)
assert.Equal(t, parsed.Posts[0].Time, parsed.Log.Date, "Log's date should be the first post's.")
}
func TestMircLogErrors(t *testing.T) {
_, err1 := parsers.MircLog("\n\n\n\n\n \n\t\r\n", time.Time{}, false)
_, err2 := parsers.MircLog("\n\n\n\n\n[14:57]* Stuff \n\t\r\n", time.Time{}, true)
assert.Equal(t, parsers.ErrEmptyLog, err1)
assert.True(t, parsers.IsParseError(err2))
}
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
}
var testLog = strings.Join([]string{
"[11:21] * Va`ynna_Atana returns to the apartment at the end of another long day. She hangs up her jacket and a green scarf next to the door before going straight to the bathroom; she leaves the door open, though. She walked home with Uvena this time, but the two parted ways downstairs.",
"",
"[11:21] <=Scene=> The weather outside is not pleasant in the slightest. The storm is still going on, visibility is reduced and the worst gusts of wind can be felt inside the apartment as a faint shudder. ",
"",
"[11:",
" [11:27] <Test> Stuff and things.",
}, "\r\n")
var testLogPosts = []*models.Post{
{
ID: "UNASSIGNED",
LogID: "UNASSIGNED",
Time: parseDate(nil, "2018-05-11 11:21:00"),
Kind: "action",
Nick: "Va`ynna_Atana",
Position: 1,
Text: "returns to the apartment at the end of another long day. She hangs up her jacket and a green scarf next to the door before going straight to the bathroom; she leaves the door open, though. She walked home with Uvena this time, but the two parted ways downstairs.",
},
{
ID: "UNASSIGNED",
LogID: "UNASSIGNED",
Time: parseDate(nil, "2018-05-11 11:21:00"),
Kind: "scene",
Nick: "=Scene=",
Position: 2,
Text: "The weather outside is not pleasant in the slightest. The storm is still going on, visibility is reduced and the worst gusts of wind can be felt inside the apartment as a faint shudder.",
},
{
ID: "UNASSIGNED",
LogID: "UNASSIGNED",
Time: parseDate(nil, "2018-05-11 11:27:00"),
Kind: "text",
Nick: "Test",
Position: 3,
Text: "Stuff and things.",
},
}