@@ -2,18 +2,22 @@ package main
22
33import (
44 "context"
5+ "io/ioutil"
56 "testing"
7+ c "testsuites/collector"
68
79 "github.com/stretchr/testify/assert"
10+ y "gopkg.in/yaml.v2"
811)
912
10- func TestExtractPublicMethodsFromFile (t * testing.T ) {
11- fnsAnn , err := extractPublicMethodsFromFile (context .Background (), "./mocks/event.go" )
13+ func TestCrawlSingleFileForFunctions (t * testing.T ) {
14+ ctx := context .Background ()
15+ fnsAnn , err := crawlSingleFileForFunctions (ctx , "./mocks/event.go" )
1216 if err != nil {
1317 t .Errorf ("got error: %v" , err .Error ())
1418 }
1519
16- if len (fnsAnn ) != 2 {
20+ if len (fnsAnn ) != 4 {
1721 t .Errorf ("got %q, expected %q methods" , len (fnsAnn ), 2 )
1822 }
1923
@@ -24,4 +28,52 @@ func TestExtractPublicMethodsFromFile(t *testing.T) {
2428 assert .Equal (t , "HelloEventWithParameter" , fnsAnn [1 ].Name )
2529 assert .Equal (t , "(param string)" , fnsAnn [1 ].InputParams )
2630 assert .Equal (t , "(string, error)" , fnsAnn [1 ].ReturnValues )
31+
32+ assert .Equal (t , "FunctionWithoutParameters" , fnsAnn [2 ].Name )
33+ assert .Equal (t , "()" , fnsAnn [2 ].InputParams )
34+ assert .Equal (t , "" , fnsAnn [2 ].ReturnValues )
35+
36+ assert .Equal (t , "FunctionWithPointerReturnValue" , fnsAnn [3 ].Name )
37+ assert .Equal (t , "()" , fnsAnn [3 ].InputParams )
38+ assert .Equal (t , "*Event" , fnsAnn [3 ].ReturnValues )
39+ }
40+
41+ func TestMakeYAML (t * testing.T ) {
42+
43+ yaml := []c.FunctionAnnotation {
44+ {
45+ ID : 1 ,
46+ Name : "SomeName" ,
47+ InputParams : "(ctx context.Context, param Parameters)" ,
48+ ReturnValues : "error" ,
49+ Description : "" ,
50+ Public : true ,
51+ },
52+ {
53+ ID : 2 ,
54+ Name : "SomeName2" ,
55+ InputParams : "(ctx context.Context, param2 Parameters2)" ,
56+ ReturnValues : "error" ,
57+ Description : "" ,
58+ Public : true ,
59+ },
60+ }
61+
62+ yamlData , err := y .Marshal (& yaml )
63+ if err != nil {
64+ t .Errorf ("got error: %v" , err .Error ())
65+ }
66+ fileName := "test.yaml"
67+ err = ioutil .WriteFile (fileName , yamlData , 0644 )
68+ if err != nil {
69+ panic ("Unable to write data into the file" )
70+ }
71+
72+ bytes , err := ioutil .ReadFile (fileName )
73+ if err != nil {
74+ t .Errorf ("got error: %v" , err .Error ())
75+ }
76+
77+ assert .Equal (t , yamlData , bytes )
78+
2779}
0 commit comments