| 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 |
"math" |
| 12 |
"reflect" |
| 13 |
|
| 14 |
"github.com/maxatome/go-testdeep/internal/ctxerr" |
| 15 |
"github.com/maxatome/go-testdeep/internal/types" |
| 16 |
) |
| 17 |
|
| 18 |
type tdLenCapBase struct { |
| 19 |
tdSmugglerBase |
| 20 |
} |
| 21 |
|
| 22 |
func (b *tdLenCapBase) initLenCapBase(val any) { |
| 23 |
b.tdSmugglerBase = newSmugglerBase(val, 1) |
| 24 |
|
| 25 |
// math.MaxInt appeared in go1.17 |
| 26 |
const ( |
| 27 |
maxUint = ^uint(0) |
| 28 |
maxInt = int(maxUint >> 1) |
| 29 |
minInt = -maxInt - 1 |
| 30 |
usage = "(TESTDEEP_OPERATOR|INT)" |
| 31 |
) |
| 32 |
|
| 33 |
if val == nil { |
| 34 |
b.err = ctxerr.OpBadUsage(b.GetLocation().Func, usage, val, 1, true) |
| 35 |
return |
| 36 |
} |
| 37 |
|
| 38 |
if b.isTestDeeper { |
| 39 |
return |
| 40 |
} |
| 41 |
|
| 42 |
vval := reflect.ValueOf(val) |
| 43 |
|
| 44 |
// A len or capacity is always an int, but accept any MinInt ≤ num ≤ MaxInt, |
| 45 |
// so it can be used in JSON, SubJSONOf and SuperJSONOf as float64 |
| 46 |
switch vval.Kind() { |
| 47 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 48 |
num := vval.Int() |
| 49 |
if num >= int64(minInt) && num <= int64(maxInt) { |
| 50 |
b.expectedValue = reflect.ValueOf(int(num)) |
| 51 |
return |
| 52 |
} |
| 53 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 54 |
num := vval.Uint() |
| 55 |
if num <= uint64(maxInt) { |
| 56 |
b.expectedValue = reflect.ValueOf(int(num)) |
| 57 |
return |
| 58 |
} |
| 59 |
case reflect.Float32, reflect.Float64: |
| 60 |
num := vval.Float() |
| 61 |
if num == math.Trunc(num) && num >= float64(minInt) && num <= float64(maxInt) { |
| 62 |
b.expectedValue = reflect.ValueOf(int(num)) |
| 63 |
return |
| 64 |
} |
| 65 |
default: |
| 66 |
b.err = ctxerr.OpBadUsage(b.GetLocation().Func, usage, val, 1, true) |
| 67 |
return |
| 68 |
} |
| 69 |
|
| 70 |
op := b.GetLocation().Func |
| 71 |
b.err = ctxerr.OpBad(op, "usage: "+op+usage+ |
| 72 |
", but received an out of bounds or not integer 1st parameter (%v), should be in int range", val) |
| 73 |
} |
| 74 |
|
| 75 |
func (b *tdLenCapBase) isEqual(ctx ctxerr.Context, got int) (bool, *ctxerr.Error) { |
| 76 |
if b.isTestDeeper { |
| 77 |
return true, deepValueEqual(ctx, reflect.ValueOf(got), b.expectedValue) |
| 78 |
} |
| 79 |
|
| 80 |
if int64(got) == b.expectedValue.Int() { |
| 81 |
return true, nil |
| 82 |
} |
| 83 |
|
| 84 |
return false, nil |
| 85 |
} |
| 86 |
|
| 87 |
type tdLen struct { |
| 88 |
tdLenCapBase |
| 89 |
} |
| 90 |
|
| 91 |
var _ TestDeep = &tdLen{} |
| 92 |
|
| 93 |
// summary(Len): checks an array, slice, map, string or channel length |
| 94 |
// input(Len): array,slice,map,chan |
| 95 |
|
| 96 |
// Len is a smuggler operator. It takes data, applies len() function |
| 97 |
// on it and compares its result to expectedLen. Of course, the |
| 98 |
// compared value must be an array, a channel, a map, a slice or a |
| 99 |
// string. |
| 100 |
// |
| 101 |
// expectedLen can be an int value: |
| 102 |
// |
| 103 |
// td.Cmp(t, gotSlice, td.Len(12)) |
| 104 |
// |
| 105 |
// as well as an other operator: |
| 106 |
// |
| 107 |
// td.Cmp(t, gotSlice, td.Len(td.Between(3, 4))) |
| 108 |
// |
| 109 |
// See also [Cap]. |
| 110 |
func Len(expectedLen any) TestDeep { |
| 111 |
l := tdLen{} |
| 112 |
l.initLenCapBase(expectedLen) |
| 113 |
return &l |
| 114 |
} |
| 115 |
|
| 116 |
func (l *tdLen) String() string { |
| 117 |
if l.err != nil { |
| 118 |
return l.stringError() |
| 119 |
} |
| 120 |
if l.isTestDeeper { |
| 121 |
return "len: " + l.expectedValue.Interface().(TestDeep).String() |
| 122 |
} |
| 123 |
return fmt.Sprintf("len=%d", l.expectedValue.Int()) |
| 124 |
} |
| 125 |
|
| 126 |
func (l *tdLen) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 127 |
if l.err != nil { |
| 128 |
return ctx.CollectError(l.err) |
| 129 |
} |
| 130 |
|
| 131 |
switch got.Kind() { |
| 132 |
case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String: |
| 133 |
ret, err := l.isEqual(ctx.AddFunctionCall("len"), got.Len()) |
| 134 |
if ret { |
| 135 |
return err |
| 136 |
} |
| 137 |
if ctx.BooleanError { |
| 138 |
return ctxerr.BooleanError |
| 139 |
} |
| 140 |
return ctx.CollectError(&ctxerr.Error{ |
| 141 |
Message: "bad length", |
| 142 |
Got: types.RawInt(got.Len()), |
| 143 |
Expected: types.RawInt(l.expectedValue.Int()), |
| 144 |
}) |
| 145 |
|
| 146 |
default: |
| 147 |
if ctx.BooleanError { |
| 148 |
return ctxerr.BooleanError |
| 149 |
} |
| 150 |
return ctx.CollectError(ctxerr.BadKind(got, "array OR chan OR map OR slice OR string")) |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
type tdCap struct { |
| 155 |
tdLenCapBase |
| 156 |
} |
| 157 |
|
| 158 |
var _ TestDeep = &tdCap{} |
| 159 |
|
| 160 |
// summary(Cap): checks an array, slice or channel capacity |
| 161 |
// input(Cap): array,slice,chan |
| 162 |
|
| 163 |
// Cap is a smuggler operator. It takes data, applies cap() function |
| 164 |
// on it and compares its result to expectedCap. Of course, the |
| 165 |
// compared value must be an array, a channel or a slice. |
| 166 |
// |
| 167 |
// expectedCap can be an int value: |
| 168 |
// |
| 169 |
// td.Cmp(t, gotSlice, td.Cap(12)) |
| 170 |
// |
| 171 |
// as well as an other operator: |
| 172 |
// |
| 173 |
// td.Cmp(t, gotSlice, td.Cap(td.Between(3, 4))) |
| 174 |
// |
| 175 |
// See also [Len]. |
| 176 |
func Cap(expectedCap any) TestDeep { |
| 177 |
c := tdCap{} |
| 178 |
c.initLenCapBase(expectedCap) |
| 179 |
return &c |
| 180 |
} |
| 181 |
|
| 182 |
func (c *tdCap) String() string { |
| 183 |
if c.err != nil { |
| 184 |
return c.stringError() |
| 185 |
} |
| 186 |
if c.isTestDeeper { |
| 187 |
return "cap: " + c.expectedValue.Interface().(TestDeep).String() |
| 188 |
} |
| 189 |
return fmt.Sprintf("cap=%d", c.expectedValue.Int()) |
| 190 |
} |
| 191 |
|
| 192 |
func (c *tdCap) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 193 |
if c.err != nil { |
| 194 |
return ctx.CollectError(c.err) |
| 195 |
} |
| 196 |
|
| 197 |
switch got.Kind() { |
| 198 |
case reflect.Array, reflect.Chan, reflect.Slice: |
| 199 |
ret, err := c.isEqual(ctx.AddFunctionCall("cap"), got.Cap()) |
| 200 |
if ret { |
| 201 |
return err |
| 202 |
} |
| 203 |
if ctx.BooleanError { |
| 204 |
return ctxerr.BooleanError |
| 205 |
} |
| 206 |
return ctx.CollectError(&ctxerr.Error{ |
| 207 |
Message: "bad capacity", |
| 208 |
Got: types.RawInt(got.Cap()), |
| 209 |
Expected: types.RawInt(c.expectedValue.Int()), |
| 210 |
}) |
| 211 |
|
| 212 |
default: |
| 213 |
if ctx.BooleanError { |
| 214 |
return ctxerr.BooleanError |
| 215 |
} |
| 216 |
return ctx.CollectError(ctxerr.BadKind(got, "array OR chan OR slice")) |
| 217 |
} |
| 218 |
} |