Skip to content

Commit

Permalink
add cancelable context variable syntax sugar to let package
Browse files Browse the repository at this point in the history
  • Loading branch information
adamluzsi committed Oct 8, 2024
1 parent 0188472 commit 7a6962a
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 289 deletions.
26 changes: 0 additions & 26 deletions let/As.go

This file was deleted.

50 changes: 0 additions & 50 deletions let/As_test.go

This file was deleted.

26 changes: 0 additions & 26 deletions let/With.go

This file was deleted.

43 changes: 0 additions & 43 deletions let/With_test.go

This file was deleted.

74 changes: 74 additions & 0 deletions let/std.go → let/let.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,53 @@ package let

import (
"context"
"fmt"
"reflect"
"testing"
"time"

"go.llib.dev/testcase"
"go.llib.dev/testcase/internal"
"go.llib.dev/testcase/random"
)

func With[V any, FN withFN[V]](s *testcase.Spec, fn FN) testcase.Var[V] {
var init testcase.VarInit[V]
switch fnv := any(fn).(type) {
case func() V:
init = func(t *testcase.T) V { return fnv() }
case func(testing.TB) V:
init = func(t *testcase.T) V { return fnv(t) }
case func(*testcase.T) V:
init = fnv
}
return testcase.Let(s, init)
}

type withFN[V any] interface {
func() V |
func(testing.TB) V |
func(*testcase.T) V
}

func As[To, From any](Var testcase.Var[From]) testcase.Var[To] {
asID++
fromType := reflect.TypeOf((*From)(nil)).Elem()
toType := reflect.TypeOf((*To)(nil)).Elem()
if !fromType.ConvertibleTo(toType) {
panic(fmt.Sprintf("you can't have %s as %s", fromType.String(), toType.String()))
}
return testcase.Var[To]{
ID: fmt.Sprintf("%s AS %T #%d", Var.ID, *new(To), asID),
Init: func(t *testcase.T) To {
var rFrom = reflect.ValueOf(Var.Get(t))
return rFrom.Convert(toType).Interface().(To)
},
}
}

var asID int // adds extra safety that there won't be a name collision between two variables

func Context(s *testcase.Spec) testcase.Var[context.Context] {
return testcase.Let(s, func(t *testcase.T) context.Context {
ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -15,6 +57,14 @@ func Context(s *testcase.Spec) testcase.Var[context.Context] {
})
}

func ContextWithCancel(s *testcase.Spec) (testcase.Var[context.Context], testcase.Var[func()]) {
return testcase.Let2(s, func(t *testcase.T) (context.Context, func()) {
ctx, cancel := context.WithCancel(context.Background())
t.Defer(cancel)
return ctx, cancel
})
}

func Error(s *testcase.Spec) testcase.Var[error] {
return testcase.Let(s, func(t *testcase.T) error {
return t.Random.Error()
Expand Down Expand Up @@ -86,3 +136,27 @@ func DurationBetween(s *testcase.Spec, min, max time.Duration) testcase.Var[time
return t.Random.DurationBetween(min, max)
})
}

func Contact(s *testcase.Spec, opts ...internal.ContactOption) testcase.Var[random.Contact] {
return testcase.Let[random.Contact](s, func(t *testcase.T) random.Contact {
return t.Random.Contact(opts...)
})
}

func FirstName(s *testcase.Spec, opts ...internal.ContactOption) testcase.Var[string] {
return testcase.Let(s, func(t *testcase.T) string {
return t.Random.Contact(opts...).FirstName
})
}

func LastName(s *testcase.Spec) testcase.Var[string] {
return testcase.Let(s, func(t *testcase.T) string {
return t.Random.Contact().LastName
})
}

func Email(s *testcase.Spec) testcase.Var[string] {
return testcase.Let(s, func(t *testcase.T) string {
return t.Random.Contact().Email
})
}
Loading

0 comments on commit 7a6962a

Please sign in to comment.