-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor slice utils package structure
Signed-off-by: Per Goncalves da Silva <[email protected]>
- Loading branch information
Per Goncalves da Silva
committed
Feb 10, 2025
1 parent
29547aa
commit c7964fe
Showing
8 changed files
with
282 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 1 addition & 22 deletions
23
internal/util/slices/filter.go → internal/util/filter/predicates.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package filter_test | ||
|
||
import ( | ||
"github.com/operator-framework/operator-controller/internal/util/filter" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestAnd(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
predicates []filter.Predicate[int] | ||
input int | ||
want bool | ||
}{ | ||
{ | ||
name: "all true", | ||
predicates: []filter.Predicate[int]{ | ||
func(i int) bool { return i > 0 }, | ||
func(i int) bool { return i < 10 }, | ||
}, | ||
input: 5, | ||
want: true, | ||
}, | ||
{ | ||
name: "one false", | ||
predicates: []filter.Predicate[int]{ | ||
func(i int) bool { return i > 0 }, | ||
func(i int) bool { return i < 5 }, | ||
}, | ||
input: 5, | ||
want: false, | ||
}, | ||
{ | ||
name: "all false", | ||
predicates: []filter.Predicate[int]{ | ||
func(i int) bool { return i > 10 }, | ||
func(i int) bool { return i < 0 }, | ||
}, | ||
input: 5, | ||
want: false, | ||
}, | ||
{ | ||
name: "no predicates", | ||
predicates: []filter.Predicate[int]{}, | ||
input: 5, | ||
want: true, | ||
}, | ||
{ | ||
name: "nil predicates", | ||
predicates: nil, | ||
input: 5, | ||
want: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := filter.And(tt.predicates...)(tt.input) | ||
require.Equal(t, tt.want, got, "And() = %v, want %v", got, tt.want) | ||
}) | ||
} | ||
} | ||
|
||
func TestOr(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
predicates []filter.Predicate[int] | ||
input int | ||
want bool | ||
}{ | ||
{ | ||
name: "all true", | ||
predicates: []filter.Predicate[int]{ | ||
func(i int) bool { return i > 0 }, | ||
func(i int) bool { return i < 10 }, | ||
}, | ||
input: 5, | ||
want: true, | ||
}, | ||
{ | ||
name: "one false", | ||
predicates: []filter.Predicate[int]{ | ||
func(i int) bool { return i > 0 }, | ||
func(i int) bool { return i < 5 }, | ||
}, | ||
input: 5, | ||
want: true, | ||
}, | ||
{ | ||
name: "all false", | ||
predicates: []filter.Predicate[int]{ | ||
func(i int) bool { return i > 10 }, | ||
func(i int) bool { return i < 0 }, | ||
}, | ||
input: 5, | ||
want: false, | ||
}, | ||
{ | ||
name: "no predicates", | ||
predicates: []filter.Predicate[int]{}, | ||
input: 5, | ||
want: false, | ||
}, | ||
{ | ||
name: "nil predicates", | ||
predicates: nil, | ||
input: 5, | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := filter.Or(tt.predicates...)(tt.input) | ||
require.Equal(t, tt.want, got, "Or() = %v, want %v", got, tt.want) | ||
}) | ||
} | ||
} | ||
|
||
func TestNot(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
predicate filter.Predicate[int] | ||
input int | ||
want bool | ||
}{ | ||
{ | ||
name: "predicate is true", | ||
predicate: func(i int) bool { return i > 0 }, | ||
input: 5, | ||
want: false, | ||
}, | ||
{ | ||
name: "predicate is false", | ||
predicate: func(i int) bool { return i > 3 }, | ||
input: 2, | ||
want: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := filter.Not(tt.predicate)(tt.input) | ||
require.Equal(t, tt.want, got, "Not() = %v, want %v", got, tt.want) | ||
}) | ||
} | ||
} |
Oops, something went wrong.