Debian Codegraph @

td/td_smuggler_base.go

raw
1 // Copyright (c) 2018, 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 "fmt"
11 "reflect"
12
13 "github.com/maxatome/go-testdeep/internal/ctxerr"
14 )
15
16 // tdSmugglerBase is the base class of all smuggler TestDeep operators.
17 type tdSmugglerBase struct {
18 base
19 expectedValue reflect.Value
20 isTestDeeper bool
21 }
22
23 func newSmugglerBase(val any, depth ...int) (ret tdSmugglerBase) {
24 callDepth := 4
25 if len(depth) > 0 {
26 callDepth += depth[0]
27 }
28 ret.base = newBase(callDepth)
29
30 // Initializes only if TestDeep operator. Other cases are specific.
31 if _, ok := val.(TestDeep); ok {
32 ret.expectedValue = reflect.ValueOf(val)
33 ret.isTestDeeper = true
34 }
35 return
36 }
37
38 // internalTypeBehind returns the type behind expectedValue or nil if
39 // it cannot be determined.
40 func (s *tdSmugglerBase) internalTypeBehind() reflect.Type {
41 if s.isTestDeeper {
42 return s.expectedValue.Interface().(TestDeep).TypeBehind()
43 }
44 if s.expectedValue.IsValid() {
45 return s.expectedValue.Type()
46 }
47 return nil
48 }
49
50 // jsonValueEqual compares got to expectedValue, trying to do it
51 // using a JSON point of view. It is the caller responsibility to
52 // ensure that got value is either a bool, float64, string,
53 // []any, a map[string]any or simply nil.
54 //
55 // If the type behind expectedValue can be determined and is different
56 // from got type, got value is JSON marshaled, then unmarshaled
57 // in a new value of this type. This new value is then compared to
58 // expectedValue.
59 //
60 // Otherwise, got value is compared as-is to expectedValue.
61 //
62 // If opts is nil then encoding/json is used, else encoding/json/v2 is
63 // used with opts as json.Options.
64 func (s *tdSmugglerBase) jsonValueEqual(ctx ctxerr.Context, got any, opts jsonv2Options) *ctxerr.Error {
65 expectedType := s.internalTypeBehind()
66
67 // Unknown expected type (operator with nil TypeBehind() result or
68 // untyped nil), lets deepValueEqual() handles the comparison using
69 // BeLax flag
70 if expectedType == nil {
71 return deepValueEqual(ctx, reflect.ValueOf(got), s.expectedValue)
72 }
73
74 // Same type for got & expected type, no need to Marshal/Unmarshal
75 if got != nil && expectedType == reflect.TypeOf(got) {
76 return deepValueEqual(ctx, reflect.ValueOf(got), s.expectedValue)
77 }
78
79 // Unmarshal got into the expectedType
80 b, _ := jsonMarshal(got, opts) // No error can occur here
81
82 finalGot := reflect.New(expectedType)
83 if err := jsonUnmarshal(b, finalGot.Interface(), opts); err != nil {
84 if ctx.BooleanError {
85 return ctxerr.BooleanError
86 }
87 return ctx.CollectError(&ctxerr.Error{
88 Message: fmt.Sprintf(
89 "an error occurred while unmarshaling JSON into %s", expectedType),
90 Summary: ctxerr.NewSummary(err.Error()),
91 })
92 }
93
94 return deepValueEqual(ctx, finalGot.Elem(), s.expectedValue)
95 }