diff --git a/Var.go b/Var.go index dc2fb7a..02f4077 100644 --- a/Var.go +++ b/Var.go @@ -2,7 +2,6 @@ package testcase import ( "fmt" - "reflect" ) // Var is a testCase helper structure, that allows easy way to access testCase runtime variables. @@ -203,11 +202,6 @@ func (v Var[V]) Super(t *T) V { // Append will append a value[T] to a current value of Var[[]T]. // Append only possible if the value type of Var is a slice type of T. -func Append[V any](t *T, v Var[V], x ...interface{}) { - rv := reflect.ValueOf(v.Get(t)) - var rx []reflect.Value - for _, e := range x { - rx = append(rx, reflect.ValueOf(e)) - } - v.Set(t, reflect.Append(rv, rx...).Interface().(V)) +func Append[V any](t *T, list Var[[]V], vs ...V) { + list.Set(t, append(list.Get(t), vs...)) } diff --git a/Var_test.go b/Var_test.go index 0a21aa3..5129c31 100644 --- a/Var_test.go +++ b/Var_test.go @@ -76,12 +76,13 @@ func TestVar(t *testing.T) { s.When(`Var#Init is defined`, func(s *testcase.Spec) { s.HasSideEffect() - // WARN: do not use any other hook that manipulates the testVar here - // else the side effect is not guaranteed - s.Around(func(t *testcase.T) func() { + + s.Before(func(t *testcase.T) { + // WARN: do not use any other hook that manipulates the testVar here + // else the side effect is not guaranteed testVar.Init = func(t *testcase.T) int { return expected } // reset side effect - return func() { testVar.Init = nil } + t.Cleanup(func() { testVar.Init = nil }) }) thenValueIsCached := func(s *testcase.Spec) { @@ -835,30 +836,30 @@ func TestAppend(t *testing.T) { s := testcase.NewSpec(t) var ( - v = testcase.Var[any]{ID: `testcase.Var`} - e = testcase.Var[any]{ID: `new slice element`} - subject = func(t *testcase.T) { - testcase.Append(t, v, e.Get(t)) - } + v = testcase.Var[[]int]{ID: `testcase.Var`} + e = testcase.Var[int]{ID: `new slice element`} ) + act := func(t *testcase.T) { + testcase.Append(t, v, e.Get(t)) + } s.When(`var content is a slice[T]`, func(s *testcase.Spec) { - v.Let(s, func(t *testcase.T) any { + v.Let(s, func(t *testcase.T) []int { return []int{} }) s.And(`the element is a T type`, func(s *testcase.Spec) { - e.Let(s, func(t *testcase.T) interface{} { + e.Let(s, func(t *testcase.T) int { return t.Random.Int() }) s.Then(`it will append the value to the slice[T] type testcase.Var`, func(t *testcase.T) { - t.Must.Equal(len(v.Get(t).([]int)), 0) - subject(t) + t.Must.Equal(len(v.Get(t)), 0) + act(t) list := v.Get(t) elem := e.Get(t) - t.Must.Equal(len(list.([]int)), 1) + t.Must.Equal(len(list), 1) t.Must.Contain(list, elem) }) @@ -867,7 +868,7 @@ func TestAppend(t *testing.T) { for i := 0; i < 1024; i++ { expected = append(expected, i) e.Set(t, i) - subject(t) + act(t) } assert.Must(t).Equal(expected, v.Get(t)) diff --git a/examples_test.go b/examples_test.go index 5fa5be1..435126e 100644 --- a/examples_test.go +++ b/examples_test.go @@ -1226,7 +1226,7 @@ func ExampleAppend() { var tb testing.TB s := testcase.NewSpec(tb) - list := testcase.Let(s, func(t *testcase.T) interface{} { + list := testcase.Let(s, func(t *testcase.T) []int { return []int{} })