-
Notifications
You must be signed in to change notification settings - Fork 430
refactor: consolidate triplicate merge helpers and add sliceutil.SortedKeys #41388
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 |
|---|---|---|
|
|
@@ -81,6 +81,34 @@ func TestSpec_PublicAPI_FilterMapKeys(t *testing.T) { | |
| }) | ||
| } | ||
|
|
||
| // TestSpec_PublicAPI_SortedKeys validates the documented behavior of SortedKeys | ||
| // as described in the sliceutil README.md specification. | ||
| func TestSpec_PublicAPI_SortedKeys(t *testing.T) { | ||
| t.Run("returns map keys in sorted order", func(t *testing.T) { | ||
| m := map[string]int{"banana": 2, "apple": 1, "cherry": 3} | ||
| keys := SortedKeys(m) | ||
| assert.Equal(t, []string{"apple", "banana", "cherry"}, keys, "SortedKeys should return keys in sorted order") | ||
| }) | ||
|
|
||
| t.Run("returns empty slice for empty map", func(t *testing.T) { | ||
| m := map[string]int{} | ||
| keys := SortedKeys(m) | ||
| assert.Empty(t, keys, "SortedKeys of empty map should return empty slice") | ||
|
Contributor
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. Test does not pin nil-map return contract: 💡 Suggested fixAdd a nil-map sub-test that explicitly asserts the return value is t.Run("returns nil for nil map", func(t *testing.T) {
var m map[string]int
keys := SortedKeys(m)
assert.Nil(t, keys, "SortedKeys of nil map should return nil, not an empty slice")
})This matters because callers that JSON-serialize the result get |
||
| }) | ||
|
|
||
| t.Run("returns nil for nil map", func(t *testing.T) { | ||
| var m map[string]int | ||
| keys := SortedKeys(m) | ||
| assert.Nil(t, keys, "SortedKeys of nil map should return nil, not an empty slice") | ||
| }) | ||
|
|
||
| t.Run("works with non-string ordered key types", func(t *testing.T) { | ||
| m := map[int]string{3: "c", 1: "a", 2: "b"} | ||
| keys := SortedKeys(m) | ||
| assert.Equal(t, []int{1, 2, 3}, keys, "SortedKeys should sort integer keys numerically") | ||
| }) | ||
| } | ||
|
|
||
| // TestSpec_PublicAPI_Any validates the documented behavior of Any as described | ||
| // in the sliceutil README.md specification. | ||
| func TestSpec_PublicAPI_Any(t *testing.T) { | ||
|
|
||
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.
[/tdd] Missing nil-map test case — the spec covers empty maps but not
nilmaps.Go's
maps.Keyshandles nil maps safely (produces an empty iterator), but an explicit test makes the contract clear and guards against implementation changes.💡 Suggested test case
This rounds out the spec alongside the existing empty-map and typed-key cases.