| 1 | package checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/ |
| 2 | |
| 3 | import ( |
| 4 | "fmtscip-go gomod github.com/golang/go/src go1.13 fmt/" |
| 5 | "reflectscip-go gomod github.com/golang/go/src go1.13 reflect/" |
| 6 | "regexpscip-go gomod github.com/golang/go/src go1.13 regexp/" |
| 7 | ) |
| 8 | |
| 9 | // ----------------------------------------------------------------------- |
| 10 | // CommentInterface and Commentf helper, to attach extra information to checks. |
| 11 | |
| 12 | type commentscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/comment# struct { |
| 13 | formatscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/comment#format. string |
| 14 | argsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/comment#args. []interface{} |
| 15 | } |
| 16 | |
| 17 | // Commentf returns an infomational value to use with Assert or Check calls. |
| 18 | // If the checker test fails, the provided arguments will be passed to |
| 19 | // fmt.Sprintf, and will be presented next to the logged failure. |
| 20 | // |
| 21 | // For example: |
| 22 | // |
| 23 | // c.Assert(v, Equals, 42, Commentf("Iteration #%d failed.", i)) |
| 24 | // |
| 25 | // Note that if the comment is constant, a better option is to |
| 26 | // simply use a normal comment right above or next to the line, as |
| 27 | // it will also get printed with any errors: |
| 28 | // |
| 29 | // c.Assert(l, Equals, 8192) // Ensure buffer size is correct (bug #123) |
| 30 | // |
| 31 | func Commentfscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Commentf().(format string, args ...interface{}) CommentInterfacescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CommentInterface# { |
| 32 | return &commentscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/comment#{format, args} |
| 33 | } |
| 34 | |
| 35 | // CommentInterface must be implemented by types that attach extra |
| 36 | // information to failed checks. See the Commentf function for details. |
| 37 | type CommentInterfacescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CommentInterface# interface { |
| 38 | CheckCommentStringscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CommentInterface#CheckCommentString.() string |
| 39 | } |
| 40 | |
| 41 | func (c *commentscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/comment#) CheckCommentStringscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/comment#CheckCommentString().() string { |
| 42 | return fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.13 fmt/Sprintf().(c.formatscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/comment#format., c.argsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/comment#args....) |
| 43 | } |
| 44 | |
| 45 | // ----------------------------------------------------------------------- |
| 46 | // The Checker interface. |
| 47 | |
| 48 | // The Checker interface must be provided by checkers used with |
| 49 | // the Assert and Check verification methods. |
| 50 | type Checkerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker# interface { |
| 51 | Infoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker#Info.() *CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo# |
| 52 | Checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker#Check.(params []interface{}, names []string) (result bool, error string) |
| 53 | } |
| 54 | |
| 55 | // See the Checker interface. |
| 56 | type CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo# struct { |
| 57 | Namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Name. string |
| 58 | Paramsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Params. []string |
| 59 | } |
| 60 | |
| 61 | func (info *CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#) Infoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Info().() *CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo# { |
| 62 | return info |
| 63 | } |
| 64 | |
| 65 | // ----------------------------------------------------------------------- |
| 66 | // Not checker logic inverter. |
| 67 | |
| 68 | // The Not checker inverts the logic of the provided checker. The |
| 69 | // resulting checker will succeed where the original one failed, and |
| 70 | // vice-versa. |
| 71 | // |
| 72 | // For example: |
| 73 | // |
| 74 | // c.Assert(a, Not(Equals), b) |
| 75 | // |
| 76 | func Notscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Not().(checker Checkerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker#) Checkerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker# { |
| 77 | return ¬Checkerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/notChecker#{checker} |
| 78 | } |
| 79 | |
| 80 | type notCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/notChecker# struct { |
| 81 | subscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/notChecker#sub. Checkerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker# |
| 82 | } |
| 83 | |
| 84 | func (checker *notCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/notChecker#) Infoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/notChecker#Info().() *CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo# { |
| 85 | info := *checker.subscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/notChecker#sub..Infoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker#Info.() |
| 86 | info.Namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Name. = "Not(" + info.Namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Name. + ")" |
| 87 | return &info |
| 88 | } |
| 89 | |
| 90 | func (checker *notCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/notChecker#) Checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/notChecker#Check().(params []interface{}, names []string) (result bool, error string) { |
| 91 | result, error = checker.subscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/notChecker#sub..Checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker#Check.(params, names) |
| 92 | result = !result |
| 93 | return |
| 94 | } |
| 95 | |
| 96 | // ----------------------------------------------------------------------- |
| 97 | // IsNil checker. |
| 98 | |
| 99 | type isNilCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/isNilChecker# struct { |
| 100 | *CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/isNilChecker#CheckerInfo. |
| 101 | } |
| 102 | |
| 103 | // The IsNil checker tests whether the obtained value is nil. |
| 104 | // |
| 105 | // For example: |
| 106 | // |
| 107 | // c.Assert(err, IsNil) |
| 108 | // |
| 109 | var IsNilscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/IsNil. Checkerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker# = &isNilCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/isNilChecker#{ |
| 110 | &CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#{Namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Name.: "IsNil", Paramsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Params.: []string{"value"}}, |
| 111 | } |
| 112 | |
| 113 | func (checker *isNilCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/isNilChecker#) Checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/isNilChecker#Check().(params []interface{}, names []string) (result bool, error string) { |
| 114 | return isNilscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/isNil().(params[0]), "" |
| 115 | } |
| 116 | |
| 117 | func isNilscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/isNil().(obtained interface{}) (result bool) { |
| 118 | if obtained == nil { |
| 119 | result = true |
| 120 | } else { |
| 121 | switch v := reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.13 reflect/ValueOf().(obtained); v.Kindscip-go gomod github.com/golang/go/src go1.13 reflect/Value#Kind().() { |
| 122 | case reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.Chanscip-go gomod github.com/golang/go/src go1.13 reflect/Chan., reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.Funcscip-go gomod github.com/golang/go/src go1.13 reflect/Func., reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.Interfacescip-go gomod github.com/golang/go/src go1.13 reflect/Interface., reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.Mapscip-go gomod github.com/golang/go/src go1.13 reflect/Map., reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.Ptrscip-go gomod github.com/golang/go/src go1.13 reflect/Ptr., reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.Slicescip-go gomod github.com/golang/go/src go1.13 reflect/Slice.: |
| 123 | return v.IsNilscip-go gomod github.com/golang/go/src go1.13 reflect/Value#IsNil().() |
| 124 | } |
| 125 | } |
| 126 | return |
| 127 | } |
| 128 | |
| 129 | // ----------------------------------------------------------------------- |
| 130 | // NotNil checker. Alias for Not(IsNil), since it's so common. |
| 131 | |
| 132 | type notNilCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/notNilChecker# struct { |
| 133 | *CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/notNilChecker#CheckerInfo. |
| 134 | } |
| 135 | |
| 136 | // The NotNil checker verifies that the obtained value is not nil. |
| 137 | // |
| 138 | // For example: |
| 139 | // |
| 140 | // c.Assert(iface, NotNil) |
| 141 | // |
| 142 | // This is an alias for Not(IsNil), made available since it's a |
| 143 | // fairly common check. |
| 144 | // |
| 145 | var NotNilscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/NotNil. Checkerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker# = ¬NilCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/notNilChecker#{ |
| 146 | &CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#{Namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Name.: "NotNil", Paramsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Params.: []string{"value"}}, |
| 147 | } |
| 148 | |
| 149 | func (checker *notNilCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/notNilChecker#) Checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/notNilChecker#Check().(params []interface{}, names []string) (result bool, error string) { |
| 150 | return !isNilscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/isNil().(params[0]), "" |
| 151 | } |
| 152 | |
| 153 | // ----------------------------------------------------------------------- |
| 154 | // Equals checker. |
| 155 | |
| 156 | type equalsCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/equalsChecker# struct { |
| 157 | *CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/equalsChecker#CheckerInfo. |
| 158 | } |
| 159 | |
| 160 | // The Equals checker verifies that the obtained value is equal to |
| 161 | // the expected value, according to usual Go semantics for ==. |
| 162 | // |
| 163 | // For example: |
| 164 | // |
| 165 | // c.Assert(value, Equals, 42) |
| 166 | // |
| 167 | var Equalsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Equals. Checkerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker# = &equalsCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/equalsChecker#{ |
| 168 | &CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#{Namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Name.: "Equals", Paramsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Params.: []string{"obtained", "expected"}}, |
| 169 | } |
| 170 | |
| 171 | func (checker *equalsCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/equalsChecker#) Checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/equalsChecker#Check().(params []interface{}, names []string) (result bool, error string) { |
| 172 | defer func() { |
| 173 | if v := recover(); v != nil { |
| 174 | result = false |
| 175 | error = fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Sprintscip-go gomod github.com/golang/go/src go1.13 fmt/Sprint().(v) |
| 176 | } |
| 177 | }() |
| 178 | return params[0] == params[1], "" |
| 179 | } |
| 180 | |
| 181 | // ----------------------------------------------------------------------- |
| 182 | // DeepEquals checker. |
| 183 | |
| 184 | type deepEqualsCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/deepEqualsChecker# struct { |
| 185 | *CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/deepEqualsChecker#CheckerInfo. |
| 186 | } |
| 187 | |
| 188 | // The DeepEquals checker verifies that the obtained value is deep-equal to |
| 189 | // the expected value. The check will work correctly even when facing |
| 190 | // slices, interfaces, and values of different types (which always fail |
| 191 | // the test). |
| 192 | // |
| 193 | // For example: |
| 194 | // |
| 195 | // c.Assert(value, DeepEquals, 42) |
| 196 | // c.Assert(array, DeepEquals, []string{"hi", "there"}) |
| 197 | // |
| 198 | var DeepEqualsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/DeepEquals. Checkerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker# = &deepEqualsCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/deepEqualsChecker#{ |
| 199 | &CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#{Namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Name.: "DeepEquals", Paramsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Params.: []string{"obtained", "expected"}}, |
| 200 | } |
| 201 | |
| 202 | func (checker *deepEqualsCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/deepEqualsChecker#) Checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/deepEqualsChecker#Check().(params []interface{}, names []string) (result bool, error string) { |
| 203 | return reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.13 reflect/DeepEqual().(params[0], params[1]), "" |
| 204 | } |
| 205 | |
| 206 | // ----------------------------------------------------------------------- |
| 207 | // EqualsIn checker. |
| 208 | |
| 209 | type equalsInCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/equalsInChecker# struct { |
| 210 | *CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/equalsInChecker#CheckerInfo. |
| 211 | } |
| 212 | |
| 213 | // The EqualsIn checker verifies that the obtained value is equal to |
| 214 | // one of the expected values, according to usual Go semantics for ==. |
| 215 | // |
| 216 | // For example: |
| 217 | // |
| 218 | // c.Assert(value, EqualsIn, []interface{}{42, "42"} ) |
| 219 | // |
| 220 | var EqualsInscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/EqualsIn. Checkerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker# = &equalsInCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/equalsInChecker#{ |
| 221 | &CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#{Namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Name.: "EqualsIn", Paramsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Params.: []string{"obtained", "expected"}}, |
| 222 | } |
| 223 | |
| 224 | func (checker *equalsInCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/equalsInChecker#) Checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/equalsInChecker#Check().(params []interface{}, names []string) (result bool, error string) { |
| 225 | defer func() { |
| 226 | if v := recover(); v != nil { |
| 227 | result = false |
| 228 | error = fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Sprintscip-go gomod github.com/golang/go/src go1.13 fmt/Sprint().(v) |
| 229 | } |
| 230 | }() |
| 231 | for _, v := range params[1].([]interface{}) { |
| 232 | if params[0] == v { |
| 233 | return true, "" |
| 234 | } |
| 235 | } |
| 236 | return false, "" |
| 237 | } |
| 238 | |
| 239 | // ----------------------------------------------------------------------- |
| 240 | // EqualsIn checker. |
| 241 | |
| 242 | type deepEqualsInCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/deepEqualsInChecker# struct { |
| 243 | *CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/deepEqualsInChecker#CheckerInfo. |
| 244 | } |
| 245 | |
| 246 | // The EqualsIn checker verifies that the obtained value is equal to |
| 247 | // one of the expected values, according to usual Go semantics for ==. |
| 248 | // |
| 249 | // For example: |
| 250 | // |
| 251 | // c.Assert(value, EqualsIn, []interface{}{42, "42"} ) |
| 252 | // |
| 253 | var DeepEqualsInscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/DeepEqualsIn. Checkerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker# = &deepEqualsInCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/deepEqualsInChecker#{ |
| 254 | &CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#{Namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Name.: "DeepEqualsIn", Paramsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Params.: []string{"obtained", "expected"}}, |
| 255 | } |
| 256 | |
| 257 | func (checker *deepEqualsInCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/deepEqualsInChecker#) Checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/deepEqualsInChecker#Check().(params []interface{}, names []string) (result bool, error string) { |
| 258 | defer func() { |
| 259 | if v := recover(); v != nil { |
| 260 | result = false |
| 261 | error = fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Sprintscip-go gomod github.com/golang/go/src go1.13 fmt/Sprint().(v) |
| 262 | } |
| 263 | }() |
| 264 | for _, v := range params[1].([]interface{}) { |
| 265 | if reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.13 reflect/DeepEqual().(params[0], v) { |
| 266 | return true, "" |
| 267 | } |
| 268 | } |
| 269 | return false, "" |
| 270 | } |
| 271 | |
| 272 | // ----------------------------------------------------------------------- |
| 273 | // HasLen checker. |
| 274 | |
| 275 | type hasLenCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/hasLenChecker# struct { |
| 276 | *CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/hasLenChecker#CheckerInfo. |
| 277 | } |
| 278 | |
| 279 | // The HasLen checker verifies that the obtained value has the |
| 280 | // provided length. In many cases this is superior to using Equals |
| 281 | // in conjunction with the len function because in case the check |
| 282 | // fails the value itself will be printed, instead of its length, |
| 283 | // providing more details for figuring the problem. |
| 284 | // |
| 285 | // For example: |
| 286 | // |
| 287 | // c.Assert(list, HasLen, 5) |
| 288 | // |
| 289 | var HasLenscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/HasLen. Checkerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker# = &hasLenCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/hasLenChecker#{ |
| 290 | &CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#{Namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Name.: "HasLen", Paramsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Params.: []string{"obtained", "n"}}, |
| 291 | } |
| 292 | |
| 293 | func (checker *hasLenCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/hasLenChecker#) Checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/hasLenChecker#Check().(params []interface{}, names []string) (result bool, error string) { |
| 294 | n, ok := params[1].(int) |
| 295 | if !ok { |
| 296 | return false, "n must be an int" |
| 297 | } |
| 298 | value := reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.13 reflect/ValueOf().(params[0]) |
| 299 | switch value.Kindscip-go gomod github.com/golang/go/src go1.13 reflect/Value#Kind().() { |
| 300 | case reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.Mapscip-go gomod github.com/golang/go/src go1.13 reflect/Map., reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.Arrayscip-go gomod github.com/golang/go/src go1.13 reflect/Array., reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.Slicescip-go gomod github.com/golang/go/src go1.13 reflect/Slice., reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.Chanscip-go gomod github.com/golang/go/src go1.13 reflect/Chan., reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.Stringscip-go gomod github.com/golang/go/src go1.13 reflect/String.: |
| 301 | default: |
| 302 | return false, "obtained value type has no length" |
| 303 | } |
| 304 | return value.Lenscip-go gomod github.com/golang/go/src go1.13 reflect/Value#Len().() == n, "" |
| 305 | } |
| 306 | |
| 307 | // ----------------------------------------------------------------------- |
| 308 | // ErrorMatches checker. |
| 309 | |
| 310 | type errorMatchesCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/errorMatchesChecker# struct { |
| 311 | *CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/errorMatchesChecker#CheckerInfo. |
| 312 | } |
| 313 | |
| 314 | // The ErrorMatches checker verifies that the error value |
| 315 | // is non nil and matches the regular expression provided. |
| 316 | // |
| 317 | // For example: |
| 318 | // |
| 319 | // c.Assert(err, ErrorMatches, "perm.*denied") |
| 320 | // |
| 321 | var ErrorMatchesscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/ErrorMatches. Checkerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker# = errorMatchesCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/errorMatchesChecker#{ |
| 322 | &CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#{Namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Name.: "ErrorMatches", Paramsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Params.: []string{"value", "regex"}}, |
| 323 | } |
| 324 | |
| 325 | func (checker errorMatchesCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/errorMatchesChecker#) Checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/errorMatchesChecker#Check().(params []interface{}, names []string) (result bool, errStr string) { |
| 326 | if params[0] == nil { |
| 327 | return false, "Error value is nil" |
| 328 | } |
| 329 | err, ok := params[0].(error) |
| 330 | if !ok { |
| 331 | return false, "Value is not an error" |
| 332 | } |
| 333 | params[0] = err.Error() |
| 334 | names[0] = "error" |
| 335 | return matchesscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/matches().(params[0], params[1]) |
| 336 | } |
| 337 | |
| 338 | // ----------------------------------------------------------------------- |
| 339 | // Matches checker. |
| 340 | |
| 341 | type matchesCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/matchesChecker# struct { |
| 342 | *CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/matchesChecker#CheckerInfo. |
| 343 | } |
| 344 | |
| 345 | // The Matches checker verifies that the string provided as the obtained |
| 346 | // value (or the string resulting from obtained.String()) matches the |
| 347 | // regular expression provided. |
| 348 | // |
| 349 | // For example: |
| 350 | // |
| 351 | // c.Assert(err, Matches, "perm.*denied") |
| 352 | // |
| 353 | var Matchesscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Matches. Checkerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker# = &matchesCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/matchesChecker#{ |
| 354 | &CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#{Namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Name.: "Matches", Paramsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Params.: []string{"value", "regex"}}, |
| 355 | } |
| 356 | |
| 357 | func (checker *matchesCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/matchesChecker#) Checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/matchesChecker#Check().(params []interface{}, names []string) (result bool, error string) { |
| 358 | return matchesscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/matches().(params[0], params[1]) |
| 359 | } |
| 360 | |
| 361 | func matchesscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/matches().(value, regex interface{}) (result bool, error string) { |
| 362 | reStr, ok := regex.(string) |
| 363 | if !ok { |
| 364 | return false, "Regex must be a string" |
| 365 | } |
| 366 | valueStr, valueIsStr := value.(string) |
| 367 | if !valueIsStr { |
| 368 | if valueWithStr, valueHasStr := value.(fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Stringerscip-go gomod github.com/golang/go/src go1.13 fmt/Stringer#); valueHasStr { |
| 369 | valueStr, valueIsStr = valueWithStr.Stringscip-go gomod github.com/golang/go/src go1.13 fmt/Stringer#String().(), true |
| 370 | } |
| 371 | } |
| 372 | if valueIsStr { |
| 373 | matches, err := regexpscip-go gomod github.com/golang/go/src go1.13 regexp/.MatchStringscip-go gomod github.com/golang/go/src go1.13 regexp/MatchString().("^"+reStr+"$", valueStr) |
| 374 | if err != nil { |
| 375 | return false, "Can't compile regex: " + err.Error() |
| 376 | } |
| 377 | return matches, "" |
| 378 | } |
| 379 | return false, "Obtained value is not a string and has no .String()" |
| 380 | } |
| 381 | |
| 382 | // ----------------------------------------------------------------------- |
| 383 | // Panics checker. |
| 384 | |
| 385 | type panicsCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/panicsChecker# struct { |
| 386 | *CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/panicsChecker#CheckerInfo. |
| 387 | } |
| 388 | |
| 389 | // The Panics checker verifies that calling the provided zero-argument |
| 390 | // function will cause a panic which is deep-equal to the provided value. |
| 391 | // |
| 392 | // For example: |
| 393 | // |
| 394 | // c.Assert(func() { f(1, 2) }, Panics, &SomeErrorType{"BOOM"}). |
| 395 | // |
| 396 | // |
| 397 | var Panicsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Panics. Checkerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker# = &panicsCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/panicsChecker#{ |
| 398 | &CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#{Namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Name.: "Panics", Paramsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Params.: []string{"function", "expected"}}, |
| 399 | } |
| 400 | |
| 401 | func (checker *panicsCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/panicsChecker#) Checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/panicsChecker#Check().(params []interface{}, names []string) (result bool, error string) { |
| 402 | f := reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.13 reflect/ValueOf().(params[0]) |
| 403 | if f.Kindscip-go gomod github.com/golang/go/src go1.13 reflect/Value#Kind().() != reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.Funcscip-go gomod github.com/golang/go/src go1.13 reflect/Func. || f.Typescip-go gomod github.com/golang/go/src go1.13 reflect/Value#Type().().NumInscip-go gomod github.com/golang/go/src go1.13 reflect/Type#NumIn().() != 0 { |
| 404 | return false, "Function must take zero arguments" |
| 405 | } |
| 406 | defer func() { |
| 407 | // If the function has not panicked, then don't do the check. |
| 408 | if error != "" { |
| 409 | return |
| 410 | } |
| 411 | params[0] = recover() |
| 412 | names[0] = "panic" |
| 413 | result = reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.13 reflect/DeepEqual().(params[0], params[1]) |
| 414 | }() |
| 415 | f.Callscip-go gomod github.com/golang/go/src go1.13 reflect/Value#Call().(nil) |
| 416 | return false, "Function has not panicked" |
| 417 | } |
| 418 | |
| 419 | type panicMatchesCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/panicMatchesChecker# struct { |
| 420 | *CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/panicMatchesChecker#CheckerInfo. |
| 421 | } |
| 422 | |
| 423 | // The PanicMatches checker verifies that calling the provided zero-argument |
| 424 | // function will cause a panic with an error value matching |
| 425 | // the regular expression provided. |
| 426 | // |
| 427 | // For example: |
| 428 | // |
| 429 | // c.Assert(func() { f(1, 2) }, PanicMatches, `open.*: no such file or directory`). |
| 430 | // |
| 431 | // |
| 432 | var PanicMatchesscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/PanicMatches. Checkerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker# = &panicMatchesCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/panicMatchesChecker#{ |
| 433 | &CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#{Namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Name.: "PanicMatches", Paramsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Params.: []string{"function", "expected"}}, |
| 434 | } |
| 435 | |
| 436 | func (checker *panicMatchesCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/panicMatchesChecker#) Checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/panicMatchesChecker#Check().(params []interface{}, names []string) (result bool, errmsg string) { |
| 437 | f := reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.13 reflect/ValueOf().(params[0]) |
| 438 | if f.Kindscip-go gomod github.com/golang/go/src go1.13 reflect/Value#Kind().() != reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.Funcscip-go gomod github.com/golang/go/src go1.13 reflect/Func. || f.Typescip-go gomod github.com/golang/go/src go1.13 reflect/Value#Type().().NumInscip-go gomod github.com/golang/go/src go1.13 reflect/Type#NumIn().() != 0 { |
| 439 | return false, "Function must take zero arguments" |
| 440 | } |
| 441 | defer func() { |
| 442 | // If the function has not panicked, then don't do the check. |
| 443 | if errmsg != "" { |
| 444 | return |
| 445 | } |
| 446 | obtained := recover() |
| 447 | names[0] = "panic" |
| 448 | if e, ok := obtained.(error); ok { |
| 449 | params[0] = e.Error() |
| 450 | } else if _, ok := obtained.(string); ok { |
| 451 | params[0] = obtained |
| 452 | } else { |
| 453 | errmsg = "Panic value is not a string or an error" |
| 454 | return |
| 455 | } |
| 456 | result, errmsg = matchesscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/matches().(params[0], params[1]) |
| 457 | }() |
| 458 | f.Callscip-go gomod github.com/golang/go/src go1.13 reflect/Value#Call().(nil) |
| 459 | return false, "Function has not panicked" |
| 460 | } |
| 461 | |
| 462 | // ----------------------------------------------------------------------- |
| 463 | // FitsTypeOf checker. |
| 464 | |
| 465 | type fitsTypeCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/fitsTypeChecker# struct { |
| 466 | *CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/fitsTypeChecker#CheckerInfo. |
| 467 | } |
| 468 | |
| 469 | // The FitsTypeOf checker verifies that the obtained value is |
| 470 | // assignable to a variable with the same type as the provided |
| 471 | // sample value. |
| 472 | // |
| 473 | // For example: |
| 474 | // |
| 475 | // c.Assert(value, FitsTypeOf, int64(0)) |
| 476 | // c.Assert(value, FitsTypeOf, os.Error(nil)) |
| 477 | // |
| 478 | var FitsTypeOfscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/FitsTypeOf. Checkerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker# = &fitsTypeCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/fitsTypeChecker#{ |
| 479 | &CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#{Namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Name.: "FitsTypeOf", Paramsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Params.: []string{"obtained", "sample"}}, |
| 480 | } |
| 481 | |
| 482 | func (checker *fitsTypeCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/fitsTypeChecker#) Checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/fitsTypeChecker#Check().(params []interface{}, names []string) (result bool, error string) { |
| 483 | obtained := reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.13 reflect/ValueOf().(params[0]) |
| 484 | sample := reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.13 reflect/ValueOf().(params[1]) |
| 485 | if !obtained.IsValidscip-go gomod github.com/golang/go/src go1.13 reflect/Value#IsValid().() { |
| 486 | return false, "" |
| 487 | } |
| 488 | if !sample.IsValidscip-go gomod github.com/golang/go/src go1.13 reflect/Value#IsValid().() { |
| 489 | return false, "Invalid sample value" |
| 490 | } |
| 491 | return obtained.Typescip-go gomod github.com/golang/go/src go1.13 reflect/Value#Type().().AssignableToscip-go gomod github.com/golang/go/src go1.13 reflect/Type#AssignableTo().(sample.Typescip-go gomod github.com/golang/go/src go1.13 reflect/Value#Type().()), "" |
| 492 | } |
| 493 | |
| 494 | // ----------------------------------------------------------------------- |
| 495 | // Implements checker. |
| 496 | |
| 497 | type implementsCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/implementsChecker# struct { |
| 498 | *CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/implementsChecker#CheckerInfo. |
| 499 | } |
| 500 | |
| 501 | // The Implements checker verifies that the obtained value |
| 502 | // implements the interface specified via a pointer to an interface |
| 503 | // variable. |
| 504 | // |
| 505 | // For example: |
| 506 | // |
| 507 | // var e os.Error |
| 508 | // c.Assert(err, Implements, &e) |
| 509 | // |
| 510 | var Implementsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Implements. Checkerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker# = &implementsCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/implementsChecker#{ |
| 511 | &CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#{Namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Name.: "Implements", Paramsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Params.: []string{"obtained", "ifaceptr"}}, |
| 512 | } |
| 513 | |
| 514 | func (checker *implementsCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/implementsChecker#) Checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/implementsChecker#Check().(params []interface{}, names []string) (result bool, error string) { |
| 515 | obtained := reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.13 reflect/ValueOf().(params[0]) |
| 516 | ifaceptr := reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.13 reflect/ValueOf().(params[1]) |
| 517 | if !obtained.IsValidscip-go gomod github.com/golang/go/src go1.13 reflect/Value#IsValid().() { |
| 518 | return false, "" |
| 519 | } |
| 520 | if !ifaceptr.IsValidscip-go gomod github.com/golang/go/src go1.13 reflect/Value#IsValid().() || ifaceptr.Kindscip-go gomod github.com/golang/go/src go1.13 reflect/Value#Kind().() != reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.Ptrscip-go gomod github.com/golang/go/src go1.13 reflect/Ptr. || ifaceptr.Elemscip-go gomod github.com/golang/go/src go1.13 reflect/Value#Elem().().Kindscip-go gomod github.com/golang/go/src go1.13 reflect/Value#Kind().() != reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.Interfacescip-go gomod github.com/golang/go/src go1.13 reflect/Interface. { |
| 521 | return false, "ifaceptr should be a pointer to an interface variable" |
| 522 | } |
| 523 | return obtained.Typescip-go gomod github.com/golang/go/src go1.13 reflect/Value#Type().().Implementsscip-go gomod github.com/golang/go/src go1.13 reflect/Type#Implements().(ifaceptr.Elemscip-go gomod github.com/golang/go/src go1.13 reflect/Value#Elem().().Typescip-go gomod github.com/golang/go/src go1.13 reflect/Value#Type().()), "" |
| 524 | } |