github.com/everdrone/grab@v0.1.7-0.20230416223925-40674b995521/internal/utils/slices_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	tu "github.com/everdrone/grab/testutils"
     8  	"golang.org/x/exp/slices"
     9  )
    10  
    11  type testStruct struct {
    12  	Foo string
    13  	Bar int
    14  }
    15  
    16  func TestFilter(t *testing.T) {
    17  	tests := []struct {
    18  		Name     string
    19  		Slice    []testStruct
    20  		TestFunc func(testStruct) bool
    21  		Want     []testStruct
    22  	}{
    23  		{
    24  			Name:     "empty slice",
    25  			Slice:    make([]testStruct, 0),
    26  			TestFunc: func(x testStruct) bool { return x.Foo == "foo" },
    27  			Want:     make([]testStruct, 0),
    28  		},
    29  		{
    30  			Name:     "returns empty",
    31  			Slice:    make([]testStruct, 4),
    32  			TestFunc: func(x testStruct) bool { return x.Foo == "something" },
    33  			Want:     make([]testStruct, 0),
    34  		},
    35  		{
    36  			Name:     "returns one",
    37  			Slice:    []testStruct{{"foo", 1}, {"bar", 2}, {"baz", 3}},
    38  			TestFunc: func(x testStruct) bool { return x.Foo == "foo" },
    39  			Want:     []testStruct{{"foo", 1}},
    40  		},
    41  		{
    42  			Name:     "returns many",
    43  			Slice:    []testStruct{{"foo", 1}, {"bar", 2}, {"baz", 3}},
    44  			TestFunc: func(x testStruct) bool { return x.Bar > 1 },
    45  			Want:     []testStruct{{"bar", 2}, {"baz", 3}},
    46  		},
    47  	}
    48  
    49  	for _, test := range tests {
    50  		t.Run(test.Name, func(t *testing.T) {
    51  			got := Filter(test.Slice, test.TestFunc)
    52  
    53  			if !slices.Equal(got, test.Want) {
    54  				t.Errorf("got: %v, want: %v", got, test.Want)
    55  			}
    56  		})
    57  	}
    58  }
    59  
    60  func TestContains(t *testing.T) {
    61  	tests := []struct {
    62  		Name  string
    63  		Slice []string
    64  		Value string
    65  		Want  bool
    66  	}{
    67  		{
    68  			Name:  "empty slice",
    69  			Slice: make([]string, 0),
    70  			Value: "foo",
    71  			Want:  false,
    72  		},
    73  		{
    74  			Name:  "contains",
    75  			Slice: []string{"foo", "bar", "baz"},
    76  			Value: "foo",
    77  			Want:  true,
    78  		},
    79  		{
    80  			Name:  "does not contain",
    81  			Slice: []string{"foo", "bar", "baz"},
    82  			Value: "qux",
    83  			Want:  false,
    84  		},
    85  	}
    86  
    87  	for _, tt := range tests {
    88  		t.Run(tt.Name, func(t *testing.T) {
    89  			got := Contains(tt.Slice, tt.Value)
    90  			if got != tt.Want {
    91  				t.Errorf("got: %v, want: %v", got, tt.Want)
    92  			}
    93  		})
    94  	}
    95  }
    96  
    97  func TestUnique(t *testing.T) {
    98  	tests := []struct {
    99  		Name  string
   100  		Slice []string
   101  		Want  []string
   102  	}{
   103  		{
   104  			Name:  "empty slice",
   105  			Slice: make([]string, 0),
   106  			Want:  make([]string, 0),
   107  		},
   108  		{
   109  			Name:  "returns one",
   110  			Slice: []string{"foo", "foo", "foo"},
   111  			Want:  []string{"foo"},
   112  		},
   113  		{
   114  			Name:  "returns many",
   115  			Slice: []string{"foo", "bar", "baz"},
   116  			Want:  []string{"foo", "bar", "baz"},
   117  		},
   118  		{
   119  			Name:  "returns many with duplicates",
   120  			Slice: []string{"foo", "bar", "baz", "bar", "bar", "baz"},
   121  			Want:  []string{"foo", "bar", "baz"},
   122  		},
   123  	}
   124  
   125  	for _, tt := range tests {
   126  		t.Run(tt.Name, func(t *testing.T) {
   127  			got := Unique(tt.Slice)
   128  
   129  			if !slices.Equal(got, tt.Want) {
   130  				t.Errorf("got: %v, want: %v", got, tt.Want)
   131  			}
   132  		})
   133  	}
   134  }
   135  
   136  func TestAny(t *testing.T) {
   137  	tests := []struct {
   138  		Name  string
   139  		Slice []string
   140  		Test  func(string) bool
   141  		Want  bool
   142  	}{
   143  		{
   144  			Name:  "empty slice",
   145  			Slice: make([]string, 0),
   146  			Test:  func(x string) bool { return x == "foo" },
   147  			Want:  false,
   148  		},
   149  		{
   150  			Name:  "returns true",
   151  			Slice: []string{"foo", "bar", "baz", "foo"},
   152  			Test:  func(x string) bool { return x == "foo" },
   153  			Want:  true,
   154  		},
   155  		{
   156  			Name:  "returns false",
   157  			Slice: []string{"foo", "bar", "baz", "foo"},
   158  			Test:  func(x string) bool { return x == "qux" },
   159  			Want:  false,
   160  		},
   161  	}
   162  
   163  	for _, tt := range tests {
   164  		t.Run(tt.Name, func(t *testing.T) {
   165  			got := Any(tt.Slice, tt.Test)
   166  			if got != tt.Want {
   167  				t.Errorf("got: %v, want: %v", got, tt.Want)
   168  			}
   169  		})
   170  	}
   171  }
   172  
   173  func TestAll(t *testing.T) {
   174  	tests := []struct {
   175  		Name  string
   176  		Slice []string
   177  		Test  func(string) bool
   178  		Want  bool
   179  	}{
   180  		{
   181  			Name:  "empty slice",
   182  			Slice: make([]string, 0),
   183  			Test:  func(x string) bool { return x == "foo" },
   184  			Want:  false,
   185  		},
   186  		{
   187  			Name:  "some",
   188  			Slice: []string{"foo", "bar", "baz", "foo"},
   189  			Test:  func(x string) bool { return x == "foo" },
   190  			Want:  false,
   191  		},
   192  		{
   193  			Name:  "none",
   194  			Slice: []string{"foo", "bar", "baz", "foo"},
   195  			Test:  func(x string) bool { return x == "qux" },
   196  			Want:  false,
   197  		},
   198  		{
   199  			Name:  "all",
   200  			Slice: []string{"foo", "foo", "foo", "foo"},
   201  			Test:  func(x string) bool { return x == "foo" },
   202  			Want:  true,
   203  		},
   204  		{
   205  			Name:  "one",
   206  			Slice: []string{"foo", "bar", "baz", "foo"},
   207  			Test:  func(x string) bool { return x == "bar" },
   208  			Want:  false,
   209  		},
   210  	}
   211  
   212  	for _, tt := range tests {
   213  		t.Run(tt.Name, func(t *testing.T) {
   214  			got := All(tt.Slice, tt.Test)
   215  			if got != tt.Want {
   216  				t.Errorf("got: %v, want: %v", got, tt.Want)
   217  			}
   218  		})
   219  	}
   220  }
   221  
   222  func TestZipMap(t *testing.T) {
   223  	tests := []struct {
   224  		Name   string
   225  		Keys   []string
   226  		Vals   []string
   227  		Want   map[string]string
   228  		Panics bool
   229  	}{
   230  		{
   231  			Name: "empty",
   232  			Keys: make([]string, 0),
   233  			Vals: make([]string, 0),
   234  			Want: make(map[string]string),
   235  		},
   236  		{
   237  			Name: "one",
   238  			Keys: []string{"foo"},
   239  			Vals: []string{"bar"},
   240  			Want: map[string]string{"foo": "bar"},
   241  		},
   242  		{
   243  			Name: "many",
   244  			Keys: []string{"foo", "bar"},
   245  			Vals: []string{"baz", "qux"},
   246  			Want: map[string]string{"foo": "baz", "bar": "qux"},
   247  		},
   248  		{
   249  			Name:   "longer keys",
   250  			Keys:   []string{"foo", "bar", "baz"},
   251  			Vals:   []string{"qux"},
   252  			Want:   nil,
   253  			Panics: true,
   254  		},
   255  	}
   256  
   257  	for _, tt := range tests {
   258  		t.Run(tt.Name, func(t *testing.T) {
   259  			if tt.Panics {
   260  				tu.AssertPanic(t, func() { ZipMap(tt.Keys, tt.Vals) })
   261  			} else {
   262  				got := ZipMap(tt.Keys, tt.Vals)
   263  				if !reflect.DeepEqual(got, tt.Want) {
   264  					t.Errorf("got: %v, want: %v", got, tt.Want)
   265  				}
   266  			}
   267  		})
   268  	}
   269  }