| 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 tdLax struct { |
| 17 |
tdSmugglerBase |
| 18 |
} |
| 19 |
|
| 20 |
var _ TestDeep = &tdLax{} |
| 21 |
|
| 22 |
// summary(Lax): temporarily enables [`BeLax` config flag] |
| 23 |
// input(Lax): all |
| 24 |
|
| 25 |
// Lax is a smuggler operator, it temporarily enables the BeLax config |
| 26 |
// flag before letting the comparison process continue its course. |
| 27 |
// |
| 28 |
// It is more commonly used as [CmpLax] function than as an operator. It |
| 29 |
// could be used when, for example, an operator is constructed once |
| 30 |
// but applied to different, but compatible types as in: |
| 31 |
// |
| 32 |
// bw := td.Between(20, 30) |
| 33 |
// intValue := 21 |
| 34 |
// floatValue := 21.89 |
| 35 |
// td.Cmp(t, intValue, bw) // no need to be lax here: same int types |
| 36 |
// td.Cmp(t, floatValue, td.Lax(bw)) // be lax please, as float64 ≠ int |
| 37 |
// |
| 38 |
// Note that in the latter case, [CmpLax] could be used as well: |
| 39 |
// |
| 40 |
// td.CmpLax(t, floatValue, bw) |
| 41 |
// |
| 42 |
// TypeBehind method returns the greatest convertible or more common |
| 43 |
// [reflect.Type] of expectedValue if it is a base type (bool, int*, |
| 44 |
// uint*, float*, complex*, string), the [reflect.Type] of |
| 45 |
// expectedValue otherwise, except if expectedValue is a [TestDeep] |
| 46 |
// operator. In this case, it delegates TypeBehind() to the operator. |
| 47 |
func Lax(expectedValue any) TestDeep { |
| 48 |
c := tdLax{ |
| 49 |
tdSmugglerBase: newSmugglerBase(expectedValue), |
| 50 |
} |
| 51 |
|
| 52 |
if !c.isTestDeeper { |
| 53 |
c.expectedValue = reflect.ValueOf(expectedValue) |
| 54 |
} |
| 55 |
return &c |
| 56 |
} |
| 57 |
|
| 58 |
func (l *tdLax) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 59 |
ctx.BeLax = true |
| 60 |
return deepValueEqual(ctx, got, l.expectedValue) |
| 61 |
} |
| 62 |
|
| 63 |
func (l *tdLax) HandleInvalid() bool { |
| 64 |
return true // Knows how to handle untyped nil values (aka invalid values) |
| 65 |
} |
| 66 |
|
| 67 |
func (l *tdLax) String() string { |
| 68 |
return "Lax(" + util.ToString(l.expectedValue) + ")" |
| 69 |
} |
| 70 |
|
| 71 |
func (l *tdLax) TypeBehind() reflect.Type { |
| 72 |
// If the expected value is a TestDeep operator, delegate TypeBehind to it |
| 73 |
if l.isTestDeeper { |
| 74 |
return l.expectedValue.Interface().(TestDeep).TypeBehind() |
| 75 |
} |
| 76 |
|
| 77 |
// For base types, returns the greatest convertible or more common one |
| 78 |
switch l.expectedValue.Kind() { |
| 79 |
case reflect.Invalid: |
| 80 |
return nil |
| 81 |
case reflect.Bool: |
| 82 |
return reflect.TypeOf(false) |
| 83 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 84 |
return reflect.TypeOf(int64(0)) |
| 85 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 86 |
return reflect.TypeOf(uint64(0)) |
| 87 |
case reflect.Float32, reflect.Float64: |
| 88 |
return reflect.TypeOf(float64(0)) |
| 89 |
case reflect.Complex64, reflect.Complex128: |
| 90 |
return reflect.TypeOf(complex(128, -1)) |
| 91 |
case reflect.String: |
| 92 |
return reflect.TypeOf("") |
| 93 |
default: |
| 94 |
return l.expectedValue.Type() |
| 95 |
} |
| 96 |
} |