Skip to content

Commit

Permalink
Add unit tests for Set
Browse files Browse the repository at this point in the history
  • Loading branch information
efortin committed Jan 26, 2021
1 parent ad7abc9 commit bbc8220
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
15 changes: 0 additions & 15 deletions internal/utils/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,13 @@ func (s *Set) Remove(value string) *Set {

// Return a sorted list of values
func (s *Set) List() (list []string) {
if s == nil {
return []string{}
}

if s.m == nil {
s.m = make(map[string]struct{})
}
for key := range s.m {
list = append(list, key)
}
sort.Strings(list)
return
}

// Return a list of values
func (s *Set) UnsortedList() (list []string) {
for key := range s.m {
list = append(list, key)
}
return
}

func (s *Set) Contains(value string) bool {
_, c := s.m[value]
return c
Expand Down
32 changes: 32 additions & 0 deletions internal/utils/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,35 @@ func TestSet_Add(t *testing.T) {
DeepEqual(t, []string{"s1", "s2", "s3"}, actual.List())
})
}

func TestNewSetFromSlice(t *testing.T) {
t.Run("should return a slice ordered by key ( lexicographic )", func(t *testing.T) {
actual := NewSetFromSlice("z", "g", "aa")

assert.NotNil(t, actual)
assert.NotEmpty(t, actual.List())
DeepEqual(t, []string{"aa", "g", "z"}, actual.List())
})

t.Run("should return a slice of UNIQUE ordered key", func(t *testing.T) {
actual := NewSetFromSlice("c", "a", "a", "b", "b")

assert.NotNil(t, actual)
assert.NotEmpty(t, actual.List())
assert.Len(t, actual.List(), 3)
DeepEqual(t, []string{"a", "b", "c"}, actual.List())
})
}

func TestSet_Remove(t *testing.T) {
t.Run("should remove an existing key in slice", func(t *testing.T) {
actual := NewSetFromSlice("c", "a", "a", "b", "b")
assert.NotNil(t, actual)
assert.NotEmpty(t, actual.List())
assert.Len(t, actual.List(), 3)

actual.Remove("a")
assert.Len(t, actual.List(), 2)
DeepEqual(t, []string{"b", "c"}, actual.List())
})
}

0 comments on commit bbc8220

Please sign in to comment.