Skip to content

Commit

Permalink
Added test cases and delinted zerolog
Browse files Browse the repository at this point in the history
  • Loading branch information
scwx-mperry committed Oct 23, 2024
1 parent 5313a74 commit 016cc95
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
14 changes: 14 additions & 0 deletions internal/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ func AssertStringContains(t *testing.T, expectedContained string, actualContaini
}
}

func AssertAnyStringContains(t *testing.T, expectedContained string, actualContaining []string) {
t.Helper()
for _, actual := range actualContaining {
if strings.Contains(actual, expectedContained) {
return
}
}
t.Errorf(
"does not contain:\nexpected to contain: %s\nactual: %s\n",
strings.Trim(expectedContained, "\n"),
strings.Trim(strings.Join(actualContaining, "\n"), "\n"),
)
}

// AssertNotPanics is a semantic test assertion that a function does not
// panic.
func AssertNotPanics(t *testing.T, fn func()) {
Expand Down
13 changes: 10 additions & 3 deletions zerolog/zerolog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package zerolog_test

import (
"encoding/json"
"io/ioutil"
"io"
"testing"
"time"

Expand All @@ -26,7 +26,7 @@ func TestZerolog_New(t *testing.T) {

logger.Debug().Msg(testMessage)

data, err := ioutil.ReadAll(out)
data, err := io.ReadAll(out)
testutils.AssertNil(t, err)
testutils.AssertEqual(t, len(data), 0) // Nothing is logged for debug when at INFO.
})
Expand All @@ -39,7 +39,7 @@ func TestZerolog_New(t *testing.T) {

logger.Debug().Msg(testMessage)

data, err := ioutil.ReadAll(out)
data, err := io.ReadAll(out)
testutils.AssertNil(t, err)
testutils.AssertStringContains(t, testMessage, string(data))
})
Expand Down Expand Up @@ -101,8 +101,15 @@ func TestZerolog_Errors(t *testing.T) {
testutils.AssertEqual(t, testErrorValue, fields.Error)

// Stack trace.
var files, funcs []string
for _, f := range fields.Stack {
files = append(files, f.File)
funcs = append(funcs, f.Func)
}
testutils.AssertNotNil(t, fields.Stack)
testutils.AssertTrue(t, len(fields.Stack) > 0)
testutils.AssertAnyStringContains(t, "zerolog_test.go", files)
testutils.AssertAnyStringContains(t, "zerolog_test.TestZerolog_Errors", funcs)

// Metadata fields.
testutils.AssertEqual(t, testFieldValue, fields.Meta)
Expand Down
4 changes: 2 additions & 2 deletions zerolog/zerolog_unsafe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package zerolog

import (
"io/ioutil"
"io"
"reflect"
"testing"

Expand Down Expand Up @@ -44,7 +44,7 @@ func TestZerolog_ExpectedHookBehavior(t *testing.T) {
"zerolog",
nil,
log.CustomOption("Hook", hook),
log.CustomOption("Output", ioutil.Discard),
log.CustomOption("Output", io.Discard),
)
testutils.AssertNil(t, err)

Expand Down

0 comments on commit 016cc95

Please sign in to comment.