diff --git a/internal/runtime/runtime_test.go b/internal/runtime/runtime_test.go index 53263a7..1652a3b 100644 --- a/internal/runtime/runtime_test.go +++ b/internal/runtime/runtime_test.go @@ -27,34 +27,29 @@ import ( ) func Test_Runtime_New(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { runtime string expectedRuntimeType Runtime }{ - { - name: "Deno SDK", + "Deno SDK": { runtime: "deno", expectedRuntimeType: deno.New(), }, - { - name: "Bolt for JavaScript", + "Bolt for JavaScript": { runtime: "node", expectedRuntimeType: node.New(), }, - { - name: "Bolt for Python", + "Bolt for Python": { runtime: "python", expectedRuntimeType: python.New(), }, - { - name: "Unsupported Runtime", + "Unsupported Runtime": { runtime: "biggly-boo", expectedRuntimeType: nil, }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { // Run the test rt, _ := New(tc.runtime) require.IsType(t, tc.expectedRuntimeType, rt) diff --git a/internal/shared/types/app_manifest_test.go b/internal/shared/types/app_manifest_test.go index bf62684..5f6e069 100644 --- a/internal/shared/types/app_manifest_test.go +++ b/internal/shared/types/app_manifest_test.go @@ -26,21 +26,19 @@ import ( ) func Test_RawJSON_UnmarshalJSON(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { blob string expectedErrorType error expectedJSONData string }{ - { - name: "Unmarshal data", + "Unmarshal data": { blob: `{ "name": "foo" }`, expectedErrorType: nil, expectedJSONData: `{ "name": "foo" }`, }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { rawJSON := &RawJSON{} err := rawJSON.UnmarshalJSON([]byte(tc.blob)) diff --git a/internal/shared/types/auth_test.go b/internal/shared/types/auth_test.go index b8c199a..60873bb 100644 --- a/internal/shared/types/auth_test.go +++ b/internal/shared/types/auth_test.go @@ -22,24 +22,21 @@ import ( ) func Test_SlackAuth_AuthLevel(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { auth *SlackAuth expectedAuthLevel string }{ - { - name: "Workspace-level Auth", + "Workspace-level Auth": { auth: &SlackAuth{IsEnterpriseInstall: false}, expectedAuthLevel: AuthLevelWorkspace, }, - { - name: "Enterprise-level Auth", + "Enterprise-level Auth": { auth: &SlackAuth{IsEnterpriseInstall: true}, expectedAuthLevel: AuthLevelEnterprise, }, } - 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.auth.AuthLevel(), tc.expectedAuthLevel) }) } @@ -50,44 +47,37 @@ func Test_SlackAuth_ShouldRotateToken(t *testing.T) { var refreshToken = "fakeRefreshToken" var timeNow = int(time.Now().Unix()) - tests := []struct { - name string + tests := map[string]struct { input *SlackAuth expected bool }{ - { - name: "nil case", + "nil case": { input: nil, expected: false, }, - { - name: "token but no refresh token", + "token but no refresh token": { input: &SlackAuth{Token: token}, expected: false, }, - { - name: "token + refresh token present but expiration time is absent", + "token + refresh token present but expiration time is absent": { input: &SlackAuth{Token: token, RefreshToken: refreshToken}, expected: false, }, - { - name: "token + refresh token + expiration present - but token expires in less than 5min", + "token + refresh token + expiration present - but token expires in less than 5min": { input: &SlackAuth{Token: token, RefreshToken: refreshToken, ExpiresAt: timeNow + 290}, expected: true, }, - { - name: "token + refresh token + expiration present - and token does not expire in less than 5min", + "token + refresh token + expiration present - and token does not expire in less than 5min": { input: &SlackAuth{Token: token, RefreshToken: refreshToken, ExpiresAt: timeNow + 310}, expected: false, }, - { - name: "token + refresh token + expiration present - and token expires in exactly 5min", + "token + refresh token + expiration present - and token expires in exactly 5min": { input: &SlackAuth{Token: token, RefreshToken: refreshToken, ExpiresAt: timeNow + 300}, 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) { require.Equal(t, tc.expected, tc.input.ShouldRotateToken()) }) } @@ -97,34 +87,29 @@ func Test_SlackAuth_TokenIsExpired(t *testing.T) { var token = "fakeToken" var timeNow = int(time.Now().Unix()) - tests := []struct { - name string + tests := map[string]struct { input *SlackAuth expected bool }{ - { - name: "nil case", + "nil case": { input: nil, expected: false, }, - { - name: "token but no expiration", + "token but no expiration": { input: &SlackAuth{Token: token}, expected: false, }, - { - name: "token + expiration present - but token is expired", + "token + expiration present - but token is expired": { input: &SlackAuth{Token: token, ExpiresAt: timeNow - 1}, expected: true, }, - { - name: "token + expiration present - and token is not expired", + "token + expiration present - and token is not expired": { input: &SlackAuth{Token: token, ExpiresAt: timeNow + 1}, expected: 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) { require.Equal(t, tc.expected, tc.input.TokenIsExpired()) }) } diff --git a/internal/shared/types/channel_test.go b/internal/shared/types/channel_test.go index 192c0d3..78a6793 100644 --- a/internal/shared/types/channel_test.go +++ b/internal/shared/types/channel_test.go @@ -29,12 +29,12 @@ func Test_SlackChannel_String(t *testing.T) { channel: &SlackChannel{ChannelName: "#general", ID: "C01234"}, expectedString: "#general (C01234)", }, - // TODO(@mbrooks) This test represents the current behaviour, but should the expectedString be "#general" instead? + // This test represents the current behaviour, but should the expectedString be "#general" instead? "Only ChannelName exists": { channel: &SlackChannel{ChannelName: "#general"}, expectedString: "(#general)", }, - // TODO(@mbrooks) This test represents the current behaviour, but should the expectedString be "C01234" instead? + // This test represents the current behaviour, but should the expectedString be "C01234" instead? "Only ID exists": { channel: &SlackChannel{ID: "C01234"}, expectedString: "", diff --git a/internal/shared/types/icon_test.go b/internal/shared/types/icon_test.go index eb45ccc..fe24f0d 100644 --- a/internal/shared/types/icon_test.go +++ b/internal/shared/types/icon_test.go @@ -22,28 +22,24 @@ import ( ) func Test_Icons_MarshalJSON(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { icons *Icons expectedErrorType error expectedBlobs []string }{ - { - name: "Marshal no icons", + "Marshal no icons": { icons: &Icons{}, expectedErrorType: nil, expectedBlobs: []string{}, }, - { - name: "Marshal 1 icon", + "Marshal 1 icon": { icons: &Icons{"image_96": "path/to/image_96.png"}, expectedErrorType: nil, expectedBlobs: []string{ `"image_96":"path/to/image_96.png"`, }, }, - { - name: "Marshal 2 icons", + "Marshal 2 icons": { icons: &Icons{"image_96": "path/to/image_96.png", "image_192": "path/to/image_192.png"}, expectedErrorType: nil, expectedBlobs: []string{ @@ -52,8 +48,8 @@ func Test_Icons_MarshalJSON(t *testing.T) { }, }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { returnedBlob, err := json.Marshal(tc.icons) require.IsType(t, err, tc.expectedErrorType) @@ -65,65 +61,57 @@ func Test_Icons_MarshalJSON(t *testing.T) { } func Test_Icons_UnmarshalJSON(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { blob string icons *Icons expectedErrorType error expectedIcons *Icons }{ - { - name: "JSON unmarshal error", + "JSON unmarshal error": { blob: `{ "image_96": 100 }`, // expects type to be string not int icons: &Icons{}, expectedErrorType: &json.UnmarshalTypeError{}, expectedIcons: &Icons{}, }, - { - name: "image_96 and image_192 do not exist", + "image_96 and image_192 do not exist": { blob: `{ "cat": "meow", "dog": "bark" }`, icons: &Icons{}, expectedErrorType: nil, expectedIcons: &Icons{}, }, - { - name: "image_96 exists", + "image_96 exists": { blob: `{ "image_96": "path/to/image_96.png" }`, icons: &Icons{}, expectedErrorType: nil, expectedIcons: &Icons{"image_96": "path/to/image_96.png"}, }, - { - name: "image_192 exists", + "image_192 exists": { blob: `{ "image_192": "path/to/image_192.png" }`, icons: &Icons{}, expectedErrorType: nil, expectedIcons: &Icons{"image_192": "path/to/image_192.png"}, }, - { - name: "image_96 and image_192 exist", + "image_96 and image_192 exist": { blob: `{ "image_96": "path/to/image_96.png", "image_192": "path/to/image_192.png" }`, icons: &Icons{}, expectedErrorType: nil, expectedIcons: &Icons{"image_96": "path/to/image_96.png", "image_192": "path/to/image_192.png"}, }, - { - name: "image_96 exists and unsupported properties exists", + "image_96 exists and unsupported properties exists": { blob: `{ "image_96": "path/to/image_96.png", "foo": "bar" }`, icons: &Icons{}, expectedErrorType: nil, expectedIcons: &Icons{"image_96": "path/to/image_96.png", "foo": "bar"}, }, - { - name: "Icons is nil, image_96 and image_192 exist", + "Icons is nil, image_96 and image_192 exist": { blob: `{ "image_96": "path/to/image_96.png", "image_192": "path/to/image_192.png" }`, icons: nil, expectedErrorType: nil, expectedIcons: &Icons{"image_96": "path/to/image_96.png", "image_192": "path/to/image_192.png"}, }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { err := json.Unmarshal([]byte(tc.blob), &tc.icons) require.IsType(t, err, tc.expectedErrorType) diff --git a/internal/shared/types/permissions_test.go b/internal/shared/types/permissions_test.go index f2e780d..d85adfb 100644 --- a/internal/shared/types/permissions_test.go +++ b/internal/shared/types/permissions_test.go @@ -23,33 +23,29 @@ import ( ) func Test_Permissions_StringToAppCollaboratorPermission(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { input string expectedErrorType error expectedAppCollaboratorPermission AppCollaboratorPermission }{ - { - name: "owner", + "owner": { input: "owner", expectedErrorType: nil, expectedAppCollaboratorPermission: OWNER, }, - { - name: "reader", + "reader": { input: "reader", expectedErrorType: nil, expectedAppCollaboratorPermission: READER, }, - { - name: "default", + "default": { input: "", expectedErrorType: fmt.Errorf("invalid"), expectedAppCollaboratorPermission: "", }, } - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { + for name, tc := range tests { + t.Run(name, func(t *testing.T) { appCollaboratorPermission, err := StringToAppCollaboratorPermission(tc.input) require.IsType(t, err, tc.expectedErrorType) @@ -59,29 +55,25 @@ func Test_Permissions_StringToAppCollaboratorPermission(t *testing.T) { } func Test_Permissions_AppCollaboratorPermissionF(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { acp AppCollaboratorPermission expectedString string }{ - { - name: "owner", + "owner": { acp: OWNER, expectedString: "an owner collaborator", }, - { - name: "reader", + "reader": { acp: READER, expectedString: "a reader collaborator", }, - { - name: "default", + "default": { acp: "", expectedString: "a collaborator", }, } - 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.acp.AppCollaboratorPermissionF() require.Equal(t, tc.expectedString, returnedString) @@ -90,44 +82,37 @@ func Test_Permissions_AppCollaboratorPermissionF(t *testing.T) { } func Test_Permission_IsValid(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { permission Permission expectedIsValid bool }{ - { - name: "Permission App Collaborators", + "Permission App Collaborators": { permission: PermissionAppCollaborators, expectedIsValid: true, }, - { - name: "Permission Everyone", + "Permission Everyone": { permission: PermissionEveryone, expectedIsValid: true, }, - { - name: "Permission Named Entities", + "Permission Named Entities": { permission: PermissionNamedEntities, expectedIsValid: true, }, - { - name: "Invalid empty", + "Invalid empty": { permission: "", expectedIsValid: false, }, - { - name: "Invalid whitespace", + "Invalid whitespace": { permission: " \n ", expectedIsValid: false, }, - { - name: "Invalid string", + "Invalid string": { permission: "cats_and_dogs", expectedIsValid: 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) { returnedIsValid := tc.permission.IsValid() require.Equal(t, tc.expectedIsValid, returnedIsValid) @@ -136,34 +121,29 @@ func Test_Permission_IsValid(t *testing.T) { } func Test_Permission_ToString(t *testing.T) { - tests := []struct { - name string + tests := map[string]struct { permission Permission expectedString string }{ - { - name: "Permission App Collaborators", + "Permission App Collaborators": { permission: PermissionAppCollaborators, expectedString: "app collaborators", }, - { - name: "Permission Everyone", + "Permission Everyone": { permission: PermissionEveryone, expectedString: "everyone", }, - { - name: "Permission Named Entities", + "Permission Named Entities": { permission: PermissionNamedEntities, expectedString: "specific entities", }, - { - name: "Invalid string", + "Invalid string": { permission: "cats_and_dogs", 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.permission.ToString() require.Equal(t, tc.expectedString, returnedString)