Skip to content

Commit b26fd64

Browse files
committed
refactor(tests): convert the low-risk slice-to-map table tests
1 parent 7a210a8 commit b26fd64

File tree

5 files changed

+76
-130
lines changed

5 files changed

+76
-130
lines changed

internal/runtime/runtime_test.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,29 @@ import (
2727
)
2828

2929
func Test_Runtime_New(t *testing.T) {
30-
tests := []struct {
31-
name string
30+
tests := map[string]struct {
3231
runtime string
3332
expectedRuntimeType Runtime
3433
}{
35-
{
36-
name: "Deno SDK",
34+
"Deno SDK": {
3735
runtime: "deno",
3836
expectedRuntimeType: deno.New(),
3937
},
40-
{
41-
name: "Bolt for JavaScript",
38+
"Bolt for JavaScript": {
4239
runtime: "node",
4340
expectedRuntimeType: node.New(),
4441
},
45-
{
46-
name: "Bolt for Python",
42+
"Bolt for Python": {
4743
runtime: "python",
4844
expectedRuntimeType: python.New(),
4945
},
50-
{
51-
name: "Unsupported Runtime",
46+
"Unsupported Runtime": {
5247
runtime: "biggly-boo",
5348
expectedRuntimeType: nil,
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
// Run the test
5954
rt, _ := New(tc.runtime)
6055
require.IsType(t, tc.expectedRuntimeType, rt)

internal/shared/types/app_manifest_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,19 @@ import (
2626
)
2727

2828
func Test_RawJSON_UnmarshalJSON(t *testing.T) {
29-
tests := []struct {
30-
name string
29+
tests := map[string]struct {
3130
blob string
3231
expectedErrorType error
3332
expectedJSONData string
3433
}{
35-
{
36-
name: "Unmarshal data",
34+
"Unmarshal data": {
3735
blob: `{ "name": "foo" }`,
3836
expectedErrorType: nil,
3937
expectedJSONData: `{ "name": "foo" }`,
4038
},
4139
}
42-
for _, tc := range tests {
43-
t.Run(tc.name, func(t *testing.T) {
40+
for name, tc := range tests {
41+
t.Run(name, func(t *testing.T) {
4442
rawJSON := &RawJSON{}
4543
err := rawJSON.UnmarshalJSON([]byte(tc.blob))
4644

internal/shared/types/auth_test.go

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,21 @@ import (
2222
)
2323

2424
func Test_SlackAuth_AuthLevel(t *testing.T) {
25-
tests := []struct {
26-
name string
25+
tests := map[string]struct {
2726
auth *SlackAuth
2827
expectedAuthLevel string
2928
}{
30-
{
31-
name: "Workspace-level Auth",
29+
"Workspace-level Auth": {
3230
auth: &SlackAuth{IsEnterpriseInstall: false},
3331
expectedAuthLevel: AuthLevelWorkspace,
3432
},
35-
{
36-
name: "Enterprise-level Auth",
33+
"Enterprise-level Auth": {
3734
auth: &SlackAuth{IsEnterpriseInstall: true},
3835
expectedAuthLevel: AuthLevelEnterprise,
3936
},
4037
}
41-
for _, tc := range tests {
42-
t.Run(tc.name, func(t *testing.T) {
38+
for name, tc := range tests {
39+
t.Run(name, func(t *testing.T) {
4340
require.Equal(t, tc.auth.AuthLevel(), tc.expectedAuthLevel)
4441
})
4542
}
@@ -50,44 +47,37 @@ func Test_SlackAuth_ShouldRotateToken(t *testing.T) {
5047
var refreshToken = "fakeRefreshToken"
5148
var timeNow = int(time.Now().Unix())
5249

53-
tests := []struct {
54-
name string
50+
tests := map[string]struct {
5551
input *SlackAuth
5652
expected bool
5753
}{
58-
{
59-
name: "nil case",
54+
"nil case": {
6055
input: nil,
6156
expected: false,
6257
},
63-
{
64-
name: "token but no refresh token",
58+
"token but no refresh token": {
6559
input: &SlackAuth{Token: token},
6660
expected: false,
6761
},
68-
{
69-
name: "token + refresh token present but expiration time is absent",
62+
"token + refresh token present but expiration time is absent": {
7063
input: &SlackAuth{Token: token, RefreshToken: refreshToken},
7164
expected: false,
7265
},
73-
{
74-
name: "token + refresh token + expiration present - but token expires in less than 5min",
66+
"token + refresh token + expiration present - but token expires in less than 5min": {
7567
input: &SlackAuth{Token: token, RefreshToken: refreshToken, ExpiresAt: timeNow + 290},
7668
expected: true,
7769
},
78-
{
79-
name: "token + refresh token + expiration present - and token does not expire in less than 5min",
70+
"token + refresh token + expiration present - and token does not expire in less than 5min": {
8071
input: &SlackAuth{Token: token, RefreshToken: refreshToken, ExpiresAt: timeNow + 310},
8172
expected: false,
8273
},
83-
{
84-
name: "token + refresh token + expiration present - and token expires in exactly 5min",
74+
"token + refresh token + expiration present - and token expires in exactly 5min": {
8575
input: &SlackAuth{Token: token, RefreshToken: refreshToken, ExpiresAt: timeNow + 300},
8676
expected: true,
8777
},
8878
}
89-
for _, tc := range tests {
90-
t.Run(tc.name, func(t *testing.T) {
79+
for name, tc := range tests {
80+
t.Run(name, func(t *testing.T) {
9181
require.Equal(t, tc.expected, tc.input.ShouldRotateToken())
9282
})
9383
}
@@ -97,34 +87,29 @@ func Test_SlackAuth_TokenIsExpired(t *testing.T) {
9787
var token = "fakeToken"
9888
var timeNow = int(time.Now().Unix())
9989

100-
tests := []struct {
101-
name string
90+
tests := map[string]struct {
10291
input *SlackAuth
10392
expected bool
10493
}{
105-
{
106-
name: "nil case",
94+
"nil case": {
10795
input: nil,
10896
expected: false,
10997
},
110-
{
111-
name: "token but no expiration",
98+
"token but no expiration": {
11299
input: &SlackAuth{Token: token},
113100
expected: false,
114101
},
115-
{
116-
name: "token + expiration present - but token is expired",
102+
"token + expiration present - but token is expired": {
117103
input: &SlackAuth{Token: token, ExpiresAt: timeNow - 1},
118104
expected: true,
119105
},
120-
{
121-
name: "token + expiration present - and token is not expired",
106+
"token + expiration present - and token is not expired": {
122107
input: &SlackAuth{Token: token, ExpiresAt: timeNow + 1},
123108
expected: false,
124109
},
125110
}
126-
for _, tc := range tests {
127-
t.Run(tc.name, func(t *testing.T) {
111+
for name, tc := range tests {
112+
t.Run(name, func(t *testing.T) {
128113
require.Equal(t, tc.expected, tc.input.TokenIsExpired())
129114
})
130115
}

internal/shared/types/icon_test.go

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,24 @@ import (
2222
)
2323

2424
func Test_Icons_MarshalJSON(t *testing.T) {
25-
tests := []struct {
26-
name string
25+
tests := map[string]struct {
2726
icons *Icons
2827
expectedErrorType error
2928
expectedBlobs []string
3029
}{
31-
{
32-
name: "Marshal no icons",
30+
"Marshal no icons": {
3331
icons: &Icons{},
3432
expectedErrorType: nil,
3533
expectedBlobs: []string{},
3634
},
37-
{
38-
name: "Marshal 1 icon",
35+
"Marshal 1 icon": {
3936
icons: &Icons{"image_96": "path/to/image_96.png"},
4037
expectedErrorType: nil,
4138
expectedBlobs: []string{
4239
`"image_96":"path/to/image_96.png"`,
4340
},
4441
},
45-
{
46-
name: "Marshal 2 icons",
42+
"Marshal 2 icons": {
4743
icons: &Icons{"image_96": "path/to/image_96.png", "image_192": "path/to/image_192.png"},
4844
expectedErrorType: nil,
4945
expectedBlobs: []string{
@@ -52,8 +48,8 @@ func Test_Icons_MarshalJSON(t *testing.T) {
5248
},
5349
},
5450
}
55-
for _, tc := range tests {
56-
t.Run(tc.name, func(t *testing.T) {
51+
for name, tc := range tests {
52+
t.Run(name, func(t *testing.T) {
5753
returnedBlob, err := json.Marshal(tc.icons)
5854

5955
require.IsType(t, err, tc.expectedErrorType)
@@ -65,65 +61,57 @@ func Test_Icons_MarshalJSON(t *testing.T) {
6561
}
6662

6763
func Test_Icons_UnmarshalJSON(t *testing.T) {
68-
tests := []struct {
69-
name string
64+
tests := map[string]struct {
7065
blob string
7166
icons *Icons
7267
expectedErrorType error
7368
expectedIcons *Icons
7469
}{
75-
{
76-
name: "JSON unmarshal error",
70+
"JSON unmarshal error": {
7771
blob: `{ "image_96": 100 }`, // expects type to be string not int
7872
icons: &Icons{},
7973
expectedErrorType: &json.UnmarshalTypeError{},
8074
expectedIcons: &Icons{},
8175
},
82-
{
83-
name: "image_96 and image_192 do not exist",
76+
"image_96 and image_192 do not exist": {
8477
blob: `{ "cat": "meow", "dog": "bark" }`,
8578
icons: &Icons{},
8679
expectedErrorType: nil,
8780
expectedIcons: &Icons{},
8881
},
89-
{
90-
name: "image_96 exists",
82+
"image_96 exists": {
9183
blob: `{ "image_96": "path/to/image_96.png" }`,
9284
icons: &Icons{},
9385
expectedErrorType: nil,
9486
expectedIcons: &Icons{"image_96": "path/to/image_96.png"},
9587
},
96-
{
97-
name: "image_192 exists",
88+
"image_192 exists": {
9889
blob: `{ "image_192": "path/to/image_192.png" }`,
9990
icons: &Icons{},
10091
expectedErrorType: nil,
10192
expectedIcons: &Icons{"image_192": "path/to/image_192.png"},
10293
},
103-
{
104-
name: "image_96 and image_192 exist",
94+
"image_96 and image_192 exist": {
10595
blob: `{ "image_96": "path/to/image_96.png", "image_192": "path/to/image_192.png" }`,
10696
icons: &Icons{},
10797
expectedErrorType: nil,
10898
expectedIcons: &Icons{"image_96": "path/to/image_96.png", "image_192": "path/to/image_192.png"},
10999
},
110-
{
111-
name: "image_96 exists and unsupported properties exists",
100+
"image_96 exists and unsupported properties exists": {
112101
blob: `{ "image_96": "path/to/image_96.png", "foo": "bar" }`,
113102
icons: &Icons{},
114103
expectedErrorType: nil,
115104
expectedIcons: &Icons{"image_96": "path/to/image_96.png", "foo": "bar"},
116105
},
117-
{
118-
name: "Icons is nil, image_96 and image_192 exist",
106+
"Icons is nil, image_96 and image_192 exist": {
119107
blob: `{ "image_96": "path/to/image_96.png", "image_192": "path/to/image_192.png" }`,
120108
icons: nil,
121109
expectedErrorType: nil,
122110
expectedIcons: &Icons{"image_96": "path/to/image_96.png", "image_192": "path/to/image_192.png"},
123111
},
124112
}
125-
for _, tc := range tests {
126-
t.Run(tc.name, func(t *testing.T) {
113+
for name, tc := range tests {
114+
t.Run(name, func(t *testing.T) {
127115
err := json.Unmarshal([]byte(tc.blob), &tc.icons)
128116

129117
require.IsType(t, err, tc.expectedErrorType)

0 commit comments

Comments
 (0)