*: add subtests for all table driven tests
Because we use testify, this is less useful than normal, but this is still best practice for table-driven tests.
This commit is contained in:
parent
5840cd3de1
commit
2004489016
8 changed files with 84 additions and 52 deletions
|
@ -43,11 +43,13 @@ func TestClientID(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tt := range clientTable {
|
||||
t.Run(tt.peerID, func(t *testing.T) {
|
||||
var clientID ClientID
|
||||
copy(clientID[:], []byte(tt.clientID))
|
||||
parsedID := NewClientID(PeerIDFromString(tt.peerID))
|
||||
if parsedID != clientID {
|
||||
t.Error("Incorrectly parsed peer ID", tt.peerID, "as", parsedID)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package bittorrent
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
@ -22,8 +23,10 @@ func TestNew(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tt := range table {
|
||||
t.Run(fmt.Sprintf("%#v expecting %s", tt.data, tt.expectedErr), func(t *testing.T) {
|
||||
got, err := NewEvent(tt.data)
|
||||
require.Equal(t, err, tt.expectedErr, "errors should equal the expected value")
|
||||
require.Equal(t, got, tt.expected, "events should equal the expected value")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,9 +24,11 @@ var unmarshalTests = []struct {
|
|||
|
||||
func TestUnmarshal(t *testing.T) {
|
||||
for _, tt := range unmarshalTests {
|
||||
t.Run(tt.input, func(t *testing.T) {
|
||||
got, err := Unmarshal([]byte(tt.input))
|
||||
require.Nil(t, err, "unmarshal should not fail")
|
||||
require.Equal(t, got, tt.expected, "unmarshalled values should match the expected results")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ package bencode
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -35,10 +36,12 @@ var marshalTests = []struct {
|
|||
}
|
||||
|
||||
func TestMarshal(t *testing.T) {
|
||||
for _, test := range marshalTests {
|
||||
got, err := Marshal(test.input)
|
||||
for _, tt := range marshalTests {
|
||||
t.Run(fmt.Sprintf("%#v", tt.input), func(t *testing.T) {
|
||||
got, err := Marshal(tt.input)
|
||||
require.Nil(t, err, "marshal should not fail")
|
||||
require.Contains(t, test.expected, string(got), "the marshaled result should be one of the expected permutations")
|
||||
require.Contains(t, tt.expected, string(got), "the marshaled result should be one of the expected permutations")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
|
@ -18,16 +19,28 @@ func TestWriteError(t *testing.T) {
|
|||
}
|
||||
|
||||
for _, tt := range table {
|
||||
t.Run(fmt.Sprintf("%s expecting %s", tt.reason, tt.expected), func(t *testing.T) {
|
||||
r := httptest.NewRecorder()
|
||||
err := WriteError(r, bittorrent.ClientError(tt.reason))
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, r.Body.String(), tt.expected)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteStatus(t *testing.T) {
|
||||
var table = []struct {
|
||||
reason, expected string
|
||||
}{
|
||||
{"something is missing", "d14:failure reason20:something is missinge"},
|
||||
}
|
||||
|
||||
for _, tt := range table {
|
||||
t.Run(fmt.Sprintf("%s expecting %s", tt.reason, tt.expected), func(t *testing.T) {
|
||||
r := httptest.NewRecorder()
|
||||
err := WriteError(r, bittorrent.ClientError("something is missing"))
|
||||
err := WriteError(r, bittorrent.ClientError(tt.reason))
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, r.Body.String(), "d14:failure reason20:something is missinge")
|
||||
require.Equal(t, r.Body.String(), tt.expected)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package udp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
@ -20,11 +21,13 @@ var golden = []struct {
|
|||
|
||||
func TestVerification(t *testing.T) {
|
||||
for _, tt := range golden {
|
||||
t.Run(fmt.Sprintf("%s created at %d verified at %d", tt.ip, tt.createdAt, tt.now), func(t *testing.T) {
|
||||
cid := NewConnectionID(net.ParseIP(tt.ip), time.Unix(tt.createdAt, 0), tt.key)
|
||||
got := ValidConnectionID(cid, net.ParseIP(tt.ip), time.Unix(tt.now, 0), time.Minute, tt.key)
|
||||
if got != tt.valid {
|
||||
t.Errorf("expected validity: %t got validity: %t", tt.valid, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package udp
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var table = []struct {
|
||||
data []byte
|
||||
|
@ -45,27 +48,29 @@ var table = []struct {
|
|||
}
|
||||
|
||||
func TestHandleOptionalParameters(t *testing.T) {
|
||||
for _, testCase := range table {
|
||||
params, err := handleOptionalParameters(testCase.data)
|
||||
if err != testCase.err {
|
||||
if testCase.err == nil {
|
||||
t.Fatalf("expected no parsing error for %x but got %s", testCase.data, err)
|
||||
for _, tt := range table {
|
||||
t.Run(fmt.Sprintf("%#v as %#v", tt.data, tt.values), func(t *testing.T) {
|
||||
params, err := handleOptionalParameters(tt.data)
|
||||
if err != tt.err {
|
||||
if tt.err == nil {
|
||||
t.Fatalf("expected no parsing error for %x but got %s", tt.data, err)
|
||||
} else {
|
||||
t.Fatalf("expected parsing error for %x", testCase.data)
|
||||
t.Fatalf("expected parsing error for %x", tt.data)
|
||||
}
|
||||
}
|
||||
if testCase.values != nil {
|
||||
if tt.values != nil {
|
||||
if params == nil {
|
||||
t.Fatalf("expected values %v for %x", testCase.values, testCase.data)
|
||||
t.Fatalf("expected values %v for %x", tt.values, tt.data)
|
||||
} else {
|
||||
for key, want := range testCase.values {
|
||||
for key, want := range tt.values {
|
||||
if got, ok := params.String(key); !ok {
|
||||
t.Fatalf("params missing entry %s for data %x", key, testCase.data)
|
||||
t.Fatalf("params missing entry %s for data %x", key, tt.data)
|
||||
} else if got != want {
|
||||
t.Fatalf("expected param %s=%s, but was %s for data %x", key, want, got, testCase.data)
|
||||
t.Fatalf("expected param %s=%s, but was %s for data %x", key, want, got, tt.data)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package varinterval
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/chihaya/chihaya/bittorrent"
|
||||
|
@ -34,10 +35,10 @@ var configTests = []struct {
|
|||
}
|
||||
|
||||
func TestCheckConfig(t *testing.T) {
|
||||
for _, tc := range configTests {
|
||||
t.Run("", func(t *testing.T) {
|
||||
got := checkConfig(tc.cfg)
|
||||
require.Equal(t, tc.expected, got, "", tc.cfg)
|
||||
for _, tt := range configTests {
|
||||
t.Run(fmt.Sprintf("%#v", tt.cfg), func(t *testing.T) {
|
||||
got := checkConfig(tt.cfg)
|
||||
require.Equal(t, tt.expected, got, "", tt.cfg)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue