...

Source file src/github.com/twmb/franz-go/pkg/kadm/kadm_test.go

Documentation: github.com/twmb/franz-go/pkg/kadm

     1  package kadm
     2  
     3  import (
     4  	"errors"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  func input[V any](v V) V { return v }
    10  
    11  func inputErr[V any](v V, err error) (V, error) { return v, err }
    12  
    13  func TestFirst(t *testing.T) {
    14  	for _, test := range []struct {
    15  		in     []int
    16  		inErr  error
    17  		exp    int
    18  		expErr bool
    19  	}{
    20  		{[]int{3, 4, 5}, nil, 3, false},
    21  		{[]int{4}, nil, 4, false},
    22  		{[]int{4}, errors.New("foo"), 4, true},
    23  		{nil, errors.New("foo"), 0, true},
    24  	} {
    25  		got, err := FirstE(inputErr(test.in, test.inErr))
    26  		gotErr := err != nil
    27  		if gotErr != test.expErr {
    28  			t.Errorf("got err? %v, exp err? %v", gotErr, test.expErr)
    29  		}
    30  		if !gotErr && !test.expErr {
    31  			if !reflect.DeepEqual(got, test.exp) {
    32  				t.Errorf("got %v != exp %v", got, test.exp)
    33  			}
    34  		}
    35  
    36  		got, exists := First(input(test.in))
    37  		if len(test.in) == 0 {
    38  			if exists {
    39  				t.Error("got exists, expected no")
    40  			}
    41  			continue
    42  		}
    43  		if !exists {
    44  			t.Error("got not exists, expected yes")
    45  		}
    46  		if !reflect.DeepEqual(got, test.exp) {
    47  			t.Errorf("got %v != exp %v", got, test.exp)
    48  		}
    49  	}
    50  }
    51  
    52  func TestAny(t *testing.T) {
    53  	for _, test := range []struct {
    54  		in     map[int]string
    55  		inErr  error
    56  		exp    string
    57  		expErr bool
    58  	}{
    59  		{map[int]string{3: "foo"}, nil, "foo", false},
    60  		{map[int]string{3: "foo"}, errors.New("foo"), "foo", true},
    61  		{nil, errors.New("foo"), "", true},
    62  	} {
    63  		got, err := AnyE(inputErr(test.in, test.inErr))
    64  		gotErr := err != nil
    65  		if gotErr != test.expErr {
    66  			t.Errorf("got err? %v, exp err? %v", gotErr, test.expErr)
    67  		}
    68  		if !gotErr && !test.expErr {
    69  			if !reflect.DeepEqual(got, test.exp) {
    70  				t.Errorf("got %v != exp %v", got, test.exp)
    71  			}
    72  		}
    73  
    74  		got, exists := Any(input(test.in))
    75  		if len(test.in) == 0 {
    76  			if exists {
    77  				t.Error("got exists, expected no")
    78  			}
    79  			continue
    80  		}
    81  		if !exists {
    82  			t.Error("got not exists, expected yes")
    83  		}
    84  		if !reflect.DeepEqual(got, test.exp) {
    85  			t.Errorf("got %v != exp %v", got, test.exp)
    86  		}
    87  	}
    88  }
    89  

View as plain text