github.com/gogf/gf@v1.16.9/container/gvar/gvar_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 gvar_test
     8  
     9  import (
    10  	"github.com/gogf/gf/container/gvar"
    11  	"github.com/gogf/gf/frame/g"
    12  	"github.com/gogf/gf/test/gtest"
    13  	"testing"
    14  )
    15  
    16  func TestVar_ListItemValues_Map(t *testing.T) {
    17  	gtest.C(t, func(t *gtest.T) {
    18  		listMap := g.List{
    19  			g.Map{"id": 1, "score": 100},
    20  			g.Map{"id": 2, "score": 99},
    21  			g.Map{"id": 3, "score": 99},
    22  		}
    23  		t.Assert(gvar.New(listMap).ListItemValues("id"), g.Slice{1, 2, 3})
    24  		t.Assert(gvar.New(listMap).ListItemValues("score"), g.Slice{100, 99, 99})
    25  	})
    26  	gtest.C(t, func(t *gtest.T) {
    27  		listMap := g.List{
    28  			g.Map{"id": 1, "score": 100},
    29  			g.Map{"id": 2, "score": nil},
    30  			g.Map{"id": 3, "score": 0},
    31  		}
    32  		t.Assert(gvar.New(listMap).ListItemValues("id"), g.Slice{1, 2, 3})
    33  		t.Assert(gvar.New(listMap).ListItemValues("score"), g.Slice{100, nil, 0})
    34  	})
    35  }
    36  
    37  func TestVar_ListItemValues_Struct(t *testing.T) {
    38  	gtest.C(t, func(t *gtest.T) {
    39  		type T struct {
    40  			Id    int
    41  			Score float64
    42  		}
    43  		listStruct := g.Slice{
    44  			T{1, 100},
    45  			T{2, 99},
    46  			T{3, 0},
    47  		}
    48  		t.Assert(gvar.New(listStruct).ListItemValues("Id"), g.Slice{1, 2, 3})
    49  		t.Assert(gvar.New(listStruct).ListItemValues("Score"), g.Slice{100, 99, 0})
    50  	})
    51  	// Pointer items.
    52  	gtest.C(t, func(t *gtest.T) {
    53  		type T struct {
    54  			Id    int
    55  			Score float64
    56  		}
    57  		listStruct := g.Slice{
    58  			&T{1, 100},
    59  			&T{2, 99},
    60  			&T{3, 0},
    61  		}
    62  		t.Assert(gvar.New(listStruct).ListItemValues("Id"), g.Slice{1, 2, 3})
    63  		t.Assert(gvar.New(listStruct).ListItemValues("Score"), g.Slice{100, 99, 0})
    64  	})
    65  	// Nil element value.
    66  	gtest.C(t, func(t *gtest.T) {
    67  		type T struct {
    68  			Id    int
    69  			Score interface{}
    70  		}
    71  		listStruct := g.Slice{
    72  			T{1, 100},
    73  			T{2, nil},
    74  			T{3, 0},
    75  		}
    76  		t.Assert(gvar.New(listStruct).ListItemValues("Id"), g.Slice{1, 2, 3})
    77  		t.Assert(gvar.New(listStruct).ListItemValues("Score"), g.Slice{100, nil, 0})
    78  	})
    79  }
    80  
    81  func TestVar_ListItemValuesUnique(t *testing.T) {
    82  	gtest.C(t, func(t *gtest.T) {
    83  		listMap := g.List{
    84  			g.Map{"id": 1, "score": 100},
    85  			g.Map{"id": 2, "score": 100},
    86  			g.Map{"id": 3, "score": 100},
    87  			g.Map{"id": 4, "score": 100},
    88  			g.Map{"id": 5, "score": 100},
    89  		}
    90  		t.Assert(gvar.New(listMap).ListItemValuesUnique("id"), g.Slice{1, 2, 3, 4, 5})
    91  		t.Assert(gvar.New(listMap).ListItemValuesUnique("score"), g.Slice{100})
    92  	})
    93  	gtest.C(t, func(t *gtest.T) {
    94  		listMap := g.List{
    95  			g.Map{"id": 1, "score": 100},
    96  			g.Map{"id": 2, "score": 100},
    97  			g.Map{"id": 3, "score": 100},
    98  			g.Map{"id": 4, "score": 100},
    99  			g.Map{"id": 5, "score": 99},
   100  		}
   101  		t.Assert(gvar.New(listMap).ListItemValuesUnique("id"), g.Slice{1, 2, 3, 4, 5})
   102  		t.Assert(gvar.New(listMap).ListItemValuesUnique("score"), g.Slice{100, 99})
   103  	})
   104  	gtest.C(t, func(t *gtest.T) {
   105  		listMap := g.List{
   106  			g.Map{"id": 1, "score": 100},
   107  			g.Map{"id": 2, "score": 100},
   108  			g.Map{"id": 3, "score": 0},
   109  			g.Map{"id": 4, "score": 100},
   110  			g.Map{"id": 5, "score": 99},
   111  		}
   112  		t.Assert(gvar.New(listMap).ListItemValuesUnique("id"), g.Slice{1, 2, 3, 4, 5})
   113  		t.Assert(gvar.New(listMap).ListItemValuesUnique("score"), g.Slice{100, 0, 99})
   114  	})
   115  }