Skip to content

Commit e34b1cc

Browse files
committed
Added test for getGolangFuncs()
1 parent dc1e2a2 commit e34b1cc

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

ast.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"go/token"
77
)
88

9-
// Func - one go function in source with return information
9+
// Func - one go function in source
1010
type Func struct {
1111
Name string
1212
Begin, End int

ast_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
const testGolangSrc = `package somepkg
9+
10+
import "fmt"
11+
12+
type T int
13+
14+
func (r T) String() string {
15+
return fmt.Sprintf("%v", r)
16+
}
17+
18+
func fn() string {
19+
return "Hello"
20+
}
21+
`
22+
23+
func Test_getGolangFuncs(t *testing.T) {
24+
tests := []struct {
25+
name string
26+
fileContent []byte
27+
wantResult []Func
28+
wantErr bool
29+
}{
30+
{
31+
name: "without error",
32+
fileContent: []byte(testGolangSrc),
33+
wantResult: []Func{
34+
{Name: "String", Begin: 44, End: 103},
35+
{Name: "fn", Begin: 105, End: 141},
36+
},
37+
wantErr: false,
38+
},
39+
{
40+
name: "with error",
41+
fileContent: []byte("..."),
42+
wantResult: nil,
43+
wantErr: true,
44+
},
45+
}
46+
47+
for _, tt := range tests {
48+
t.Run(tt.name, func(t *testing.T) {
49+
gotResult, err := getGolangFuncs(tt.fileContent)
50+
if (err != nil) != tt.wantErr {
51+
t.Errorf("getGolangFuncs() error = %v, wantErr %v", err, tt.wantErr)
52+
return
53+
}
54+
if !reflect.DeepEqual(gotResult, tt.wantResult) {
55+
t.Errorf("getGolangFuncs() = %#v, want %#v", gotResult, tt.wantResult)
56+
}
57+
})
58+
}
59+
}

0 commit comments

Comments
 (0)