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.

130 lines
3.6 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. {"[12:34] <Stuff>", mirclike.ErrNotPost},
  68. {"[12:34] * Stuff", mirclike.ErrNotPost},
  69. {"[12:34] =Scene=", mirclike.ErrNotPost},
  70. {"[12:34] <=Scene=>", mirclike.ErrNotPost},
  71. }
  72. for i, row := range table {
  73. t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) {
  74. _, err := mirclike.ParsePost(row.Input, time.Now(), models.Post{})
  75. assert.Equal(t, row.Err, err, "Error should match")
  76. })
  77. }
  78. }
  79. func TestParseNextDay(t *testing.T) {
  80. table := []struct {
  81. Prev time.Time
  82. TS string
  83. Time time.Time
  84. }{
  85. {Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "12:01", Time: parseDate(t, "2019-01-12 12:01:00")},
  86. {Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "11:53:13", Time: parseDate(t, "2019-01-12 11:53:13")},
  87. {Prev: parseDate(t, "2019-04-08 23:51:59"), TS: "00:09", Time: parseDate(t, "2019-04-09 00:09:00")},
  88. {Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "11:29:59", Time: parseDate(t, "2019-01-13 11:29:59")},
  89. }
  90. for i, row := range table {
  91. t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) {
  92. input := fmt.Sprintf("[%s] * Stuff does things.", row.TS)
  93. post, err := mirclike.ParsePost(input, row.Prev, models.Post{Time: row.Prev})
  94. if err != nil {
  95. t.Fatal("Could not parse post:", err)
  96. }
  97. assert.Equal(t, row.Time, post.Time)
  98. })
  99. }
  100. }
  101. func parseDate(t *testing.T, date string) time.Time {
  102. result, err := time.Parse("2006-01-02 15:04:05", date)
  103. if err != nil {
  104. if t != nil {
  105. t.Fatal("Could not parse date", date, err)
  106. } else {
  107. panic("Could not parse date: " + err.Error())
  108. }
  109. }
  110. return result
  111. }
  112. func formatDate(date time.Time) string {
  113. return date.UTC().Format("2006-01-02 15:04:05")
  114. }