From 5afcd02ea68efd3faba578bdcee853c028b14e0e Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Tue, 16 May 2023 19:53:19 +0200 Subject: Add automated test Follow `golang.org/x/tools/go/analysis/passes/testinggoroutine` to write the test runner: https://github.com/golang/tools/blob/1c9fe3f82c363b929ef7239ca0ad8a5dafbbcf05/go/analysis/passes/testinggoroutine/testinggoroutine_test.go Add "want" comments to test code for validation. --- gocapturedrefrace_test.go | 14 ++++++++++++++ testdata/simple.go | 6 +++--- testdata/struct_reference.go | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 gocapturedrefrace_test.go diff --git a/gocapturedrefrace_test.go b/gocapturedrefrace_test.go new file mode 100644 index 0000000..d2e82b2 --- /dev/null +++ b/gocapturedrefrace_test.go @@ -0,0 +1,14 @@ +package gocapturedrefrace_test + +import ( + "testing" + + "golang.org/x/tools/go/analysis/analysistest" + "gopkg.teddywing.com/gocapturedrefrace" +) + +func Test(t *testing.T) { + testdata := analysistest.TestData() + + analysistest.Run(t, testdata, gocapturedrefrace.Analyzer, ".") +} diff --git a/testdata/simple.go b/testdata/simple.go index 76fb5d5..8002953 100644 --- a/testdata/simple.go +++ b/testdata/simple.go @@ -8,8 +8,8 @@ func main() { copied := 0 go func(copied int) { - capturedReference += 1 - capturedReference2 += 1 + capturedReference += 1 // want "captured reference capturedReference in goroutine closure" + capturedReference2 += 1 // want "captured reference capturedReference2 in goroutine closure" copied += 1 if capturedReference == 1 { @@ -36,6 +36,6 @@ func argumentReference() { s := aStruct{field: 0} go func(s *aStruct) { - s.field += 1 + s.field += 1 // want "reference s in goroutine closure" }(&s) } diff --git a/testdata/struct_reference.go b/testdata/struct_reference.go index 5ebc31d..98d627f 100644 --- a/testdata/struct_reference.go +++ b/testdata/struct_reference.go @@ -10,6 +10,6 @@ func (s *AStruct) setField(value string) { func (s *AStruct) method2() { go func() { - s.setField("test") + s.setField("test") // want "captured reference s in goroutine closure" }() } -- cgit v1.2.3