*: 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 {
var clientID ClientID t.Run(tt.peerID, func(t *testing.T) {
copy(clientID[:], []byte(tt.clientID)) var clientID ClientID
parsedID := NewClientID(PeerIDFromString(tt.peerID)) copy(clientID[:], []byte(tt.clientID))
if parsedID != clientID { parsedID := NewClientID(PeerIDFromString(tt.peerID))
t.Error("Incorrectly parsed peer ID", tt.peerID, "as", parsedID) if parsedID != clientID {
} 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 {
got, err := NewEvent(tt.data) t.Run(fmt.Sprintf("%#v expecting %s", tt.data, tt.expectedErr), func(t *testing.T) {
require.Equal(t, err, tt.expectedErr, "errors should equal the expected value") got, err := NewEvent(tt.data)
require.Equal(t, got, tt.expected, "events 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")
})
} }
} }

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 {
got, err := Unmarshal([]byte(tt.input)) t.Run(tt.input, func(t *testing.T) {
require.Nil(t, err, "unmarshal should not fail") got, err := Unmarshal([]byte(tt.input))
require.Equal(t, got, tt.expected, "unmarshalled values should match the expected results") require.Nil(t, err, "unmarshal should not fail")
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) {
require.Nil(t, err, "marshal should not fail") got, err := Marshal(tt.input)
require.Contains(t, test.expected, string(got), "the marshaled result should be one of the expected permutations") require.Nil(t, err, "marshal should not fail")
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 {
r := httptest.NewRecorder() t.Run(fmt.Sprintf("%s expecting %s", tt.reason, tt.expected), func(t *testing.T) {
err := WriteError(r, bittorrent.ClientError(tt.reason)) r := httptest.NewRecorder()
require.Nil(t, err) err := WriteError(r, bittorrent.ClientError(tt.reason))
require.Equal(t, r.Body.String(), tt.expected) require.Nil(t, err)
require.Equal(t, r.Body.String(), tt.expected)
})
} }
} }
func TestWriteStatus(t *testing.T) { func TestWriteStatus(t *testing.T) {
r := httptest.NewRecorder() var table = []struct {
err := WriteError(r, bittorrent.ClientError("something is missing")) reason, expected string
require.Nil(t, err) }{
require.Equal(t, r.Body.String(), "d14:failure reason20:something is missinge") {"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(tt.reason))
require.Nil(t, err)
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 {
cid := NewConnectionID(net.ParseIP(tt.ip), time.Unix(tt.createdAt, 0), tt.key) t.Run(fmt.Sprintf("%s created at %d verified at %d", tt.ip, tt.createdAt, tt.now), func(t *testing.T) {
got := ValidConnectionID(cid, net.ParseIP(tt.ip), time.Unix(tt.now, 0), time.Minute, tt.key) cid := NewConnectionID(net.ParseIP(tt.ip), time.Unix(tt.createdAt, 0), tt.key)
if got != tt.valid { got := ValidConnectionID(cid, net.ParseIP(tt.ip), time.Unix(tt.now, 0), time.Minute, tt.key)
t.Errorf("expected validity: %t got validity: %t", tt.valid, got) if got != tt.valid {
} 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 {
} else { t.Fatalf("expected no parsing error for %x but got %s", tt.data, err)
t.Fatalf("expected parsing error for %x", testCase.data) } else {
t.Fatalf("expected parsing error for %x", tt.data)
}
} }
} if tt.values != nil {
if testCase.values != nil { if params == nil {
if params == nil { t.Fatalf("expected values %v for %x", tt.values, tt.data)
t.Fatalf("expected values %v for %x", testCase.values, testCase.data) } else {
} else { for key, want := range tt.values {
for key, want := range testCase.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, tt.data)
t.Fatalf("params missing entry %s for data %x", key, testCase.data) } else if got != want {
} else if got != want { t.Fatalf("expected param %s=%s, but was %s for data %x", key, want, got, tt.data)
t.Fatalf("expected param %s=%s, but was %s for data %x", key, want, got, testCase.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)
}) })
} }
} }