github.com/unionj-cloud/go-doudou@v1.3.8-0.20221011095552-0088008e5b31/cmd/internal/name/exec_test.go (about)

     1  package name
     2  
     3  import (
     4  	"github.com/stretchr/testify/assert"
     5  	"github.com/unionj-cloud/go-doudou/toolkit/pathutils"
     6  	"io/ioutil"
     7  	"os"
     8  	"testing"
     9  )
    10  
    11  const initCode = `package testdata
    12  
    13  // 筛选条件
    14  type PageFilter struct {
    15  	// 真实姓名,前缀匹配
    16  	Name string
    17  	// 所属部门ID
    18  	Dept int
    19  }
    20  
    21  //排序条件
    22  type Order struct {
    23  	Col  string	` + "`" + `json:"-"` + "`" + `
    24  	sort string
    25  }
    26  
    27  type PageRet struct {
    28  	Items    interface{}
    29  	PageNo   int
    30  	PageSize int
    31  	Total    int
    32  	HasNext  bool
    33  }
    34  
    35  type Field struct {
    36  	Name   string
    37  	Type   string
    38  	Format string
    39  }
    40  
    41  type Base struct {
    42  	Index string
    43  	Type  string
    44  }
    45  
    46  type MappingPayload struct {
    47  	Base
    48  	Fields []Field
    49  	Index  string
    50  }
    51  `
    52  
    53  func TestName_Exec(t *testing.T) {
    54  	type fields struct {
    55  		File      string
    56  		Strategy  string
    57  		Omitempty bool
    58  	}
    59  	tests := []struct {
    60  		name     string
    61  		fields   fields
    62  		want     string
    63  		initCode string
    64  	}{
    65  		{
    66  			name: "",
    67  			fields: fields{
    68  				File:     pathutils.Abs("testdata/vo.go"),
    69  				Strategy: lowerCamelStrategy,
    70  			},
    71  			initCode: initCode,
    72  			want: `package testdata
    73  
    74  // 筛选条件
    75  type PageFilter struct {
    76  	// 真实姓名,前缀匹配
    77  	Name string ` + "`" + `json:"name"` + "`" + `
    78  	// 所属部门ID
    79  	Dept int ` + "`" + `json:"dept"` + "`" + `
    80  }
    81  
    82  //排序条件
    83  type Order struct {
    84  	Col  string ` + "`" + `json:"-"` + "`" + `
    85  	sort string
    86  }
    87  
    88  type PageRet struct {
    89  	Items    interface{} ` + "`" + `json:"items"` + "`" + `
    90  	PageNo   int         ` + "`" + `json:"pageNo"` + "`" + `
    91  	PageSize int         ` + "`" + `json:"pageSize"` + "`" + `
    92  	Total    int         ` + "`" + `json:"total"` + "`" + `
    93  	HasNext  bool        ` + "`" + `json:"hasNext"` + "`" + `
    94  }
    95  
    96  type Field struct {
    97  	Name   string ` + "`" + `json:"name"` + "`" + `
    98  	Type   string ` + "`" + `json:"type"` + "`" + `
    99  	Format string ` + "`" + `json:"format"` + "`" + `
   100  }
   101  
   102  type Base struct {
   103  	Index string ` + "`" + `json:"index"` + "`" + `
   104  	Type  string ` + "`" + `json:"type"` + "`" + `
   105  }
   106  
   107  type MappingPayload struct {
   108  	Base
   109  	Fields []Field ` + "`" + `json:"fields"` + "`" + `
   110  	Index  string  ` + "`" + `json:"index"` + "`" + `
   111  }
   112  `,
   113  		},
   114  		{
   115  			name: "",
   116  			fields: fields{
   117  				File:      pathutils.Abs("testdata/vo.go"),
   118  				Strategy:  lowerCamelStrategy,
   119  				Omitempty: true,
   120  			},
   121  			initCode: initCode,
   122  			want: `package testdata
   123  
   124  // 筛选条件
   125  type PageFilter struct {
   126  	// 真实姓名,前缀匹配
   127  	Name string ` + "`" + `json:"name,omitempty"` + "`" + `
   128  	// 所属部门ID
   129  	Dept int ` + "`" + `json:"dept,omitempty"` + "`" + `
   130  }
   131  
   132  //排序条件
   133  type Order struct {
   134  	Col  string ` + "`" + `json:"-"` + "`" + `
   135  	sort string
   136  }
   137  
   138  type PageRet struct {
   139  	Items    interface{} ` + "`" + `json:"items,omitempty"` + "`" + `
   140  	PageNo   int         ` + "`" + `json:"pageNo,omitempty"` + "`" + `
   141  	PageSize int         ` + "`" + `json:"pageSize,omitempty"` + "`" + `
   142  	Total    int         ` + "`" + `json:"total,omitempty"` + "`" + `
   143  	HasNext  bool        ` + "`" + `json:"hasNext,omitempty"` + "`" + `
   144  }
   145  
   146  type Field struct {
   147  	Name   string ` + "`" + `json:"name,omitempty"` + "`" + `
   148  	Type   string ` + "`" + `json:"type,omitempty"` + "`" + `
   149  	Format string ` + "`" + `json:"format,omitempty"` + "`" + `
   150  }
   151  
   152  type Base struct {
   153  	Index string ` + "`" + `json:"index,omitempty"` + "`" + `
   154  	Type  string ` + "`" + `json:"type,omitempty"` + "`" + `
   155  }
   156  
   157  type MappingPayload struct {
   158  	Base
   159  	Fields []Field ` + "`" + `json:"fields,omitempty"` + "`" + `
   160  	Index  string  ` + "`" + `json:"index,omitempty"` + "`" + `
   161  }
   162  `,
   163  		},
   164  		{
   165  			name: "",
   166  			fields: fields{
   167  				File:      pathutils.Abs("testdata/vo.go"),
   168  				Strategy:  snakeStrategy,
   169  				Omitempty: false,
   170  			},
   171  			initCode: initCode,
   172  			want: `package testdata
   173  
   174  // 筛选条件
   175  type PageFilter struct {
   176  	// 真实姓名,前缀匹配
   177  	Name string ` + "`" + `json:"name"` + "`" + `
   178  	// 所属部门ID
   179  	Dept int ` + "`" + `json:"dept"` + "`" + `
   180  }
   181  
   182  //排序条件
   183  type Order struct {
   184  	Col  string ` + "`" + `json:"-"` + "`" + `
   185  	sort string
   186  }
   187  
   188  type PageRet struct {
   189  	Items    interface{} ` + "`" + `json:"items"` + "`" + `
   190  	PageNo   int         ` + "`" + `json:"page_no"` + "`" + `
   191  	PageSize int         ` + "`" + `json:"page_size"` + "`" + `
   192  	Total    int         ` + "`" + `json:"total"` + "`" + `
   193  	HasNext  bool        ` + "`" + `json:"has_next"` + "`" + `
   194  }
   195  
   196  type Field struct {
   197  	Name   string ` + "`" + `json:"name"` + "`" + `
   198  	Type   string ` + "`" + `json:"type"` + "`" + `
   199  	Format string ` + "`" + `json:"format"` + "`" + `
   200  }
   201  
   202  type Base struct {
   203  	Index string ` + "`" + `json:"index"` + "`" + `
   204  	Type  string ` + "`" + `json:"type"` + "`" + `
   205  }
   206  
   207  type MappingPayload struct {
   208  	Base
   209  	Fields []Field ` + "`" + `json:"fields"` + "`" + `
   210  	Index  string  ` + "`" + `json:"index"` + "`" + `
   211  }
   212  `,
   213  		},
   214  		{
   215  			name: "",
   216  			fields: fields{
   217  				File:      pathutils.Abs("testdata/vo1.go"),
   218  				Strategy:  snakeStrategy,
   219  				Omitempty: false,
   220  			},
   221  			initCode: `package testdata
   222  
   223  import "time"
   224  
   225  // comment for alia age
   226  type age int
   227  
   228  type Event struct {
   229  	Name      string
   230  	EventType int
   231  }
   232  
   233  type TestAlias struct {
   234  	Age    age
   235  	School []struct {
   236  		Name string
   237  		Addr struct {
   238  			Zip   string
   239  			Block string
   240  			Full  string
   241  		}
   242  	}
   243  	EventChan chan Event
   244  	SigChan   chan int
   245  	Callback  func(string) bool
   246  	CallbackN func(param string) bool
   247  }
   248  
   249  type ta TestAlias
   250  
   251  type tt time.Time
   252  
   253  type mm map[string]interface{}
   254  
   255  type MyInter interface {
   256  	Speak() error
   257  }
   258  
   259  type starM *time.Time
   260  `,
   261  			want: `package testdata
   262  
   263  import "time"
   264  
   265  // comment for alia age
   266  type age int
   267  
   268  type Event struct {
   269  	Name      string ` + "`" + `json:"name"` + "`" + `
   270  	EventType int    ` + "`" + `json:"event_type"` + "`" + `
   271  }
   272  
   273  type TestAlias struct {
   274  	Age    age ` + "`" + `json:"age"` + "`" + `
   275  	School []struct {
   276  		Name string ` + "`" + `json:"name"` + "`" + `
   277  		Addr struct {
   278  			Zip   string ` + "`" + `json:"zip"` + "`" + `
   279  			Block string ` + "`" + `json:"block"` + "`" + `
   280  			Full  string ` + "`" + `json:"full"` + "`" + `
   281  		} ` + "`" + `json:"addr"` + "`" + `
   282  	} ` + "`" + `json:"school"` + "`" + `
   283  	EventChan chan Event              ` + "`" + `json:"event_chan"` + "`" + `
   284  	SigChan   chan int                ` + "`" + `json:"sig_chan"` + "`" + `
   285  	Callback  func(string) bool       ` + "`" + `json:"callback"` + "`" + `
   286  	CallbackN func(param string) bool ` + "`" + `json:"callback_n"` + "`" + `
   287  }
   288  
   289  type ta TestAlias
   290  
   291  type tt time.Time
   292  
   293  type mm map[string]interface{}
   294  
   295  type MyInter interface {
   296  	Speak() error
   297  }
   298  
   299  type starM *time.Time
   300  `,
   301  		},
   302  	}
   303  	for _, tt := range tests {
   304  		t.Run(tt.name, func(t *testing.T) {
   305  			receiver := Name{
   306  				File:      tt.fields.File,
   307  				Strategy:  tt.fields.Strategy,
   308  				Omitempty: tt.fields.Omitempty,
   309  			}
   310  			receiver.Exec()
   311  			f, err := os.Open(tt.fields.File)
   312  			if err != nil {
   313  				t.Fatal(err)
   314  			}
   315  			content, err := ioutil.ReadAll(f)
   316  			if err != nil {
   317  				t.Fatal(err)
   318  			}
   319  			assert.Equal(t, tt.want, string(content))
   320  			ioutil.WriteFile(tt.fields.File, []byte(tt.initCode), os.ModePerm)
   321  		})
   322  	}
   323  }
   324  
   325  func TestPanic(t *testing.T) {
   326  	receiver := Name{
   327  		File:      pathutils.Abs("testdata/vo1.go"),
   328  		Strategy:  "unknownstrategy",
   329  		Omitempty: false,
   330  	}
   331  	assert.Panics(t, func() {
   332  		receiver.Exec()
   333  	})
   334  }
   335  
   336  func TestPanic2(t *testing.T) {
   337  	receiver := Name{
   338  		File:      pathutils.Abs("testdata/vo2"),
   339  		Strategy:  lowerCamelStrategy,
   340  		Omitempty: false,
   341  	}
   342  	assert.Panics(t, func() {
   343  		receiver.Exec()
   344  	})
   345  }