Skip to content

Commit

Permalink
Merge 3230741 into e15b0be
Browse files Browse the repository at this point in the history
  • Loading branch information
maxatome committed May 30, 2018
2 parents e15b0be + 3230741 commit d8c2892
Show file tree
Hide file tree
Showing 30 changed files with 247 additions and 4 deletions.
31 changes: 31 additions & 0 deletions check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package testdeep_test

import (
"fmt"
"reflect"
"regexp"
"strings"
"testing"
Expand Down Expand Up @@ -312,6 +313,36 @@ func equalStr(t *testing.T, got, expected string, args ...interface{}) bool {
return false
}

func equalTypes(t *testing.T, got TestDeep, expected interface{}, args ...interface{}) bool {
gotType := got.TypeOf()
expectedType := reflect.TypeOf(expected)

if gotType == expectedType {
return true
}

var gotStr, expectedStr string

if gotType == nil {
gotStr = "nil"
} else {
gotStr = gotType.String()
}

if expected == nil {
expectedStr = "nil"
} else {
expectedStr = expectedType.String()
}

t.Helper()
t.Errorf(`%sFailed test
got: %s
expected: %s`,
buildTestName(args), gotStr, expectedStr)
return false
}

func buildTestName(args []interface{}) string {
switch len(args) {
case 0:
Expand Down
4 changes: 4 additions & 0 deletions td_all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,7 @@ func TestAll(t *testing.T) {
equalStr(t, All(6).String(), "All((int) 6)")
equalStr(t, All(6, 7).String(), "All((int) 6,\n (int) 7)")
}

func TestAllTypeOf(t *testing.T) {
equalTypes(t, All(6), nil)
}
4 changes: 4 additions & 0 deletions td_any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ func TestAny(t *testing.T) {
equalStr(t, Any(6).String(), "Any((int) 6)")
equalStr(t, Any(6, 7).String(), "Any((int) 6,\n (int) 7)")
}

func TestAnyTypeOf(t *testing.T) {
equalTypes(t, Any(6), nil)
}
11 changes: 11 additions & 0 deletions td_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type ArrayEntries map[int]interface{}
//
// "expectedEntries" can be nil, if no zero entries are expected and
// no TestDeep operator are involved.
//
// TypeOf method returns the reflect.Type of "model".
func Array(model interface{}, expectedEntries ArrayEntries) TestDeep {
vmodel := reflect.ValueOf(model)

Expand Down Expand Up @@ -68,6 +70,8 @@ func Array(model interface{}, expectedEntries ArrayEntries) TestDeep {
//
// "expectedEntries" can be nil, if no zero entries are expected and
// no TestDeep operator are involved.
//
// TypeOf method returns the reflect.Type of "model".
func Slice(model interface{}, expectedEntries ArrayEntries) TestDeep {
vmodel := reflect.ValueOf(model)

Expand Down Expand Up @@ -271,6 +275,13 @@ func (a *tdArray) String() string {
return buf.String()
}

func (s *tdArray) TypeOf() reflect.Type {
if s.isPtr {
return reflect.New(s.expectedModel.Type()).Type()
}
return s.expectedModel.Type()
}

func (a *tdArray) expectedTypeStr() string {
if a.isPtr {
return "*" + a.expectedModel.Type().String()
Expand Down
4 changes: 4 additions & 0 deletions td_array_each_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,7 @@ func TestArrayEach(t *testing.T) {
`ArrayEach(All((int) 1,
(int) 2))`)
}

func TestArrayEachTypeOf(t *testing.T) {
equalTypes(t, ArrayEach(6), nil)
}
16 changes: 16 additions & 0 deletions td_array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ func TestArray(t *testing.T) {
`Array([0]int{})`)
}

func TestArrayTypeOf(t *testing.T) {
type MyArray [12]int

equalTypes(t, Array([12]int{}, nil), [12]int{})
equalTypes(t, Array(MyArray{}, nil), MyArray{})
equalTypes(t, Array(&MyArray{}, nil), &MyArray{})
}

func TestSlice(t *testing.T) {
type MySlice []int

Expand Down Expand Up @@ -322,3 +330,11 @@ func TestSlice(t *testing.T) {
equalStr(t, Slice(&MySlice{}, ArrayEntries{}).String(),
`Slice(*testdeep_test.MySlice{})`)
}

func TestSliceTypeOf(t *testing.T) {
type MySlice []int

equalTypes(t, Slice([]int{}, nil), []int{})
equalTypes(t, Slice(MySlice{}, nil), MySlice{})
equalTypes(t, Slice(&MySlice{}, nil), &MySlice{})
}
6 changes: 6 additions & 0 deletions td_bag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,9 @@ func TestBag(t *testing.T) {
equalStr(t, SuperBagOf(1, 2).String(),
"SuperBagOf((int) 1,\n (int) 2)")
}

func TestBagTypeOf(t *testing.T) {
equalTypes(t, Bag(6), nil)
equalTypes(t, SubBagOf(6), nil)
equalTypes(t, SuperBagOf(6), nil)
}
20 changes: 20 additions & 0 deletions td_between.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ var _ TestDeep = &tdBetweenTime{}
// assignable). "bounds" allows to specify whether bounds are included
// or not. See Bounds* constants for details. If "bounds" is missing,
// it defaults to BoundsInIn.
//
// TypeOf method returns the reflect.Type of "from" (same as the "to" one.)
func Between(from interface{}, to interface{}, bounds ...BoundsKind) TestDeep {
b := tdBetween{
expectedMin: reflect.ValueOf(from),
Expand Down Expand Up @@ -209,6 +211,8 @@ func (b *tdBetween) nFloat(tolerance reflect.Value) {
// N operator compares a numeric data against "num" ± "tolerance". If
// "tolerance" is missing, it defaults to 0. "num" and "tolerance"
// must be the same kind as the compared value.
//
// TypeOf method returns the reflect.Type of "num".
func N(num interface{}, tolerance ...interface{}) TestDeep {
n := tdBetween{
Base: NewBase(3),
Expand Down Expand Up @@ -259,6 +263,8 @@ func N(num interface{}, tolerance ...interface{}) TestDeep {
// any numeric or time.Time (or assignable) value. "val" must be the
// same kind as the compared value if numeric, and the same type if
// time.Time (or assignable).
//
// TypeOf method returns the reflect.Type of "val".
func Gt(val interface{}) TestDeep {
b := &tdBetween{
expectedMin: reflect.ValueOf(val),
Expand All @@ -271,6 +277,8 @@ func Gt(val interface{}) TestDeep {
// can be any numeric or time.Time (or assignable) value. "val" must
// be the same kind as the compared value if numeric, and the same
// type if time.Time (or assignable).
//
// TypeOf method returns the reflect.Type of "val".
func Gte(val interface{}) TestDeep {
b := &tdBetween{
expectedMin: reflect.ValueOf(val),
Expand All @@ -283,6 +291,8 @@ func Gte(val interface{}) TestDeep {
// any numeric or time.Time (or assignable) value. "val" must be the
// same kind as the compared value if numeric, and the same type if
// time.Time (or assignable).
//
// TypeOf method returns the reflect.Type of "val".
func Lt(val interface{}) TestDeep {
b := &tdBetween{
expectedMin: reflect.ValueOf(val),
Expand All @@ -295,6 +305,8 @@ func Lt(val interface{}) TestDeep {
// can be any numeric or time.Time (or assignable) value. "val" must
// be the same kind as the compared value if numeric, and the same
// type if time.Time (or assignable).
//
// TypeOf method returns the reflect.Type of "val".
func Lte(val interface{}) TestDeep {
b := &tdBetween{
expectedMin: reflect.ValueOf(val),
Expand Down Expand Up @@ -441,6 +453,10 @@ func (b *tdBetween) String() string {
ternRune(b.maxBound == boundIn, '≤', '<'), max)
}

func (b *tdBetween) TypeOf() reflect.Type {
return b.expectedMin.Type()
}

var _ TestDeep = &tdBetweenTime{}

func (b *tdBetweenTime) Match(ctx Context, got reflect.Value) *Error {
Expand Down Expand Up @@ -500,3 +516,7 @@ func (b *tdBetweenTime) Match(ctx Context, got reflect.Value) *Error {
Location: b.GetLocation(),
}
}

func (b *tdBetweenTime) TypeOf() reflect.Type {
return b.expectedType
}
16 changes: 16 additions & 0 deletions td_between_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,19 @@ func TestBetweenTime(t *testing.T) {
checkOK(t, now, Gt(now.Add(-time.Second)))
checkOK(t, now, Lt(now.Add(time.Second)))
}

func TestBetweenTypeOf(t *testing.T) {
equalTypes(t, Between(0, 10), 23)
equalTypes(t, Between(int64(0), int64(10)), int64(23))

type MyTime time.Time

equalTypes(t, Between(time.Time{}, time.Time{}), time.Time{})
equalTypes(t, Between(MyTime{}, MyTime{}), MyTime{})

equalTypes(t, N(int64(23), int64(5)), int64(0))
equalTypes(t, Gt(int32(23)), int32(0))
equalTypes(t, Gte(int32(23)), int32(0))
equalTypes(t, Lt(int32(23)), int32(0))
equalTypes(t, Lte(int32(23)), int32(0))
}
4 changes: 4 additions & 0 deletions td_code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,7 @@ func TestCode(t *testing.T) {
Code(func(n int) (MyBool, MyString) { return false, "" }).String(),
"Code(func(int) (testdeep_test.MyBool, testdeep_test.MyString))")
}

func TestCodeTypeOf(t *testing.T) {
equalTypes(t, Code(func(n int) bool { return false }), nil)
}
4 changes: 4 additions & 0 deletions td_ignore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ func TestIgnore(t *testing.T) {
// String
equalStr(t, Ignore().String(), "Ignore()")
}

func TestIgnoreTypeOf(t *testing.T) {
equalTypes(t, Ignore(), nil)
}
6 changes: 6 additions & 0 deletions td_isa.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var _ TestDeep = &tdIsa{}
// Of course, in the latter case, if data type is *fmt.Stringer, Isa
// will match too (in fact before checking whether it implements
// fmt.Stringer or not.)
//
// TypeOf method returns the reflect.Type of "model".
func Isa(model interface{}) TestDeep {
modelType := reflect.ValueOf(model).Type()

Expand Down Expand Up @@ -75,3 +77,7 @@ func (i *tdIsa) Match(ctx Context, got reflect.Value) (err *Error) {
func (i *tdIsa) String() string {
return i.expectedType.String()
}

func (i *tdIsa) TypeOf() reflect.Type {
return i.expectedType
}
4 changes: 4 additions & 0 deletions td_isa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,7 @@ func TestIsa(t *testing.T) {
// String
equalStr(t, Isa((*MyStruct)(nil)).String(), "*testdeep_test.MyStruct")
}

func TestIsaTypeOf(t *testing.T) {
equalTypes(t, Isa(([]int)(nil)), []int{})
}
5 changes: 5 additions & 0 deletions td_len_cap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,8 @@ func TestCap(t *testing.T) {
// Bad usage
checkPanic(t, func() { Cap(int64(12)) }, "usage: Cap(")
}

func TestLenCapTypeOf(t *testing.T) {
equalTypes(t, Cap(3), nil)
equalTypes(t, Len(3), nil)
}
13 changes: 13 additions & 0 deletions td_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ func (m *tdMap) populateExpectedEntries(entries MapEntries) {
//
// During a match, all expected entries must be found and all data
// entries must be expected to succeed.
//
// TypeOf method returns the reflect.Type of "model".
func Map(model interface{}, expectedEntries MapEntries) TestDeep {
return newMap(model, expectedEntries, allMap)
}
Expand All @@ -151,6 +153,8 @@ func Map(model interface{}, expectedEntries MapEntries) TestDeep {
//
// CmpDeeply(t, map[string]int{"a": 1, "c": 3},
// SubMapOf(map[string]int{"a": 1, "b": 2}, nil) // fails, extra {"c": 3}
//
// TypeOf method returns the reflect.Type of "model".
func SubMapOf(model interface{}, expectedEntries MapEntries) TestDeep {
return newMap(model, expectedEntries, subMap)
}
Expand All @@ -171,6 +175,8 @@ func SubMapOf(model interface{}, expectedEntries MapEntries) TestDeep {
//
// CmpDeeply(t, map[string]int{"a": 1, "c": 3},
// SuperMapOf(map[string]int{"a": 1, "b": 2}, nil) // fails, missing {"b": 2}
//
// TypeOf method returns the reflect.Type of "model".
func SuperMapOf(model interface{}, expectedEntries MapEntries) TestDeep {
return newMap(model, expectedEntries, superMap)
}
Expand Down Expand Up @@ -331,6 +337,13 @@ func (m *tdMap) String() string {
return buf.String()
}

func (s *tdMap) TypeOf() reflect.Type {
if s.isPtr {
return reflect.New(s.expectedModel.Type()).Type()
}
return s.expectedModel.Type()
}

func (m *tdMap) expectedTypeStr() string {
if m.isPtr {
return "*" + m.expectedModel.Type().String()
Expand Down
4 changes: 4 additions & 0 deletions td_map_each_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,7 @@ func TestMapEach(t *testing.T) {
`MapEach(All((int) 1,
(int) 2))`)
}

func TestMapEachTypeOf(t *testing.T) {
equalTypes(t, MapEach(4), nil)
}
21 changes: 20 additions & 1 deletion td_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
. "github.com/maxatome/go-testdeep"
)

func TestHash(t *testing.T) {
func TestMap(t *testing.T) {
type MyMap map[string]int

//
Expand Down Expand Up @@ -296,3 +296,22 @@ func TestHash(t *testing.T) {
(string) (len=3) "foo": (int) 2,
})`)
}

func TestMapTypeOf(t *testing.T) {
type MyMap map[string]int

// Map
equalTypes(t, Map(map[string]int{}, nil), map[string]int{})
equalTypes(t, Map(MyMap{}, nil), MyMap{})
equalTypes(t, Map(&MyMap{}, nil), &MyMap{})

// SubMap
equalTypes(t, SubMapOf(map[string]int{}, nil), map[string]int{})
equalTypes(t, SubMapOf(MyMap{}, nil), MyMap{})
equalTypes(t, SubMapOf(&MyMap{}, nil), &MyMap{})

// SuperMap
equalTypes(t, SuperMapOf(map[string]int{}, nil), map[string]int{})
equalTypes(t, SuperMapOf(MyMap{}, nil), MyMap{})
equalTypes(t, SuperMapOf(&MyMap{}, nil), &MyMap{})
}
5 changes: 5 additions & 0 deletions td_nil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,8 @@ func TestNotNil(t *testing.T) {
// String
equalStr(t, NotNil().String(), "not nil")
}

func TestNilTypeOf(t *testing.T) {
equalTypes(t, Nil(), nil)
equalTypes(t, NotNil(), nil)
}
5 changes: 5 additions & 0 deletions td_none_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,8 @@ func TestNot(t *testing.T) {
// String
equalStr(t, Not(6).String(), "Not((int) 6)")
}

func TestNoneTypeOf(t *testing.T) {
equalTypes(t, None(6), nil)
equalTypes(t, Not(6), nil)
}
5 changes: 5 additions & 0 deletions td_ptr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,8 @@ func TestPtr(t *testing.T) {
equalStr(t, Ptr(Ptr(13)).String(), "*<something>")
equalStr(t, PPtr(Ptr(13)).String(), "**<something>")
}

func TestPtrTypeOf(t *testing.T) {
equalTypes(t, Ptr(6), nil)
equalTypes(t, PPtr(6), nil)
}
4 changes: 4 additions & 0 deletions td_re_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,7 @@ func TestRe(t *testing.T) {
const reAllUsage = "usage: ReAll("
checkPanic(t, func() { ReAll(123, 456) }, reAllUsage)
}

func TestReTypeOf(t *testing.T) {
equalTypes(t, Re("x"), nil)
}

0 comments on commit d8c2892

Please sign in to comment.