3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-23 12:29:25 +01:00
ergo/irc/whowas_test.go

104 lines
2.8 KiB
Go
Raw Normal View History

2018-05-04 06:24:54 +02:00
// Copyright (c) 2018 Shivaram Lingamneni <slingamn@cs.stanford.edu>
// released under the MIT license
package irc
import (
"testing"
)
func makeTestWhowas(nick string) WhoWas {
cfnick, err := CasefoldName(nick)
if err != nil {
panic(err)
}
return WhoWas{
nickCasefolded: cfnick,
nick: nick,
username: "user",
hostname: "oragono.io",
realname: "Real Name",
2018-05-04 06:24:54 +02:00
}
}
func TestWhoWas(t *testing.T) {
var results []WhoWas
2019-03-12 00:24:45 +01:00
var wwl WhoWasList
wwl.Initialize(3)
2018-05-04 06:24:54 +02:00
// test Find on empty list
results = wwl.Find("nobody", 10)
if len(results) != 0 {
t.Fatalf("incorrect whowas results: %v", results)
}
wwl.Append(makeTestWhowas("dan-"))
results = wwl.Find("nobody", 10)
if len(results) != 0 {
t.Fatalf("incorrect whowas results: %v", results)
}
results = wwl.Find("dan-", 10)
if len(results) != 1 || results[0].nick != "dan-" {
2018-05-04 06:24:54 +02:00
t.Fatalf("incorrect whowas results: %v", results)
}
wwl.Append(makeTestWhowas("slingamn"))
results = wwl.Find("slingamN", 10)
if len(results) != 1 || results[0].nick != "slingamn" {
2018-05-04 06:24:54 +02:00
t.Fatalf("incorrect whowas results: %v", results)
}
wwl.Append(makeTestWhowas("Dan-"))
results = wwl.Find("dan-", 10)
// reverse chronological order
if len(results) != 2 || results[0].nick != "Dan-" || results[1].nick != "dan-" {
2018-05-04 06:24:54 +02:00
t.Fatalf("incorrect whowas results: %v", results)
}
// 0 means no limit
results = wwl.Find("dan-", 0)
if len(results) != 2 || results[0].nick != "Dan-" || results[1].nick != "dan-" {
2018-05-04 06:24:54 +02:00
t.Fatalf("incorrect whowas results: %v", results)
}
// a limit of 1 should return the most recent entry only
results = wwl.Find("dan-", 1)
if len(results) != 1 || results[0].nick != "Dan-" {
2018-05-04 06:24:54 +02:00
t.Fatalf("incorrect whowas results: %v", results)
}
wwl.Append(makeTestWhowas("moocow"))
results = wwl.Find("moocow", 10)
if len(results) != 1 || results[0].nick != "moocow" {
2018-05-04 06:24:54 +02:00
t.Fatalf("incorrect whowas results: %v", results)
}
results = wwl.Find("dan-", 10)
// should have overwritten the original entry, leaving the second
if len(results) != 1 || results[0].nick != "Dan-" {
2018-05-04 06:24:54 +02:00
t.Fatalf("incorrect whowas results: %v", results)
}
// overwrite the second entry
wwl.Append(makeTestWhowas("enckse"))
results = wwl.Find("enckse", 10)
if len(results) != 1 || results[0].nick != "enckse" {
2018-05-04 06:24:54 +02:00
t.Fatalf("incorrect whowas results: %v", results)
}
results = wwl.Find("slingamn", 10)
if len(results) != 0 {
t.Fatalf("incorrect whowas results: %v", results)
}
}
func TestEmptyWhoWas(t *testing.T) {
// stupid edge case; setting an empty whowas buffer should not panic
2019-03-12 00:24:45 +01:00
var wwl WhoWasList
wwl.Initialize(0)
2018-05-04 06:24:54 +02:00
results := wwl.Find("slingamn", 10)
if len(results) != 0 {
t.Fatalf("incorrect whowas results: %v", results)
}
wwl.Append(makeTestWhowas("slingamn"))
results = wwl.Find("slingamn", 10)
if len(results) != 0 {
t.Fatalf("incorrect whowas results: %v", results)
}
}