github.com/unionj-cloud/go-doudou/v2@v2.3.5/toolkit/astutils/funcs_test.go (about)

     1  package astutils
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/davecgh/go-spew/spew"
     6  	"github.com/sirupsen/logrus"
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  	"github.com/unionj-cloud/go-doudou/v2/toolkit/pathutils"
    10  	"github.com/unionj-cloud/go-doudou/v2/toolkit/stringutils"
    11  	"go/ast"
    12  	"go/parser"
    13  	"go/token"
    14  	"io/ioutil"
    15  	"os"
    16  	"path/filepath"
    17  	"testing"
    18  )
    19  
    20  func TestFixImport(t *testing.T) {
    21  	code := `package main
    22  
    23  import (
    24  	"encoding/json"
    25  	"fmt"
    26  )
    27  
    28  type UserVo struct {
    29  					Id    int
    30  					Name  string
    31  	Phone string
    32  	Dept  string
    33  }
    34  
    35  type Page struct {
    36  	PageNo int
    37  Size   int
    38  Items  []UserVo
    39  }
    40  
    41  func main() {
    42  	page := Page{
    43  	PageNo: 10,
    44  	Size:   30,
    45  }
    46  b, _ := json.Marshal(page)
    47  fmt.Println(string(b))
    48  }
    49  `
    50  	expect := `package main
    51  
    52  import (
    53  	"encoding/json"
    54  	"fmt"
    55  )
    56  
    57  type UserVo struct {
    58  	Id    int
    59  	Name  string
    60  	Phone string
    61  	Dept  string
    62  }
    63  
    64  type Page struct {
    65  	PageNo int
    66  	Size   int
    67  	Items  []UserVo
    68  }
    69  
    70  func main() {
    71  	page := Page{
    72  		PageNo: 10,
    73  		Size:   30,
    74  	}
    75  	b, _ := json.Marshal(page)
    76  	fmt.Println(string(b))
    77  }
    78  `
    79  	file := pathutils.Abs("testdata/output.go")
    80  	FixImport([]byte(code), file)
    81  	f, err := os.Open(file)
    82  	if err != nil {
    83  		t.Fatal(err)
    84  	}
    85  	got, err := ioutil.ReadAll(f)
    86  	if err != nil {
    87  		t.Fatal(err)
    88  	}
    89  	assert.Equal(t, expect, string(got))
    90  }
    91  
    92  func TestMethodMeta_String(t *testing.T) {
    93  	type fields struct {
    94  		Recv     string
    95  		Name     string
    96  		Params   []FieldMeta
    97  		Results  []FieldMeta
    98  		Comments []string
    99  	}
   100  	tests := []struct {
   101  		name   string
   102  		fields fields
   103  		want   string
   104  		panic  bool
   105  	}{
   106  		{
   107  			name: "",
   108  			fields: fields{
   109  				Recv: "handler",
   110  				Name: "HandleEvent",
   111  				Params: []FieldMeta{
   112  					{
   113  						Name:     "ctx",
   114  						Type:     "context.Context",
   115  						Tag:      "",
   116  						Comments: nil,
   117  						IsExport: false,
   118  						DocName:  "",
   119  					},
   120  					{
   121  						Name:     "etype",
   122  						Type:     "int",
   123  						Tag:      "",
   124  						Comments: nil,
   125  						IsExport: false,
   126  						DocName:  "",
   127  					},
   128  					{
   129  						Name:     "uid",
   130  						Type:     "string",
   131  						Tag:      "",
   132  						Comments: nil,
   133  						IsExport: false,
   134  						DocName:  "",
   135  					},
   136  				},
   137  				Results: []FieldMeta{
   138  					{
   139  						Name:     "",
   140  						Type:     "bool",
   141  						Tag:      "",
   142  						Comments: nil,
   143  						IsExport: false,
   144  						DocName:  "",
   145  					},
   146  					{
   147  						Name:     "",
   148  						Type:     "error",
   149  						Tag:      "",
   150  						Comments: nil,
   151  						IsExport: false,
   152  						DocName:  "",
   153  					},
   154  				},
   155  				Comments: nil,
   156  			},
   157  			want: "func (receiver handler) HandleEvent(ctx context.Context, etype int, uid string) (bool, error)",
   158  		},
   159  		{
   160  			name: "",
   161  			fields: fields{
   162  				Recv: "",
   163  				Name: "HandleEvent",
   164  				Params: []FieldMeta{
   165  					{
   166  						Name:     "ctx",
   167  						Type:     "context.Context",
   168  						Tag:      "",
   169  						Comments: nil,
   170  						IsExport: false,
   171  						DocName:  "",
   172  					},
   173  					{
   174  						Name:     "etype",
   175  						Type:     "int",
   176  						Tag:      "",
   177  						Comments: nil,
   178  						IsExport: false,
   179  						DocName:  "",
   180  					},
   181  					{
   182  						Name:     "uid",
   183  						Type:     "string",
   184  						Tag:      "",
   185  						Comments: nil,
   186  						IsExport: false,
   187  						DocName:  "",
   188  					},
   189  				},
   190  				Results: []FieldMeta{
   191  					{
   192  						Name:     "",
   193  						Type:     "error",
   194  						Tag:      "",
   195  						Comments: nil,
   196  						IsExport: false,
   197  						DocName:  "",
   198  					},
   199  				},
   200  				Comments: nil,
   201  			},
   202  			want: "func HandleEvent(ctx context.Context, etype int, uid string) error",
   203  		},
   204  		{
   205  			name: "",
   206  			fields: fields{
   207  				Recv: "",
   208  				Name: "",
   209  				Params: []FieldMeta{
   210  					{
   211  						Name:     "etype",
   212  						Type:     "int",
   213  						Tag:      "",
   214  						Comments: nil,
   215  						IsExport: false,
   216  						DocName:  "",
   217  					},
   218  					{
   219  						Name:     "uid",
   220  						Type:     "string",
   221  						Tag:      "",
   222  						Comments: nil,
   223  						IsExport: false,
   224  						DocName:  "",
   225  					},
   226  				},
   227  				Results: []FieldMeta{
   228  					{
   229  						Name:     "",
   230  						Type:     "error",
   231  						Tag:      "",
   232  						Comments: nil,
   233  						IsExport: false,
   234  						DocName:  "",
   235  					},
   236  				},
   237  				Comments: nil,
   238  			},
   239  			want: "func(etype int, uid string) error",
   240  		},
   241  		{
   242  			name: "",
   243  			fields: fields{
   244  				Recv: "PlaceHolder",
   245  				Name: "",
   246  				Params: []FieldMeta{
   247  					{
   248  						Name:     "etype",
   249  						Type:     "int",
   250  						Tag:      "",
   251  						Comments: nil,
   252  						IsExport: false,
   253  						DocName:  "",
   254  					},
   255  					{
   256  						Name:     "uid",
   257  						Type:     "string",
   258  						Tag:      "",
   259  						Comments: nil,
   260  						IsExport: false,
   261  						DocName:  "",
   262  					},
   263  				},
   264  				Results: []FieldMeta{
   265  					{
   266  						Name:     "",
   267  						Type:     "error",
   268  						Tag:      "",
   269  						Comments: nil,
   270  						IsExport: false,
   271  						DocName:  "",
   272  					},
   273  				},
   274  				Comments: nil,
   275  			},
   276  			want:  "",
   277  			panic: true,
   278  		},
   279  	}
   280  	for _, tt := range tests {
   281  		t.Run(tt.name, func(t *testing.T) {
   282  			mm := MethodMeta{
   283  				Recv:     tt.fields.Recv,
   284  				Name:     tt.fields.Name,
   285  				Params:   tt.fields.Params,
   286  				Results:  tt.fields.Results,
   287  				Comments: tt.fields.Comments,
   288  			}
   289  			if tt.panic {
   290  				assert.Panics(t, func() {
   291  					mm.String()
   292  				})
   293  			} else {
   294  				if got := mm.String(); stringutils.IsNotEmpty(tt.want) && got != tt.want {
   295  					t.Errorf("String() = %v, want %v", got, tt.want)
   296  				}
   297  			}
   298  		})
   299  	}
   300  }
   301  
   302  func TestVisit(t *testing.T) {
   303  	testDir := pathutils.Abs("./testdata")
   304  	vodir := filepath.Join(testDir, "vo")
   305  	var files []string
   306  	err := filepath.Walk(vodir, Visit(&files))
   307  	if err != nil {
   308  		logrus.Panicln(err)
   309  	}
   310  	assert.Len(t, files, 1)
   311  }
   312  
   313  func TestNewMethodMeta(t *testing.T) {
   314  	file := pathutils.Abs("testdata/cat.go")
   315  	fset := token.NewFileSet()
   316  	root, err := parser.ParseFile(fset, file, nil, parser.ParseComments)
   317  	if err != nil {
   318  		panic(err)
   319  	}
   320  	sc := NewStructCollector(ExprString)
   321  	ast.Walk(sc, root)
   322  	spew.Dump(root)
   323  }
   324  
   325  func TestGetImportStatements(t *testing.T) {
   326  	input := `import (
   327  	"context"
   328  	"encoding/json"
   329  	"fmt"
   330  	"github.com/sirupsen/logrus"
   331  	v3 "github.com/unionj-cloud/go-doudou/v2/toolkit/openapi/v3"
   332  	"github.com/unionj-cloud/go-doudou/v2/framework/rest"
   333  	"github.com/unionj-cloud/go-doudou/v2/toolkit/cast"
   334  	{{.ServiceAlias}} "{{.ServicePackage}}"
   335  	"net/http"
   336  	"github.com/pkg/errors"
   337  )`
   338  	ret := GetImportStatements([]byte(input))
   339  	fmt.Println(string(ret))
   340  }
   341  
   342  func TestAppendImportStatements(t *testing.T) {
   343  	input := `import (
   344  	"context"
   345  	"encoding/json"
   346  	"fmt"
   347  
   348  	"github.com/sirupsen/logrus"
   349  	v3 "github.com/unionj-cloud/go-doudou/v2/toolkit/openapi/v3"
   350  	"github.com/unionj-cloud/go-doudou/v2/framework/rest"
   351  	"github.com/unionj-cloud/go-doudou/v2/toolkit/cast"
   352  
   353  	{{.ServiceAlias}} "{{.ServicePackage}}"
   354  	"net/http"
   355  )
   356  
   357  type UsersvcHandlerImpl struct {
   358  	usersvc service.Usersvc
   359  }
   360  `
   361  	ret := AppendImportStatements([]byte(input), []byte(`
   362  	"context"
   363  	"encoding/json"
   364  	"fmt"
   365  	"github.com/pkg/errors"
   366  `))
   367  
   368  	ret = AppendImportStatements(ret, []byte(`
   369  	"context"
   370  	"encoding/json"
   371  	"fmt"
   372  	"github.com/pkg/errors"
   373  `))
   374  
   375  	ret = AppendImportStatements(ret, []byte(`
   376  	"context"
   377  	"encoding/json"
   378  	"fmt"
   379  	"github.com/pkg/errors"
   380  `))
   381  
   382  	expected := `import (
   383  	"context"
   384  	"encoding/json"
   385  	"fmt"
   386  
   387  	"github.com/sirupsen/logrus"
   388  	v3 "github.com/unionj-cloud/go-doudou/v2/toolkit/openapi/v3"
   389  	"github.com/unionj-cloud/go-doudou/v2/framework/rest"
   390  	"github.com/unionj-cloud/go-doudou/v2/toolkit/cast"
   391  
   392  	{{.ServiceAlias}} "{{.ServicePackage}}"
   393  	"net/http"
   394  
   395  	"github.com/pkg/errors"
   396  )
   397  
   398  type UsersvcHandlerImpl struct {
   399  	usersvc service.Usersvc
   400  }
   401  `
   402  
   403  	fmt.Println(string(ret))
   404  	require.Equal(t, expected, string(ret))
   405  }
   406  
   407  func TestAppendImportStatements1(t *testing.T) {
   408  	input := `import ()`
   409  	ret := AppendImportStatements([]byte(input), []byte(`
   410  	"context"
   411  	"encoding/json"
   412  	"fmt"
   413  	"github.com/pkg/errors"
   414  `))
   415  	expected := `import (
   416  	"context"
   417  	"encoding/json"
   418  	"fmt"
   419  	"github.com/pkg/errors"
   420  )`
   421  
   422  	fmt.Println(string(ret))
   423  	require.Equal(t, expected, string(ret))
   424  }
   425  
   426  func TestAppendImportStatements2(t *testing.T) {
   427  	input := `import ()`
   428  	ret := AppendImportStatements([]byte(input), []byte(`
   429  
   430  `))
   431  	expected := input
   432  
   433  	fmt.Println(string(ret))
   434  	require.Equal(t, expected, string(ret))
   435  }
   436  
   437  func ExampleGetAnnotations() {
   438  	ret := GetAnnotations(`// <b style="color: red">NEW</b> 删除数据接口(不删数据文件)@role(SUPER_ADMIN)@permission(create,update)这是几个注解@sss()`)
   439  	fmt.Println(ret)
   440  	// Output:
   441  	// [{@role [SUPER_ADMIN]} {@permission [create update]} {@sss []}]
   442  }
   443  
   444  func TestGrpcRelatedModify(t *testing.T) {
   445  	input := `
   446  var _ Helloworld = (*HelloworldImpl)(nil)
   447  
   448  type HelloworldImpl struct {
   449  	conf *config.Config
   450  }
   451  
   452  func (receiver *HelloworldImpl) Greeting(ctx context.Context, greeting string) (data string, err error) {
   453  	var _result struct {
   454  		Data string
   455  	}
   456  	_ = gofakeit.Struct(&_result)
   457  	return _result.Data, nil
   458  }
   459  `
   460  	ret := GrpcRelatedModify([]byte(input), "Helloworld", "HelloworldRpc")
   461  	fmt.Println(string(ret))
   462  }