support nil receivers

This commit is contained in:
2025-11-11 10:02:35 +00:00
parent 265d115647
commit 8e4093b647
2 changed files with 44 additions and 7 deletions

View File

@@ -4,7 +4,7 @@ import (
"testing"
)
func TestRecordQueue_Push(t *testing.T) {
func TestRecordQueue(t *testing.T) {
var recCount int
newRecord := func() Record {
recCount++
@@ -52,6 +52,37 @@ func TestRecordQueue_Push(t *testing.T) {
}
}
func TestRecordQueueNilReceiver(t *testing.T) {
var rq *RecordQueue
if rq.Len() > 0 {
t.Fatalf("nil receiver should have zero lenght")
}
_, ok := rq.Pop()
if ok {
t.Fatalf("Pop() on a nil receiver should return (_,false)")
}
rq.Push(nil)
if rq.Len() != 0 {
t.Fatalf("Push(nil) on a nil receiver should be a no-op")
}
defer func() {
r := recover()
if r == nil {
t.Fatalf("expected a panic but got nothing")
}
expMsg := "Push to nil RecordQueue"
if msg, ok := r.(string); !ok || msg != expMsg {
t.Fatalf(`want panic message %q but got "%v"`, expMsg, r)
}
}()
rq.Push(testRecord{})
}
type testRecord struct {
Record