| 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 |
"strings" |
| 13 |
|
| 14 |
"github.com/maxatome/go-testdeep/internal/ctxerr" |
| 15 |
"github.com/maxatome/go-testdeep/internal/types" |
| 16 |
"github.com/maxatome/go-testdeep/internal/util" |
| 17 |
) |
| 18 |
|
| 19 |
type tdStringBase struct { |
| 20 |
base |
| 21 |
expected string |
| 22 |
} |
| 23 |
|
| 24 |
func newStringBase(expected string) tdStringBase { |
| 25 |
return tdStringBase{ |
| 26 |
base: newBase(4), |
| 27 |
expected: expected, |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func getString(ctx ctxerr.Context, got reflect.Value) (string, *ctxerr.Error) { |
| 32 |
switch got.Kind() { |
| 33 |
case reflect.String: |
| 34 |
return got.String(), nil |
| 35 |
|
| 36 |
case reflect.Slice: |
| 37 |
if got.Type().Elem() == types.Uint8 { |
| 38 |
return string(got.Bytes()), nil |
| 39 |
} |
| 40 |
fallthrough |
| 41 |
|
| 42 |
default: |
| 43 |
if got.CanInterface() { |
| 44 |
switch iface := got.Interface().(type) { |
| 45 |
case error: |
| 46 |
return iface.Error(), nil |
| 47 |
case fmt.Stringer: |
| 48 |
return iface.String(), nil |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
if ctx.BooleanError { |
| 54 |
return "", ctxerr.BooleanError |
| 55 |
} |
| 56 |
return "", &ctxerr.Error{ |
| 57 |
Message: "bad type", |
| 58 |
Got: types.RawString(got.Type().String()), |
| 59 |
Expected: types.RawString( |
| 60 |
"string (convertible) OR []byte (convertible) OR fmt.Stringer OR error"), |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
type tdString struct { |
| 65 |
tdStringBase |
| 66 |
} |
| 67 |
|
| 68 |
var _ TestDeep = &tdString{} |
| 69 |
|
| 70 |
// summary(String): checks a string, []byte, error or fmt.Stringer |
| 71 |
// interfaces string contents |
| 72 |
// input(String): str,slice([]byte),if(✓ + fmt.Stringer/error) |
| 73 |
|
| 74 |
// String operator allows to compare a string (or convertible), []byte |
| 75 |
// (or convertible), error or [fmt.Stringer] interface (error interface |
| 76 |
// is tested before [fmt.Stringer]). |
| 77 |
// |
| 78 |
// err := errors.New("error!") |
| 79 |
// td.Cmp(t, err, td.String("error!")) // succeeds |
| 80 |
// |
| 81 |
// bstr := bytes.NewBufferString("fmt.Stringer!") |
| 82 |
// td.Cmp(t, bstr, td.String("fmt.Stringer!")) // succeeds |
| 83 |
// |
| 84 |
// See also [Contains], [HasPrefix], [HasSuffix], [Re] and [ReAll]. |
| 85 |
func String(expected string) TestDeep { |
| 86 |
return &tdString{ |
| 87 |
tdStringBase: newStringBase(expected), |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
func (s *tdString) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 92 |
str, err := getString(ctx, got) |
| 93 |
if err != nil { |
| 94 |
return ctx.CollectError(err) |
| 95 |
} |
| 96 |
|
| 97 |
if str == s.expected { |
| 98 |
return nil |
| 99 |
} |
| 100 |
if ctx.BooleanError { |
| 101 |
return ctxerr.BooleanError |
| 102 |
} |
| 103 |
return ctx.CollectError(&ctxerr.Error{ |
| 104 |
Message: "does not match", |
| 105 |
Got: str, |
| 106 |
Expected: s, |
| 107 |
}) |
| 108 |
} |
| 109 |
|
| 110 |
func (s *tdString) String() string { |
| 111 |
return util.ToString(s.expected) |
| 112 |
} |
| 113 |
|
| 114 |
type tdHasPrefix struct { |
| 115 |
tdStringBase |
| 116 |
} |
| 117 |
|
| 118 |
var _ TestDeep = &tdHasPrefix{} |
| 119 |
|
| 120 |
// summary(HasPrefix): checks the prefix of a string, []byte, error or |
| 121 |
// fmt.Stringer interfaces |
| 122 |
// input(HasPrefix): str,slice([]byte),if(✓ + fmt.Stringer/error) |
| 123 |
|
| 124 |
// HasPrefix operator allows to compare the prefix of a string (or |
| 125 |
// convertible), []byte (or convertible), error or [fmt.Stringer] |
| 126 |
// interface (error interface is tested before [fmt.Stringer]). |
| 127 |
// |
| 128 |
// td.Cmp(t, []byte("foobar"), td.HasPrefix("foo")) // succeeds |
| 129 |
// |
| 130 |
// type Foobar string |
| 131 |
// td.Cmp(t, Foobar("foobar"), td.HasPrefix("foo")) // succeeds |
| 132 |
// |
| 133 |
// err := errors.New("error!") |
| 134 |
// td.Cmp(t, err, td.HasPrefix("err")) // succeeds |
| 135 |
// |
| 136 |
// bstr := bytes.NewBufferString("fmt.Stringer!") |
| 137 |
// td.Cmp(t, bstr, td.HasPrefix("fmt")) // succeeds |
| 138 |
// |
| 139 |
// See also [Contains], [HasSuffix], [Re], [ReAll] and [String]. |
| 140 |
func HasPrefix(expected string) TestDeep { |
| 141 |
return &tdHasPrefix{ |
| 142 |
tdStringBase: newStringBase(expected), |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func (s *tdHasPrefix) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 147 |
str, err := getString(ctx, got) |
| 148 |
if err != nil { |
| 149 |
return ctx.CollectError(err) |
| 150 |
} |
| 151 |
|
| 152 |
if strings.HasPrefix(str, s.expected) { |
| 153 |
return nil |
| 154 |
} |
| 155 |
if ctx.BooleanError { |
| 156 |
return ctxerr.BooleanError |
| 157 |
} |
| 158 |
return ctx.CollectError(&ctxerr.Error{ |
| 159 |
Message: "has not prefix", |
| 160 |
Got: str, |
| 161 |
Expected: s, |
| 162 |
}) |
| 163 |
} |
| 164 |
|
| 165 |
func (s *tdHasPrefix) String() string { |
| 166 |
return "HasPrefix(" + util.ToString(s.expected) + ")" |
| 167 |
} |
| 168 |
|
| 169 |
type tdHasSuffix struct { |
| 170 |
tdStringBase |
| 171 |
} |
| 172 |
|
| 173 |
var _ TestDeep = &tdHasSuffix{} |
| 174 |
|
| 175 |
// summary(HasSuffix): checks the suffix of a string, []byte, error or |
| 176 |
// fmt.Stringer interfaces |
| 177 |
// input(HasSuffix): str,slice([]byte),if(✓ + fmt.Stringer/error) |
| 178 |
|
| 179 |
// HasSuffix operator allows to compare the suffix of a string (or |
| 180 |
// convertible), []byte (or convertible), error or [fmt.Stringer] |
| 181 |
// interface (error interface is tested before [fmt.Stringer]). |
| 182 |
// |
| 183 |
// td.Cmp(t, []byte("foobar"), td.HasSuffix("bar")) // succeeds |
| 184 |
// |
| 185 |
// type Foobar string |
| 186 |
// td.Cmp(t, Foobar("foobar"), td.HasSuffix("bar")) // succeeds |
| 187 |
// |
| 188 |
// err := errors.New("error!") |
| 189 |
// td.Cmp(t, err, td.HasSuffix("!")) // succeeds |
| 190 |
// |
| 191 |
// bstr := bytes.NewBufferString("fmt.Stringer!") |
| 192 |
// td.Cmp(t, bstr, td.HasSuffix("!")) // succeeds |
| 193 |
// |
| 194 |
// See also [Contains], [HasPrefix], [Re], [ReAll] and [String]. |
| 195 |
func HasSuffix(expected string) TestDeep { |
| 196 |
return &tdHasSuffix{ |
| 197 |
tdStringBase: newStringBase(expected), |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
func (s *tdHasSuffix) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 202 |
str, err := getString(ctx, got) |
| 203 |
if err != nil { |
| 204 |
return ctx.CollectError(err) |
| 205 |
} |
| 206 |
|
| 207 |
if strings.HasSuffix(str, s.expected) { |
| 208 |
return nil |
| 209 |
} |
| 210 |
if ctx.BooleanError { |
| 211 |
return ctxerr.BooleanError |
| 212 |
} |
| 213 |
return ctx.CollectError(&ctxerr.Error{ |
| 214 |
Message: "has not suffix", |
| 215 |
Got: str, |
| 216 |
Expected: s, |
| 217 |
}) |
| 218 |
} |
| 219 |
|
| 220 |
func (s *tdHasSuffix) String() string { |
| 221 |
return "HasSuffix(" + util.ToString(s.expected) + ")" |
| 222 |
} |