Skip to content

Commit

Permalink
Smuggle operator more lax [WIP]
Browse files Browse the repository at this point in the history
Signed-off-by: Maxime Soulé <btik-git@scoubidou.com>
  • Loading branch information
maxatome committed Dec 4, 2018
1 parent 2b771f5 commit 160d19c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
18 changes: 17 additions & 1 deletion td_smuggle.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,24 @@ func Smuggle(fn interface{}, expectedValue interface{}) TestDeep {
": FUNC must return value or (value, bool) or (value, bool, string)")
}

func (s *tdSmuggle) convert(got reflect.Value) (reflect.Value, bool) {
if !got.Type().ConvertibleTo(s.argType) {
if got.Kind() != reflect.Interface || got.IsNil() {
return got, false
}

got = got.Elem()
if !got.Type().ConvertibleTo(s.argType) {
return got, false
}
}

return got.Convert(s.argType), true
}

func (s *tdSmuggle) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {
if !got.Type().AssignableTo(s.argType) {
got, ok := s.convert(got)
if !ok {
if ctx.BooleanError {
return ctxerr.BooleanError
}
Expand Down
45 changes: 37 additions & 8 deletions td_smuggle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,25 +146,54 @@ func TestSmuggle(t *testing.T) {
},
testdeep.Contains("oob")))

//
// Convertible types
checkOK(t, 123,
testdeep.Smuggle(func(n float64) int { return int(n) }, 123))

type xInt int
checkOK(t, xInt(123),
testdeep.Smuggle(func(n int) int64 { return int64(n) }, int64(123)))
checkOK(t, xInt(123),
testdeep.Smuggle(func(n uint32) int64 { return int64(n) }, int64(123)))

type tVal struct{ Val interface{} }
checkOK(t, tVal{Val: int32(123)},
testdeep.Struct(tVal{}, testdeep.StructFields{
"Val": testdeep.Smuggle(func(n int64) int { return int(n) }, 123),
}))

//
// Errors
checkError(t, 123,
checkError(t, "123",
testdeep.Smuggle(func(n float64) int { return int(n) }, 123),
expectedError{
Message: mustBe("incompatible parameter type"),
Path: mustBe("DATA"),
Got: mustBe("int"),
Got: mustBe("string"),
Expected: mustBe("float64"),
})

type xInt int
checkError(t, xInt(12),
testdeep.Smuggle(func(n int) int64 { return int64(n) }, 12),
checkError(t, tVal{},
testdeep.Struct(tVal{}, testdeep.StructFields{
"Val": testdeep.Smuggle(func(n int64) int { return int(n) }, 123),
}),
expectedError{
Message: mustBe("incompatible parameter type"),
Path: mustBe("DATA"),
Got: mustBe("testdeep_test.xInt"),
Expected: mustBe("int"),
Path: mustBe("DATA.Val"),
Got: mustBe("interface {}"),
Expected: mustBe("int64"),
})

checkError(t, tVal{Val: "str"},
testdeep.Struct(tVal{}, testdeep.StructFields{
"Val": testdeep.Smuggle(func(n int64) int { return int(n) }, 123),
}),
expectedError{
Message: mustBe("incompatible parameter type"),
Path: mustBe("DATA.Val"),
Got: mustBe("string"),
Expected: mustBe("int64"),
})

checkError(t, 12,
Expand Down

0 comments on commit 160d19c

Please sign in to comment.