github.com/sandwich-go/boost@v1.3.29/misc/goformat/func_test.go (about)

     1  package goformat
     2  
     3  import (
     4  	. "github.com/smartystreets/goconvey/convey"
     5  	"go/ast"
     6  	"go/parser"
     7  	"go/token"
     8  	"testing"
     9  )
    10  
    11  func TestFunc(t *testing.T) {
    12  	var tests = []struct {
    13  		src                           string
    14  		hasSingleReturnValueFuncCount int
    15  	}{
    16  		{
    17  			src: `package c
    18  		func a() (error) { return nil }`,
    19  		},
    20  		{
    21  			src: `package c
    22  		func a() (int, error) { return b() }
    23  		func b() (int, error) { return 0, nil }`,
    24  		},
    25  		{
    26  			src: `package c
    27  		func a() (error) { return b() }
    28  		func b() (error) { return nil }`,
    29  			hasSingleReturnValueFuncCount: 1,
    30  		},
    31  		{
    32  			src: `package c
    33  		func d() (error) { return b() }
    34  		func a() (error) { return b() }
    35  		func b() (error) { return nil }`,
    36  			hasSingleReturnValueFuncCount: 2,
    37  		},
    38  		{
    39  			src: `package c
    40  func a() (int, error) { return b() }
    41  func b() (error) { return nil }`,
    42  			hasSingleReturnValueFuncCount: 1,
    43  		},
    44  	}
    45  	Convey("func return value count", t, func() {
    46  		for _, test := range tests {
    47  			f, err := parser.ParseFile(token.NewFileSet(), "", test.src, 0)
    48  			So(err, ShouldBeNil)
    49  			returns := map[*ast.ReturnStmt]*ast.FuncType{}
    50  			ast.Walk(visitor{returns: returns}, f)
    51  
    52  			var count int
    53  			for ret := range returns {
    54  				if len(ret.Results) == 0 {
    55  					continue
    56  				}
    57  				if e, ok := ret.Results[0].(*ast.CallExpr); ok && hasSingleCallReturnVal(e) {
    58  					count++
    59  				}
    60  			}
    61  			So(count, ShouldEqual, test.hasSingleReturnValueFuncCount)
    62  		}
    63  	})
    64  
    65  	Convey("fill return values", t, func() {
    66  		for _, test := range tests {
    67  			f, err := parser.ParseFile(token.NewFileSet(), "", test.src, 0)
    68  			So(err, ShouldBeNil)
    69  			err = fillReturnValues(f)
    70  			So(err, ShouldBeNil)
    71  
    72  			returns := map[*ast.ReturnStmt]*ast.FuncType{}
    73  			ast.Walk(visitor{returns: returns}, f)
    74  
    75  			for ret, ftRet := range returns {
    76  				if len(ret.Results) == 0 || ftRet.Results == nil {
    77  					continue
    78  				}
    79  				if e, ok := ret.Results[0].(*ast.CallExpr); ok {
    80  					if !hasSingleCallReturnVal(e) {
    81  						continue
    82  					}
    83  				}
    84  				So(len(ret.Results), ShouldEqual, len(ftRet.Results.List))
    85  			}
    86  		}
    87  	})
    88  
    89  	Convey("remove bare returns", t, func() {
    90  		for _, test := range []struct {
    91  			src string
    92  		}{
    93  			{
    94  				src: `package c
    95  func b() (err error) { return  }`,
    96  			},
    97  		} {
    98  			f, err := parser.ParseFile(token.NewFileSet(), "", test.src, 0)
    99  			So(err, ShouldBeNil)
   100  			err = removeBareReturns(f)
   101  			So(err, ShouldBeNil)
   102  
   103  			returns := map[*ast.ReturnStmt]*ast.FuncType{}
   104  			ast.Walk(visitor{returns: returns}, f)
   105  
   106  			for ret, ftRet := range returns {
   107  				if ftRet.Results == nil {
   108  					continue
   109  				}
   110  				So(len(ret.Results), ShouldEqual, len(ftRet.Results.List))
   111  			}
   112  		}
   113  	})
   114  
   115  	Convey("contains main func", t, func() {
   116  		for _, test := range []struct {
   117  			src      string
   118  			contains bool
   119  		}{
   120  			{
   121  				src: `package c
   122  func b() (err error) { return  }`,
   123  			},
   124  			{
   125  				src: `package c
   126  func main() (err error) { return  }`,
   127  			},
   128  			{
   129  				src: `package c
   130  func main() { return  }`, contains: true,
   131  			},
   132  		} {
   133  			f, err := parser.ParseFile(token.NewFileSet(), "", test.src, 0)
   134  			So(err, ShouldBeNil)
   135  			So(containsMainFunc(f), ShouldEqual, test.contains)
   136  		}
   137  	})
   138  }