github.com/songzhibin97/gkit@v1.2.13/tools/stm/stm_test.go (about)

     1  package stm
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  type testS struct {
    10  	S1 string `stm:"S_1"  stm2:"-"`
    11  	s2 string `stm:"s_2"`
    12  	T1 struct {
    13  		Name string `stm:"name"`
    14  		age  int    `stm:"age" stm2:"-"`
    15  	} `stm:"T_1"`
    16  	t2 struct {
    17  		Gender bool `stm:"gender"`
    18  	} `stm2:"-"`
    19  	List1 []string `stm:"list1"  stm2:"-"`
    20  	list2 []int    `stm:"list2"`
    21  }
    22  
    23  func Test_StructToMapExtraExport(t *testing.T) {
    24  	ts := testS{
    25  		S1: "s1",
    26  		s2: "s2",
    27  		T1: struct {
    28  			Name string `stm:"name"`
    29  			age  int    `stm:"age" stm2:"-"`
    30  		}{
    31  			Name: "name",
    32  			age:  18,
    33  		},
    34  		t2: struct {
    35  			Gender bool `stm:"gender"`
    36  		}{
    37  			Gender: true,
    38  		},
    39  		List1: []string{"1", "2", "3"},
    40  		list2: []int{1, 2, 3},
    41  	}
    42  	type args struct {
    43  		tag string
    44  	}
    45  	tests := []struct {
    46  		name string
    47  		args args
    48  		want map[string]interface{}
    49  	}{
    50  		{
    51  			name: "",
    52  			args: args{
    53  				tag: "",
    54  			},
    55  			want: map[string]interface{}{
    56  				"S1": "s1",
    57  				"s2": "s2",
    58  				"T1": map[string]interface{}{
    59  					"Name": "name",
    60  					"age":  18,
    61  				},
    62  				"t2": map[string]interface{}{
    63  					"Gender": true,
    64  				},
    65  				"List1": []string{"1", "2", "3"},
    66  				"list2": []int{1, 2, 3},
    67  			},
    68  		},
    69  		{
    70  			name: "",
    71  			args: args{
    72  				tag: "stm",
    73  			},
    74  			want: map[string]interface{}{
    75  				"S_1": "s1",
    76  				"s_2": "s2",
    77  				"T_1": map[string]interface{}{
    78  					"name": "name",
    79  					"age":  18,
    80  				},
    81  				"t2": map[string]interface{}{
    82  					"gender": true,
    83  				},
    84  				"list1": []string{"1", "2", "3"},
    85  				"list2": []int{1, 2, 3},
    86  			},
    87  		},
    88  		{
    89  			name: "",
    90  			args: args{
    91  				tag: "stm2",
    92  			},
    93  			want: map[string]interface{}{
    94  				"s2": "s2",
    95  				"T1": map[string]interface{}{
    96  					"Name": "name",
    97  				},
    98  				"list2": []int{1, 2, 3},
    99  			},
   100  		},
   101  	}
   102  	for _, tt := range tests {
   103  		t.Run(tt.name, func(t *testing.T) {
   104  			assert.Equal(t, StructToMapExtraExport(ts, tt.args.tag), tt.want)
   105  		})
   106  	}
   107  }
   108  
   109  func Test_StructToMap(t *testing.T) {
   110  	ts := testS{
   111  		S1: "s1",
   112  		s2: "s2",
   113  		T1: struct {
   114  			Name string `stm:"name"`
   115  			age  int    `stm:"age" stm2:"-"`
   116  		}{
   117  			Name: "name",
   118  			age:  18,
   119  		},
   120  		t2: struct {
   121  			Gender bool `stm:"gender"`
   122  		}{
   123  			Gender: true,
   124  		},
   125  		List1: []string{"1", "2", "3"},
   126  		list2: []int{1, 2, 3},
   127  	}
   128  	type args struct {
   129  		tag string
   130  	}
   131  	tests := []struct {
   132  		name string
   133  		args args
   134  		want map[string]interface{}
   135  	}{
   136  		{
   137  			name: "",
   138  			args: args{
   139  				tag: "",
   140  			},
   141  			want: map[string]interface{}{
   142  				"S1": "s1",
   143  				"T1": map[string]interface{}{
   144  					"Name": "name",
   145  				},
   146  				"List1": []string{"1", "2", "3"},
   147  			},
   148  		},
   149  		{
   150  			name: "",
   151  			args: args{
   152  				tag: "stm",
   153  			},
   154  			want: map[string]interface{}{
   155  				"S_1": "s1",
   156  				"T_1": map[string]interface{}{
   157  					"name": "name",
   158  				},
   159  				"list1": []string{"1", "2", "3"},
   160  			},
   161  		},
   162  		{
   163  			name: "",
   164  			args: args{
   165  				tag: "stm2",
   166  			},
   167  			want: map[string]interface{}{
   168  				"T1": map[string]interface{}{
   169  					"Name": "name",
   170  				},
   171  			},
   172  		},
   173  	}
   174  	for _, tt := range tests {
   175  		t.Run(tt.name, func(t *testing.T) {
   176  			assert.Equal(t, StructToMap(ts, tt.args.tag), tt.want)
   177  		})
   178  	}
   179  }