Skip to content

Commit b56661a

Browse files
committed
- Add utility functions under bdd package to circumvent unused variable errors in test
- Add utility functions under `bdd` package to produce `reflect.Type` for IsA() predicate - Add logic and integration for IsA() predicate
1 parent bff157c commit b56661a

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

pkg/bdd/utils.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package bdd
2+
3+
import "reflect"
4+
5+
// Used is a no-op function to mark variables as used, to avoid compiler while
6+
// in the middle of writing tests
7+
func Used(vv ...any) {}
8+
9+
// TypeOf returns the reflect.Type for a given type T, for use with IsA()
10+
// predicate.
11+
func TypeOf[T any]() reflect.Type {
12+
var p *T = nil
13+
return reflect.TypeOf(p).Elem()
14+
}

pkg/bdd/utils_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package bdd_test
2+
3+
import (
4+
"io"
5+
"reflect"
6+
"testing"
7+
8+
"github.com/maargenton/go-testpredicate/pkg/bdd"
9+
"github.com/maargenton/go-testpredicate/pkg/verify"
10+
)
11+
12+
func TestUsed(t *testing.T) {
13+
var v = 0 // bdd.Used() should suppress the 'declared and not used' error
14+
bdd.Used(v)
15+
16+
// Nothing further to test here
17+
}
18+
19+
func TestTypeOf(t *testing.T) {
20+
verify.That(t, bdd.TypeOf[int]()).Eq(reflect.TypeOf(int(0)))
21+
verify.That(t, bdd.TypeOf[string]()).Eq(reflect.TypeOf(""))
22+
verify.That(t, bdd.TypeOf[float64]()).Eq(reflect.TypeOf(float64(0.0)))
23+
verify.That(t, bdd.TypeOf[[]string]()).Eq(reflect.TypeOf([]string{}))
24+
25+
verify.That(t, bdd.TypeOf[io.Reader]()).ToString().Eq("io.Reader")
26+
}

pkg/utils/builder/builder_api.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
package builder
44

55
import (
6+
"reflect"
7+
68
"github.com/maargenton/go-testpredicate/pkg/utils/predicate"
79
"github.com/maargenton/go-testpredicate/pkg/utils/predicate/impl"
810
)
@@ -528,3 +530,19 @@ func (b *Builder) Field(keypath string) *Builder {
528530

529531
// From pkg/utils/predicate/impl/struct.go
530532
// ---------------------------------------------------------------------------
533+
534+
// ---------------------------------------------------------------------------
535+
// From pkg/utils/predicate/impl/type.go
536+
537+
// IsA tests if a value is of a given type.
538+
func (b *Builder) IsA(typ reflect.Type) *predicate.Predicate {
539+
b.p.RegisterPredicate(impl.IsA(typ))
540+
if b.t != nil {
541+
b.t.Helper()
542+
Evaluate(b)
543+
}
544+
return &b.p
545+
}
546+
547+
// From pkg/utils/predicate/impl/type.go
548+
// ---------------------------------------------------------------------------

pkg/utils/predicate/impl/type.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package impl
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
7+
"github.com/maargenton/go-testpredicate/pkg/utils/predicate"
8+
)
9+
10+
// IsA tests if a value is of a given type.
11+
func IsA(typ reflect.Type) (desc string, f predicate.PredicateFunc) {
12+
desc = fmt.Sprintf("{} is a %v", typ)
13+
14+
f = func(v interface{}) (r bool, ctx []predicate.ContextValue, err error) {
15+
var t = reflect.TypeOf(v)
16+
17+
if t != nil {
18+
if typ.Kind() == reflect.Interface {
19+
if t.Implements(typ) {
20+
return true, nil, nil
21+
}
22+
} else if t == typ {
23+
return true, nil, nil
24+
}
25+
}
26+
27+
ctx = append(ctx, predicate.ContextValue{
28+
Name: "type",
29+
Value: fmt.Sprintf("%T", v),
30+
})
31+
return false, ctx, nil
32+
}
33+
return
34+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package impl_test
2+
3+
import (
4+
"io"
5+
"strings"
6+
"testing"
7+
8+
"github.com/maargenton/go-testpredicate/pkg/bdd"
9+
"github.com/maargenton/go-testpredicate/pkg/utils/predicate/impl"
10+
)
11+
12+
func TestIsA(t *testing.T) {
13+
verifyPredicate(t, pr(impl.IsA(bdd.TypeOf[int]())), expectation{value: 123, pass: true})
14+
verifyPredicate(t, pr(impl.IsA(bdd.TypeOf[string]())), expectation{value: "test", pass: true})
15+
16+
verifyPredicate(t, pr(impl.IsA(bdd.TypeOf[float64]())), expectation{value: 123.45, pass: true})
17+
verifyPredicate(t, pr(impl.IsA(bdd.TypeOf[int]())), expectation{value: 123.45, pass: false})
18+
19+
var r = strings.NewReader("test")
20+
verifyPredicate(t, pr(impl.IsA(bdd.TypeOf[io.Reader]())), expectation{value: r, pass: true})
21+
verifyPredicate(t, pr(impl.IsA(bdd.TypeOf[io.Writer]())), expectation{value: r, pass: false})
22+
}

0 commit comments

Comments
 (0)