Skip to content

Commit

Permalink
Merge pull request #2133 from seans3/inventory-set
Browse files Browse the repository at this point in the history
Adds InventorySet Equals() function
  • Loading branch information
k8s-ci-robot authored Jan 22, 2020
2 parents 758d428 + bf2e398 commit 39c7a06
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cmd/kubectl/kubectlcobra/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ func (is *InventorySet) Subtract(other *InventorySet) (*InventorySet, error) {
return result, nil
}

// Equals returns true if the "other" inventory set is the same
// as this current inventory set. Relies on the fact that the
// inventory items are sorted for the String() function.
func (is *InventorySet) Equals(other *InventorySet) bool {
if other == nil {
return false
}
return is.String() == other.String()
}

// String returns a string describing set of Inventory structs.
func (is *InventorySet) String() string {
strs := []string{}
Expand Down
57 changes: 57 additions & 0 deletions cmd/kubectl/kubectlcobra/inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,3 +487,60 @@ func TestInventorySetSubtract(t *testing.T) {
}
}
}

func TestInventorySetEquals(t *testing.T) {
tests := []struct {
set1 []*Inventory
set2 []*Inventory
isEqual bool
}{
{
set1: []*Inventory{},
set2: []*Inventory{&inventory1},
isEqual: false,
},
{
set1: []*Inventory{&inventory1},
set2: []*Inventory{},
isEqual: false,
},
{
set1: []*Inventory{&inventory1, &inventory2},
set2: []*Inventory{&inventory1},
isEqual: false,
},
{
set1: []*Inventory{&inventory1, &inventory2},
set2: []*Inventory{&inventory3, &inventory4},
isEqual: false,
},
// Empty sets are equal.
{
set1: []*Inventory{},
set2: []*Inventory{},
isEqual: true,
},
{
set1: []*Inventory{&inventory1},
set2: []*Inventory{&inventory1},
isEqual: true,
},
// Ordering of the inventory items does not matter for equality.
{
set1: []*Inventory{&inventory1, &inventory2},
set2: []*Inventory{&inventory2, &inventory1},
isEqual: true,
},
}

for _, test := range tests {
invSet1 := NewInventorySet(test.set1)
invSet2 := NewInventorySet(test.set2)
if !invSet1.Equals(invSet2) && test.isEqual {
t.Errorf("Expected equal inventory sets; got unequal (%s)/(%s)\n", invSet1, invSet2)
}
if invSet1.Equals(invSet2) && !test.isEqual {
t.Errorf("Expected inequal inventory sets; got equal (%s)/(%s)\n", invSet1, invSet2)
}
}
}

0 comments on commit 39c7a06

Please sign in to comment.