Skip to content

Commit

Permalink
test(slog): Use testify's assert, require directly (#1336)
Browse files Browse the repository at this point in the history
We don't instantiate `require.New` or `assert.New` in our tests,
preferring to use the package-level functions of require and assert
directly.

Change zapslog tests to match that convention.
  • Loading branch information
abhinav authored Aug 23, 2023
1 parent 75a5534 commit fd37f1f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions exp/zapslog/slog_go121_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ import (
"log/slog"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
)

func TestAddSource(t *testing.T) {
r := require.New(t)
fac, logs := observer.New(zapcore.DebugLevel)
sl := slog.New(NewHandler(fac, &HandlerOptions{
AddSource: true,
}))
sl.Info("msg")

r.Len(logs.AllUntimed(), 1, "Expected exactly one entry to be logged")
require.Len(t, logs.AllUntimed(), 1, "Expected exactly one entry to be logged")
entry := logs.AllUntimed()[0]
r.Equal("msg", entry.Message, "Unexpected message")
r.Regexp(
assert.Equal(t, "msg", entry.Message, "Unexpected message")
assert.Regexp(t,
`/slog_go121_test.go:\d+$`,
entry.Caller.String(),
"Unexpected caller annotation.",
Expand Down
8 changes: 4 additions & 4 deletions exp/zapslog/slog_pre_go121_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@ package zapslog
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
"golang.org/x/exp/slog"
)

func TestAddSource(t *testing.T) {
r := require.New(t)
fac, logs := observer.New(zapcore.DebugLevel)
sl := slog.New(NewHandler(fac, &HandlerOptions{
AddSource: true,
}))
sl.Info("msg")

r.Len(logs.AllUntimed(), 1, "Expected exactly one entry to be logged")
require.Len(t, logs.AllUntimed(), 1, "Expected exactly one entry to be logged")
entry := logs.AllUntimed()[0]
r.Equal("msg", entry.Message, "Unexpected message")
r.Regexp(
assert.Equal(t, "msg", entry.Message, "Unexpected message")
assert.Regexp(t,
`/slog_pre_go121_test.go:\d+$`,
entry.Caller.String(),
"Unexpected caller annotation.",
Expand Down

0 comments on commit fd37f1f

Please sign in to comment.