Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors comments in pkg/common/filter/filter.go and adds a new unit test file pkg/common/filter/filter_test.go to verify the filtering logic. The review feedback suggests improving the tests by comparing typedmap.TypedKey structs directly instead of using their string representations, and flattening the table-driven test cases in TestEnabledFilter_ShouldInclude and TestFilterTypedMapCollection to make them more idiomatic and maintainable.
| if got.Key() != wantKey.Key() { | ||
| t.Errorf("KeyToFilter().Key() = %q, want %q", got.Key(), wantKey.Key()) | ||
| } |
There was a problem hiding this comment.
Since typedmap.TypedKey is a comparable struct, you can compare the keys directly instead of comparing their string representations via .Key(). This is more robust and idiomatic.
| if got.Key() != wantKey.Key() { | |
| t.Errorf("KeyToFilter().Key() = %q, want %q", got.Key(), wantKey.Key()) | |
| } | |
| if got != wantKey { | |
| t.Errorf("KeyToFilter() = %v, want %v", got, wantKey) | |
| } |
| testCases := []struct { | ||
| name string | ||
| inputs []shouldIncludeInput | ||
| want []bool | ||
| }{ | ||
| // Absent keys ignore the passed value and return includeUndefined across all four combinations. | ||
| { | ||
| name: "absent key uses includeUndefined fallback", | ||
| inputs: []shouldIncludeInput{ | ||
| {includeUndefined: false, found: false, value: false}, | ||
| {includeUndefined: false, found: false, value: true}, | ||
| {includeUndefined: true, found: false, value: false}, | ||
| {includeUndefined: true, found: false, value: true}, | ||
| }, | ||
| want: []bool{false, false, true, true}, | ||
| }, | ||
| // Present keys ignore includeUndefined and return the stored value across all four combinations. | ||
| { | ||
| name: "present key uses stored value regardless of includeUndefined", | ||
| inputs: []shouldIncludeInput{ | ||
| {includeUndefined: false, found: true, value: false}, | ||
| {includeUndefined: false, found: true, value: true}, | ||
| {includeUndefined: true, found: true, value: false}, | ||
| {includeUndefined: true, found: true, value: true}, | ||
| }, | ||
| want: []bool{false, true, false, true}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| // Arrange. | ||
| got := make([]bool, 0, len(tc.inputs)) | ||
|
|
||
| // Act. | ||
| for _, input := range tc.inputs { | ||
| enabledFilter := NewEnabledFilter(key, input.includeUndefined) | ||
| got = append(got, enabledFilter.ShouldInclude(input.found, input.value)) | ||
| } | ||
|
|
||
| // Assert. | ||
| if diff := cmp.Diff(tc.want, got); diff != "" { | ||
| t.Errorf("ShouldInclude() mismatch (-want +got):\n%s", diff) | ||
| } | ||
| }) | ||
| } |
There was a problem hiding this comment.
The table-driven test groups multiple test inputs and expected outputs into slices within a single test case. In Go, it is more idiomatic and maintainable to flatten the table so that each test case represents a single input combination and expected output. This improves test readability and ensures that any failure points to the exact combination that failed, rather than a mismatch in a slice of results.
testCases := []struct {
name string
includeUndefined bool
found bool
value bool
want bool
}{
{
name: "absent key uses includeUndefined fallback (false)",
includeUndefined: false,
found: false,
value: false,
want: false,
},
{
name: "absent key uses includeUndefined fallback (false, value true)",
includeUndefined: false,
found: false,
value: true,
want: false,
},
{
name: "absent key uses includeUndefined fallback (true)",
includeUndefined: true,
found: false,
value: false,
want: true,
},
{
name: "absent key uses includeUndefined fallback (true, value true)",
includeUndefined: true,
found: false,
value: true,
want: true,
},
{
name: "present key uses stored value (false)",
includeUndefined: false,
found: true,
value: false,
want: false,
},
{
name: "present key uses stored value (true)",
includeUndefined: false,
found: true,
value: true,
want: true,
},
{
name: "present key uses stored value regardless of includeUndefined (false)",
includeUndefined: true,
found: true,
value: false,
want: false,
},
{
name: "present key uses stored value regardless of includeUndefined (true)",
includeUndefined: true,
found: true,
value: true,
want: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Arrange.
enabledFilter := NewEnabledFilter(key, tc.includeUndefined)
// Act.
got := enabledFilter.ShouldInclude(tc.found, tc.value)
// Assert.
if got != tc.want {
t.Errorf("ShouldInclude() = %v, want %v", got, tc.want)
}
})
}| func TestFilterTypedMapCollection(t *testing.T) { | ||
| targetKey := typedmap.NewTypedKey[bool]("enabled") | ||
| unrelatedKey := typedmap.NewTypedKey[bool]("unrelated") | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| itemBatches [][]string | ||
| includeUndefined bool | ||
| mapsByItem map[string]*typedmap.ReadonlyTypedMap | ||
| want [][]string | ||
| wantMappedItems []string | ||
| }{ | ||
| // Nil and empty item slices return nil without invoking getMap. | ||
| { | ||
| name: "nil and empty inputs return nil without invoking getMap", | ||
| itemBatches: [][]string{nil, {}}, | ||
| want: [][]string{nil, nil}, | ||
| }, | ||
| // Items that are false, missing targetKey, or only have an unrelated true key return nil. | ||
| { | ||
| name: "zero matching items returns nil", | ||
| itemBatches: [][]string{{"disabled", "missing", "other-key-true"}}, | ||
| mapsByItem: map[string]*typedmap.ReadonlyTypedMap{ | ||
| "disabled": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: false}), | ||
| "missing": newReadonlyTypedMap[bool](), | ||
| "other-key-true": newReadonlyTypedMap(mapEntry[bool]{key: unrelatedKey, value: true}), | ||
| }, | ||
| want: [][]string{nil}, | ||
| wantMappedItems: []string{"disabled", "missing", "other-key-true"}, | ||
| }, | ||
| // Matching and undefined items when includeUndefined is true are all returned in original non-sorted order. | ||
| { | ||
| name: "all matching items preserved in input order", | ||
| itemBatches: [][]string{{"item-c", "item-a", "item-b"}}, | ||
| includeUndefined: true, | ||
| mapsByItem: map[string]*typedmap.ReadonlyTypedMap{ | ||
| "item-c": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: true}), | ||
| "item-a": newReadonlyTypedMap[bool](), | ||
| "item-b": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: true}), | ||
| }, | ||
| want: [][]string{{"item-c", "item-a", "item-b"}}, | ||
| wantMappedItems: []string{"item-c", "item-a", "item-b"}, | ||
| }, | ||
| // Mixed true, false, and missing items return only true items in original order without mutating input. | ||
| { | ||
| name: "mixed items include only matches in original order without mutating input", | ||
| itemBatches: [][]string{{"item-z", "item-a", "item-m", "item-b"}}, | ||
| mapsByItem: map[string]*typedmap.ReadonlyTypedMap{ | ||
| "item-z": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: true}), | ||
| "item-a": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: false}), | ||
| "item-m": newReadonlyTypedMap[bool](), | ||
| "item-b": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: true}), | ||
| }, | ||
| want: [][]string{{"item-z", "item-b"}}, | ||
| wantMappedItems: []string{"item-z", "item-a", "item-m", "item-b"}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| // Arrange. | ||
| enabledFilter := NewEnabledFilter(targetKey, tc.includeUndefined) | ||
| originalBatches := make([][]string, len(tc.itemBatches)) | ||
| for idx, batch := range tc.itemBatches { | ||
| originalBatches[idx] = slices.Clone(batch) | ||
| } | ||
| var gotMappedItems []string | ||
| getMap := func(item string) *typedmap.ReadonlyTypedMap { | ||
| gotMappedItems = append(gotMappedItems, item) | ||
| return tc.mapsByItem[item] | ||
| } | ||
|
|
||
| // Act. | ||
| got := make([][]string, 0, len(tc.itemBatches)) | ||
| for _, batch := range tc.itemBatches { | ||
| got = append(got, FilterTypedMapCollection(batch, getMap, enabledFilter)) | ||
| } | ||
|
|
||
| // Assert. | ||
| if diff := cmp.Diff(tc.want, got); diff != "" { | ||
| t.Errorf("FilterTypedMapCollection() mismatch (-want +got):\n%s", diff) | ||
| } | ||
| if diff := cmp.Diff(tc.wantMappedItems, gotMappedItems); diff != "" { | ||
| t.Errorf("getMap call sequence mismatch (-want +got):\n%s", diff) | ||
| } | ||
| if diff := cmp.Diff(originalBatches, tc.itemBatches); diff != "" { | ||
| t.Errorf("input slices mutated (-want +got):\n%s", diff) | ||
| } | ||
| }) | ||
| } | ||
| } |
There was a problem hiding this comment.
The test case groups multiple independent slice batches into a single test case using itemBatches [][]string. Flattening the table-driven test to use a single items []string per test case simplifies the test logic, removes the need for nested loops and complex slice cloning, and aligns with Go's idiomatic testing patterns.
func TestFilterTypedMapCollection(t *testing.T) {
targetKey := typedmap.NewTypedKey[bool]("enabled")
unrelatedKey := typedmap.NewTypedKey[bool]("unrelated")
testCases := []struct {
name string
items []string
includeUndefined bool
mapsByItem map[string]*typedmap.ReadonlyTypedMap
want []string
wantMappedItems []string
}{
{
name: "nil input returns nil without invoking getMap",
items: nil,
want: nil,
},
{
name: "empty input returns nil without invoking getMap",
items: []string{},
want: nil,
},
{
name: "zero matching items returns nil",
items: []string{"disabled", "missing", "other-key-true"},
mapsByItem: map[string]*typedmap.ReadonlyTypedMap{
"disabled": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: false}),
"missing": newReadonlyTypedMap[bool](),
"other-key-true": newReadonlyTypedMap(mapEntry[bool]{key: unrelatedKey, value: true}),
},
want: nil,
wantMappedItems: []string{"disabled", "missing", "other-key-true"},
},
{
name: "all matching items preserved in input order",
items: []string{"item-c", "item-a", "item-b"},
includeUndefined: true,
mapsByItem: map[string]*typedmap.ReadonlyTypedMap{
"item-c": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: true}),
"item-a": newReadonlyTypedMap[bool](),
"item-b": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: true}),
},
want: []string{"item-c", "item-a", "item-b"},
wantMappedItems: []string{"item-c", "item-a", "item-b"},
},
{
name: "mixed items include only matches in original order without mutating input",
items: []string{"item-z", "item-a", "item-m", "item-b"},
mapsByItem: map[string]*typedmap.ReadonlyTypedMap{
"item-z": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: true}),
"item-a": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: false}),
"item-m": newReadonlyTypedMap[bool](),
"item-b": newReadonlyTypedMap(mapEntry[bool]{key: targetKey, value: true}),
},
want: []string{"item-z", "item-b"},
wantMappedItems: []string{"item-z", "item-a", "item-m", "item-b"},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Arrange.
enabledFilter := NewEnabledFilter(targetKey, tc.includeUndefined)
originalItems := slices.Clone(tc.items)
var gotMappedItems []string
getMap := func(item string) *typedmap.ReadonlyTypedMap {
gotMappedItems = append(gotMappedItems, item)
return tc.mapsByItem[item]
}
// Act.
got := FilterTypedMapCollection(tc.items, getMap, enabledFilter)
// Assert.
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("FilterTypedMapCollection() mismatch (-want +got):\n%s", diff)
}
if diff := cmp.Diff(tc.wantMappedItems, gotMappedItems); diff != "" {
t.Errorf("getMap call sequence mismatch (-want +got):\n%s", diff)
}
if diff := cmp.Diff(originalItems, tc.items); diff != "" {
t.Errorf("input slice mutated (-want +got):\n%s", diff)
}
})
}
}
Go Quality Report (
|
| Metric | Result |
|---|---|
| Changed Scope | 1 prod file(s), 1 test file(s), 5 unit(s) |
| Patch Coverage | 100.0% (0/0 lines) |
| Mutation Score | 100.0% (0/0 killed, 0 survived) |
Commit: af2eec193dc5 · Cloud Build Logs
Summary
Aligns
pkg/common/filterwith KHI Go coding and testing conventions without modifying exported APIs or executable production code:pkg/common/filter/filter.goto accurately documentTypedMapFilter,EnabledFilter,FilterTypedMapCollection,NewEnabledFilter,KeyToFilter, andShouldInclude(zero executable code changes).pkg/common/filter/filter_test.gocovering key retention, the full(includeUndefined, found, value)truth table, nil/empty inputs, zero-match nil returns, order preservation, input immutability, and custom non-booleanTypedMapFilterimplementations (0%→100.0%statement coverage).Verification
make pre-push— PASS (0 mustfix, 0 advisory, 0 skipped)make qa-all-advisory CODEQUALITY_SIMILARITY=true— PASS (0 mustfix, 0 advisory, 0 skipped; 0 similarity advisories)go test -race -count=1 -cover ./pkg/common/filter/... ./pkg/core/task/... ./pkg/core/inspection/metadata/... ./pkg/core/inspection/...— PASS (pkg/common/filter:100.0%statement coverage)