-
Notifications
You must be signed in to change notification settings - Fork 31
test: add units tests for untested functions in style, api, types, and create packages #427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,24 @@ import ( | |
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func Test_Template_GetSubdir(t *testing.T) { | ||
| t.Run("default is empty string", func(t *testing.T) { | ||
| tmpl := Template{} | ||
| assert.Equal(t, "", tmpl.GetSubdir()) | ||
| }) | ||
| t.Run("returns value set by SetSubdir", func(t *testing.T) { | ||
| tmpl := Template{} | ||
| tmpl.SetSubdir("subpath") | ||
| assert.Equal(t, "subpath", tmpl.GetSubdir()) | ||
| }) | ||
| } | ||
|
|
||
| func Test_Template_SetSubdir(t *testing.T) { | ||
| tmpl := Template{} | ||
| tmpl.SetSubdir("custom/dir") | ||
| assert.Equal(t, "custom/dir", tmpl.GetSubdir()) | ||
| } | ||
|
|
||
|
Comment on lines
+26
to
+43
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: It makes me feel good to see these have some tests added. |
||
| func TestTemplate_ResolveTemplateURL(t *testing.T) { | ||
| tests := map[string]struct { | ||
| url string | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,34 @@ func Test_RawJSON_UnmarshalJSON(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func Test_RawJSON_MarshalJSON(t *testing.T) { | ||
| tests := map[string]struct { | ||
| rawJSON RawJSON | ||
| expected string | ||
| }{ | ||
| "marshals JSONData when present": { | ||
| rawJSON: func() RawJSON { | ||
| raw := json.RawMessage(`{"name":"foo"}`) | ||
| return RawJSON{JSONData: &raw} | ||
| }(), | ||
|
Comment on lines
+57
to
+60
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: TIL about an anonymous function as a way to populate a variable in our test tables. It's actually handy, although it can be difficult to read when overused. |
||
| expected: `{"name":"foo"}`, | ||
| }, | ||
| "marshals from Data when JSONData is nil": { | ||
| rawJSON: RawJSON{Data: &yaml.MapSlice{ | ||
| {Key: "name", Value: "bar"}, | ||
| }}, | ||
| expected: `{"name":"bar"}`, | ||
| }, | ||
| } | ||
| for name, tc := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| result, err := tc.rawJSON.MarshalJSON() | ||
| require.NoError(t, err) | ||
| assert.Equal(t, tc.expected, string(result)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func Test_RawJSON_UnmarshalYAML(t *testing.T) { | ||
| rawJSON := RawJSON{Data: &yaml.MapSlice{ | ||
| {Key: "name", Value: "foo"}, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // Copyright 2022-2026 Salesforce, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package types | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func Test_SlackYaml_hasValidIconPath(t *testing.T) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. threat: I'm coming for you |
||
| tests := map[string]struct { | ||
| icon string | ||
| setup func(t *testing.T, dir string) | ||
| expected bool | ||
| }{ | ||
| "valid custom icon path returns true": { | ||
| icon: "custom/icon.png", | ||
| setup: func(t *testing.T, dir string) { | ||
| require.NoError(t, os.MkdirAll(filepath.Join(dir, "custom"), 0o755)) | ||
| require.NoError(t, os.WriteFile(filepath.Join(dir, "custom", "icon.png"), []byte("img"), 0o644)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎁 praise: Exciting tests to find for this YAML package to get in meantimes! |
||
| }, | ||
| expected: true, | ||
| }, | ||
| "invalid custom icon path returns false": { | ||
| icon: "missing/icon.png", | ||
| setup: func(t *testing.T, dir string) {}, | ||
| expected: false, | ||
| }, | ||
| "no icon with default assets/icon.png present returns true": { | ||
| icon: "", | ||
| setup: func(t *testing.T, dir string) { | ||
| require.NoError(t, os.MkdirAll(filepath.Join(dir, "assets"), 0o755)) | ||
| require.NoError(t, os.WriteFile(filepath.Join(dir, "assets", "icon.png"), []byte("img"), 0o644)) | ||
| }, | ||
| expected: true, | ||
| }, | ||
| "no icon and no default returns true": { | ||
| icon: "", | ||
| setup: func(t *testing.T, dir string) {}, | ||
| expected: true, | ||
| }, | ||
| } | ||
| for name, tc := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| dir := t.TempDir() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 thought: It stinks we aren't using
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, we have a few places where we use |
||
| tc.setup(t, dir) | ||
|
|
||
| origDir, err := os.Getwd() | ||
| require.NoError(t, err) | ||
| require.NoError(t, os.Chdir(dir)) | ||
| defer func() { require.NoError(t, os.Chdir(origDir)) }() | ||
|
|
||
| sy := &SlackYaml{Icon: tc.icon} | ||
| assert.Equal(t, tc.expected, sy.hasValidIconPath()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func Test_SlackYaml_Verify(t *testing.T) { | ||
| tests := map[string]struct { | ||
| icon string | ||
| setup func(t *testing.T, dir string) | ||
| expectErr bool | ||
| }{ | ||
| "valid icon returns nil error": { | ||
| icon: "icon.png", | ||
| setup: func(t *testing.T, dir string) { | ||
| require.NoError(t, os.WriteFile(filepath.Join(dir, "icon.png"), []byte("img"), 0o644)) | ||
| }, | ||
| expectErr: false, | ||
| }, | ||
| "invalid icon returns error": { | ||
| icon: "missing.png", | ||
| setup: func(t *testing.T, dir string) {}, | ||
| expectErr: true, | ||
| }, | ||
| } | ||
| for name, tc := range tests { | ||
| t.Run(name, func(t *testing.T) { | ||
| dir := t.TempDir() | ||
| tc.setup(t, dir) | ||
|
|
||
| origDir, err := os.Getwd() | ||
| require.NoError(t, err) | ||
| require.NoError(t, os.Chdir(dir)) | ||
| defer func() { require.NoError(t, os.Chdir(origDir)) }() | ||
|
|
||
| sy := &SlackYaml{Icon: tc.icon} | ||
| if tc.expectErr { | ||
| assert.Error(t, sy.Verify()) | ||
| } else { | ||
| assert.NoError(t, sy.Verify()) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐞 praise: Pretty time at
L222!