Skip to content

Commit

Permalink
cm: add test for (Result).Result()
Browse files Browse the repository at this point in the history
  • Loading branch information
ydnar committed Oct 31, 2024
1 parent 2eb90f9 commit b90797c
Showing 1 changed file with 41 additions and 18 deletions.
59 changes: 41 additions & 18 deletions cm/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,47 @@ type resulter[OK, Err any] interface {
Err() *Err
}

func TestResultOKOrErr(t *testing.T) {
r1 := OK[Result[string, string, struct{}]]("hello")
if ok := r1.OK(); ok == nil {
t.Errorf("OK(): %v, expected non-nil OK", ok)
}
if err := r1.Err(); err != nil {
t.Errorf("Err(): %v, expected nil Err", err)
}

r2 := Err[Result[bool, struct{}, bool]](true)
if ok := r2.OK(); ok != nil {
t.Errorf("OK(): %v, expected nil OK", ok)
}
if err := r2.Err(); err == nil {
t.Errorf("Err(): %v, expected non-nil Err", err)
}
}

func TestResultResult(t *testing.T) {
ok, err, isErr := OK[Result[string, string, int]]("hello").Result()
if got, want := ok, "hello"; got != want {
t.Errorf("Result(): ok = %v; expected %v", got, want)
}
if got, want := err, 0; got != want {
t.Errorf("Result(): err = %v; expected %v", got, want)
}
if got, want := isErr, false; got != want {
t.Errorf("Result(): isErr = %v; expected %v", got, want)
}
ok, err, isErr = Err[Result[string, string, int]](42).Result()
if got, want := ok, ""; got != want {
t.Errorf("Result(): ok = %v; expected %v", got, want)
}
if got, want := err, 42; got != want {
t.Errorf("Result(): err = %v; expected %v", got, want)
}
if got, want := isErr, true; got != want {
t.Errorf("Result(): isErr = %v; expected %v", got, want)
}
}

func TestResultLayout(t *testing.T) {
// 8 on 64-bit, 4 on 32-bit
ptrSize := unsafe.Sizeof(uintptr(0))
Expand Down Expand Up @@ -70,24 +111,6 @@ func TestResultLayout(t *testing.T) {
}
}

func TestResultOKOrErr(t *testing.T) {
r1 := OK[Result[string, string, struct{}]]("hello")
if ok := r1.OK(); ok == nil {
t.Errorf("OK(): %v, expected non-nil OK", ok)
}
if err := r1.Err(); err != nil {
t.Errorf("Err(): %v, expected nil Err", err)
}

r2 := Err[Result[bool, struct{}, bool]](true)
if ok := r2.OK(); ok != nil {
t.Errorf("OK(): %v, expected nil OK", ok)
}
if err := r2.Err(); err == nil {
t.Errorf("Err(): %v, expected non-nil Err", err)
}
}

func TestAltResult1(t *testing.T) {
type alt1[Shape, OK, Err any] struct {
_ [0]OK
Expand Down

0 comments on commit b90797c

Please sign in to comment.