-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspect_test.go
50 lines (43 loc) · 1.31 KB
/
inspect_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package astTool
import (
"go/ast"
"go/token"
"testing"
)
func TestHappyAst_FindFuncDeclNode(t *testing.T) {
srcode := `package miclient
var ppx int
func testFoo(){}`
h := ParseFromCode(srcode)
wantedFuncName := "testFoo"
fpos := h.FindFuncDeclNode(wantedFuncName)
fnode := h.FindNodeByPos(fpos)
gotName := (*fnode).(*ast.FuncDecl).Name.Name
if gotName != wantedFuncName {
t.Errorf("got %q;wanted %q", gotName, wantedFuncName)
}
}
func TestHappyAst_FindStructDeclNode(t *testing.T) {
srcode := `package miclient
type Microservice struct {
ActivityHost string ` + "`" + `json:"activity_service_host"` + "`" + `
}`
h := ParseFromCode(srcode)
pos := h.FindStructDeclNode("Microservice")
if pos == token.NoPos || h.Position(pos).String() != "2:6" {
t.Errorf("got %q;wanted %q", h.Position(pos), "2:6")
}
}
func TestHappyAst_FindStructFieldFromNode(t *testing.T) {
srcode := `package miclient
type Microservice struct {
ActivityHost string ` + "`" + `json:"activity_service_host"` + "`" + `
}`
h := ParseFromCode(srcode)
structPos := h.FindStructDeclNode("Microservice")
structNode := h.FindNodeByPos(structPos)
pos := h.FindStructFieldFromNode(*structNode, "ActivityHost")
if pos == token.NoPos || h.Position(pos).String() != "3:1" {
t.Errorf("got %q;wanted %q", h.Position(pos), "3:1")
}
}