Skip to content
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

Simplify UnitGroup #24

Merged
merged 1 commit into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 21 additions & 48 deletions pkg/specter/unitloading.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,25 @@ func (g UnitGroup) Merge(group UnitGroup) UnitGroup {

type UnitMatcher func(u Unit) bool

func UnitWithKindMatcher(kind UnitKind) UnitMatcher {
func UnitWithKindsMatcher(kinds ...UnitKind) UnitMatcher {
return func(u Unit) bool {
return u.Kind() == kind
for _, kind := range kinds {
if u.Kind() == kind {
return true
}
}
return false
}
}

func UnitWithIDsMatcher(id ...UnitID) UnitMatcher {
return func(u Unit) bool {
for _, id := range id {
if u.ID() == id {
return true
}
}
return false
}
}

Expand All @@ -144,64 +160,21 @@ func (g UnitGroup) Find(m UnitMatcher) (Unit, bool) {
return u, true
}
}
return nil, false
}

func (g UnitGroup) SelectType(t UnitKind) UnitGroup {
return g.Select(func(u Unit) bool {
return u.Kind() == t
})
}

func (g UnitGroup) SelectName(t UnitID) Unit {
for _, u := range g {
if u.ID() == t {
return u
}
}

return nil
}

func (g UnitGroup) SelectNames(names ...UnitID) UnitGroup {
return g.Select(func(u Unit) bool {
for _, name := range names {
if u.ID() == name {
return true
}
}
return false
})
return nil, false
}

