*: 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:
Jimmy Zelinskie 2017-12-29 17:44:45 -05:00
parent 5840cd3de1
commit 2004489016
8 changed files with 84 additions and 52 deletions

View file

@ -43,11 +43,13 @@ func TestClientID(t *testing.T) {
} }
for _, tt := range clientTable { for _, tt := range clientTable {
t.Run(tt.peerID, func(t *testing.T) {
var clientID ClientID var clientID ClientID
copy(clientID[:], []byte(tt.clientID)) copy(clientID[:], []byte(tt.clientID))
parsedID := NewClientID(PeerIDFromString(tt.peerID)) parsedID := NewClientID(PeerIDFromString(tt.peerID))
if parsedID != clientID { if parsedID != clientID {
t.Error("Incorrectly parsed peer ID", tt.peerID, "as", parsedID) t.Error("Incorrectly parsed peer ID", tt.peerID, "as", parsedID)
} }
})
} }
} }

View file

@ -1,6 +1,7 @@
package bittorrent package bittorrent
import ( import (
"fmt"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -22,8 +23,10 @@ func TestNew(t *testing.T) {
} }
for _, tt := range table { for _, tt := range table {
t.Run(fmt.Sprintf("%#v expecting %s", tt.data, tt.expectedErr), func(t *testing.T) {
got, err := NewEvent(tt.data) got, err := NewEvent(tt.data)
require.Equal(t, err, tt.expectedErr, "errors should equal the expected value") require.Equal(t, err, tt.expectedErr, "errors should equal the expected value")
require.Equal(t, got, tt.expected, "events should equal the expected value") require.Equal(t, got, tt.expected, "events should equal the expected value")
})
} }
} }

View file

@ -24,9 +24,11 @@ var unmarshalTests = []struct {
func TestUnmarshal(t *testing.T) { func TestUnmarshal(t *testing.T) {
for _, tt := range unmarshalTests { for _, tt := range unmarshalTests {
t.Run(tt.input, func(t *testing.T) {
got, err := Unmarshal([]byte(tt.input)) got, err := Unmarshal([]byte(tt.input))
require.Nil(t, err, "unmarshal should not fail") require.Nil(t, err, "unmarshal should not fail")
require.Equal(t, got, tt.expected, "unmarshalled values should match the expected results") require.Equal(t, got, tt.expected, "unmarshalled values should match the expected results")
})
} }
} }

View file

@ -2,6 +2,7 @@ package bencode
import ( import (
"bytes" "bytes"
"fmt"
"testing" "testing"
"time" "time"
@ -35,10 +36,12 @@ var marshalTests = []struct {
} }
func TestMarshal(t *testing.T) { func TestMarshal(t *testing.T) {
for _, test := range marshalTests { for _, tt := range marshalTests {
got, err := Marshal(test.input) 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.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")
})
} }
} }

View file

@ -1,6 +1,7 @@
package http package http
import ( import (
"fmt"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
@ -18,16 +19,28 @@ func TestWriteError(t *testing.T) {
} }
for _, tt := range table { for _, tt := range table {
t.Run(fmt.Sprintf("%s expecting %s", tt.reason, tt.expected), func(t *testing.T) {
r := httptest.NewRecorder() r := httptest.NewRecorder()
err := WriteError(r, bittorrent.ClientError(tt.reason)) err := WriteError(r, bittorrent.ClientError(tt.reason))
require.Nil(t, err) require.Nil(t, err)
require.Equal(t, r.Body.String(), tt.expected) require.Equal(t, r.Body.String(), tt.expected)
})
} }
} }
func TestWriteStatus(t *testing.T) { 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() r := httptest.NewRecorder()
err := WriteError(r, bittorrent.ClientError("something is missing")) err := WriteError(r, bittorrent.ClientError(tt.reason))
require.Nil(t, err) require.Nil(t, err)
require.Equal(t, r.Body.String(), "d14:failure reason20:something is missinge") require.Equal(t, r.Body.String(), tt.expected)
})
}
} }

View file

@ -1,6 +1,7 @@
package udp package udp
import ( import (
"fmt"
"net" "net"
"testing" "testing"
"time" "time"
@ -20,11 +21,13 @@ var golden = []struct {
func TestVerification(t *testing.T) { func TestVerification(t *testing.T) {
for _, tt := range golden { 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) 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) got := ValidConnectionID(cid, net.ParseIP(tt.ip), time.Unix(tt.now, 0), time.Minute, tt.key)
if got != tt.valid { if got != tt.valid {
t.Errorf("expected validity: %t got validity: %t", tt.valid, got) t.Errorf("expected validity: %t got validity: %t", tt.valid, got)
} }
})
} }
} }

View file

@ -1,6 +1,9 @@
package udp package udp
import "testing" import (
"fmt"
"testing"
)
var table = []struct { var table = []struct {
data []byte data []byte
@ -45,27 +48,29 @@ var table = []struct {
} }
func TestHandleOptionalParameters(t *testing.T) { func TestHandleOptionalParameters(t *testing.T) {
for _, testCase := range table { for _, tt := range table {
params, err := handleOptionalParameters(testCase.data) t.Run(fmt.Sprintf("%#v as %#v", tt.data, tt.values), func(t *testing.T) {
if err != testCase.err { params, err := handleOptionalParameters(tt.data)
if testCase.err == nil { if err != tt.err {
t.Fatalf("expected no parsing error for %x but got %s", testCase.data, err) if tt.err == nil {
t.Fatalf("expected no parsing error for %x but got %s", tt.data, err)
} else { } 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 { 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 { } else {
for key, want := range testCase.values { for key, want := range tt.values {
if got, ok := params.String(key); !ok { 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 { } 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)
} }
} }
} }
} }
})
} }
} }

View file

@ -2,6 +2,7 @@ package varinterval
import ( import (
"context" "context"
"fmt"
"testing" "testing"
"github.com/chihaya/chihaya/bittorrent" "github.com/chihaya/chihaya/bittorrent"
@ -34,10 +35,10 @@ var configTests = []struct {
} }
func TestCheckConfig(t *testing.T) { func TestCheckConfig(t *testing.T) {
for _, tc := range configTests { for _, tt := range configTests {
t.Run("", func(t *testing.T) { t.Run(fmt.Sprintf("%#v", tt.cfg), func(t *testing.T) {
got := checkConfig(tc.cfg) got := checkConfig(tt.cfg)
require.Equal(t, tc.expected, got, "", tc.cfg) require.Equal(t, tt.expected, got, "", tt.cfg)
}) })
} }
} }