Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 39 additions & 19 deletions internal/goutils/slices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,75 @@ 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)
})
}
}

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)
}
Expand Down
107 changes: 39 additions & 68 deletions internal/goutils/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -65,98 +60,83 @@ 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)
})
}
}

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)
})
Expand Down Expand Up @@ -388,64 +368,55 @@ 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)
})
}
}

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)
})
Expand Down
19 changes: 7 additions & 12 deletions internal/shared/types/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand Down
Loading
Loading