diff options
| author | Teddy Wing | 2023-05-16 19:53:19 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2023-05-16 19:53:19 +0200 | 
| commit | 5afcd02ea68efd3faba578bdcee853c028b14e0e (patch) | |
| tree | fa5e230b7403febab284616814fa23d43c2fa70b | |
| parent | 3eef30cf630eda3837a27a237e89207e1ce3dabf (diff) | |
| download | gocapturedrefrace-5afcd02ea68efd3faba578bdcee853c028b14e0e.tar.bz2 | |
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.
| -rw-r--r-- | gocapturedrefrace_test.go | 14 | ||||
| -rw-r--r-- | testdata/simple.go | 6 | ||||
| -rw-r--r-- | testdata/struct_reference.go | 2 | 
3 files changed, 18 insertions, 4 deletions
| 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"  	}()  } | 
