Skip to content

Commit

Permalink
improve Append signature to enforce slice types
Browse files Browse the repository at this point in the history
  • Loading branch information
adamluzsi committed Apr 17, 2024
1 parent e1b54cf commit 79df412
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
10 changes: 2 additions & 8 deletions Var.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package testcase

import (
"fmt"
"reflect"
)

// Var is a testCase helper structure, that allows easy way to access testCase runtime variables.
Expand Down Expand Up @@ -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...))
}
31 changes: 16 additions & 15 deletions Var_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
})

Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
})

Expand Down

0 comments on commit 79df412

Please sign in to comment.