diff --git a/internal/goutils/slices_test.go b/internal/goutils/slices_test.go index 7527543..944a1ff 100644 --- a/internal/goutils/slices_test.go +++ b/internal/goutils/slices_test.go @@ -21,33 +21,29 @@ import ( ) func Test_AppendStringIfNotMember(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { newElement string originalSlice []string expectedSlice []string }{ - { - name: "Append new element", + "Append new element": { newElement: "four", originalSlice: []string{"one", "two", "three"}, expectedSlice: []string{"one", "two", "three", "four"}, }, - { - name: "Do not append existing element", + "Do not append existing element": { newElement: "two", originalSlice: []string{"one", "two", "three"}, expectedSlice: []string{"one", "two", "three"}, }, - { - name: "Append new element to empty list", + "Append new element to empty list": { newElement: "one", originalSlice: []string{}, expectedSlice: []string{"one"}, }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { actualSlice := AppendStringIfNotMember(tc.originalSlice, tc.newElement) require.ElementsMatch(t, tc.expectedSlice, actualSlice) }) @@ -55,21 +51,45 @@ func Test_AppendStringIfNotMember(t *testing.T) { } func Test_Contains(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { listToCheck []string toFind string isCaseSensitive bool want bool }{ - {name: "not_case_sensitive_success", listToCheck: []string{"hi", "hey"}, toFind: "Hey", isCaseSensitive: false, want: true}, - {name: "case_sensitive_success", listToCheck: []string{"hi", "Hey"}, toFind: "Hey", isCaseSensitive: true, want: true}, - {name: "case_sensitive_fail", listToCheck: []string{"hi", "hey", "hello", "apple", "pear"}, toFind: "Hey", isCaseSensitive: true, want: false}, - {name: "not_case_sensitive_fail", listToCheck: []string{"hi", "hey", "hello", "apple", "pear"}, toFind: "Peach", isCaseSensitive: false, want: false}, - {name: "not_case_sensitive_substring", listToCheck: []string{"hi", "hey hello"}, toFind: "hey", isCaseSensitive: false, want: false}, + "not_case_sensitive_success": { + listToCheck: []string{"hi", "hey"}, + toFind: "Hey", + isCaseSensitive: false, + want: true, + }, + "case_sensitive_success": { + listToCheck: []string{"hi", "Hey"}, + toFind: "Hey", + isCaseSensitive: true, + want: true, + }, + "case_sensitive_fail": { + listToCheck: []string{"hi", "hey", "hello", "apple", "pear"}, + toFind: "Hey", + isCaseSensitive: true, + want: false, + }, + "not_case_sensitive_fail": { + listToCheck: []string{"hi", "hey", "hello", "apple", "pear"}, + toFind: "Peach", + isCaseSensitive: false, + want: false, + }, + "not_case_sensitive_substring": { + listToCheck: []string{"hi", "hey hello"}, + toFind: "hey", + isCaseSensitive: false, + want: false, + }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { if got := Contains(tc.listToCheck, tc.toFind, tc.isCaseSensitive); got != tc.want { t.Errorf("method() = %v, want %v", got, tc.want) } diff --git a/internal/goutils/strings_test.go b/internal/goutils/strings_test.go index b84993c..dba9a2e 100644 --- a/internal/goutils/strings_test.go +++ b/internal/goutils/strings_test.go @@ -22,39 +22,34 @@ import ( ) func Test_HashString(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { text1 string text2 string expected bool }{ - { - name: "happy path - same string", + "happy path - same string": { text1: "one", text2: "one", expected: true, }, - { - name: "different strings", + "different strings": { text1: "one", text2: "two", expected: false, }, - { - name: "almost identical", + "almost identical": { text1: "one ", // add a space text2: "one", expected: false, }, - { - name: "empty strings should be same", + "empty strings should be same": { text1: "", text2: "", expected: true, }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { hash1, err1 := HashString(tc.text1) hash2, err2 := HashString(tc.text2) require.Equal(t, hash1 == hash2, tc.expected) @@ -65,64 +60,53 @@ func Test_HashString(t *testing.T) { } func Test_ExtractFirstJSONFromString(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { text string expected string }{ - { - name: "one json - 1", + "one json - 1": { text: "{}", expected: "{}", }, - { - name: "one json - 2", + "one json - 2": { text: "blah blah blah {a: 1}", expected: "{a: 1}", }, - { - name: "one json - 3", + "one json - 3": { text: "blah {a: 1} blah blah", expected: "{a: 1}", }, - { - name: "multiple json", + "multiple json": { text: "{a: 1} {b: 2}", expected: "{a: 1}", }, - { - name: "no json present", + "no json present": { text: "foo bar", expected: "", }, - { - name: "nested json", + "nested json": { text: "{a: b: {c: {d: 1}}} {1}", expected: "{a: b: {c: {d: 1}}}", }, - { - name: "nested json - 2", + "nested json - 2": { text: "{{{}}}", expected: "{{{}}}", }, - { - name: "malformed json", + "malformed json": { text: "{{", expected: "", }, - { - name: "malformed json - 2", + "malformed json - 2": { text: "{{}", expected: "", }, - { - name: "malformed json - 3", + "malformed json - 3": { text: "}}", expected: "", }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { actualRes := ExtractFirstJSONFromString(tc.text) require.Equal(t, tc.expected, actualRes) }) @@ -130,33 +114,29 @@ func Test_ExtractFirstJSONFromString(t *testing.T) { } func Test_addLogWhenValExist(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { title string val string expected string }{ - { - name: "Empty string when val is empty", + "Empty string when val is empty": { title: "hello world", val: "", expected: "", }, - { - name: "Empty string when val only has space", + "Empty string when val only has space": { title: "hello world", val: " ", expected: "", }, - { - name: "Return string when val is not empty", + "Return string when val is not empty": { title: "hello world", val: "slack", expected: "hello world: [slack]\n", }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { output := AddLogWhenValExist(tc.title, tc.val) require.Equal(t, output, tc.expected) }) @@ -388,34 +368,29 @@ func Test_RedactPII(t *testing.T) { } } func Test_UpperCaseTrimAll(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { namedEntities string expected string }{ - { - name: "Empty string when val is empty", + "Empty string when val is empty": { namedEntities: "", expected: "", }, - { - name: "Empty string when val only has space", + "Empty string when val only has space": { namedEntities: " ", expected: "", }, - { - name: "Return string when val is all lower-case", + "Return string when val is all lower-case": { namedEntities: "slack", expected: "SLACK", }, - { - name: "Return string when val contains spaces", + "Return string when val contains spaces": { namedEntities: "hello, world", expected: "HELLO,WORLD", }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { output := UpperCaseTrimAll(tc.namedEntities) require.Equal(t, output, tc.expected) }) @@ -423,29 +398,25 @@ func Test_UpperCaseTrimAll(t *testing.T) { } func Test_ToHTTPS(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { urlAddr string expected string }{ - { - name: "url with https protocol", + "url with https protocol": { urlAddr: "https://www.xyz.com", expected: "https://www.xyz.com", }, - { - name: "url with http protocol", + "url with http protocol": { urlAddr: "http://www.xyz.com", expected: "https://www.xyz.com", }, - { - name: "url with no protocol", + "url with no protocol": { urlAddr: "www.xyz.com", expected: "https://www.xyz.com", }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { output := ToHTTPS(tc.urlAddr) require.Equal(t, output, tc.expected) }) diff --git a/internal/shared/types/channel_test.go b/internal/shared/types/channel_test.go index dc9c35a..192c0d3 100644 --- a/internal/shared/types/channel_test.go +++ b/internal/shared/types/channel_test.go @@ -21,36 +21,31 @@ import ( ) func Test_SlackChannel_String(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { channel *SlackChannel expectedString string }{ - { - name: "Both ChannelName and ID exist", + "Both ChannelName and ID exist": { channel: &SlackChannel{ChannelName: "#general", ID: "C01234"}, expectedString: "#general (C01234)", }, // TODO(@mbrooks) This test represents the current behaviour, but should the expectedString be "#general" instead? - { - name: "Only ChannelName exists", + "Only ChannelName exists": { channel: &SlackChannel{ChannelName: "#general"}, expectedString: "(#general)", }, // TODO(@mbrooks) This test represents the current behaviour, but should the expectedString be "C01234" instead? - { - name: "Only ID exists", + "Only ID exists": { channel: &SlackChannel{ID: "C01234"}, expectedString: "", }, - { - name: "Both ChannelName and ID do not exist", + "Both ChannelName and ID do not exist": { channel: &SlackChannel{}, expectedString: "", }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { require.Equal(t, tc.channel.String(), tc.expectedString) }) } diff --git a/internal/shared/types/team_test.go b/internal/shared/types/team_test.go index f23fcf5..92ae247 100644 --- a/internal/shared/types/team_test.go +++ b/internal/shared/types/team_test.go @@ -21,34 +21,29 @@ import ( ) func Test_SlackTeam_String(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { slackTeam *SlackTeam expectedString string }{ - { - name: "ID and TeamName exists", + "ID and TeamName exists": { slackTeam: &SlackTeam{ID: "T1234", TeamName: "Team Cats"}, expectedString: "Team Cats (T1234)", }, - { - name: "Only TeamName exists", + "Only TeamName exists": { slackTeam: &SlackTeam{ID: "", TeamName: "Team Cats"}, expectedString: "(Team Cats)", // TODO - confirm that this is the actual format that's desired }, - { - name: "Only ID exists", + "Only ID exists": { slackTeam: &SlackTeam{ID: "T1234", TeamName: ""}, expectedString: "", // TODO - confirm that this is the actual format that's desired }, - { - name: "None exists", + "None exists": { slackTeam: &SlackTeam{ID: "", TeamName: ""}, expectedString: "", }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { returnedString := tc.slackTeam.String() require.Equal(t, tc.expectedString, returnedString) }) diff --git a/internal/shared/types/triggers_test.go b/internal/shared/types/triggers_test.go index 501a21c..c72bb6b 100644 --- a/internal/shared/types/triggers_test.go +++ b/internal/shared/types/triggers_test.go @@ -21,49 +21,41 @@ import ( ) func Test_Triggers_IsKnownType(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { trigger *Trigger expectedBool bool }{ - { - name: "Trigger type is TriggerTypeShortcut", + "Trigger type is TriggerTypeShortcut": { trigger: &Trigger{Type: TriggerTypeShortcut}, expectedBool: true, }, - { - name: "Trigger type is TriggerTypeSlashCommand", + "Trigger type is TriggerTypeSlashCommand": { trigger: &Trigger{Type: TriggerTypeSlashCommand}, expectedBool: true, }, - { - name: "Trigger type is TriggerTypeMessageShortcut", + "Trigger type is TriggerTypeMessageShortcut": { trigger: &Trigger{Type: TriggerTypeMessageShortcut}, expectedBool: true, }, - { - name: "Trigger type is TriggerTypeEvent", + "Trigger type is TriggerTypeEvent": { trigger: &Trigger{Type: TriggerTypeEvent}, expectedBool: true, }, - { - name: "Trigger type is TriggerTypeWebhook", + "Trigger type is TriggerTypeWebhook": { trigger: &Trigger{Type: TriggerTypeWebhook}, expectedBool: true, }, - { - name: "Trigger type is TriggerTypeScheduled", + "Trigger type is TriggerTypeScheduled": { trigger: &Trigger{Type: TriggerTypeScheduled}, expectedBool: true, }, - { - name: "Trigger type is invalid", + "Trigger type is invalid": { trigger: &Trigger{Type: "pickle pie"}, expectedBool: false, }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { returnedBool := tc.trigger.IsKnownType() require.Equal(t, tc.expectedBool, returnedBool) }) diff --git a/internal/shared/types/user_test.go b/internal/shared/types/user_test.go index dbe5a72..79cf2f2 100644 --- a/internal/shared/types/user_test.go +++ b/internal/shared/types/user_test.go @@ -21,44 +21,37 @@ import ( ) func Test_SlackUser_String(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { slackUser *SlackUser expectedString string }{ - { - name: "UserName exists only", + "UserName exists only": { slackUser: &SlackUser{UserName: "charlie"}, expectedString: "charlie", }, - { - name: "UserName and ID exists", + "UserName and ID exists": { slackUser: &SlackUser{UserName: "charlie", ID: "U1234"}, expectedString: "charlie (U1234)", }, - { - name: "UserName,ID, and Email exists", + "UserName,ID, and Email exists": { slackUser: &SlackUser{UserName: "charlie", ID: "U1234", Email: "user@domain.com"}, expectedString: "charlie (U1234, user@domain.com)", }, - { - name: "UserName, ID, Email, and PermissionType exists", + "UserName, ID, Email, and PermissionType exists": { slackUser: &SlackUser{UserName: "charlie", ID: "U1234", Email: "user@domain.com", PermissionType: "owner"}, expectedString: "charlie (U1234, user@domain.com, owner)", }, - { - name: "UserName does not exist", + "UserName does not exist": { slackUser: &SlackUser{}, expectedString: "", }, - { - name: "UserName does not exist but other properties exist", + "UserName does not exist but other properties exist": { slackUser: &SlackUser{ID: "U1234", PermissionType: "owner"}, expectedString: " (U1234, owner)", // TODO - confirm that this is the result we want }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { returnedString := tc.slackUser.String() require.Equal(t, tc.expectedString, returnedString) }) @@ -66,34 +59,29 @@ func Test_SlackUser_String(t *testing.T) { } func Test_SlackUser_ShorthandF(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { slackUser *SlackUser expectedString string }{ - { - name: "ID exists", + "ID exists": { slackUser: &SlackUser{ID: "U1234"}, expectedString: "U1234", }, - { - name: "Email exists", + "Email exists": { slackUser: &SlackUser{Email: "user@domain.com"}, expectedString: "user@domain.com", }, - { - name: "ID and Email exist", + "ID and Email exist": { slackUser: &SlackUser{ID: "U1234", Email: "user@domain.com"}, expectedString: "user@domain.com", }, - { - name: "ID and Email do not exist", + "ID and Email do not exist": { slackUser: &SlackUser{}, expectedString: "", }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { returnedString := tc.slackUser.ShorthandF() require.Equal(t, tc.expectedString, returnedString) })