github.com/gogf/gf@v1.16.9/util/gutil/gutil_z_unit_list_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gutil_test
     8  
     9  import (
    10  	"github.com/gogf/gf/frame/g"
    11  	"testing"
    12  
    13  	"github.com/gogf/gf/test/gtest"
    14  	"github.com/gogf/gf/util/gutil"
    15  )
    16  
    17  func Test_ListItemValues_Map(t *testing.T) {
    18  	gtest.C(t, func(t *gtest.T) {
    19  		listMap := g.List{
    20  			g.Map{"id": 1, "score": 100},
    21  			g.Map{"id": 2, "score": 99},
    22  			g.Map{"id": 3, "score": 99},
    23  		}
    24  		t.Assert(gutil.ListItemValues(listMap, "id"), g.Slice{1, 2, 3})
    25  		t.Assert(gutil.ListItemValues(listMap, "score"), g.Slice{100, 99, 99})
    26  	})
    27  	gtest.C(t, func(t *gtest.T) {
    28  		listMap := g.List{
    29  			g.Map{"id": 1, "score": 100},
    30  			g.Map{"id": 2, "score": nil},
    31  			g.Map{"id": 3, "score": 0},
    32  		}
    33  		t.Assert(gutil.ListItemValues(listMap, "id"), g.Slice{1, 2, 3})
    34  		t.Assert(gutil.ListItemValues(listMap, "score"), g.Slice{100, nil, 0})
    35  	})
    36  }
    37  
    38  func Test_ListItemValues_Map_SubKey(t *testing.T) {
    39  	type Scores struct {
    40  		Math    int
    41  		English int
    42  	}
    43  	gtest.C(t, func(t *gtest.T) {
    44  		listMap := g.List{
    45  			g.Map{"id": 1, "scores": Scores{100, 60}},
    46  			g.Map{"id": 2, "scores": Scores{0, 100}},
    47  			g.Map{"id": 3, "scores": Scores{59, 99}},
    48  		}
    49  		t.Assert(gutil.ListItemValues(listMap, "scores", "Math"), g.Slice{100, 0, 59})
    50  		t.Assert(gutil.ListItemValues(listMap, "scores", "English"), g.Slice{60, 100, 99})
    51  		t.Assert(gutil.ListItemValues(listMap, "scores", "PE"), g.Slice{})
    52  	})
    53  }
    54  
    55  func Test_ListItemValues_Map_Array_SubKey(t *testing.T) {
    56  	type Scores struct {
    57  		Math    int
    58  		English int
    59  	}
    60  	gtest.C(t, func(t *gtest.T) {
    61  		listMap := g.List{
    62  			g.Map{"id": 1, "scores": []Scores{{1, 2}, {3, 4}}},
    63  			g.Map{"id": 2, "scores": []Scores{{5, 6}, {7, 8}}},
    64  			g.Map{"id": 3, "scores": []Scores{{9, 10}, {11, 12}}},
    65  		}
    66  		t.Assert(gutil.ListItemValues(listMap, "scores", "Math"), g.Slice{1, 3, 5, 7, 9, 11})
    67  		t.Assert(gutil.ListItemValues(listMap, "scores", "English"), g.Slice{2, 4, 6, 8, 10, 12})
    68  		t.Assert(gutil.ListItemValues(listMap, "scores", "PE"), g.Slice{})
    69  	})
    70  }
    71  
    72  func Test_ListItemValues_Struct(t *testing.T) {
    73  	gtest.C(t, func(t *gtest.T) {
    74  		type T struct {
    75  			Id    int
    76  			Score float64
    77  		}
    78  		listStruct := g.Slice{
    79  			T{1, 100},
    80  			T{2, 99},
    81  			T{3, 0},
    82  		}
    83  		t.Assert(gutil.ListItemValues(listStruct, "Id"), g.Slice{1, 2, 3})
    84  		t.Assert(gutil.ListItemValues(listStruct, "Score"), g.Slice{100, 99, 0})
    85  	})
    86  	// Pointer items.
    87  	gtest.C(t, func(t *gtest.T) {
    88  		type T struct {
    89  			Id    int
    90  			Score float64
    91  		}
    92  		listStruct := g.Slice{
    93  			&T{1, 100},
    94  			&T{2, 99},
    95  			&T{3, 0},
    96  		}
    97  		t.Assert(gutil.ListItemValues(listStruct, "Id"), g.Slice{1, 2, 3})
    98  		t.Assert(gutil.ListItemValues(listStruct, "Score"), g.Slice{100, 99, 0})
    99  	})
   100  	// Nil element value.
   101  	gtest.C(t, func(t *gtest.T) {
   102  		type T struct {
   103  			Id    int
   104  			Score interface{}
   105  		}
   106  		listStruct := g.Slice{
   107  			T{1, 100},
   108  			T{2, nil},
   109  			T{3, 0},
   110  		}
   111  		t.Assert(gutil.ListItemValues(listStruct, "Id"), g.Slice{1, 2, 3})
   112  		t.Assert(gutil.ListItemValues(listStruct, "Score"), g.Slice{100, nil, 0})
   113  	})
   114  }
   115  
   116  func Test_ListItemValues_Struct_SubKey(t *testing.T) {
   117  	gtest.C(t, func(t *gtest.T) {
   118  		type Student struct {
   119  			Id    int
   120  			Score float64
   121  		}
   122  		type Class struct {
   123  			Total    int
   124  			Students []Student
   125  		}
   126  		listStruct := g.Slice{
   127  			Class{2, []Student{{1, 1}, {2, 2}}},
   128  			Class{3, []Student{{3, 3}, {4, 4}, {5, 5}}},
   129  			Class{1, []Student{{6, 6}}},
   130  		}
   131  		t.Assert(gutil.ListItemValues(listStruct, "Total"), g.Slice{2, 3, 1})
   132  		t.Assert(gutil.ListItemValues(listStruct, "Students"), `[[{"Id":1,"Score":1},{"Id":2,"Score":2}],[{"Id":3,"Score":3},{"Id":4,"Score":4},{"Id":5,"Score":5}],[{"Id":6,"Score":6}]]`)
   133  		t.Assert(gutil.ListItemValues(listStruct, "Students", "Id"), g.Slice{1, 2, 3, 4, 5, 6})
   134  	})
   135  	gtest.C(t, func(t *gtest.T) {
   136  		type Student struct {
   137  			Id    int
   138  			Score float64
   139  		}
   140  		type Class struct {
   141  			Total    int
   142  			Students []*Student
   143  		}
   144  		listStruct := g.Slice{
   145  			&Class{2, []*Student{{1, 1}, {2, 2}}},
   146  			&Class{3, []*Student{{3, 3}, {4, 4}, {5, 5}}},
   147  			&Class{1, []*Student{{6, 6}}},
   148  		}
   149  		t.Assert(gutil.ListItemValues(listStruct, "Total"), g.Slice{2, 3, 1})
   150  		t.Assert(gutil.ListItemValues(listStruct, "Students"), `[[{"Id":1,"Score":1},{"Id":2,"Score":2}],[{"Id":3,"Score":3},{"Id":4,"Score":4},{"Id":5,"Score":5}],[{"Id":6,"Score":6}]]`)
   151  		t.Assert(gutil.ListItemValues(listStruct, "Students", "Id"), g.Slice{1, 2, 3, 4, 5, 6})
   152  	})
   153  }
   154  
   155  func Test_ListItemValuesUnique(t *testing.T) {
   156  	gtest.C(t, func(t *gtest.T) {
   157  		listMap := g.List{
   158  			g.Map{"id": 1, "score": 100},
   159  			g.Map{"id": 2, "score": 100},
   160  			g.Map{"id": 3, "score": 100},
   161  			g.Map{"id": 4, "score": 100},
   162  			g.Map{"id": 5, "score": 100},
   163  		}
   164  		t.Assert(gutil.ListItemValuesUnique(listMap, "id"), g.Slice{1, 2, 3, 4, 5})
   165  		t.Assert(gutil.ListItemValuesUnique(listMap, "score"), g.Slice{100})
   166  	})
   167  	gtest.C(t, func(t *gtest.T) {
   168  		listMap := g.List{
   169  			g.Map{"id": 1, "score": 100},
   170  			g.Map{"id": 2, "score": 100},
   171  			g.Map{"id": 3, "score": 100},
   172  			g.Map{"id": 4, "score": 100},
   173  			g.Map{"id": 5, "score": 99},
   174  		}
   175  		t.Assert(gutil.ListItemValuesUnique(listMap, "id"), g.Slice{1, 2, 3, 4, 5})
   176  		t.Assert(gutil.ListItemValuesUnique(listMap, "score"), g.Slice{100, 99})
   177  	})
   178  	gtest.C(t, func(t *gtest.T) {
   179  		listMap := g.List{
   180  			g.Map{"id": 1, "score": 100},
   181  			g.Map{"id": 2, "score": 100},
   182  			g.Map{"id": 3, "score": 0},
   183  			g.Map{"id": 4, "score": 100},
   184  			g.Map{"id": 5, "score": 99},
   185  		}
   186  		t.Assert(gutil.ListItemValuesUnique(listMap, "id"), g.Slice{1, 2, 3, 4, 5})
   187  		t.Assert(gutil.ListItemValuesUnique(listMap, "score"), g.Slice{100, 0, 99})
   188  	})
   189  }
   190  
   191  func Test_ListItemValuesUnique_Struct_SubKey(t *testing.T) {
   192  	gtest.C(t, func(t *gtest.T) {
   193  		type Student struct {
   194  			Id    int
   195  			Score float64
   196  		}
   197  		type Class struct {
   198  			Total    int
   199  			Students []Student
   200  		}
   201  		listStruct := g.Slice{
   202  			Class{2, []Student{{1, 1}, {1, 2}}},
   203  			Class{3, []Student{{2, 3}, {2, 4}, {5, 5}}},
   204  			Class{1, []Student{{6, 6}}},
   205  		}
   206  		t.Assert(gutil.ListItemValuesUnique(listStruct, "Total"), g.Slice{2, 3, 1})
   207  		t.Assert(gutil.ListItemValuesUnique(listStruct, "Students", "Id"), g.Slice{1, 2, 5, 6})
   208  	})
   209  	gtest.C(t, func(t *gtest.T) {
   210  		type Student struct {
   211  			Id    int
   212  			Score float64
   213  		}
   214  		type Class struct {
   215  			Total    int
   216  			Students []*Student
   217  		}
   218  		listStruct := g.Slice{
   219  			&Class{2, []*Student{{1, 1}, {1, 2}}},
   220  			&Class{3, []*Student{{2, 3}, {2, 4}, {5, 5}}},
   221  			&Class{1, []*Student{{6, 6}}},
   222  		}
   223  		t.Assert(gutil.ListItemValuesUnique(listStruct, "Total"), g.Slice{2, 3, 1})
   224  		t.Assert(gutil.ListItemValuesUnique(listStruct, "Students", "Id"), g.Slice{1, 2, 5, 6})
   225  	})
   226  }
   227  
   228  func Test_ListItemValuesUnique_Map_Array_SubKey(t *testing.T) {
   229  	type Scores struct {
   230  		Math    int
   231  		English int
   232  	}
   233  	gtest.C(t, func(t *gtest.T) {
   234  		listMap := g.List{
   235  			g.Map{"id": 1, "scores": []Scores{{1, 2}, {1, 2}}},
   236  			g.Map{"id": 2, "scores": []Scores{{5, 8}, {5, 8}}},
   237  			g.Map{"id": 3, "scores": []Scores{{9, 10}, {11, 12}}},
   238  		}
   239  		t.Assert(gutil.ListItemValuesUnique(listMap, "scores", "Math"), g.Slice{1, 5, 9, 11})
   240  		t.Assert(gutil.ListItemValuesUnique(listMap, "scores", "English"), g.Slice{2, 8, 10, 12})
   241  		t.Assert(gutil.ListItemValuesUnique(listMap, "scores", "PE"), g.Slice{})
   242  	})
   243  }