| 1 | // Extensions to the go-check unittest framework. |
| 2 | // |
| 3 | // NOTE: see https://github.com/go-check/check/pull/6 for reasons why these |
| 4 | // checkers live here. |
| 5 | package checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/ |
| 6 | |
| 7 | import ( |
| 8 | "bytesscip-go gomod github.com/golang/go/src go1.13 bytes/" |
| 9 | "reflectscip-go gomod github.com/golang/go/src go1.13 reflect/" |
| 10 | ) |
| 11 | |
| 12 | // ----------------------------------------------------------------------- |
| 13 | // IsTrue / IsFalse checker. |
| 14 | |
| 15 | type isBoolValueCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/isBoolValueChecker# struct { |
| 16 | *CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/isBoolValueChecker#CheckerInfo. |
| 17 | expectedscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/isBoolValueChecker#expected. bool |
| 18 | } |
| 19 | |
| 20 | func (checker *isBoolValueCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/isBoolValueChecker#) Checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/isBoolValueChecker#Check().( |
| 21 | params []interface{}, |
| 22 | names []string) ( |
| 23 | result bool, |
| 24 | error string) { |
| 25 | |
| 26 | obtained, ok := params[0].(bool) |
| 27 | if !ok { |
| 28 | return false, "Argument to " + checker.Namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Name. + " must be bool" |
| 29 | } |
| 30 | |
| 31 | return obtained == checker.expectedscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/isBoolValueChecker#expected., "" |
| 32 | } |
| 33 | |
| 34 | // The IsTrue checker verifies that the obtained value is true. |
| 35 | // |
| 36 | // For example: |
| 37 | // |
| 38 | // c.Assert(value, IsTrue) |
| 39 | // |
| 40 | var IsTruescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/IsTrue. Checkerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker# = &isBoolValueCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/isBoolValueChecker#{ |
| 41 | &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.: "IsTrue", Paramsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Params.: []string{"obtained"}}, |
| 42 | true, |
| 43 | } |
| 44 | |
| 45 | // The IsFalse checker verifies that the obtained value is false. |
| 46 | // |
| 47 | // For example: |
| 48 | // |
| 49 | // c.Assert(value, IsFalse) |
| 50 | // |
| 51 | var IsFalsescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/IsFalse. Checkerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/Checker# = &isBoolValueCheckerscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/isBoolValueChecker#{ |
| 52 | &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.: "IsFalse", Paramsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Params.: []string{"obtained"}}, |
| 53 | false, |
| 54 | } |
| 55 | |
| 56 | // ----------------------------------------------------------------------- |
| 57 | // BytesEquals checker. |
| 58 | |
| 59 | type bytesEqualsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/bytesEquals# struct{} |
| 60 | |
| 61 | func (b *bytesEqualsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/bytesEquals#) Checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/bytesEquals#Check().(params []interface{}, names []string) (bool, string) { |
| 62 | if len(params) != 2 { |
| 63 | return false, "BytesEqual takes 2 bytestring arguments" |
| 64 | } |
| 65 | b1, ok1 := params[0].([]byte) |
| 66 | b2, ok2 := params[1].([]byte) |
| 67 | |
| 68 | if !(ok1 && ok2) { |
| 69 | return false, "Arguments to BytesEqual must both be bytestrings" |
| 70 | } |
| 71 | |
| 72 | return bytesscip-go gomod github.com/golang/go/src go1.13 bytes/.Equalscip-go gomod github.com/golang/go/src go1.13 bytes/Equal().(b1, b2), "" |
| 73 | } |
| 74 | |
| 75 | func (b *bytesEqualsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/bytesEquals#) Infoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/bytesEquals#Info().() *CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo# { |
| 76 | return &CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#{ |
| 77 | Namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Name.: "BytesEquals", |
| 78 | Paramsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Params.: []string{"bytes_one", "bytes_two"}, |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // BytesEquals checker compares two bytes sequence using bytes.Equal. |
| 83 | // |
| 84 | // For example: |
| 85 | // |
| 86 | // c.Assert(b, BytesEquals, []byte("bar")) |
| 87 | // |
| 88 | // Main difference between DeepEquals and BytesEquals is that BytesEquals treats |
| 89 | // `nil` as empty byte sequence while DeepEquals doesn't. |
| 90 | // |
| 91 | // c.Assert(nil, BytesEquals, []byte("")) // succeeds |
| 92 | // c.Assert(nil, DeepEquals, []byte("")) // fails |
| 93 | var BytesEqualsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/BytesEquals. = &bytesEqualsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/bytesEquals#{} |
| 94 | |
| 95 | // ----------------------------------------------------------------------- |
| 96 | // HasKey checker. |
| 97 | |
| 98 | type hasKeyscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/hasKey# struct{} |
| 99 | |
| 100 | func (h *hasKeyscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/hasKey#) Checkscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/hasKey#Check().(params []interface{}, names []string) (bool, string) { |
| 101 | if len(params) != 2 { |
| 102 | return false, "HasKey takes 2 arguments: a map and a key" |
| 103 | } |
| 104 | |
| 105 | mapValue := 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]) |
| 106 | if mapValue.Kindscip-go gomod github.com/golang/go/src go1.13 reflect/Value#Kind().() != reflectscip-go gomod github.com/golang/go/src go1.13 reflect/.Mapscip-go gomod github.com/golang/go/src go1.13 reflect/Map. { |
| 107 | return false, "First argument to HasKey must be a map" |
| 108 | } |
| 109 | |
| 110 | keyValue := 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]) |
| 111 | if !keyValue.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().(mapValue.Typescip-go gomod github.com/golang/go/src go1.13 reflect/Value#Type().().Keyscip-go gomod github.com/golang/go/src go1.13 reflect/Type#Key().()) { |
| 112 | return false, "Second argument must be assignable to the map key type" |
| 113 | } |
| 114 | |
| 115 | return mapValue.MapIndexscip-go gomod github.com/golang/go/src go1.13 reflect/Value#MapIndex().(keyValue).IsValidscip-go gomod github.com/golang/go/src go1.13 reflect/Value#IsValid().(), "" |
| 116 | } |
| 117 | |
| 118 | func (h *hasKeyscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/hasKey#) Infoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/hasKey#Info().() *CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo# { |
| 119 | return &CheckerInfoscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#{ |
| 120 | Namescip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Name.: "HasKey", |
| 121 | Paramsscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/CheckerInfo#Params.: []string{"obtained", "key"}, |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // The HasKey checker verifies that the obtained map contains the given key. |
| 126 | // |
| 127 | // For example: |
| 128 | // |
| 129 | // c.Assert(myMap, HasKey, "foo") |
| 130 | // |
| 131 | var HasKeyscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/HasKey. = &hasKeyscip-go gomod github.com/pingcap/check . `github.com/pingcap/check`/hasKey#{} |