Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: replace t.Errorf and t.Fatalf with assert and require #17720

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 41 additions & 54 deletions go/cache/lru_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type CacheValue struct {
Expand All @@ -44,14 +45,12 @@ func TestSetInsertsValue(t *testing.T) {
cache.Set(key, data)

v, ok := cache.Get(key)
if !ok || v != data {
t.Errorf("Cache has incorrect value: %v != %v", data, v)
}
assert.True(t, ok)
assert.EqualValues(t, v, data)

values := cache.Items()
if len(values) != 1 || values[0].Key != key {
t.Errorf("Cache.Values() returned incorrect values: %v", values)
}
require.NotEmpty(t, values)
assert.Equal(t, key, values[0].Key)
}

func TestGetValueWithMultipleTypes(t *testing.T) {
Expand All @@ -61,14 +60,12 @@ func TestGetValueWithMultipleTypes(t *testing.T) {
cache.Set(key, data)

v, ok := cache.Get("key")
if !ok || v != data {
t.Errorf("Cache has incorrect value for \"key\": %v != %v", data, v)
}
assert.True(t, ok)
assert.Equal(t, data, v)

v, ok = cache.Get(string([]byte{'k', 'e', 'y'}))
if !ok || v != data {
t.Errorf("Cache has incorrect value for []byte {'k','e','y'}: %v != %v", data, v)
}
assert.True(t, ok)
assert.Equal(t, data, v)
}

func TestSetWithOldKeyUpdatesValue(t *testing.T) {
Expand All @@ -80,17 +77,15 @@ func TestSetWithOldKeyUpdatesValue(t *testing.T) {
cache.Set(key, someValue)

v, ok := cache.Get(key)
if !ok || v != someValue {
t.Errorf("Cache has incorrect value: %v != %v", someValue, v)
}
assert.True(t, ok)
assert.Equal(t, someValue, v)
}

func TestGetNonExistent(t *testing.T) {
cache := NewLRUCache[*CacheValue](100)

if _, ok := cache.Get("notthere"); ok {
t.Error("Cache returned a notthere value after no inserts.")
}
val, ok := cache.Get("notthere")
assert.False(t, ok, "Cache returned a notthere value after no inserts val=%v", val)
}

func TestDelete(t *testing.T) {
Expand All @@ -102,13 +97,11 @@ func TestDelete(t *testing.T) {
cache.Set(key, value)
cache.Delete(key)

if sz := cache.UsedCapacity(); sz != 0 {
t.Errorf("cache.UsedCapacity() = %v, expected 0", sz)
}
sz := cache.UsedCapacity()
assert.Zero(t, sz)

if _, ok := cache.Get(key); ok {
t.Error("Cache returned a value after deletion.")
}
val, ok := cache.Get("notthere")
assert.False(t, ok, "Cache returned a value after deletion: val=%v", val)
}

func TestCapacityIsObeyed(t *testing.T) {
Expand All @@ -121,31 +114,29 @@ func TestCapacityIsObeyed(t *testing.T) {
cache.Set("key1", value)
cache.Set("key2", value)
cache.Set("key3", value)
if sz := cache.UsedCapacity(); sz != size {
t.Errorf("cache.UsedCapacity() = %v, expected %v", sz, size)
}
sz := cache.UsedCapacity()
assert.EqualValues(t, size, sz)
// Insert one more; something should be evicted to make room.
cache.Set("key4", value)
sz, evictions := cache.UsedCapacity(), cache.Evictions()
assert.Equal(t, size, sz)
assert.EqualValues(t, 1, evictions)

// Check various other stats
if l := cache.Len(); int64(l) != size {
t.Errorf("cache.Len() returned bad length: %v", l)
}
if s := cache.UsedCapacity(); s != size {
t.Errorf("cache.UsedCapacity() returned bad size: %v", s)
}
if c := cache.MaxCapacity(); c != size {
t.Errorf("cache.UsedCapacity() returned bad length: %v", c)
}
if c := cache.Hits(); c != 0 {
t.Errorf("cache.Hits() returned hits when there should be none: %v", c)
}
if c := cache.Misses(); c != 0 {
t.Errorf("cache.Misses() returned misses when there should be none: %v", c)
}
l := cache.Len()
assert.EqualValues(t, size, l)

s := cache.UsedCapacity()
assert.EqualValues(t, size, s)

c := cache.MaxCapacity()
assert.EqualValues(t, size, c)

hits := cache.Hits()
assert.Zero(t, hits)

misses := cache.Misses()
assert.Zero(t, misses)
}

func TestLRUIsEvicted(t *testing.T) {
Expand All @@ -167,19 +158,15 @@ func TestLRUIsEvicted(t *testing.T) {
// lru: [key0, key1, key2]

// The least recently used one should have been evicted.
if _, ok := cache.Get("key3"); ok {
t.Error("Least recently used element was not evicted.")
}
v, ok := cache.Get("key3")
assert.False(t, ok, "Least recently used element was not evicted: %v", v)

if e, want := cache.Evictions(), int64(1); e != want {
t.Errorf("evictions: %d, want: %d", e, want)
}
e := cache.Evictions()
assert.EqualValues(t, 1, e)

if h, want := cache.Hits(), int64(3); h != want {
t.Errorf("hits: %d, want: %d", h, want)
}
h := cache.Hits()
assert.EqualValues(t, 3, h)

if m, want := cache.Misses(), int64(1); m != want {
t.Errorf("misses: %d, want: %d", m, want)
}
m := cache.Misses()
assert.EqualValues(t, 1, m)
}
41 changes: 19 additions & 22 deletions go/event/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ func TestStaticListener(t *testing.T) {

triggered := false
AddListener(func(testEvent1) { triggered = true })
AddListener(func(testEvent2) { t.Errorf("wrong listener type triggered") })
AddListener(func(testEvent2) {
assert.Fail(t, "wrong listener type triggered")
})
Dispatch(testEvent1{})
assert.True(t, triggered, "static listener failed to trigger")
}
Expand All @@ -66,7 +68,9 @@ func TestPointerListener(t *testing.T) {

testEvent := new(testEvent2)
AddListener(func(ev *testEvent2) { ev.triggered = true })
AddListener(func(testEvent2) { t.Errorf("non-pointer listener triggered on pointer type") })
AddListener(func(testEvent2) {
assert.Fail(t, "non-pointer listener triggered on pointer type")
})
Dispatch(testEvent)
assert.True(t, testEvent.triggered, "pointer listener failed to trigger")
}
Expand All @@ -76,7 +80,9 @@ func TestInterfaceListener(t *testing.T) {

triggered := false
AddListener(func(testInterface1) { triggered = true })
AddListener(func(testInterface2) { t.Errorf("interface listener triggered on non-matching type") })
AddListener(func(testInterface2) {
assert.Fail(t, "interface listener triggered on non-matching type")
})
Dispatch(testEvent1{})
assert.True(t, triggered, "interface listener failed to trigger")
}
Expand All @@ -98,19 +104,17 @@ func TestMultipleListeners(t *testing.T) {
AddListener(func(testEvent1) { triggered2 = true })
Dispatch(testEvent1{})

if !triggered1 || !triggered2 {
t.Errorf("not all matching listeners triggered")
}
assert.True(t, triggered1, "listener 1 failed to trigger")
assert.True(t, triggered2, "listener 2 failed to trigger")
}

func TestBadListenerWrongInputs(t *testing.T) {
clearListeners()

defer func() {
err := recover()

assert.NotNil(t, err, "bad listener func (wrong # of inputs) failed to trigger panic")
if err == nil {
t.Errorf("bad listener func (wrong # of inputs) failed to trigger panic")
return
}

Expand All @@ -120,9 +124,7 @@ func TestBadListenerWrongInputs(t *testing.T) {
}

want := "bad listener func: listener must take exactly one input argument"
if got := blErr.Error(); got != want {
t.Errorf(`BadListenerError.Error() = "%s", want "%s"`, got, want)
}
assert.Equal(t, want, blErr.Error())
}()

AddListener(func() {})
Expand All @@ -134,19 +136,15 @@ func TestBadListenerWrongType(t *testing.T) {

defer func() {
err := recover()
if err == nil {
t.Errorf("bad listener type (not a func) failed to trigger panic")
}
assert.NotNil(t, err, "bad listener type (not a func) failed to trigger panic")
akagami-harsh marked this conversation as resolved.
Show resolved Hide resolved

blErr, ok := err.(BadListenerError)
if !ok {
panic(err) // this is not the error we were looking for; re-panic
}

want := "bad listener func: listener must be a function"
if got := blErr.Error(); got != want {
t.Errorf(`BadListenerError.Error() = "%s", want "%s"`, got, want)
}
assert.Equal(t, want, blErr.Error())
}()

AddListener("this is not a function")
Expand All @@ -163,7 +161,7 @@ func TestAsynchronousDispatch(t *testing.T) {
select {
case <-triggered:
case <-time.After(time.Second):
t.Errorf("asynchronous dispatch failed to trigger listener")
assert.Fail(t, "asynchronous dispatch failed to trigger listener")
}
}

Expand Down Expand Up @@ -204,7 +202,7 @@ func TestDispatchValueToPointerInterfaceListener(t *testing.T) {
clearListeners()

AddListener(func(testInterface2) {
t.Errorf("interface listener triggered for value dispatch")
assert.Fail(t, "interface listener triggered for value dispatch")
})
Dispatch(testEvent2{})
}
Expand All @@ -230,7 +228,6 @@ func TestDispatchUpdate(t *testing.T) {
assert.True(t, triggered, "listener failed to trigger on DispatchUpdate()")

want := "hello"
if got := ev.update.(string); got != want {
t.Errorf("ev.update = %#v, want %#v", got, want)
}
got := ev.update.(string)
assert.Equal(t, want, got)
}
26 changes: 10 additions & 16 deletions go/exit/exit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,20 @@ package exit

import (
"testing"

"github.com/stretchr/testify/assert"
)

type repanicType int

func TestReturn(t *testing.T) {
defer func() {
err := recover()
if err == nil {
t.Errorf("Return() did not panic with exit code")
}
assert.NotNil(t, err, "Return() did not panic with exit code")
akagami-harsh marked this conversation as resolved.
Show resolved Hide resolved

switch code := err.(type) {
case exitCode:
if code != 152 {
t.Errorf("got %v, want %v", code, 152)
}
assert.Equal(t, exitCode(152), code)
default:
panic(err)
}
Expand All @@ -54,21 +52,20 @@ func TestRecover(t *testing.T) {
Return(8235)
}()

if code != 8235 {
t.Errorf("got %v, want %v", code, 8235)
}
assert.EqualValues(t, 8235, code)
}

func TestRecoverRepanic(t *testing.T) {
defer func() {
err := recover()

assert.NotNil(t, err, "Recover() didn't re-panic an error other than exitCode")
if err == nil {
t.Errorf("Recover() didn't re-panic an error other than exitCode")
return
}

if _, ok := err.(repanicType); !ok {
_, ok := err.(repanicType)
assert.True(t, ok, "unexpected error type recovered")
if !ok {
panic(err) // something unexpected went wrong
}
}()
Expand All @@ -83,10 +80,7 @@ func TestRecoverAll(t *testing.T) {

defer func() {
err := recover()

if err != nil {
t.Errorf("RecoverAll() didn't absorb all panics")
}
assert.Nil(t, err, "RecoverAll() didn't absorb all panics")
}()

defer RecoverAll()
Expand Down
Loading
Loading