-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support to convert testcase variables from one type to an another
- Loading branch information
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package let | ||
|
||
import ( | ||
"fmt" | ||
"github.com/adamluzsi/testcase" | ||
"reflect" | ||
) | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package let_test | ||
|
||
import ( | ||
"github.com/adamluzsi/testcase" | ||
"github.com/adamluzsi/testcase/assert" | ||
"github.com/adamluzsi/testcase/let" | ||
"github.com/adamluzsi/testcase/sandbox" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestAs(t *testing.T) { | ||
t.Run("primitive type", func(t *testing.T) { | ||
type MyString string | ||
|
||
s := testcase.NewSpec(t) | ||
v1 := let.String(s) | ||
v2 := let.As[MyString](v1) | ||
|
||
s.Test("", func(t *testcase.T) { | ||
t.Must.Equal(MyString(v1.Get(t)), v2.Get(t)) | ||
}) | ||
}) | ||
|
||
t.Run("interface type", func(t *testing.T) { | ||
type TimeAfterer interface { | ||
After(u time.Time) bool | ||
} | ||
|
||
s := testcase.NewSpec(t) | ||
v1 := let.Time(s) | ||
v2 := let.As[TimeAfterer](v1) | ||
|
||
s.Test("", func(t *testcase.T) { | ||
t.Must.Equal(TimeAfterer(v1.Get(t)), v2.Get(t)) | ||
}) | ||
}) | ||
|
||
t.Run("panics on incorrect conversation", func(t *testing.T) { | ||
ro := sandbox.Run(func() { | ||
s := testcase.NewSpec(t) | ||
v1 := let.Time(s) | ||
_ = let.As[string](v1) | ||
}) | ||
assert.False(t, ro.OK) | ||
assert.False(t, ro.Goexit) | ||
assert.NotNil(t, ro.PanicValue) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters