Skip to content

Commit

Permalink
implement slice unique elemets assertsion (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppapapetrou76 committed Oct 18, 2021
1 parent f3eee5d commit 6e56683
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions assert/bool.go
Expand Up @@ -24,6 +24,7 @@ func ThatBool(t *testing.T, actual bool) AssertableBool {
// IsEqualTo asserts if the expected bool is equal to the assertable bool value
// It errors the tests if the compared values (actual VS expected) are not equal.
func (a AssertableBool) IsEqualTo(expected interface{}) AssertableBool {
a.t.Helper()
if !a.actual.IsEqualTo(expected) {
a.t.Error(shouldBeEqual(a.actual, expected))
}
Expand Down
4 changes: 4 additions & 0 deletions assert/error_messages.go
Expand Up @@ -200,3 +200,7 @@ func shouldBeDefined(actual types.Assertable) string {
func shouldNotBeDefined(actual types.Assertable) string {
return fmt.Sprintf("assertion failed: expected value of = %+v, to be un-defined but it was", actual.Value())
}

func shouldContainUniqueElements(actual types.Assertable) string {
return fmt.Sprintf("assertion failed: expected value of = %+v, to contain unique elements but it doesn't", actual.Value())
}
9 changes: 9 additions & 0 deletions assert/slice.go
Expand Up @@ -106,3 +106,12 @@ func (a AssertableSlice) DoesNotContain(elements interface{}) AssertableSlice {
}
return a
}

// HasUniqueElements asserts if the assertable string slice does not contain the given element
// It errors the test if it contains it/them.
func (a AssertableSlice) HasUniqueElements() AssertableSlice {
if !(a.actual.HasUniqueElements()) {
a.t.Error(shouldContainUniqueElements(a.actual))
}
return a
}
31 changes: 31 additions & 0 deletions assert/slice_test.go
Expand Up @@ -329,3 +329,34 @@ func TestAssertableSlice_DoesNotContain(t *testing.T) {
})
}
}

func TestAssertableSlice_HasUniqueElements(t *testing.T) {
tests := []struct {
name string
actual []string
shouldFail bool
}{
{
name: "should not fail if slice has unique elements",
actual: []string{"element", "element2", "element3"},
shouldFail: false,
},
{
name: "should fail if slice has unique elements",
actual: []string{"element", "element", "element3"},
shouldFail: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
test := &testing.T{}
ThatSlice(test, tt.actual).HasUniqueElements()
ThatBool(t, test.Failed()).IsEqualTo(tt.shouldFail)
})
}
test := &testing.T{}
ThatSlice(test, []int{1, 2, 3}).HasUniqueElements()
ThatBool(t, test.Failed()).IsEqualTo(false)
ThatSlice(test, []int{1, 1, 3}).HasUniqueElements()
ThatBool(t, test.Failed()).IsEqualTo(true)
}
17 changes: 17 additions & 0 deletions internal/pkg/values/slice_value.go
Expand Up @@ -83,6 +83,23 @@ func (s SliceValue) ContainsOnly(elements interface{}) bool {
return s.Contains(elements) && s.HasSize(reflect.ValueOf(elements).Len())
}

// HasUniqueElements returns true if the slice contains only unique elements else false.
func (s SliceValue) HasUniqueElements() bool {
if !IsSlice(s.Value()) {
return false
}
sliceValue := reflect.ValueOf(s.value)
elements := map[interface{}]bool{}

for i := 0; i < sliceValue.Len(); i++ {
if _, ok := elements[sliceValue.Index(i).Interface()]; ok {
return false
}
elements[sliceValue.Index(i).Interface()] = true
}
return true
}

// Value returns the actual value of the structure.
func (s SliceValue) Value() interface{} {
return s.value
Expand Down
1 change: 1 addition & 0 deletions types/types.go
Expand Up @@ -34,6 +34,7 @@ type Containable interface {
Contains(elements interface{}) bool
ContainsOnly(elements interface{}) bool
DoesNotContain(elements interface{}) bool
HasUniqueElements() bool
Sizeable
}

Expand Down

0 comments on commit 6e56683

Please sign in to comment.