github.com/seeker-insurance/kit@v0.0.13/stringslice/stringslice_test.go (about)

     1  package stringslice
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestNonEmpty(t *testing.T) {
    12  	type args struct {
    13  		a []string
    14  	}
    15  	tests := []struct {
    16  		name string
    17  		args args
    18  		want []string
    19  	}{
    20  		{"nil", args{nil}, nil},
    21  		{"full", args{[]string{"foo", "bar", "baz"}}, []string{"foo", "bar", "baz"}},
    22  		{"missing pieces", args{[]string{"0", "", "1"}}, []string{"0", "1"}},
    23  		{"nonempty slice with all blank args", args{[]string{"", "", ""}}, nil},
    24  	}
    25  	for _, tt := range tests {
    26  		t.Run(tt.name, func(t *testing.T) {
    27  			if got := NonEmpty(tt.args.a); !reflect.DeepEqual(got, tt.want) {
    28  				t.Errorf("NonEmpty() = %v, want %v", got, tt.want)
    29  			}
    30  		})
    31  	}
    32  }
    33  
    34  func nonempty(s string) bool {
    35  	return len(s) > 0
    36  }
    37  func TestAll(t *testing.T) {
    38  	yes := []string{"a", "foo", "bar"}
    39  	no := []string{"a", "", "c"}
    40  	var noelems []string
    41  	assert.True(t, All(yes, nonempty))
    42  	assert.False(t, All(no, nonempty))
    43  	assert.True(t, All(noelems, nonempty))
    44  
    45  }
    46  
    47  func TestCombine(t *testing.T) {
    48  	a := []string{"foo", "bar"}
    49  	b := []string{}
    50  	c := []string{"baz"}
    51  	assert.Equal(t, []string{"foo", "bar", "baz"}, Combine(a, b, c))
    52  }
    53  func TestAny(t *testing.T) {
    54  	yes := []string{"", "c", ""}
    55  	no := []string{"", "", ""}
    56  	var noelems []string
    57  	assert.True(t, Any(yes, nonempty))
    58  	assert.False(t, Any(no, nonempty))
    59  	assert.False(t, Any(noelems, nonempty))
    60  }
    61  
    62  func TestFilter(t *testing.T) {
    63  	a := []string{"", "c", ""}
    64  	want := []string{"c"}
    65  	assert.Equal(t, want, Filter(a, nonempty))
    66  }
    67  
    68  func TestFilterFalse(t *testing.T) {
    69  	a := []string{"", "c", ""}
    70  	want := []string{"", ""}
    71  	assert.Equal(t, want, FilterFalse(a, nonempty))
    72  }
    73  
    74  func TestMap(t *testing.T) {
    75  	got := Map([]string{"aa", "bcd"}, strings.ToUpper)
    76  	want := []string{"AA", "BCD"}
    77  	assert.Equal(t, want, got)
    78  }
    79  
    80  func TestAppendIfNonEmpty(t *testing.T) {
    81  	want := []string{"foo", "aa", "bb"}
    82  	toAppend := []string{"aa", "", "", "bb"}
    83  	assert.Equal(t, want, AppendIfNonEmpty([]string{"foo"}, toAppend...))
    84  }
    85  
    86  func TestTotalDistance(t *testing.T) {
    87  	_, ok := TotalDistance([]string{}, []string{"aa"})
    88  	assert.False(t, ok)
    89  
    90  	a := []string{"foo", "bar", "baz"}
    91  	b := []string{"food", "BAR", "BASZ"}
    92  	want := 1 + 3 + 4
    93  	got, ok := TotalDistance(a, b)
    94  	assert.True(t, ok)
    95  	assert.Equal(t, want, got)
    96  
    97  }
    98  
    99  func sum(ints ...int) int {
   100  	total := 0
   101  	for _, n := range ints {
   102  		total += n
   103  	}
   104  	return total
   105  }
   106  
   107  func Test_levenshteinDistance(t *testing.T) {
   108  	type args struct {
   109  		s string
   110  		t string
   111  	}
   112  	tests := []struct {
   113  		name string
   114  		args args
   115  		want int
   116  	}{
   117  	// TODO: Add test cases.
   118  	}
   119  	for _, tt := range tests {
   120  		t.Run(tt.name, func(t *testing.T) {
   121  			if got := levenshteinDistance(tt.args.s, tt.args.t); got != tt.want {
   122  				t.Errorf("levenshteinDistance() = %v, want %v", got, tt.want)
   123  			}
   124  		})
   125  	}
   126  }
   127  
   128  func Test_min(t *testing.T) {
   129  	type args struct {
   130  		a    int
   131  		ints []int
   132  	}
   133  	tests := []struct {
   134  		name string
   135  		args args
   136  		want int
   137  	}{
   138  	// TODO: Add test cases.
   139  	}
   140  	for _, tt := range tests {
   141  		t.Run(tt.name, func(t *testing.T) {
   142  			if got := min(tt.args.a, tt.args.ints...); got != tt.want {
   143  				t.Errorf("min() = %v, want %v", got, tt.want)
   144  			}
   145  		})
   146  	}
   147  }