Skip to content

Commit

Permalink
Added tests for Set
Browse files Browse the repository at this point in the history
  • Loading branch information
w-edd committed Mar 4, 2024
1 parent de10d00 commit 583dbb8
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions pkg/helpers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package helpers
import (
"fmt"
"os"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -197,3 +198,57 @@ func TestMatchesPattern(t *testing.T) {
})
}
}

// HashableString is a simple Hashable type for testing.
type HashableString string

func (h HashableString) Hash() string {
return string(h)
}

func TestNewSet(t *testing.T) {
s := NewSet()
if s == nil || len(s.Elements) != 0 {
t.Errorf("NewSet() = %v, want a new Set instance with empty Elements", s)
}
}

func TestAddAndContains(t *testing.T) {
s := NewSet()
element := HashableString("test")
s.Add(element)
if !s.Contains(element) {
t.Errorf("Set does not contain element %v after Add", element)
}
}

func TestRemove(t *testing.T) {
s := NewSet()
element := HashableString("test")
s.Add(element)
s.Remove(element)
if s.Contains(element) {
t.Errorf("Set contains element %v after Remove", element)
}
}

func TestSize(t *testing.T) {
s := NewSet()
s.Add(HashableString("one"))
s.Add(HashableString("two"))
if s.Size() != 2 {
t.Errorf("Size() = %d, want 2", s.Size())
}
}

func TestList(t *testing.T) {
s := NewSet()
elements := []Hashable{HashableString("one"), HashableString("two")}
for _, e := range elements {
s.Add(e)
}
list := s.List()
if !reflect.DeepEqual(list, elements) && len(list) == len(elements) {
t.Errorf("List() = %v, want %v", list, elements)
}
}

0 comments on commit 583dbb8

Please sign in to comment.