context: use consistent names for test-tables

Also renamed a var that collided with a type, and marked a helper
as t.Helper()

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-04-30 14:43:13 +02:00
parent f238f7dcaa
commit 2829f7b7d5
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
2 changed files with 20 additions and 19 deletions

View file

@ -22,7 +22,7 @@ func TestWithRequest(t *testing.T) {
req.Header.Set("User-Agent", "test/0.1") req.Header.Set("User-Agent", "test/0.1")
ctx := WithRequest(Background(), &req) ctx := WithRequest(Background(), &req)
for _, testcase := range []struct { for _, tc := range []struct {
key string key string
expected interface{} expected interface{}
}{ }{
@ -61,18 +61,18 @@ func TestWithRequest(t *testing.T) {
key: "http.request.startedat", key: "http.request.startedat",
}, },
} { } {
v := ctx.Value(testcase.key) v := ctx.Value(tc.key)
if v == nil { if v == nil {
t.Fatalf("value not found for %q", testcase.key) t.Fatalf("value not found for %q", tc.key)
} }
if testcase.expected != nil && v != testcase.expected { if tc.expected != nil && v != tc.expected {
t.Fatalf("%s: %v != %v", testcase.key, v, testcase.expected) t.Fatalf("%s: %v != %v", tc.key, v, tc.expected)
} }
// Key specific checks! // Key specific checks!
switch testcase.key { switch tc.key {
case "http.request.id": case "http.request.id":
if _, ok := v.(string); !ok { if _, ok := v.(string); !ok {
t.Fatalf("request id not a string: %v", v) t.Fatalf("request id not a string: %v", v)
@ -195,7 +195,7 @@ func TestWithVars(t *testing.T) {
} }
ctx := WithVars(Background(), &req) ctx := WithVars(Background(), &req)
for _, testcase := range []struct { for _, tc := range []struct {
key string key string
expected interface{} expected interface{}
}{ }{
@ -212,10 +212,10 @@ func TestWithVars(t *testing.T) {
expected: "qwer", expected: "qwer",
}, },
} { } {
v := ctx.Value(testcase.key) v := ctx.Value(tc.key)
if !reflect.DeepEqual(v, testcase.expected) { if !reflect.DeepEqual(v, tc.expected) {
t.Fatalf("%q: %v != %v", testcase.key, v, testcase.expected) t.Fatalf("%q: %v != %v", tc.key, v, tc.expected)
} }
} }
} }

View file

@ -41,7 +41,7 @@ func TestWithTrace(t *testing.T) {
expected: f.Name(), expected: f.Name(),
})) }))
traced := func() { tracedFn := func() {
parentID := ctx.Value("trace.id") // ensure the parent trace id is correct. parentID := ctx.Value("trace.id") // ensure the parent trace id is correct.
pc, _, _, _ := runtime.Caller(0) // get current caller. pc, _, _, _ := runtime.Caller(0) // get current caller.
@ -57,7 +57,7 @@ func TestWithTrace(t *testing.T) {
expected: parentID, expected: parentID,
})) }))
} }
traced() tracedFn()
time.Sleep(time.Second) time.Sleep(time.Second)
} }
@ -68,18 +68,19 @@ type valueTestCase struct {
notnilorempty bool // just check not empty/not nil notnilorempty bool // just check not empty/not nil
} }
func checkContextForValues(ctx context.Context, t *testing.T, values []valueTestCase) { func checkContextForValues(ctx context.Context, t *testing.T, tests []valueTestCase) {
for _, testcase := range values { t.Helper()
v := ctx.Value(testcase.key) for _, tc := range tests {
if testcase.notnilorempty { v := ctx.Value(tc.key)
if tc.notnilorempty {
if v == nil || v == "" { if v == nil || v == "" {
t.Fatalf("value was nil or empty for %q: %#v", testcase.key, v) t.Fatalf("value was nil or empty for %q: %#v", tc.key, v)
} }
continue continue
} }
if v != testcase.expected { if v != tc.expected {
t.Fatalf("unexpected value for key %q: %v != %v", testcase.key, v, testcase.expected) t.Fatalf("unexpected value for key %q: %v != %v", tc.key, v, tc.expected)
} }
} }
} }