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

     1  package astutils
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/stretchr/testify/assert"
     6  	"github.com/unionj-cloud/go-doudou/v2/toolkit/pathutils"
     7  	"go/ast"
     8  	"go/parser"
     9  	"go/token"
    10  	"testing"
    11  )
    12  
    13  func TestInterfaceCollector(t *testing.T) {
    14  	file := pathutils.Abs("testdata/svc.go")
    15  	fset := token.NewFileSet()
    16  	root, err := parser.ParseFile(fset, file, nil, parser.ParseComments)
    17  	if err != nil {
    18  		panic(err)
    19  	}
    20  	//spew.Dump(root)
    21  	sc := NewInterfaceCollector(ExprString)
    22  	sc.cmap = ast.NewCommentMap(fset, root, root.Comments)
    23  	ast.Walk(sc, root)
    24  	fmt.Println(sc)
    25  }
    26  
    27  func TestBuildInterfaceCollector(t *testing.T) {
    28  	file := pathutils.Abs("testdata/svc.go")
    29  	ic := BuildInterfaceCollector(file, ExprString)
    30  	assert.NotNil(t, ic)
    31  }
    32  
    33  func Test_pattern(t *testing.T) {
    34  	type args struct {
    35  		method string
    36  	}
    37  	tests := []struct {
    38  		name string
    39  		args args
    40  		want string
    41  	}{
    42  		{
    43  			name: "1",
    44  			args: args{
    45  				method: "GetBooks",
    46  			},
    47  			want: "books",
    48  		},
    49  		{
    50  			name: "2",
    51  			args: args{
    52  				method: "PageUsers",
    53  			},
    54  			want: "page/users",
    55  		},
    56  		{
    57  			name: "",
    58  			args: args{
    59  				method: "Get",
    60  			},
    61  			want: "",
    62  		},
    63  		{
    64  			name: "",
    65  			args: args{
    66  				method: "GetShelves_ShelfBooks_Book",
    67  			},
    68  			want: "shelves/:shelf/books/:book",
    69  		},
    70  		{
    71  			name: "",
    72  			args: args{
    73  				method: "Goodfood_BigappleBooks_Mybird",
    74  			},
    75  			want: "goodfood/:bigapple/books/:mybird",
    76  		},
    77  		{
    78  			name: "",
    79  			args: args{
    80  				method: "ApiV1Query_range",
    81  			},
    82  			want: "api/v1/query_range",
    83  		},
    84  		{
    85  			name: "",
    86  			args: args{
    87  				method: "GetQuery_range",
    88  			},
    89  			want: "query_range",
    90  		},
    91  		{
    92  			name: "",
    93  			args: args{
    94  				method: "GetQuery",
    95  			},
    96  			want: "query",
    97  		},
    98  	}
    99  	for _, tt := range tests {
   100  		t.Run(tt.name, func(t *testing.T) {
   101  			_, endpoint := Pattern(tt.args.method)
   102  			assert.Equalf(t, tt.want, endpoint, "pattern(%v)", tt.args.method)
   103  		})
   104  	}
   105  }