github.com/safedep/dry@v0.0.0-20241016050132-a15651f0548b/utils/list_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestFindInSlice(t *testing.T) {
    10  	cases := []struct {
    11  		name  string
    12  		items []string
    13  		item  string
    14  		idx   int
    15  	}{
    16  		{
    17  			"When string is present in slice",
    18  			[]string{"Hello", "World"},
    19  			"World",
    20  			1,
    21  		},
    22  		{
    23  			"When string is not present in slice",
    24  			[]string{"Hello", "World"},
    25  			"None",
    26  			-1,
    27  		},
    28  		{
    29  			"When case is not matched",
    30  			[]string{"Hello", "World"},
    31  			"world",
    32  			-1,
    33  		},
    34  	}
    35  
    36  	for _, test := range cases {
    37  		t.Run(test.name, func(t *testing.T) {
    38  			idx := FindInSlice(test.items, test.item)
    39  			assert.Equal(t, test.idx, idx)
    40  		})
    41  	}
    42  }
    43  
    44  func TestFindAnyWith(t *testing.T) {
    45  	fn := func(item *map[string]string) bool {
    46  		if _, ok := (*item)["a"]; ok {
    47  			return true
    48  		}
    49  
    50  		return false
    51  	}
    52  
    53  	cases := []struct {
    54  		name  string
    55  		items []map[string]string
    56  		found bool
    57  	}{
    58  		{
    59  			"Item is found",
    60  			[]map[string]string{
    61  				{
    62  					"a": "a",
    63  				},
    64  			},
    65  			true,
    66  		},
    67  		{
    68  			"Item is not found",
    69  			[]map[string]string{
    70  				{
    71  					"b": "a",
    72  				},
    73  			},
    74  			false,
    75  		},
    76  	}
    77  
    78  	for _, test := range cases {
    79  		t.Run(test.name, func(t *testing.T) {
    80  			x := FindAnyWith(test.items, fn)
    81  			found := x != nil
    82  
    83  			assert.Equal(t, found, test.found)
    84  		})
    85  	}
    86  }