Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi committed Jul 25, 2024
1 parent d4a858a commit c1a1fcc
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 28 deletions.
35 changes: 20 additions & 15 deletions internal/util/testutil/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@ package testutil
import (
"bufio"
"encoding/hex"
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/FerretDB/wire/internal/util/lazyerrors"
)

// ParseDump decodes from hex dump to the byte array.
func ParseDump(s string) ([]byte, error) {
// parseDump decodes from hex dump to the byte array.
func parseDump(s string) ([]byte, error) {
var res []byte

scanner := bufio.NewScanner(strings.NewReader(strings.TrimSpace(s)))
Expand Down Expand Up @@ -69,7 +67,7 @@ func MustParseDumpFile(path ...string) []byte {
panic(err)
}

b, err = ParseDump(string(b))
b, err = parseDump(string(b))
if err != nil {
panic(err)
}
Expand All @@ -78,24 +76,31 @@ func MustParseDumpFile(path ...string) []byte {
}

// Unindent removes the common number of leading tabs from all lines in s.
func Unindent(tb testing.TB, s string) string {
tb.Helper()

require.NotEmpty(tb, s)
func Unindent(s string) string {
if s == "" {
panic("input must not be empty")
}

parts := strings.Split(s, "\n")
require.Positive(tb, len(parts))
if len(parts) == 0 {
panic("zero parts")
}

if parts[0] == "" {
parts = parts[1:]
}

indent := len(parts[0]) - len(strings.TrimLeft(parts[0], "\t"))
require.GreaterOrEqual(tb, indent, 0)
if indent < 0 {
panic("invalid indent")
}

for i, l := range parts {
if len(l) <= indent {
panic(fmt.Sprintf("invalid indent on line %q", l))
}

for i := range parts {
require.Greater(tb, len(parts[i]), indent, "line: %q", parts[i])
parts[i] = parts[i][indent:]
parts[i] = l[indent:]
}

return strings.Join(parts, "\n")
Expand Down
4 changes: 2 additions & 2 deletions internal/util/testutil/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ const goExpected = `` +
func TestParseDump(t *testing.T) {
t.Parallel()

actual, err := ParseDump(wiresharkDump)
actual, err := parseDump(wiresharkDump)
require.NoError(t, err)
assert.Equal(t, []byte(wiresharkExpected), actual)

actual, err = ParseDump(goDump)
actual, err = parseDump(goDump)
require.NoError(t, err)
goExpectedB, err := hex.DecodeString(goExpected)
require.NoError(t, err)
Expand Down
7 changes: 1 addition & 6 deletions internal/util/testutil/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,8 @@ func (lw *logWriter) Write(p []byte) (int, error) {

// Logger returns slog test logger.
func Logger(tb testing.TB) *slog.Logger {
return LevelLogger(tb, slog.LevelDebug)
}

// LevelLogger returns a slog test logger for the given level (which might be dynamic).
func LevelLogger(tb testing.TB, level slog.Leveler) *slog.Logger {
h := slog.NewTextHandler(&logWriter{tb: tb}, &slog.HandlerOptions{
Level: level,
Level: slog.LevelDebug,
})

return slog.New(h)
Expand Down
2 changes: 1 addition & 1 deletion wire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func testMessages(t *testing.T, testCases []testCase) {
require.NotNil(t, msgHeader)
require.NotNil(t, msgBody)
assert.NotEmpty(t, msgHeader.String())
assert.Equal(t, testutil.Unindent(t, tc.m), msgBody.String())
assert.Equal(t, testutil.Unindent(tc.m), msgBody.String())
assert.NotEmpty(t, msgBody.StringBlock())
assert.NotEmpty(t, msgBody.StringFlow())

Expand Down
2 changes: 1 addition & 1 deletion wirebson/bson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ func TestNormal(t *testing.T) {
assert.NotContains(t, ls, "panicked")
assert.NotContains(t, ls, "called too many times")

assert.Equal(t, testutil.Unindent(t, tc.m), LogMessage(doc))
assert.Equal(t, testutil.Unindent(tc.m), LogMessage(doc))
assert.NotEmpty(t, LogMessageBlock(doc))
assert.NotEmpty(t, LogMessageFlow(doc))

Expand Down
4 changes: 2 additions & 2 deletions wirebson/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ func TestLogging(t *testing.T) {
jbuf.Reset()

m := LogMessage(tc.doc)
assert.Equal(t, testutil.Unindent(t, tc.m), m, "actual LogMessage result:\n%s", m)
assert.Equal(t, testutil.Unindent(tc.m), m, "actual LogMessage result:\n%s", m)

b := LogMessageBlock(tc.doc)
assert.Equal(t, testutil.Unindent(t, tc.b), b, "actual LogMessageBlock result:\n%s", b)
assert.Equal(t, testutil.Unindent(tc.b), b, "actual LogMessageBlock result:\n%s", b)
})
}
}
Expand Down
9 changes: 8 additions & 1 deletion wireclient/wireclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@

package wireclient

import "testing"
import (
"context"
"testing"

"github.com/FerretDB/wire/internal/util/testutil"
)

func TestConn(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration tests for -short")
}

// TODO https://github.com/FerretDB/wire/issues/1

Connect(context.TODO(), "mongodb://127.0.0.1:27017/", testutil.Logger(t))
}

0 comments on commit c1a1fcc

Please sign in to comment.