Skip to content

Commit c5fec0e

Browse files
Write unit test with mocked golang file used for parsing methods
1 parent e964764 commit c5fec0e

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed

test-crawler/go.mod

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ module testsuites
22

33
go 1.17
44

5-
require github.com/smacker/go-tree-sitter v0.0.0-20211116060328-db7fde9b5e82
5+
require (
6+
github.com/smacker/go-tree-sitter v0.0.0-20211116060328-db7fde9b5e82
7+
github.com/stretchr/testify v1.4.0
8+
)
69

7-
require gopkg.in/yaml.v2 v2.4.0 // indirect
10+
require (
11+
github.com/davecgh/go-spew v1.1.0 // indirect
12+
github.com/pmezard/go-difflib v1.0.0 // indirect
13+
gopkg.in/yaml.v2 v2.4.0
14+
)

test-crawler/go.sum

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ github.com/smacker/go-tree-sitter v0.0.0-20211116060328-db7fde9b5e82/go.mod h1:E
77
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
88
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
99
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
10+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1011
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
11-
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
1212
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
1313
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
1414
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

test-crawler/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ func main() {
2727
config := NewConfig()
2828

2929
crawlRepoBehaviorsAndSaveToJSON(config)
30-
crawlSingleFileForMethods("../repo-to-crawl/system/subsystem/file.go") // // ex: ../repo-to-crawl/venus-gateway/proofevent/proof_event.go
3130
}
3231

3332
func crawlRepoBehaviorsAndSaveToJSON(config Config) {

test-crawler/main_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestExtractPublicMethodsFromFile(t *testing.T) {
11+
fnsAnn, err := extractPublicMethodsFromFile(context.Background(), "./mocks/event.go")
12+
if err != nil {
13+
t.Errorf("got error: %v", err.Error())
14+
}
15+
16+
if len(fnsAnn) != 2 {
17+
t.Errorf("got %q, expected %q methods", len(fnsAnn), 2)
18+
}
19+
20+
assert.Equal(t, "HelloEvent", fnsAnn[0].Name)
21+
assert.Equal(t, "()", fnsAnn[0].InputParams) // input param
22+
assert.Equal(t, "string", fnsAnn[0].ReturnValues) // return param
23+
24+
assert.Equal(t, "HelloEventWithParameter", fnsAnn[1].Name)
25+
assert.Equal(t, "(param string)", fnsAnn[1].InputParams)
26+
assert.Equal(t, "(string, error)", fnsAnn[1].ReturnValues)
27+
}

test-crawler/mocks/event.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package mocks
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"time"
7+
)
8+
9+
type Event struct {
10+
ID string `json:"id"`
11+
}
12+
13+
func NewEvent() *Event {
14+
return &Event{
15+
ID: time.Now().String(),
16+
}
17+
}
18+
19+
// HelloEvent simple method that just formats message.
20+
func (e *Event) HelloEvent() string {
21+
return fmt.Sprintf("Simple HelloEvent")
22+
}
23+
24+
// HelloEventWithParameter accepts one param that got formated in message.
25+
func (e *Event) HelloEventWithParameter(param string) (string, error) {
26+
if param == "" {
27+
return "", errors.New("no param provided")
28+
}
29+
30+
return fmt.Sprintf("HelloEventWithParameter: %v", param), nil
31+
}

0 commit comments

Comments
 (0)