diff --git a/internal/assert/assert.go b/internal/assert/assert.go index 5d50a35..04821a0 100644 --- a/internal/assert/assert.go +++ b/internal/assert/assert.go @@ -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) - } -} diff --git a/json_test.go b/json_test.go index e54f753..8dcedc8 100644 --- a/json_test.go +++ b/json_test.go @@ -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"` } @@ -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"` } @@ -41,6 +49,8 @@ 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) @@ -48,6 +58,8 @@ func TestUnmarshalSome(t *testing.T) { } func TestUnmarshalNone(t *testing.T) { + t.Parallel() + var number option.Option[int] err := json.Unmarshal([]byte("null"), &number) assert.Nil(t, err) @@ -55,6 +67,8 @@ func TestUnmarshalNone(t *testing.T) { } func TestUnmarshalEmpty(t *testing.T) { + t.Parallel() + type name struct { MiddleName option.Option[string] `json:"middle_name"` } @@ -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) diff --git a/option_test.go b/option_test.go index 31c054a..05ae283 100644 --- a/option_test.go +++ b/option_test.go @@ -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") }() @@ -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") }()