| 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/util" |
| 14 |
) |
| 15 |
|
| 16 |
type tdTag struct { |
| 17 |
tdSmugglerBase |
| 18 |
tag string |
| 19 |
} |
| 20 |
|
| 21 |
var _ TestDeep = &tdTag{} |
| 22 |
|
| 23 |
// summary(Tag): names an operator or a value. Only useful as a |
| 24 |
// parameter of JSON operator, to name placeholders |
| 25 |
// input(Tag): all |
| 26 |
|
| 27 |
// Tag is a smuggler operator. It only allows to name expectedValue, |
| 28 |
// which can be an operator or a value. The data is then compared |
| 29 |
// against expectedValue as if Tag was never called. It is only useful |
| 30 |
// as [JSON] operator parameter, to name placeholders. See [JSON] |
| 31 |
// operator for more details. |
| 32 |
// |
| 33 |
// td.Cmp(t, gotValue, |
| 34 |
// td.JSON(`{"fullname": $name, "age": $age, "gender": $gender}`, |
| 35 |
// td.Tag("name", td.HasPrefix("Foo")), // matches $name |
| 36 |
// td.Tag("age", td.Between(41, 43)), // matches $age |
| 37 |
// td.Tag("gender", "male"))) // matches $gender |
| 38 |
// |
| 39 |
// TypeBehind method is delegated to expectedValue one if |
| 40 |
// expectedValue is a [TestDeep] operator, otherwise it returns the |
| 41 |
// type of expectedValue (or nil if it is originally untyped nil). |
| 42 |
func Tag(tag string, expectedValue any) TestDeep { |
| 43 |
t := tdTag{ |
| 44 |
tdSmugglerBase: newSmugglerBase(expectedValue), |
| 45 |
tag: tag, |
| 46 |
} |
| 47 |
|
| 48 |
if err := util.CheckTag(tag); err != nil { |
| 49 |
t.err = ctxerr.OpBad("Tag", err.Error()) |
| 50 |
return &t |
| 51 |
} |
| 52 |
|
| 53 |
if !t.isTestDeeper { |
| 54 |
t.expectedValue = reflect.ValueOf(expectedValue) |
| 55 |
} |
| 56 |
return &t |
| 57 |
} |
| 58 |
|
| 59 |
func (t *tdTag) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 60 |
if t.err != nil { |
| 61 |
return ctx.CollectError(t.err) |
| 62 |
} |
| 63 |
return deepValueEqual(ctx, got, t.expectedValue) |
| 64 |
} |
| 65 |
|
| 66 |
func (t *tdTag) HandleInvalid() bool { |
| 67 |
return true // Knows how to handle untyped nil values (aka invalid values) |
| 68 |
} |
| 69 |
|
| 70 |
func (t *tdTag) String() string { |
| 71 |
if t.err != nil { |
| 72 |
return t.stringError() |
| 73 |
} |
| 74 |
if t.isTestDeeper { |
| 75 |
return t.expectedValue.Interface().(TestDeep).String() |
| 76 |
} |
| 77 |
return util.ToString(t.expectedValue) |
| 78 |
} |
| 79 |
|
| 80 |
func (t *tdTag) TypeBehind() reflect.Type { |
| 81 |
if t.err != nil { |
| 82 |
return nil |
| 83 |
} |
| 84 |
if t.isTestDeeper { |
| 85 |
return t.expectedValue.Interface().(TestDeep).TypeBehind() |
| 86 |
} |
| 87 |
if t.expectedValue.IsValid() { |
| 88 |
return t.expectedValue.Type() |
| 89 |
} |
| 90 |
return nil |
| 91 |
} |