1
0
mirror of https://github.com/golang/go synced 2024-11-26 21:21:34 -07:00

go/ast: add test for Filter

Change-Id: Ia14659a9c44f9e1504eb88b5693932b9dd4bb286
Reviewed-on: https://go-review.googlesource.com/c/go/+/252939
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Dmitri Shuralyov <dmitshur@golang.org>
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Heisenberg 2020-09-04 16:56:54 +08:00 committed by Robert Griesemer
parent cc0930cd1d
commit 974def803e

View File

@ -140,4 +140,31 @@ func TestCommentMap(t *testing.T) {
}
}
// TODO(gri): add tests for Filter.
func TestFilter(t *testing.T) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
if err != nil {
t.Fatal(err)
}
cmap := NewCommentMap(fset, f, f.Comments)
// delete variable declaration
for i, decl := range f.Decls {
if gen, ok := decl.(*GenDecl); ok && gen.Tok == token.VAR {
copy(f.Decls[i:], f.Decls[i+1:])
f.Decls = f.Decls[:len(f.Decls)-1]
break
}
}
// check if comments are filtered correctly
cc := cmap.Filter(f)
for n, list := range cc {
key := fmt.Sprintf("%2d: %T", fset.Position(n.Pos()).Line, n)
got := ctext(list)
want := res[key]
if key == "25: *ast.GenDecl" || got != want {
t.Errorf("%s: got %q; want %q", key, got, want)
}
}
}