-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from gostaticanalysis/add-Under
Add Under
- Loading branch information
Showing
5 changed files
with
124 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package analysisutil_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"golang.org/x/tools/go/analysis/analysistest" | ||
) | ||
|
||
func WriteFiles(t *testing.T, filemap map[string]string) string { | ||
t.Helper() | ||
dir, clean, err := analysistest.WriteFiles(filemap) | ||
if err != nil { | ||
t.Fatal("unexpected error:", err) | ||
} | ||
t.Cleanup(clean) | ||
return dir | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,117 @@ | ||
package analysisutil_test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"go/token" | ||
"go/types" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/gostaticanalysis/analysisutil" | ||
"golang.org/x/tools/go/analysis" | ||
"golang.org/x/tools/go/analysis/analysistest" | ||
"golang.org/x/tools/go/analysis/passes/buildssa" | ||
) | ||
|
||
const pkg = "objectof" | ||
func TestObjectOf(t *testing.T) { | ||
t.Parallel() | ||
|
||
var typesAnalyzer = &analysis.Analyzer{ | ||
Name: "test_types", | ||
Run: run_test_types, | ||
Requires: []*analysis.Analyzer{ | ||
buildssa.Analyzer, | ||
}, | ||
} | ||
|
||
func Test_Types(t *testing.T) { | ||
testdata := analysistest.TestData() | ||
analysistest.Run(t, testdata, typesAnalyzer, pkg) | ||
} | ||
|
||
func run_test_types(pass *analysis.Pass) (interface{}, error) { | ||
tests := []struct { | ||
path, name string | ||
found bool | ||
cases := map[string]struct { | ||
src string | ||
pkg string // blank means same as the map key | ||
name string | ||
found bool | ||
}{ | ||
{"fmt", "Println", true}, | ||
{pkg, "A", true}, | ||
{pkg, "EOF", true}, | ||
{"io", "EOF", true}, | ||
{"reflect", "Kind", false}, | ||
{"a", "ok", false}, | ||
{"vendored", "EOF", true}, | ||
{"c", "EOF", false}, | ||
{"database/sql", "*DB", false}, | ||
"standard": {`import _ "fmt"`, "fmt", "Println", true}, | ||
"unimport": {"", "fmt", "Println", false}, | ||
"notexiststd": {`import _ "fmt"`, "fmt", "NOTEXIST", false}, | ||
"typename": {"type A int", "", "A", true}, | ||
"unexportvar": {"var n int", "", "n", true}, | ||
"exportvar": {"var N int", "", "N", true}, | ||
"notexist": {"", "", "NOTEXIST", false}, | ||
"vendored": {`import _ "fmt"`, "vendor/fmt", "Println", true}, | ||
"pointer": {"type A int", "", "*A", false}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
obj := analysisutil.ObjectOf(pass, tt.path, tt.name) | ||
for name, tt := range cases { | ||
name, tt := name, tt | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
a := &analysis.Analyzer{ | ||
Name: name + "Analyzer", | ||
Run: func(pass *analysis.Pass) (interface{}, error) { | ||
pkg := name | ||
if tt.pkg != "" { | ||
pkg = tt.pkg | ||
} | ||
obj := analysisutil.ObjectOf(pass, pkg, tt.name) | ||
switch { | ||
case tt.found && obj == nil: | ||
return nil, errors.New("expect found but not found") | ||
case !tt.found && obj != nil: | ||
return nil, fmt.Errorf("unexpected return value: %v", obj) | ||
} | ||
return nil, nil | ||
}, | ||
} | ||
path := filepath.Join(name, name+".go") | ||
dir := WriteFiles(t, map[string]string{ | ||
path: fmt.Sprintf("package %s\n%s", name, tt.src), | ||
}) | ||
analysistest.Run(t, dir, a, name) | ||
}) | ||
} | ||
|
||
if obj == nil && tt.found { | ||
pass.Reportf(token.NoPos, "objectof could not find %s.%s", tt.path, tt.name) | ||
} | ||
if obj != nil && !tt.found { | ||
pass.Reportf(token.NoPos, "objectof found %s.%s, which does not exist", tt.path, tt.name) | ||
} | ||
|
||
func TestUnder(t *testing.T) { | ||
t.Parallel() | ||
|
||
lookup := func(pass *analysis.Pass, n string) (types.Type, error) { | ||
_, obj := pass.Pkg.Scope().LookupParent(n, token.NoPos) | ||
if obj == nil { | ||
return nil, fmt.Errorf("does not find: %s", n) | ||
} | ||
return obj.Type(), nil | ||
} | ||
|
||
cases := map[string]struct { | ||
src string | ||
typ string | ||
want string | ||
}{ | ||
"nonamed": {"", "int", "int"}, | ||
"named": {"type A int", "A", "int"}, | ||
"twonamed": {"type A int; type B A", "B", "int"}, | ||
} | ||
|
||
return nil, nil | ||
for name, tt := range cases { | ||
name, tt := name, tt | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
a := &analysis.Analyzer{ | ||
Name: name + "Analyzer", | ||
Run: func(pass *analysis.Pass) (interface{}, error) { | ||
typ, err := lookup(pass, tt.typ) | ||
if err != nil { | ||
return nil, err | ||
} | ||
want, err := lookup(pass, tt.want) | ||
if err != nil { | ||
return nil, err | ||
} | ||
got := analysisutil.Under(typ) | ||
if !types.Identical(want, got) { | ||
return nil, fmt.Errorf("want %v but got %v", want, got) | ||
} | ||
return nil, nil | ||
}, | ||
} | ||
path := filepath.Join(name, name+".go") | ||
dir := WriteFiles(t, map[string]string{ | ||
path: fmt.Sprintf("package %s\n%s", name, tt.src), | ||
}) | ||
analysistest.Run(t, dir, a, name) | ||
}) | ||
} | ||
} |