Skip to content

Commit

Permalink
Merge pull request #17 from life4/test-everything
Browse files Browse the repository at this point in the history
WIP: Increase test coverage to 100%
  • Loading branch information
orsinium authored Jul 25, 2023
2 parents 5c9c40a + c86197b commit 4d239b1
Show file tree
Hide file tree
Showing 7 changed files with 356 additions and 11 deletions.
72 changes: 72 additions & 0 deletions lambdas/checks_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lambdas_test

import (
"math"
"testing"

"github.com/life4/genesis/lambdas"
Expand Down Expand Up @@ -33,6 +34,50 @@ func TestNot(t *testing.T) {
is.True(l(-1))
}

func TestIsZero(t *testing.T) {
is := is.New(t)
is.True(lambdas.IsZero(int(0)))
is.True(lambdas.IsZero(int32(0)))
is.True(lambdas.IsZero(int64(0)))
is.True(lambdas.IsZero(uint64(0)))
is.True(lambdas.IsZero(float64(0)))
is.True(lambdas.IsZero(float32(0)))

is.True(!lambdas.IsZero(int(1)))
is.True(!lambdas.IsZero(int64(1)))
is.True(!lambdas.IsZero(float64(13)))
is.True(!lambdas.IsZero(-1))
}

func TestIsNotZero(t *testing.T) {
is := is.New(t)
is.True(!lambdas.IsNotZero(int(0)))
is.True(!lambdas.IsNotZero(int32(0)))
is.True(!lambdas.IsNotZero(int64(0)))
is.True(!lambdas.IsNotZero(uint64(0)))
is.True(!lambdas.IsNotZero(float64(0)))
is.True(!lambdas.IsNotZero(float32(0)))

is.True(lambdas.IsNotZero(int(1)))
is.True(lambdas.IsNotZero(int64(1)))
is.True(lambdas.IsNotZero(float64(13)))
is.True(lambdas.IsNotZero(-1))
}

func TestIsEmpty(t *testing.T) {
is := is.New(t)
is.True(!lambdas.IsEmpty([]any{1}))
is.True(lambdas.IsEmpty([]any{}))
is.True(lambdas.IsEmpty[[]any](nil))
}

func TestIsNotEmpty(t *testing.T) {
is := is.New(t)
is.True(lambdas.IsNotEmpty([]any{1}))
is.True(!lambdas.IsNotEmpty([]any{}))
is.True(!lambdas.IsNotEmpty[[]any](nil))
}

func TestIsDefault(t *testing.T) {
is := is.New(t)
is.True(!lambdas.IsDefault(1))
Expand All @@ -42,6 +87,15 @@ func TestIsDefault(t *testing.T) {
is.True(lambdas.IsDefault(0))
}

func TestIsNotDefault(t *testing.T) {
is := is.New(t)
is.True(lambdas.IsNotDefault(1))
is.True(lambdas.IsNotDefault("hi"))

is.True(!lambdas.IsNotDefault(""))
is.True(!lambdas.IsNotDefault(0))
}

func TestIsNil(t *testing.T) {
is := is.New(t)
var v1 *int
Expand All @@ -52,6 +106,24 @@ func TestIsNil(t *testing.T) {
is.True(!lambdas.IsNil(&v3))
}

func TestIsNaN(t *testing.T) {
is := is.New(t)
is.True(!lambdas.IsNaN(1.3))
is.True(!lambdas.IsNaN(1.3))
is.True(!lambdas.IsNaN(1))
is.True(!lambdas.IsNaN(int64(1)))
is.True(lambdas.IsNaN(float64(math.NaN())))
}

func TestIsNotNaN(t *testing.T) {
is := is.New(t)
is.True(lambdas.IsNotNaN(1.3))
is.True(lambdas.IsNotNaN(1.3))
is.True(lambdas.IsNotNaN(1))
is.True(lambdas.IsNotNaN(int64(1)))
is.True(!lambdas.IsNotNaN(float64(math.NaN())))
}

func TestIsNotNil(t *testing.T) {
is := is.New(t)
var v1 *int
Expand Down
15 changes: 15 additions & 0 deletions lambdas/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,31 @@ import (
"github.com/matryer/is"
)

func panics(is *is.I, f func()) {
defer func() {
r := recover()
is.True(r != nil)
}()
f()
}

func TestMust(t *testing.T) {
is := is.New(t)
f := func() (int, error) { return 13, nil }
res := lambdas.Must(f())
is.Equal(res, 13)

f = func() (int, error) { return 13, errors.New("oh no") }
panics(is, func() { lambdas.Must(f()) })
}

func TestEnsure(t *testing.T) {
f := func() error { return nil }
lambdas.Ensure(f())

is := is.New(t)
f = func() error { return errors.New("oh no") }
panics(is, func() { lambdas.Ensure(f()) })
}

func TestSafe(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions lambdas/glambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ func Max[T constraints.Ordered](a T, b T) T {
//
// A few examples:
//
// + 0 for int and float
// + "" for string
// + nil for slice
func Default[T any](value T) T {
// - 0 for int and float
// - "" for string
// - nil for slice
func Default[T any](T) T {
var def T
return def
}
10 changes: 10 additions & 0 deletions lambdas/glambda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,13 @@ func TestMax(t *testing.T) {
is.Equal(lambdas.Max(2, -3), 2)
is.Equal(lambdas.Max(2, 2), 2)
}

func TestDefault(t *testing.T) {
is := is.New(t)
is.Equal(lambdas.Default(3), 0)
is.Equal(lambdas.Default(int32(3)), int32(0))
is.Equal(lambdas.Default(int64(3)), int64(0))
is.Equal(lambdas.Default(0), 0)
is.Equal(lambdas.Default(3.5), 0.0)
is.Equal(lambdas.Default("hi"), "")
}
9 changes: 8 additions & 1 deletion slices/slice.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package slices

import (
"github.com/life4/genesis/constraints"
"fmt"
"math/rand"
"sort"
"strings"
"time"

"github.com/life4/genesis/constraints"
)

// Choice chooses a random element from the slice.
Expand Down Expand Up @@ -570,6 +571,9 @@ func ToChannel[S ~[]T, T any](items S) chan T {
// ToMap converts the given slice into a map where keys are indices and values are items
// from the given slice.
func ToMap[S ~[]V, V any](items S) map[int]V {
if items == nil {
return nil
}
result := make(map[int]V)
for index, item := range items {
result[index] = item
Expand All @@ -591,6 +595,9 @@ func ToMapGroupedBy[V any, T comparable](items []V, keyExtractor func(V) T) map[
// ToKeys converts the given slice into a map where items from the slice are the keys
// of the resulting map and all values are equal to the given `val` value.
func ToKeys[S ~[]K, K comparable, V any](items S, val V) map[K]V {
if items == nil {
return nil
}
result := make(map[K]V)
for _, item := range items {
result[item] = val
Expand Down
91 changes: 91 additions & 0 deletions slices/slice_func_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slices_test

import (
"errors"
"fmt"
"testing"

Expand Down Expand Up @@ -94,6 +95,69 @@ func TestDropWhile(t *testing.T) {
f([]int{2, 4, 6, 1, 8}, []int{1, 8})
}

func TestEach(t *testing.T) {
is := is.New(t)
f := func(given []int, expected []int) {
actual := make([]int, 0)
double := func(t int) {
actual = append(actual, t*2)
}
slices.Each(given, double)
is.Equal(expected, actual)
}
f([]int{}, []int{})
f([]int{1}, []int{2})
f([]int{1, 2, 3}, []int{2, 4, 6})
f([]int{2, 5, 3}, []int{4, 10, 6})
}

func TestEachErr(t *testing.T) {
is := is.New(t)
f := func(given []int, expected []int) {
actual := make([]int, 0)
double := func(t int) error {
if t == 13 {
return errors.New("oh no")
}
actual = append(actual, t*2)
return nil
}
_ = slices.EachErr(given, double)
is.Equal(expected, actual)
}
f([]int{}, []int{})
f([]int{1}, []int{2})
f([]int{1, 2, 3}, []int{2, 4, 6})
f([]int{2, 5, 3}, []int{4, 10, 6})
f([]int{2, 5, 3, 13, 4}, []int{4, 10, 6})
}

func TestEqualBy(t *testing.T) {
is := is.New(t)
f := func(left []int, right []int, expected bool) {
f := func(a, b int) bool { return a == -b }
actual := slices.EqualBy(left, right, f)
is.Equal(expected, actual)

actual = slices.EqualBy(right, left, f)
is.Equal(expected, actual)
}
f([]int{}, []int{}, true)
f([]int{1}, []int{-1}, true)
f([]int{1}, []int{1}, false)
f([]int{1}, []int{2}, false)
f([]int{1, 2, 3, 3}, []int{-1, -2, -3, -3}, true)
f([]int{1, 2, 3, 3}, []int{-1, -2, -3, 3}, false)
f([]int{1, 2, 3, 3}, []int{-1, -2, -2, -3}, false)
f([]int{1, 2, 3, 3}, []int{-1, -2, -4, -3}, false)

// different len
f([]int{1, 2, 3}, []int{-1, -2}, false)
f([]int{1, 2}, []int{-1, -2, 3}, false)
f([]int{}, []int{-1, -2, -3}, false)
f([]int{1, 2, 3}, []int{}, false)
}

func TestFilter(t *testing.T) {
is := is.New(t)
f := func(given []int, expected []int) {
Expand Down Expand Up @@ -153,6 +217,20 @@ func TestGroupBy(t *testing.T) {
f([]int{1, 3, 2, 4, 5}, map[int][]int{0: {2, 4}, 1: {1, 3, 5}})
}

func TestIndexBy(t *testing.T) {
is := is.New(t)
f := func(given []int, expectedInd int) {
even := func(t int) bool { return (t % 2) == 0 }
index, _ := slices.IndexBy(given, even)
is.Equal(expectedInd, index)
}
f([]int{}, 0)
f([]int{6}, 0)
f([]int{3, 6}, 1)
f([]int{3, 6, 8}, 1)
f([]int{3, 5, 8}, 2)
}

func TestMap(t *testing.T) {
is := is.New(t)
f := func(given []int, expected []int) {
Expand Down Expand Up @@ -216,6 +294,19 @@ func TestReduceWhile(t *testing.T) {
f([]int{1, 2, 0, 3}, 3)
}

func TestReject(t *testing.T) {
is := is.New(t)
f := func(given []int, expected []int) {
odd := func(t int) bool { return (t % 2) == 1 }
actual := slices.Reject(given, odd)
is.Equal(expected, actual)
}
f([]int{}, []int{})
f([]int{1, 2, 3, 4}, []int{2, 4})
f([]int{1, 3}, []int{})
f([]int{2, 4}, []int{2, 4})
}

func TestScan(t *testing.T) {
is := is.New(t)
f := func(given []int, expected []int) {
Expand Down
Loading

0 comments on commit 4d239b1

Please sign in to comment.