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.

whowas_test.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (c) 2018 Shivaram Lingamneni <slingamn@cs.stanford.edu>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "testing"
  6. )
  7. func makeTestWhowas(nick string) WhoWas {
  8. cfnick, err := CasefoldName(nick)
  9. if err != nil {
  10. panic(err)
  11. }
  12. return WhoWas{
  13. nickCasefolded: cfnick,
  14. nick: nick,
  15. username: "user",
  16. hostname: "oragono.io",
  17. realname: "Real Name",
  18. }
  19. }
  20. func TestWhoWas(t *testing.T) {
  21. var results []WhoWas
  22. var wwl WhoWasList
  23. wwl.Initialize(3)
  24. // test Find on empty list
  25. results = wwl.Find("nobody", 10)
  26. if len(results) != 0 {
  27. t.Fatalf("incorrect whowas results: %v", results)
  28. }
  29. wwl.Append(makeTestWhowas("dan-"))
  30. results = wwl.Find("nobody", 10)
  31. if len(results) != 0 {
  32. t.Fatalf("incorrect whowas results: %v", results)
  33. }
  34. results = wwl.Find("dan-", 10)
  35. if len(results) != 1 || results[0].nick != "dan-" {
  36. t.Fatalf("incorrect whowas results: %v", results)
  37. }
  38. wwl.Append(makeTestWhowas("slingamn"))
  39. results = wwl.Find("slingamN", 10)
  40. if len(results) != 1 || results[0].nick != "slingamn" {
  41. t.Fatalf("incorrect whowas results: %v", results)
  42. }
  43. wwl.Append(makeTestWhowas("Dan-"))
  44. results = wwl.Find("dan-", 10)
  45. // reverse chronological order
  46. if len(results) != 2 || results[0].nick != "Dan-" || results[1].nick != "dan-" {
  47. t.Fatalf("incorrect whowas results: %v", results)
  48. }
  49. // 0 means no limit
  50. results = wwl.Find("dan-", 0)
  51. if len(results) != 2 || results[0].nick != "Dan-" || results[1].nick != "dan-" {
  52. t.Fatalf("incorrect whowas results: %v", results)
  53. }
  54. // a limit of 1 should return the most recent entry only
  55. results = wwl.Find("dan-", 1)
  56. if len(results) != 1 || results[0].nick != "Dan-" {
  57. t.Fatalf("incorrect whowas results: %v", results)
  58. }
  59. wwl.Append(makeTestWhowas("moocow"))
  60. results = wwl.Find("moocow", 10)
  61. if len(results) != 1 || results[0].nick != "moocow" {
  62. t.Fatalf("incorrect whowas results: %v", results)
  63. }
  64. results = wwl.Find("dan-", 10)
  65. // should have overwritten the original entry, leaving the second
  66. if len(results) != 1 || results[0].nick != "Dan-" {
  67. t.Fatalf("incorrect whowas results: %v", results)
  68. }
  69. // overwrite the second entry
  70. wwl.Append(makeTestWhowas("enckse"))
  71. results = wwl.Find("enckse", 10)
  72. if len(results) != 1 || results[0].nick != "enckse" {
  73. t.Fatalf("incorrect whowas results: %v", results)
  74. }
  75. results = wwl.Find("slingamn", 10)
  76. if len(results) != 0 {
  77. t.Fatalf("incorrect whowas results: %v", results)
  78. }
  79. }
  80. func TestEmptyWhoWas(t *testing.T) {
  81. // stupid edge case; setting an empty whowas buffer should not panic
  82. var wwl WhoWasList
  83. wwl.Initialize(0)
  84. results := wwl.Find("slingamn", 10)
  85. if len(results) != 0 {
  86. t.Fatalf("incorrect whowas results: %v", results)
  87. }
  88. wwl.Append(makeTestWhowas("slingamn"))
  89. results = wwl.Find("slingamn", 10)
  90. if len(results) != 0 {
  91. t.Fatalf("incorrect whowas results: %v", results)
  92. }
  93. }