Debian Codegraph @

go/analysis/passes/modernize/testdata/src/testingcontext/testingcontext_test.go

raw
1 package testingcontext
2
3 import (
4 "context"
5
6 "testing"
7 )
8
9 func Test(t *testing.T) {
10 ctx, cancel := context.WithCancel(context.Background()) // want "context.WithCancel can be modernized using t.Context"
11 defer cancel()
12 _ = ctx
13
14 func() {
15 ctx, cancel := context.WithCancel(context.Background()) // Nope. scope of defer is not the testing func.
16 defer cancel()
17 _ = ctx
18 }()
19
20 {
21 ctx, cancel := context.WithCancel(context.TODO()) // want "context.WithCancel can be modernized using t.Context"
22 defer cancel()
23 _ = ctx
24 var t int // not in scope of the call to WithCancel
25 _ = t
26 }
27
28 {
29 ctx := context.Background()
30 ctx, cancel := context.WithCancel(context.Background()) // Nope. ctx is redeclared.
31 defer cancel()
32 _ = ctx
33 }
34
35 {
36 var t int
37 ctx, cancel := context.WithCancel(context.Background()) // Nope. t is shadowed.
38 defer cancel()
39 _ = ctx
40 _ = t
41 }
42
43 t.Run("subtest", func(t2 *testing.T) {
44 ctx, cancel := context.WithCancel(context.Background()) // want "context.WithCancel can be modernized using t2.Context"
45 defer cancel()
46 _ = ctx
47 })
48 }
49
50 func TestAlt(t2 *testing.T) {
51 ctx, cancel := context.WithCancel(context.Background()) // want "context.WithCancel can be modernized using t2.Context"
52 defer cancel()
53 _ = ctx
54 }
55
56 func Testnot(t *testing.T) {
57 ctx, cancel := context.WithCancel(context.Background()) // Nope. Not a test func.
58 defer cancel()
59 _ = ctx
60 }
61
62 func Benchmark(b *testing.B) {
63 ctx, cancel := context.WithCancel(context.Background()) // want "context.WithCancel can be modernized using b.Context"
64 defer cancel()
65 _ = ctx
66
67 b.Run("subtest", func(b2 *testing.B) {
68 ctx, cancel := context.WithCancel(context.Background()) // want "context.WithCancel can be modernized using b2.Context"
69 defer cancel()
70 _ = ctx
71 })
72 }
73
74 func Fuzz(f *testing.F) {
75 ctx, cancel := context.WithCancel(context.Background()) // want "context.WithCancel can be modernized using f.Context"
76 defer cancel()
77 _ = ctx
78 }