Skip to content

Port tests from testify and frankban/quicktest to go-quicktest/qt - #85

Merged
anacrolix merged 1 commit into
masterfrom
claude/anacrolix-quicktest-porting-iemauj
Sep 8, 2026
Merged

anacrolix merged 1 commit into
masterfrom
claude/anacrolix-quicktest-porting-iemauj

Conversation

@anacrolix

Copy link
Copy Markdown
Owner

The test suite currently uses four assertion styles across 18 files:

  1. testify package functions — require.NoError(t, err)
  2. testify's bound form — require := require.New(t), then require.NoError(err)
  3. frankban's *qt.C wrapper — c := qt.New(t), then c.Assert(err, qt.IsNil)
  4. frankban's package-level form — qt.Check(t, got, qt.IsNil)

This collapses all four onto github.com/go-quicktest/qt (v1.102.0), the successor to frankban/quicktest, whose generic checker functions are type-checked at compile time (rationale). Both stretchr/testify and frankban/quicktest drop out of go.mod.

Translation applied

before after
require.NoError(t, err) qt.Assert(t, qt.IsNil(err))
assert.NoError(t, err) qt.Check(t, qt.IsNil(err))
assert.Equal/EqualValues(t, want, got) qt.Check(t, qt.Equals(got, want))
assert.Len(t, v, n) qt.Check(t, qt.HasLen(v, n))
assert.True/False/Nil(t, x) qt.Check(t, qt.IsTrue/IsFalse/IsNil(x))
require.EqualError(t, err, msg) qt.Assert(t, qt.ErrorMatches(err, regexp.QuoteMeta(msg)))
c.Assert(err, qt.IsNil) qt.Assert(t, qt.IsNil(err))
qt.Check(t, got, qt.IsNil) qt.Check(t, qt.IsNil(got))
trailing msgAndArgs qt.Commentf(...)

Note the argument order flips: testify takes (expected, actual), qt takes (got, want). assert.* → qt.Check (non-fatal), require.* → qt.Assert (fatal), preserving control flow.

Three places needed more than a rename

  1. require := require.New(t) (server_test.go, bep44/store_test.go) has no qt equivalent — qt has no bound form. The binding is dropped and t is passed explicitly at each call.

  2. TestMarshalCompactIPv4NodeAddrs assigned an assertion function to a variable:

    runFunc := assert.NotPanics
    if tc.panics { runFunc = assert.Panics }
    runFunc(t, func() { ... })

    qt checkers are values of a different shape, so this can't carry over directly. The closure is lifted into a local and the branch picks the checker:

    marshal := func() { ... }
    if tc.panics {
        qt.Check(t, qt.PanicMatches(marshal, ".*"))
    } else {
        qt.Check(t, qt.Not(qt.PanicMatches(marshal, ".*")))
    }

    qt has no NotPanics; qt.Not(qt.PanicMatches(f, ".*")) is exact, since PanicMatches fails when f doesn't panic and .* matches any panic value.

  3. qt.Contains is split by container type in the new API. The AddrMaybeIdsByDistance test checks a []addrMaybeId, so it becomes qt.SliceContains.

Where Equals isn't enough

qt.Equals is ==, which panics on uncomparable types and compares addresses for pointers. These use qt.DeepEquals instead:

  • []krpc.NodeInfo (nodes_file_test.go), f[:] (msg_test.go), []byte output (compact_test.go) — slices.
  • krpc.Msg — its IP NodeAddr field holds a net.IP (a slice), so Msg is not comparable. qt.Equals(m, Msg{}) compiles but would panic at runtime.
  • *krpc.Msg (dht_test.go) — == would compare pointer addresses, not contents, and the test would have silently started failing.

Separately, int160.T.Bytes() returns a []byte compared against a string constant, which qt.Equals can't unify; that one converts explicitly with string(...).

Verification

go test -gcflags=-e -c -o /dev/null <each package>   # all test binaries build clean
go test -count=1 ./...
gofmt -l .                                           # clean

All seven test packages pass — dht/v2, bep44, containers, krpc, peer-store, traversal, types — matching the green baseline I took on master before starting.

🤖 Generated with Claude Code

https://claude.ai/code/session_013BA3b7BbaJkPeeTYjhvi5g


Generated by Claude Code

…st/qt

The tests used four assertion styles: testify's package functions,
testify's bound require.New(t) form, frankban's *qt.C wrapper, and
frankban's package-level qt.Assert(t, got, checker) form. This collapses
them onto go-quicktest/qt, the successor to frankban/quicktest, whose
generic checker functions are type-checked at compile time.

  require.NoError(t, err)      ->  qt.Assert(t, qt.IsNil(err))
  assert.Equal(t, want, got)   ->  qt.Check(t, qt.Equals(got, want))
  assert.Len(t, v, n)          ->  qt.Check(t, qt.HasLen(v, n))
  c.Assert(err, qt.IsNil)      ->  qt.Assert(t, qt.IsNil(err))
  trailing msgAndArgs          ->  qt.Commentf(...)

Note the argument order flips: testify takes (expected, actual), qt takes
(got, want). assert.* becomes Check and require.* becomes Assert.

Three call sites needed more than a rename:

  - require := require.New(t) has no equivalent; the binding is dropped and
    t passed explicitly.
  - TestMarshalCompactIPv4NodeAddrs assigned assert.NotPanics or
    assert.Panics to a variable. qt checkers are values of a different
    shape, so the closure is lifted into a local and the branch selects the
    checker instead. qt has no NotPanics, so the negative case is
    qt.Not(qt.PanicMatches(f, ".*")).
  - qt.Contains is split by container type; the AddrMaybeIdsByDistance test
    checks a slice, so it becomes qt.SliceContains.

qt.Equals is ==, so comparisons of slices, of krpc.Msg (its NodeAddr field
holds a net.IP, making Msg uncomparable), and of *krpc.Msg (where == would
compare addresses) use DeepEquals. int160.T.Bytes() returns a []byte
compared against a string constant, so that one converts explicitly.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013BA3b7BbaJkPeeTYjhvi5g
@anacrolix
anacrolix merged commit a669bdd into master Sep 8, 2026
2 checks passed
@anacrolix
anacrolix deleted the claude/anacrolix-quicktest-porting-iemauj branch September 8, 2026 04:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant