|
| 1 | +package state_utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestSliceOrNil(t *testing.T) { |
| 9 | + type args struct { |
| 10 | + slice []string |
| 11 | + } |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + args args |
| 15 | + want []string |
| 16 | + }{ |
| 17 | + { |
| 18 | + name: "Should return non nil slice of type T", |
| 19 | + args: args{ |
| 20 | + slice: []string{"abc"}, |
| 21 | + }, |
| 22 | + want: []string{"abc"}, |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "Should return nil", |
| 26 | + args: args{ |
| 27 | + slice: []string{}, |
| 28 | + }, |
| 29 | + want: nil, |
| 30 | + }, |
| 31 | + } |
| 32 | + for _, tt := range tests { |
| 33 | + t.Run(tt.name, func(t *testing.T) { |
| 34 | + if got := SliceOrNil(tt.args.slice); !reflect.DeepEqual(got, tt.want) { |
| 35 | + t.Errorf("SliceOrNil() = %v, want %v", got, tt.want) |
| 36 | + } |
| 37 | + }) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func TestReadSliceState(t *testing.T) { |
| 42 | + type args struct { |
| 43 | + slice interface{} |
| 44 | + } |
| 45 | + tests := []struct { |
| 46 | + name string |
| 47 | + args args |
| 48 | + want []string |
| 49 | + }{ |
| 50 | + { |
| 51 | + name: "Should return string slice", |
| 52 | + args: args{ |
| 53 | + slice: []string{"abc"}, |
| 54 | + }, |
| 55 | + want: []string{"abc"}, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "Should construct string array", |
| 59 | + args: args{ |
| 60 | + slice: []interface{}{"abc"}, |
| 61 | + }, |
| 62 | + want: []string{"abc"}, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "Should return empty string array on unexpected type", |
| 66 | + args: args{ |
| 67 | + slice: []int{}, |
| 68 | + }, |
| 69 | + want: []string{}, |
| 70 | + }, |
| 71 | + } |
| 72 | + for _, tt := range tests { |
| 73 | + t.Run(tt.name, func(t *testing.T) { |
| 74 | + if got := ReadSliceState(tt.args.slice); !reflect.DeepEqual(got, tt.want) { |
| 75 | + t.Errorf("ReadSliceState() = %v, want %v", got, tt.want) |
| 76 | + } |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
0 commit comments