|
| 1 | +package require |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func NoError(t *testing.T, err error, msgs ...string) { |
| 9 | + if err != nil { |
| 10 | + t.Errorf("Expected no error, but got: %s", err) |
| 11 | + for _, msg := range msgs { |
| 12 | + t.Errorf("\n" + msg) |
| 13 | + } |
| 14 | + t.FailNow() |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +func NotNil(t *testing.T, object interface{}, msgs ...string) { |
| 19 | + if isNil(object) { |
| 20 | + t.Errorf("Expected nil, but got an object") |
| 21 | + for _, msg := range msgs { |
| 22 | + t.Errorf("\n" + msg) |
| 23 | + } |
| 24 | + t.FailNow() |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func Nil(t *testing.T, object interface{}, msgs ...string) { |
| 29 | + if !isNil(object) { |
| 30 | + t.Errorf("Expected an object, but got nil") |
| 31 | + for _, msg := range msgs { |
| 32 | + t.Errorf("\n" + msg) |
| 33 | + } |
| 34 | + t.FailNow() |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func isNil(object interface{}) bool { |
| 39 | + if object == nil { |
| 40 | + return true |
| 41 | + } |
| 42 | + |
| 43 | + value := reflect.ValueOf(object) |
| 44 | + kind := value.Kind() |
| 45 | + if kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil() { |
| 46 | + return true |
| 47 | + } |
| 48 | + |
| 49 | + return false |
| 50 | +} |
| 51 | + |
| 52 | +func EqualError(t *testing.T, theError error, errString string, msgs ...string) { |
| 53 | + if theError == nil { |
| 54 | + t.Errorf("Expected an error but got nil") |
| 55 | + for _, msg := range msgs { |
| 56 | + t.Errorf("\n" + msg) |
| 57 | + } |
| 58 | + t.FailNow() |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + if theError.Error() != errString { |
| 63 | + t.Errorf("Expected an error with message:\n%qbut got \n%q", errString, theError) |
| 64 | + for _, msg := range msgs { |
| 65 | + t.Errorf("\n" + msg) |
| 66 | + } |
| 67 | + t.FailNow() |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func EqualFloat64(t *testing.T, expected, actual float64, msgs ...string) { |
| 72 | + if expected != actual { |
| 73 | + t.Errorf("Expected %f but got %f", expected, actual) |
| 74 | + for _, msg := range msgs { |
| 75 | + t.Errorf("\n" + msg) |
| 76 | + } |
| 77 | + t.FailNow() |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func EqualStringPtr(t *testing.T, expected, actual *string, msgs ...string) { |
| 82 | + if expected == nil && actual == nil { |
| 83 | + return |
| 84 | + } |
| 85 | + if expected != nil && actual != nil { |
| 86 | + EqualString(t, *expected, *actual, msgs...) |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + t.Errorf("Expected %v but got %v", expected, actual) |
| 91 | + for _, msg := range msgs { |
| 92 | + t.Errorf("\n" + msg) |
| 93 | + } |
| 94 | + t.FailNow() |
| 95 | +} |
| 96 | + |
| 97 | +func EqualString(t *testing.T, expected, actual string, msgs ...string) { |
| 98 | + if expected != actual { |
| 99 | + t.Errorf("Expected %s but got %s", expected, actual) |
| 100 | + for _, msg := range msgs { |
| 101 | + t.Errorf("\n" + msg) |
| 102 | + } |
| 103 | + t.FailNow() |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func EqualUInt32(t *testing.T, expected, actual uint32, msgs ...string) { |
| 108 | + if expected != actual { |
| 109 | + t.Errorf("Expected %d but got %d", expected, actual) |
| 110 | + for _, msg := range msgs { |
| 111 | + t.Errorf("\n" + msg) |
| 112 | + } |
| 113 | + t.FailNow() |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func EqualUInt64Ptr(t *testing.T, expected, actual *uint64, msgs ...string) { |
| 118 | + if expected == nil && actual == nil { |
| 119 | + return |
| 120 | + } |
| 121 | + if expected != nil && actual != nil { |
| 122 | + EqualUInt64(t, *expected, *actual, msgs...) |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + t.Errorf("Expected %v but got %v", expected, actual) |
| 127 | + for _, msg := range msgs { |
| 128 | + t.Errorf("\n" + msg) |
| 129 | + } |
| 130 | + t.FailNow() |
| 131 | +} |
| 132 | + |
| 133 | +func EqualUInt64(t *testing.T, expected, actual uint64, msgs ...string) { |
| 134 | + if expected != actual { |
| 135 | + t.Errorf("Expected %d but got %d", expected, actual) |
| 136 | + for _, msg := range msgs { |
| 137 | + t.Errorf("\n" + msg) |
| 138 | + } |
| 139 | + t.FailNow() |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func EqualIntPtr(t *testing.T, expected, actual *int, msgs ...string) { |
| 144 | + if expected == nil && actual == nil { |
| 145 | + return |
| 146 | + } |
| 147 | + if expected != nil && actual != nil { |
| 148 | + EqualInt(t, *expected, *actual, msgs...) |
| 149 | + return |
| 150 | + } |
| 151 | + |
| 152 | + t.Errorf("Expected %v but got %v", expected, actual) |
| 153 | + for _, msg := range msgs { |
| 154 | + t.Errorf("\n" + msg) |
| 155 | + } |
| 156 | + t.FailNow() |
| 157 | +} |
| 158 | + |
| 159 | +func EqualInt(t *testing.T, expected, actual int, msgs ...string) { |
| 160 | + if expected != actual { |
| 161 | + t.Errorf("Expected %d but got %d", expected, actual) |
| 162 | + for _, msg := range msgs { |
| 163 | + t.Errorf("\n" + msg) |
| 164 | + } |
| 165 | + t.FailNow() |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func EqualErr(t *testing.T, expected, actual error, msgs ...string) { |
| 170 | + if expected != actual { |
| 171 | + t.Errorf("Expected %s but got %s", expected, actual) |
| 172 | + for _, msg := range msgs { |
| 173 | + t.Errorf("\n" + msg) |
| 174 | + } |
| 175 | + t.FailNow() |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +func Implements(t *testing.T, interfaceObject interface{}, object interface{}, msgs ...string) { |
| 180 | + interfaceType := reflect.TypeOf(interfaceObject).Elem() |
| 181 | + |
| 182 | + if !reflect.TypeOf(object).Implements(interfaceType) { |
| 183 | + t.Errorf("Expected %T but got %v", object, interfaceType) |
| 184 | + for _, msg := range msgs { |
| 185 | + t.Errorf("\n" + msg) |
| 186 | + } |
| 187 | + t.FailNow() |
| 188 | + } |
| 189 | +} |
0 commit comments