Debian Codegraph @

td/td_catch.go

raw
1 // Copyright (c) 2019, Maxime Soulé
2 // All rights reserved.
3 //
4 // This source code is licensed under the BSD-style license found in the
5 // LICENSE file in the root directory of this source tree.
6
7 package td
8
9 import (
10 "reflect"
11
12 "github.com/maxatome/go-testdeep/internal/ctxerr"
13 "github.com/maxatome/go-testdeep/internal/types"
14 "github.com/maxatome/go-testdeep/internal/util"
15 )
16
17 type tdCatch struct {
18 tdSmugglerBase
19 target reflect.Value
20 }
21
22 var _ TestDeep = &tdCatch{}
23
24 // summary(Catch): catches data on the fly before comparing it
25 // input(Catch): all
26
27 // Catch is a smuggler operator. It allows to copy data in target on
28 // the fly before comparing it as usual against expectedValue.
29 //
30 // target must be a non-nil pointer and data should be assignable to
31 // its pointed type. If BeLax config flag is true or called under [Lax]
32 // (and so [JSON]) operator, data should be convertible to its pointer
33 // type.
34 //
35 // var id int64
36 // if td.Cmp(t, CreateRecord("test"),
37 // td.JSON(`{"id": $1, "name": "test"}`, td.Catch(&id, td.NotZero()))) {
38 // t.Logf("Created record ID is %d", id)
39 // }
40 //
41 // It is really useful when used with [JSON] operator and/or [tdhttp] helper.
42 //
43 // var id int64
44 // ta := tdhttp.NewTestAPI(t, api.Handler).
45 // PostJSON("/item", `{"name":"foo"}`).
46 // CmpStatus(http.StatusCreated).
47 // CmpJSONBody(td.JSON(`{"id": $1, "name": "foo"}`, td.Catch(&id, td.Gt(0))))
48 // if !ta.Failed() {
49 // t.Logf("Created record ID is %d", id)
50 // }
51 //
52 // If you need to only catch data without comparing it, use [Ignore]
53 // operator as expectedValue as in:
54 //
55 // var id int64
56 // if td.Cmp(t, CreateRecord("test"),
57 // td.JSON(`{"id": $1, "name": "test"}`, td.Catch(&id, td.Ignore()))) {
58 // t.Logf("Created record ID is %d", id)
59 // }
60 //
61 // TypeBehind method returns the [reflect.Type] of expectedValue,
62 // except if expectedValue is a [TestDeep] operator. In this case, it
63 // delegates TypeBehind() to the operator, but if nil is returned by
64 // this call, the dereferenced [reflect.Type] of target is returned.
65 //
66 // [tdhttp]: https://pkg.go.dev/github.com/maxatome/go-testdeep/helpers/tdhttp
67 func Catch(target, expectedValue any) TestDeep {
68 vt := reflect.ValueOf(target)
69 c := tdCatch{
70 tdSmugglerBase: newSmugglerBase(expectedValue),
71 target: vt,
72 }
73
74 if vt.Kind() != reflect.Ptr || vt.IsNil() || !vt.Elem().CanSet() {
75 c.err = ctxerr.OpBadUsage("Catch", "(NON_NIL_PTR, EXPECTED_VALUE)", target, 1, true)
76 return &c
77 }
78
79 if !c.isTestDeeper {
80 c.expectedValue = reflect.ValueOf(expectedValue)
81 }
82 return &c
83 }
84
85 func (c *tdCatch) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {
86 if c.err != nil {
87 return ctx.CollectError(c.err)
88 }
89
90 if targetType := c.target.Elem().Type(); !got.Type().AssignableTo(targetType) {
91 if !ctx.BeLax || !types.IsConvertible(got, targetType) {
92 if ctx.BooleanError {
93 return ctxerr.BooleanError
94 }
95 return ctx.CollectError(ctxerr.TypeMismatch(got.Type(), c.target.Elem().Type()))
96 }
97
98 c.target.Elem().Set(got.Convert(targetType))
99 } else {
100 c.target.Elem().Set(got)
101 }
102
103 return deepValueEqual(ctx, got, c.expectedValue)
104 }
105
106 func (c *tdCatch) String() string {
107 if c.err != nil {
108 return c.stringError()
109 }
110
111 if c.isTestDeeper {
112 return c.expectedValue.Interface().(TestDeep).String()
113 }
114 return util.ToString(c.expectedValue)
115 }
116
117 func (c *tdCatch) TypeBehind() reflect.Type {
118 if c.err != nil {
119 return nil
120 }
121
122 if c.isTestDeeper {
123 if typ := c.expectedValue.Interface().(TestDeep).TypeBehind(); typ != nil {
124 return typ
125 }
126 // Operator unknown type behind, fallback on target dereferenced type
127 return c.target.Type().Elem()
128 }
129 if c.expectedValue.IsValid() {
130 return c.expectedValue.Type()
131 }
132 return nil
133 }