package monutil import ( "testing" "github.com/stretchr/testify/assert" ) func TestIsForeman(t *testing.T) { tcs := []struct { project string expected bool desc string }{ { project: "ret-edge-dev0-foreman", expected: true, desc: "standard foreman project ID should match", }, { project: "foreman-dev0-edge", expected: false, desc: "non-standard foreman project ID should not be matched", }, { project: "ret-edge-235234kd3f2-232423", expected: false, desc: "non foreman project ID should not be matched", }, { project: "ret-edge-stage1-foreman-235f", expected: true, desc: "other foreman project ID values should match", }, } for _, tc := range tcs { actual := IsForeman(tc.project) assert.Equal(t, tc.expected, actual, tc.desc) } } func TestCompareStrSlice(t *testing.T) { tcs := []struct { slice1 []string slice2 []string expected []string desc string }{ { slice1: []string{"test1", "test2", "test3"}, slice2: []string{"test1", "test2", "test3"}, expected: []string(nil), desc: "slices should match and return nothing", }, { slice1: []string{"test1", "test3"}, slice2: []string{"test1", "test2", "test3", "test4"}, expected: []string{"test2", "test4"}, desc: "2 values present in second slice but not present in first slice should be returned", }, { slice1: []string{"test1", "test2", "test3", "test4"}, slice2: []string{"test1", "test3"}, expected: []string{"test2", "test4"}, desc: "2 values present in first slice but not present in second slice should be returned", }, { slice1: []string{"test1"}, slice2: []string{"test2", "test3", "test4"}, expected: []string{"test2", "test3", "test4", "test1"}, desc: "all values in first and second slice should be returned since none are present in both slices", }, { slice1: []string{}, slice2: []string{"test1", "test2", "test3"}, expected: []string{"test1", "test2", "test3"}, desc: "all values in second slice should be returned since first slice is empty", }, } for _, tc := range tcs { actual := CompareStrSlice(tc.slice1, tc.slice2) assert.Equal(t, tc.expected, actual, tc.desc) } } func TestInSlice(t *testing.T) { tcs := []struct { slice1 []string slice2 []string expected bool desc string }{ { slice1: []string{"one", "two"}, slice2: []string{"one", "two", "helloworld"}, expected: false, desc: "the slice should not be found", }, { slice1: []string{"one", "two", "helloworld"}, slice2: []string{"three"}, expected: false, desc: "the slice should not be found", }, { slice1: []string{"one", "hello"}, slice2: []string{"hello"}, expected: true, desc: "the slice should be found", }, { slice1: []string{"one", "hello"}, slice2: []string{}, expected: false, desc: "empty slice should not be found", }, { slice1: []string{"one"}, slice2: []string{"one", "two"}, expected: false, desc: "the slice should not be found", }, { slice1: []string{}, slice2: []string{"one"}, expected: false, desc: "the slice should not be found in an empty slice", }, } for _, tc := range tcs { actual := InSlice(tc.slice1, tc.slice2) assert.Equal(t, tc.expected, actual, tc.desc) } } func TestInStrList(t *testing.T) { tcs := []struct { str string slice []string expected bool desc string }{ { str: "hello", slice: []string{"one", "two"}, expected: false, desc: "the string should not be found", }, { str: "hello", slice: []string{"one", "two", "helloworld"}, expected: false, desc: "the string should not be found", }, { str: "hello", slice: []string{"one", "hello"}, expected: true, desc: "the string should be found", }, { str: "", slice: []string{"one", "hello"}, expected: false, desc: "empty string should not be found", }, } for _, tc := range tcs { actual := InStrList(tc.str, tc.slice) assert.Equal(t, tc.expected, actual, tc.desc) } } func TestInIntList(t *testing.T) { tcs := []struct { num int slice []int expected bool desc string }{ { num: 1, slice: []int{3, 4}, expected: false, desc: "the number should not be found", }, { num: 1, slice: []int{2, 3, 1}, expected: true, desc: "the number should be found", }, { num: 0, slice: []int{}, expected: false, desc: "the number should not be found in an empty slice", }, } for _, tc := range tcs { actual := InIntList(tc.num, tc.slice) assert.Equal(t, tc.expected, actual, tc.desc) } } func TestReconcileFileNames(t *testing.T) { tcs := []struct { names []string expected []string desc string }{ { names: []string{"myfile", "myfile"}, expected: []string{"myfile", "myfile_(Duplicate_Name_1).json"}, desc: "duplicate files should be renamed", }, { names: []string{"myfile", "anotherfile"}, expected: []string{"myfile", "anotherfile"}, desc: "unique files should not be renamed", }, { names: []string{"myfile", "myfile1"}, expected: []string{"myfile", "myfile1"}, desc: "unique files should not be renamed", }, } for _, tc := range tcs { actual := ReconcileFileNames(tc.names) assert.ElementsMatch(t, tc.expected, actual, tc.desc) } }