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.

182 lines
5.7 KiB

  1. package parsers_test
  2. import (
  3. "fmt"
  4. "git.aiterp.net/rpdata/api/models"
  5. "git.aiterp.net/rpdata/api/services/parsers"
  6. "github.com/stretchr/testify/assert"
  7. "strings"
  8. "testing"
  9. "time"
  10. )
  11. func TestMircPost(t *testing.T) {
  12. table := []struct {
  13. Input string
  14. TS string
  15. Kind string
  16. Nick string
  17. Text string
  18. }{
  19. {
  20. "[12:34] * Stuff does things.",
  21. "12:34:00", "action", "Stuff", "does things.",
  22. },
  23. {
  24. "[12:34] <Stuff> Things said.",
  25. "12:34:00", "text", "Stuff", "Things said.",
  26. },
  27. {
  28. "[13:36:59] <Stuff> Things said.",
  29. "13:36:59", "text", "Stuff", "Things said.",
  30. },
  31. {
  32. "[23:59] <=Scene=> Scenery and such.",
  33. "23:59:00", "scene", "=Scene=", "Scenery and such.",
  34. },
  35. {
  36. "[01:10:11] * =Scene= Scenery and such from the forum or mIRC using my old script.",
  37. "01:10:11", "scene", "=Scene=", "Scenery and such from the forum or mIRC using my old script.",
  38. },
  39. }
  40. for i, row := range table {
  41. t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) {
  42. post, err := parsers.MircPost(row.Input, time.Now(), models.Post{})
  43. if err != nil {
  44. t.Fatal("Could not parse post:", err)
  45. }
  46. assert.Equal(t, row.TS, post.Time.Format("15:04:05"), "Timestamps should match.")
  47. assert.Equal(t, row.Kind, post.Kind, "Kinds should match.")
  48. assert.Equal(t, row.Nick, post.Nick, "Kinds should match.")
  49. assert.Equal(t, row.Text, post.Text, "Kinds should match.")
  50. })
  51. }
  52. }
  53. func TestMircPostErrors(t *testing.T) {
  54. table := []struct {
  55. Input string
  56. Err error
  57. }{
  58. {"[12:34] <Stuff> Things said.", nil},
  59. {"[12:34] >Stuff> Things said.", parsers.ErrNotPost},
  60. {"12:34] <Stuff> Things said.", parsers.ErrNotPost},
  61. {"* Stuff Things said.", parsers.ErrNotPost},
  62. {"", parsers.ErrNotPost},
  63. {"[12:34 <Stuff> Things said.", parsers.ErrNotPost},
  64. {"[TE:XT] <Stuff> Things said.", parsers.ErrNotPost},
  65. {"[10] <Stuff> Things said.", parsers.ErrNotPost},
  66. {"[12:34:56:789] <Stuff> Things said.", nil},
  67. {"[12:34:56.789] <Stuff> Things said.", parsers.ErrNotPost},
  68. {"[12:34] <Stuff>", parsers.ErrNotPost},
  69. {"[12:34] * Stuff", parsers.ErrNotPost},
  70. {"[12:34] =Scene=", parsers.ErrNotPost},
  71. {"[12:34] <=Scene=>", parsers.ErrNotPost},
  72. }
  73. for i, row := range table {
  74. t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) {
  75. _, err := parsers.MircPost(row.Input, time.Now(), models.Post{})
  76. assert.Equal(t, row.Err, err, "Error should match")
  77. })
  78. }
  79. }
  80. func TestMircPostNextDay(t *testing.T) {
  81. table := []struct {
  82. Prev time.Time
  83. TS string
  84. Time time.Time
  85. }{
  86. {Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "12:01", Time: parseDate(t, "2019-01-12 12:01:00")},
  87. {Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "11:53:13", Time: parseDate(t, "2019-01-12 11:53:13")},
  88. {Prev: parseDate(t, "2019-04-08 23:51:59"), TS: "00:09", Time: parseDate(t, "2019-04-09 00:09:00")},
  89. {Prev: parseDate(t, "2019-01-12 12:00:00"), TS: "11:29:59", Time: parseDate(t, "2019-01-13 11:29:59")},
  90. }
  91. for i, row := range table {
  92. t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) {
  93. input := fmt.Sprintf("[%s] * Stuff does things.", row.TS)
  94. post, err := parsers.MircPost(input, row.Prev, models.Post{Time: row.Prev})
  95. if err != nil {
  96. t.Fatal("Could not parse post:", err)
  97. }
  98. assert.Equal(t, row.Time, post.Time)
  99. })
  100. }
  101. }
  102. func TestMircLog(t *testing.T) {
  103. parsed, err := parsers.MircLog(testLog, parseDate(t, "2018-05-11 00:00:00"), false)
  104. if err != nil {
  105. t.Fatal("ParseLog", err)
  106. }
  107. assert.Equal(t, testLogPosts, parsed.Posts)
  108. assert.Equal(t, parsed.Posts[0].Time, parsed.Log.Date, "Log's date should be the first post's.")
  109. }
  110. func TestMircLogErrors(t *testing.T) {
  111. _, err1 := parsers.MircLog("\n\n\n\n\n \n\t\r\n", time.Time{}, false)
  112. _, err2 := parsers.MircLog("\n\n\n\n\n[14:57]* Stuff \n\t\r\n", time.Time{}, true)
  113. assert.Equal(t, parsers.ErrEmptyLog, err1)
  114. assert.Equal(t, parsers.ErrNotPost, err2)
  115. }
  116. func parseDate(t *testing.T, date string) time.Time {
  117. result, err := time.Parse("2006-01-02 15:04:05", date)
  118. if err != nil {
  119. if t != nil {
  120. t.Fatal("Could not parse date", date, err)
  121. } else {
  122. panic("Could not parse date: " + err.Error())
  123. }
  124. }
  125. return result
  126. }
  127. var testLog = strings.Join([]string{
  128. "[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.",
  129. "",
  130. "[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. ",
  131. "",
  132. "[11:",
  133. " [11:27] <Test> Stuff and things.",
  134. }, "\r\n")
  135. var testLogPosts = []*models.Post{
  136. {
  137. ID: "UNASSIGNED",
  138. LogID: "UNASSIGNED",
  139. Time: parseDate(nil, "2018-05-11 11:21:00"),
  140. Kind: "action",
  141. Nick: "Va`ynna_Atana",
  142. Position: 1,
  143. 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.",
  144. },
  145. {
  146. ID: "UNASSIGNED",
  147. LogID: "UNASSIGNED",
  148. Time: parseDate(nil, "2018-05-11 11:21:00"),
  149. Kind: "scene",
  150. Nick: "=Scene=",
  151. Position: 2,
  152. 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.",
  153. },
  154. {
  155. ID: "UNASSIGNED",
  156. LogID: "UNASSIGNED",
  157. Time: parseDate(nil, "2018-05-11 11:27:00"),
  158. Kind: "text",
  159. Nick: "Test",
  160. Position: 3,
  161. Text: "Stuff and things.",
  162. },
  163. }