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.

126 lines
3.4 KiB

  1. package mirclike_test
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. "git.aiterp.net/rpdata/api/internal/importers/mirclike"
  8. "git.aiterp.net/rpdata/api/models"
  9. )
  10. func TestParsePost(t *testing.T) {
  11. table := []struct {
  12. Input string
  13. TS string
  14. Kind string
  15. Nick string
  16. Text string
  17. }{
  18. {
  19. "[12:34] * Stuff does things.",
  20. "12:34:00", "action", "Stuff", "does things.",
  21. },
  22. {
  23. "[12:34] <Stuff> Things said.",
  24. "12:34:00", "text", "Stuff", "Things said.",
  25. },
  26. {
  27. "[13:36:59] <Stuff> Things said.",
  28. "13:36:59", "text", "Stuff", "Things said.",
  29. },
  30. {
  31. "[23:59] <=Scene=> Scenery and such.",
  32. "23:59:00", "scene", "=Scene=", "Scenery and such.",
  33. },
  34. {
  35. "[01:10:11] * =Scene= Scenery and such from the forum or mIRC using my old script.",
  36. "01:10:11", "scene", "=Scene=", "Scenery and such from the forum or mIRC using my old script.",
  37. },
  38. }
  39. for i, row := range table {
  40. t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) {
  41. post, err := mirclike.ParsePost(row.Input, time.Now(), models.Post{})
  42. if err != nil {
  43. t.Fatal("Could not parse post:", err)
  44. }
  45. assert.Equal(t, row.TS, post.Time.Format("15:04:05"), "Timestamps should match.")
  46. assert.Equal(t, row.Kind, post.Kind, "Kinds should match.")
  47. assert.Equal(t, row.Nick, post.Nick, "Kinds should match.")
  48. assert.Equal(t, row.Text, post.Text, "Kinds should match.")
  49. })
  50. }
  51. }
  52. func TestParsePostErrors(t *testing.T) {
  53. table := []struct {
  54. Input string
  55. Err error
  56. }{
  57. {"[12:34] <Stuff> Things said.", nil},
  58. {"[12:34] >Stuff> Things said.", mirclike.ErrNotPost},
  59. {"12:34] <Stuff> Things said.", mirclike.ErrNotPost},
  60. {"* Stuff Things said.", mirclike.ErrNotPost},
  61. {"", mirclike.ErrNotPost},
  62. {"[12:34 <Stuff> Things said.", mirclike.ErrNotPost},
  63. {"[TE:XT] <Stuff> Things said.", mirclike.ErrNotPost},
  64. {"[10] <Stuff> Things said.", mirclike.ErrNotPost},
  65. {"[12:34:56:789] <Stuff> Things said.", nil},
  66. {"[12:34:56.789] <Stuff> Things said.", mirclike.ErrNotPost},
  67. }
  68. for i, row := range table {
  69. t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) {
  70. _, err := mirclike.ParsePost(row.Input, time.Now(), models.Post{})
  71. assert.Equal(t, row.Err, err, "Error should match")
  72. })
  73. }
  74. }
  75. func TestParseNextDay(t *testing.T) {
  76. table := []struct {
  77. Prev time.Time
  78. TS string
  79. Time time.Time
  80. }{
  81. {Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "12:01", Time: parseDate(t, "2019-01-12 12:01:00")},
  82. {Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "11:53:13", Time: parseDate(t, "2019-01-12 11:53:13")},
  83. {Prev: parseDate(t, "2019-04-08 23:51:59"), TS: "00:09", Time: parseDate(t, "2019-04-09 00:09:00")},
  84. {Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "11:29:59", Time: parseDate(t, "2019-01-13 11:29:59")},
  85. }
  86. for i, row := range table {
  87. t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) {
  88. input := fmt.Sprintf("[%s] * Stuff does things.", row.TS)
  89. post, err := mirclike.ParsePost(input, row.Prev, models.Post{Time: row.Prev})
  90. if err != nil {
  91. t.Fatal("Could not parse post:", err)
  92. }
  93. assert.Equal(t, row.Time, post.Time)
  94. })
  95. }
  96. }
  97. func parseDate(t *testing.T, date string) time.Time {
  98. result, err := time.Parse("2006-01-02 15:04:05", date)
  99. if err != nil {
  100. if t != nil {
  101. t.Fatal("Could not parse date", date, err)
  102. } else {
  103. panic("Could not parse date: " + err.Error())
  104. }
  105. }
  106. return result
  107. }
  108. func formatDate(date time.Time) string {
  109. return date.UTC().Format("2006-01-02 15:04:05")
  110. }