github.com/searKing/golang/go@v1.2.117/exp/slices/first_test.go (about)

     1  // Copyright 2022 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package slices_test
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  
    11  	slices_ "github.com/searKing/golang/go/exp/slices"
    12  )
    13  
    14  func TestFirstOrZero_Int(t *testing.T) {
    15  	tests := []struct {
    16  		data []int
    17  		want int
    18  	}{
    19  		{nil, 0},
    20  		{[]int{}, 0},
    21  		{[]int{0}, 0},
    22  		{[]int{1, 0}, 1},
    23  		{[]int{1, 2}, 1},
    24  		{[]int{0, 1, 2}, 1},
    25  	}
    26  	for _, tt := range tests {
    27  		t.Run(fmt.Sprintf("%v", tt.data), func(t *testing.T) {
    28  			{
    29  				got := slices_.FirstOrZero(tt.data...)
    30  				if got != tt.want {
    31  					t.Errorf("first of %v = %v", tt.data, tt.want)
    32  					t.Errorf("   got %v", got)
    33  				}
    34  			}
    35  		})
    36  	}
    37  }
    38  
    39  func TestFirstOrZero_String(t *testing.T) {
    40  	tests := []struct {
    41  		data []string
    42  		want string
    43  	}{
    44  		{nil, ""},
    45  		{[]string{}, ""},
    46  		{[]string{""}, ""},
    47  		{[]string{"a", ""}, "a"},
    48  		{[]string{"a", "b"}, "a"},
    49  		{[]string{"", "a", "b"}, "a"},
    50  	}
    51  	for _, tt := range tests {
    52  		t.Run(fmt.Sprintf("%v", tt.data), func(t *testing.T) {
    53  			{
    54  				got := slices_.FirstOrZero(tt.data...)
    55  				if got != tt.want {
    56  					t.Errorf("first of %v = %v", tt.data, tt.want)
    57  					t.Errorf("   got %v", got)
    58  				}
    59  			}
    60  		})
    61  	}
    62  }