support nil receivers
This commit is contained in:
@@ -18,22 +18,28 @@ type RecordQueue struct {
|
|||||||
l *list.List
|
l *list.List
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Push inserts the Record at the back of the queue. If pushing a nil Record then it's a no-op.
|
||||||
func (rq *RecordQueue) Push(r Record) {
|
func (rq *RecordQueue) Push(r Record) {
|
||||||
if rq.l == nil {
|
|
||||||
rq.l = list.New()
|
|
||||||
}
|
|
||||||
|
|
||||||
if r == nil {
|
if r == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if rq == nil {
|
||||||
|
// This would cause a panic anyway so, we panic with a more meaningful message
|
||||||
|
panic("Push to nil RecordQueue")
|
||||||
|
}
|
||||||
|
|
||||||
|
if rq.l == nil {
|
||||||
|
rq.l = list.New()
|
||||||
|
}
|
||||||
|
|
||||||
rq.l.PushBack(r)
|
rq.l.PushBack(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pop removes and returns the first element of the list as the first return value. If the list is
|
// Pop removes and returns the first element of the list as the first return value. If the list is
|
||||||
// empty returns falso on the 2nd return value, true otherwise.
|
// empty returns falso on the 2nd return value, true otherwise.
|
||||||
func (rq *RecordQueue) Pop() (Record, bool) {
|
func (rq *RecordQueue) Pop() (Record, bool) {
|
||||||
if rq.l == nil {
|
if rq == nil || rq.l == nil {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,7 +54,7 @@ func (rq *RecordQueue) Pop() (Record, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rq *RecordQueue) Len() int {
|
func (rq *RecordQueue) Len() int {
|
||||||
if rq.l == nil {
|
if rq == nil || rq.l == nil {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRecordQueue_Push(t *testing.T) {
|
func TestRecordQueue(t *testing.T) {
|
||||||
var recCount int
|
var recCount int
|
||||||
newRecord := func() Record {
|
newRecord := func() Record {
|
||||||
recCount++
|
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 {
|
type testRecord struct {
|
||||||
Record
|
Record
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user