func (g UnitGroup) Exclude(p func(u Unit) bool) UnitGroup {
func (g UnitGroup) Exclude(m UnitMatcher) UnitGroup {
r := UnitGroup{}
for _, u := range g {
if !p(u) {
if !m(u) {
r = append(r, u)
}
}

return r
}

func (g UnitGroup) ExcludeType(t UnitKind) UnitGroup {
return g.Exclude(func(u Unit) bool {
return u.Kind() == t
})
}

func (g UnitGroup) ExcludeNames(names ...UnitID) UnitGroup {
return g.Exclude(func(u Unit) bool {
for _, name := range names {
if u.ID() == name {
return true
}
}
return false
})
}

// MapUnitGroup performs a map operation on a UnitGroup
func MapUnitGroup[T any](g UnitGroup, p func(u Unit) T) []T {
var mapped []T
Expand Down
222 changes: 36 additions & 186 deletions pkg/specter/unitloading_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,117 +154,6 @@ func TestUnitGroup_Select(t *testing.T) {
}
}

func TestUnitGroup_SelectType(t *testing.T) {
tests := []struct {
name string
given specter.UnitGroup
when specter.UnitKind
then specter.UnitGroup
}{
{
name: "GIVEN no units matches, THEN return an empty group",
given: specter.UnitGroup{
testutils.NewUnitStub("unit2name", "type", specter.Source{}),
},
when: "not_found",
then: specter.UnitGroup{},
},
{
name: "GIVEN a unit matches, THEN return a group with matching unit",
given: specter.UnitGroup{
testutils.NewUnitStub("unit1", "type1", specter.Source{}),
testutils.NewUnitStub("unit2", "type2", specter.Source{}),
},
when: "type1",
then: specter.UnitGroup{
testutils.NewUnitStub("unit1", "type1", specter.Source{}),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.given.SelectType(tt.when)
require.Equal(t, tt.then, got)
})
}
}

func TestUnitGroup_SelectName(t *testing.T) {
tests := []struct {
name string
given specter.UnitGroup
when specter.UnitID
then specter.Unit
}{
{
name: "GIVEN a group with multiple units WHEN selecting an existing name THEN return the corresponding unit",
given: specter.NewUnitGroup(
testutils.NewUnitStub("unit1", "type", specter.Source{}),
testutils.NewUnitStub("unit2", "type", specter.Source{}),
),
when: "unit2",
then: testutils.NewUnitStub("unit2", "type", specter.Source{}),
},
{
name: "GIVEN a group with multiple units WHEN selecting a non-existent name THEN return nil",
given: specter.NewUnitGroup(
testutils.NewUnitStub("unit1", "type", specter.Source{}),
testutils.NewUnitStub("unit2", "type", specter.Source{}),
),
when: "spec3",
then: nil,
},
{
name: "GIVEN an empty group WHEN selecting a name THEN return nil",
given: specter.NewUnitGroup(),
when: "unit1",
then: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.given.SelectName(tt.when)
require.Equal(t, tt.then, got)
})
}
}

func TestUnitGroup_SelectNames(t *testing.T) {
tests := []struct {
name string
given specter.UnitGroup
when []specter.UnitID
then specter.UnitGroup
}{
{
name: "GIVEN no units matches, THEN return a group with no values",
given: specter.UnitGroup{
testutils.NewUnitStub("name", "type", specter.Source{}),
},
when: []specter.UnitID{"not_found"},
then: specter.UnitGroup{},
},
{
name: "GIVEN a unit matches, THEN return a group with matching unit",
given: specter.UnitGroup{
testutils.NewUnitStub("unit1", "type", specter.Source{}),
testutils.NewUnitStub("unit2", "type", specter.Source{}),
},
when: []specter.UnitID{"unit1"},
then: specter.UnitGroup{
testutils.NewUnitStub("unit1", "type", specter.Source{}),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.given.SelectNames(tt.when...)
require.Equal(t, tt.then, got)
})
}
}

func TestUnitGroup_Exclude(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -304,80 +193,6 @@ func TestUnitGroup_Exclude(t *testing.T) {
}
}

func TestUnitGroup_ExcludeType(t *testing.T) {
tests := []struct {
name string
given specter.UnitGroup
when specter.UnitKind
then specter.UnitGroup
}{
{
name: "GIVEN no units matches, THEN return a group with the same values",
given: specter.UnitGroup{
testutils.NewUnitStub("unit2name", "type", specter.Source{}),
},
when: "not_found",
then: specter.UnitGroup{
testutils.NewUnitStub("unit2name", "type", specter.Source{}),
},
},
{
name: "GIVEN a unit matches, THEN return a group without matching unit",
given: specter.UnitGroup{
testutils.NewUnitStub("unit1", "type1", specter.Source{}),
testutils.NewUnitStub("unit2", "type2", specter.Source{}),
},
when: "type1",
then: specter.UnitGroup{
testutils.NewUnitStub("unit2", "type2", specter.Source{}),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.given.ExcludeType(tt.when)
require.Equal(t, tt.then, got)
})
}
}

func TestUnitGroup_ExcludeNames(t *testing.T) {
tests := []struct {
name string
given specter.UnitGroup
when []specter.UnitID
then specter.UnitGroup
}{
{
name: "GIVEN no units matches, THEN return a group with the same values",
given: specter.UnitGroup{
testutils.NewUnitStub("unit2name", "type", specter.Source{}),
},
when: []specter.UnitID{"not_found"},
then: specter.UnitGroup{
testutils.NewUnitStub("unit2name", "type", specter.Source{}),
},
},
{
name: "GIVEN a unit matches, THEN return a group without matching unit",
given: specter.UnitGroup{
testutils.NewUnitStub("unit1", "type", specter.Source{}),
testutils.NewUnitStub("unit2", "type", specter.Source{}),
},
when: []specter.UnitID{"unit1"},
then: specter.UnitGroup{
testutils.NewUnitStub("unit2", "type", specter.Source{}),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.given.ExcludeNames(tt.when...)
require.Equal(t, tt.then, got)
})
}
}

func TestMapUnitGroup(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -520,7 +335,42 @@ func TestUnitWithKindMatcher(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := specter.UnitWithKindMatcher(tt.when.kind)(tt.when.unit)
got := specter.UnitWithKindsMatcher(tt.when.kind)(tt.when.unit)
assert.Equal(t, tt.then, got)
})
}
}

func TestUnitWithIDsMatcher(t *testing.T) {
type when struct {
id specter.UnitID
unit specter.Unit
}
tests := []struct {
name string
when when
then bool
}{
{
name: "WHEN unit with id THEN return true",
when: when{
id: "unit1",
unit: testutils.NewUnitStub("unit1", "kind", specter.Source{}),
},
then: true,
},
{
name: "WHEN unit not with id THEN return false",
when: when{
id: "id",
unit: testutils.NewUnitStub("not_id", "kind", specter.Source{}),
},
then: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := specter.UnitWithIDsMatcher(tt.when.id)(tt.when.unit)
assert.Equal(t, tt.then, got)
})
}
Expand Down
Loading