Skip to content

Commit

Permalink
Run tests in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
BooleanCat committed May 16, 2024
1 parent 67c68af commit 2f776c8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
23 changes: 0 additions & 23 deletions internal/assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,3 @@ func NotNil(t *testing.T, v interface{}) {
t.Error("expected `nil` not to equal `nil`")
}
}

func SliceEqual[T comparable](t *testing.T, a, b []T) {
t.Helper()

if len(a) != len(b) {
t.Errorf("expected `%v` to equal `%v` but lengths differ", a, b)
return
}

for i, v := range a {
if v != b[i] {
t.Errorf("expected `%v` to equal `%v`", a, b)
}
}
}

func Empty[S any, T []S | ~string](t *testing.T, items T) {
t.Helper()

if len(items) != 0 {
t.Errorf("expected `%v` to be empty", items)
}
}
16 changes: 16 additions & 0 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@ import (
)

func TestMarshalSome(t *testing.T) {
t.Parallel()

data, err := json.Marshal(option.Some(4))
assert.Nil(t, err)
assert.Equal(t, string(data), "4")
}

func TestMarshalNone(t *testing.T) {
t.Parallel()

data, err := json.Marshal(option.None[int]())
assert.Nil(t, err)
assert.Equal(t, string(data), "null")
}

func TestMarshalSomeParsed(t *testing.T) {
t.Parallel()

type name struct {
MiddleName option.Option[string] `json:"middle_name"`
}
Expand All @@ -31,6 +37,8 @@ func TestMarshalSomeParsed(t *testing.T) {
}

func TestMarshalNoneParsed(t *testing.T) {
t.Parallel()

type name struct {
MiddleName option.Option[string] `json:"middle_name"`
}
Expand All @@ -41,20 +49,26 @@ func TestMarshalNoneParsed(t *testing.T) {
}

func TestUnmarshalSome(t *testing.T) {
t.Parallel()

var number option.Option[int]
err := json.Unmarshal([]byte("4"), &number)
assert.Nil(t, err)
assert.Equal(t, number, option.Some(4))
}

func TestUnmarshalNone(t *testing.T) {
t.Parallel()

var number option.Option[int]
err := json.Unmarshal([]byte("null"), &number)
assert.Nil(t, err)
assert.True(t, number.IsNone())
}

func TestUnmarshalEmpty(t *testing.T) {
t.Parallel()

type name struct {
MiddleName option.Option[string] `json:"middle_name"`
}
Expand All @@ -66,6 +80,8 @@ func TestUnmarshalEmpty(t *testing.T) {
}

func TestUnmarshalError(t *testing.T) {
t.Parallel()

var number option.Option[int]
err := number.UnmarshalJSON([]byte("not a number"))
assert.NotNil(t, err)
Expand Down
32 changes: 32 additions & 0 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,27 @@ func ExampleOption_Expect() {
}

func TestSomeStringer(t *testing.T) {
t.Parallel()

assert.Equal(t, fmt.Sprintf("%s", option.Some("foo")), "Some(foo)") //nolint:gosimple
assert.Equal(t, fmt.Sprintf("%s", option.Some(42)), "Some(42)") //nolint:gosimple
}

func TestNoneStringer(t *testing.T) {
t.Parallel()

assert.Equal(t, fmt.Sprintf("%s", option.None[string]()), "None") //nolint:gosimple
}

func TestSomeUnwrap(t *testing.T) {
t.Parallel()

assert.Equal(t, option.Some(42).Unwrap(), 42)
}

func TestNoneUnwrap(t *testing.T) {
t.Parallel()

defer func() {
assert.Equal(t, fmt.Sprint(recover()), "called `Option.Unwrap()` on a `None` value")
}()
Expand All @@ -101,56 +109,80 @@ func TestNoneUnwrap(t *testing.T) {
}

func TestSomeUnwrapOr(t *testing.T) {
t.Parallel()

assert.Equal(t, option.Some(42).UnwrapOr(3), 42)
}

func TestNoneUnwrapOr(t *testing.T) {
t.Parallel()

assert.Equal(t, option.None[int]().UnwrapOr(3), 3)
}

func TestSomeUnwrapOrElse(t *testing.T) {
t.Parallel()

assert.Equal(t, option.Some(42).UnwrapOrElse(func() int { return 41 }), 42)
}

func TestNoneUnwrapOrElse(t *testing.T) {
t.Parallel()

assert.Equal(t, option.None[int]().UnwrapOrElse(func() int { return 41 }), 41)
}

func TestSomeUnwrapOrZero(t *testing.T) {
t.Parallel()

assert.Equal(t, option.Some(42).UnwrapOrZero(), 42)
}

func TestNoneUnwrapOrZero(t *testing.T) {
t.Parallel()

assert.Equal(t, option.None[int]().UnwrapOrZero(), 0)
}

func TestIsSome(t *testing.T) {
t.Parallel()

assert.True(t, option.Some(42).IsSome())
assert.False(t, option.None[int]().IsSome())
}

func TestIsNone(t *testing.T) {
t.Parallel()

assert.False(t, option.Some(42).IsNone())
assert.True(t, option.None[int]().IsNone())
}

func TestSomeValue(t *testing.T) {
t.Parallel()

value, ok := option.Some(42).Value()
assert.Equal(t, value, 42)
assert.True(t, ok)
}

func TestNoneValue(t *testing.T) {
t.Parallel()

value, ok := option.None[int]().Value()
assert.Equal(t, value, 0)
assert.False(t, ok)
}

func TestSomeExpect(t *testing.T) {
t.Parallel()

assert.Equal(t, option.Some(42).Expect("oops"), 42)
}

func TestNoneExpect(t *testing.T) {
t.Parallel()

defer func() {
assert.Equal(t, fmt.Sprint(recover()), "oops")
}()
Expand Down

0 comments on commit 2f776c8

Please sign in to comment.