Skip to content

Commit 2da1d1a

Browse files
authored
refactor(tests): update slice pattern table tests to map pattern (low risk tests) (#320)
* refactor(tests): use tests/tc naming in table tests using the slice pattern * test: update map pattern table tests to use common naming pattern * test: update slice pattern table tests to map pattern (simple use-cases) * refactor(tests): cleanup formatting
1 parent 66766a9 commit 2da1d1a

File tree

6 files changed

+118
-157
lines changed

6 files changed

+118
-157
lines changed

internal/goutils/slices_test.go

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,55 +21,75 @@ import (
2121
)
2222

2323
func Test_AppendStringIfNotMember(t *testing.T) {
24-
tests := []struct {
25-
name string
24+
tests := map[string]struct {
2625
newElement string
2726
originalSlice []string
2827
expectedSlice []string
2928
}{
30-
{
31-
name: "Append new element",
29+
"Append new element": {
3230
newElement: "four",
3331
originalSlice: []string{"one", "two", "three"},
3432
expectedSlice: []string{"one", "two", "three", "four"},
3533
},
36-
{
37-
name: "Do not append existing element",
34+
"Do not append existing element": {
3835
newElement: "two",
3936
originalSlice: []string{"one", "two", "three"},
4037
expectedSlice: []string{"one", "two", "three"},
4138
},
42-
{
43-
name: "Append new element to empty list",
39+
"Append new element to empty list": {
4440
newElement: "one",
4541
originalSlice: []string{},
4642
expectedSlice: []string{"one"},
4743
},
4844
}
49-
for _, tc := range tests {
50-
t.Run(tc.name, func(t *testing.T) {
45+
for name, tc := range tests {
46+
t.Run(name, func(t *testing.T) {
5147
actualSlice := AppendStringIfNotMember(tc.originalSlice, tc.newElement)
5248
require.ElementsMatch(t, tc.expectedSlice, actualSlice)
5349
})
5450
}
5551
}
5652

5753
func Test_Contains(t *testing.T) {
58-
tests := []struct {
59-
name string
54+
tests := map[string]struct {
6055
listToCheck []string
6156
toFind string
6257
isCaseSensitive bool
6358
want bool
6459
}{
65-
{name: "not_case_sensitive_success", listToCheck: []string{"hi", "hey"}, toFind: "Hey", isCaseSensitive: false, want: true},
66-
{name: "case_sensitive_success", listToCheck: []string{"hi", "Hey"}, toFind: "Hey", isCaseSensitive: true, want: true},
67-
{name: "case_sensitive_fail", listToCheck: []string{"hi", "hey", "hello", "apple", "pear"}, toFind: "Hey", isCaseSensitive: true, want: false},
68-
{name: "not_case_sensitive_fail", listToCheck: []string{"hi", "hey", "hello", "apple", "pear"}, toFind: "Peach", isCaseSensitive: false, want: false},
69-
{name: "not_case_sensitive_substring", listToCheck: []string{"hi", "hey hello"}, toFind: "hey", isCaseSensitive: false, want: false},
60+
"not_case_sensitive_success": {
61+
listToCheck: []string{"hi", "hey"},
62+
toFind: "Hey",
63+
isCaseSensitive: false,
64+
want: true,
65+
},
66+
"case_sensitive_success": {
67+
listToCheck: []string{"hi", "Hey"},
68+
toFind: "Hey",
69+
isCaseSensitive: true,
70+
want: true,
71+
},
72+
"case_sensitive_fail": {
73+
listToCheck: []string{"hi", "hey", "hello", "apple", "pear"},
74+
toFind: "Hey",
75+
isCaseSensitive: true,
76+
want: false,
77+
},
78+
"not_case_sensitive_fail": {
79+
listToCheck: []string{"hi", "hey", "hello", "apple", "pear"},
80+
toFind: "Peach",
81+
isCaseSensitive: false,
82+
want: false,
83+
},
84+
"not_case_sensitive_substring": {
85+
listToCheck: []string{"hi", "hey hello"},
86+
toFind: "hey",
87+
isCaseSensitive: false,
88+
want: false,
89+
},
7090
}
71-
for _, tc := range tests {
72-
t.Run(tc.name, func(t *testing.T) {
91+
for name, tc := range tests {
92+
t.Run(name, func(t *testing.T) {
7393
if got := Contains(tc.listToCheck, tc.toFind, tc.isCaseSensitive); got != tc.want {
7494
t.Errorf("method() = %v, want %v", got, tc.want)
7595
}

internal/goutils/strings_test.go

Lines changed: 39 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -22,39 +22,34 @@ import (
2222
)
2323

2424
func Test_HashString(t *testing.T) {
25-
tests := []struct {
26-
name string
25+
tests := map[string]struct {
2726
text1 string
2827
text2 string
2928
expected bool
3029
}{
31-
{
32-
name: "happy path - same string",
30+
"happy path - same string": {
3331
text1: "one",
3432
text2: "one",
3533
expected: true,
3634
},
37-
{
38-
name: "different strings",
35+
"different strings": {
3936
text1: "one",
4037
text2: "two",
4138
expected: false,
4239
},
43-
{
44-
name: "almost identical",
40+
"almost identical": {
4541
text1: "one ", // add a space
4642
text2: "one",
4743
expected: false,
4844
},
49-
{
50-
name: "empty strings should be same",
45+
"empty strings should be same": {
5146
text1: "",
5247
text2: "",
5348
expected: true,
5449
},
5550
}
56-
for _, tc := range tests {
57-
t.Run(tc.name, func(t *testing.T) {
51+
for name, tc := range tests {
52+
t.Run(name, func(t *testing.T) {
5853
hash1, err1 := HashString(tc.text1)
5954
hash2, err2 := HashString(tc.text2)
6055
require.Equal(t, hash1 == hash2, tc.expected)
@@ -65,98 +60,83 @@ func Test_HashString(t *testing.T) {
6560
}
6661

6762
func Test_ExtractFirstJSONFromString(t *testing.T) {
68-
tests := []struct {
69-
name string
63+
tests := map[string]struct {
7064
text string
7165
expected string
7266
}{
73-
{
74-
name: "one json - 1",
67+
"one json - 1": {
7568
text: "{}",
7669
expected: "{}",
7770
},
78-
{
79-
name: "one json - 2",
71+
"one json - 2": {
8072
text: "blah blah blah {a: 1}",
8173
expected: "{a: 1}",
8274
},
83-
{
84-
name: "one json - 3",
75+
"one json - 3": {
8576
text: "blah {a: 1} blah blah",
8677
expected: "{a: 1}",
8778
},
88-
{
89-
name: "multiple json",
79+
"multiple json": {
9080
text: "{a: 1} {b: 2}",
9181
expected: "{a: 1}",
9282
},
93-
{
94-
name: "no json present",
83+
"no json present": {
9584
text: "foo bar",
9685
expected: "",
9786
},
98-
{
99-
name: "nested json",
87+
"nested json": {
10088
text: "{a: b: {c: {d: 1}}} {1}",
10189
expected: "{a: b: {c: {d: 1}}}",
10290
},
103-
{
104-
name: "nested json - 2",
91+
"nested json - 2": {
10592
text: "{{{}}}",
10693
expected: "{{{}}}",
10794
},
108-
{
109-
name: "malformed json",
95+
"malformed json": {
11096
text: "{{",
11197
expected: "",
11298
},
113-
{
114-
name: "malformed json - 2",
99+
"malformed json - 2": {
115100
text: "{{}",
116101
expected: "",
117102
},
118-
{
119-
name: "malformed json - 3",
103+
"malformed json - 3": {
120104
text: "}}",
121105
expected: "",
122106
},
123107
}
124-
for _, tc := range tests {
125-
t.Run(tc.name, func(t *testing.T) {
108+
for name, tc := range tests {
109+
t.Run(name, func(t *testing.T) {
126110
actualRes := ExtractFirstJSONFromString(tc.text)
127111
require.Equal(t, tc.expected, actualRes)
128112
})
129113
}
130114
}
131115

132116
func Test_addLogWhenValExist(t *testing.T) {
133-
tests := []struct {
134-
name string
117+
tests := map[string]struct {
135118
title string
136119
val string
137120
expected string
138121
}{
139-
{
140-
name: "Empty string when val is empty",
122+
"Empty string when val is empty": {
141123
title: "hello world",
142124
val: "",
143125
expected: "",
144126
},
145-
{
146-
name: "Empty string when val only has space",
127+
"Empty string when val only has space": {
147128
title: "hello world",
148129
val: " ",
149130
expected: "",
150131
},
151-
{
152-
name: "Return string when val is not empty",
132+
"Return string when val is not empty": {
153133
title: "hello world",
154134
val: "slack",
155135
expected: "hello world: [slack]\n",
156136
},
157137
}
158-
for _, tc := range tests {
159-
t.Run(tc.name, func(t *testing.T) {
138+
for name, tc := range tests {
139+
t.Run(name, func(t *testing.T) {
160140
output := AddLogWhenValExist(tc.title, tc.val)
161141
require.Equal(t, output, tc.expected)
162142
})
@@ -388,64 +368,55 @@ func Test_RedactPII(t *testing.T) {
388368
}
389369
}
390370
func Test_UpperCaseTrimAll(t *testing.T) {
391-
tests := []struct {
392-
name string
371+
tests := map[string]struct {
393372
namedEntities string
394373
expected string
395374
}{
396-
{
397-
name: "Empty string when val is empty",
375+
"Empty string when val is empty": {
398376
namedEntities: "",
399377
expected: "",
400378
},
401-
{
402-
name: "Empty string when val only has space",
379+
"Empty string when val only has space": {
403380
namedEntities: " ",
404381
expected: "",
405382
},
406-
{
407-
name: "Return string when val is all lower-case",
383+
"Return string when val is all lower-case": {
408384
namedEntities: "slack",
409385
expected: "SLACK",
410386
},
411-
{
412-
name: "Return string when val contains spaces",
387+
"Return string when val contains spaces": {
413388
namedEntities: "hello, world",
414389
expected: "HELLO,WORLD",
415390
},
416391
}
417-
for _, tc := range tests {
418-
t.Run(tc.name, func(t *testing.T) {
392+
for name, tc := range tests {
393+
t.Run(name, func(t *testing.T) {
419394
output := UpperCaseTrimAll(tc.namedEntities)
420395
require.Equal(t, output, tc.expected)
421396
})
422397
}
423398
}
424399

425400
func Test_ToHTTPS(t *testing.T) {
426-
tests := []struct {
427-
name string
401+
tests := map[string]struct {
428402
urlAddr string
429403
expected string
430404
}{
431-
{
432-
name: "url with https protocol",
405+
"url with https protocol": {
433406
urlAddr: "https://www.xyz.com",
434407
expected: "https://www.xyz.com",
435408
},
436-
{
437-
name: "url with http protocol",
409+
"url with http protocol": {
438410
urlAddr: "http://www.xyz.com",
439411
expected: "https://www.xyz.com",
440412
},
441-
{
442-
name: "url with no protocol",
413+
"url with no protocol": {
443414
urlAddr: "www.xyz.com",
444415
expected: "https://www.xyz.com",
445416
},
446417
}
447-
for _, tc := range tests {
448-
t.Run(tc.name, func(t *testing.T) {
418+
for name, tc := range tests {
419+
t.Run(name, func(t *testing.T) {
449420
output := ToHTTPS(tc.urlAddr)
450421
require.Equal(t, output, tc.expected)
451422
})

internal/shared/types/channel_test.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,31 @@ import (
2121
)
2222

2323
func Test_SlackChannel_String(t *testing.T) {
24-
tests := []struct {
25-
name string
24+
tests := map[string]struct {
2625
channel *SlackChannel
2726
expectedString string
2827
}{
29-
{
30-
name: "Both ChannelName and ID exist",
28+
"Both ChannelName and ID exist": {
3129
channel: &SlackChannel{ChannelName: "#general", ID: "C01234"},
3230
expectedString: "#general (C01234)",
3331
},
3432
// TODO(@mbrooks) This test represents the current behaviour, but should the expectedString be "#general" instead?
35-
{
36-
name: "Only ChannelName exists",
33+
"Only ChannelName exists": {
3734
channel: &SlackChannel{ChannelName: "#general"},
3835
expectedString: "(#general)",
3936
},
4037
// TODO(@mbrooks) This test represents the current behaviour, but should the expectedString be "C01234" instead?
41-
{
42-
name: "Only ID exists",
38+
"Only ID exists": {
4339
channel: &SlackChannel{ID: "C01234"},
4440
expectedString: "",
4541
},
46-
{
47-
name: "Both ChannelName and ID do not exist",
42+
"Both ChannelName and ID do not exist": {
4843
channel: &SlackChannel{},
4944
expectedString: "",
5045
},
5146
}
52-
for _, tc := range tests {
53-
t.Run(tc.name, func(t *testing.T) {
47+
for name, tc := range tests {
48+
t.Run(name, func(t *testing.T) {
5449
require.Equal(t, tc.channel.String(), tc.expectedString)
5550
})
5651
}

0 commit comments

Comments
 (0)