1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
// Code created by gotmpl. DO NOT MODIFY.
// source: internal/shared/matchers/expectation.go.tmpl
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package matchers // import "go.opentelemetry.io/otel/sdk/internal/matchers"
import (
"fmt"
"reflect"
"regexp"
"runtime/debug"
"strings"
"testing"
"time"
)
var stackTracePruneRE = regexp.MustCompile(`runtime\/debug|testing|internal\/matchers`)
type Expectation struct {
t *testing.T
actual interface{}
}
func (e *Expectation) ToEqual(expected interface{}) {
e.verifyExpectedNotNil(expected)
if !reflect.DeepEqual(e.actual, expected) {
e.fail(fmt.Sprintf("Expected\n\t%v\nto equal\n\t%v", e.actual, expected))
}
}
func (e *Expectation) NotToEqual(expected interface{}) {
e.verifyExpectedNotNil(expected)
if reflect.DeepEqual(e.actual, expected) {
e.fail(fmt.Sprintf("Expected\n\t%v\nnot to equal\n\t%v", e.actual, expected))
}
}
func (e *Expectation) ToBeNil() {
if e.actual != nil {
e.fail(fmt.Sprintf("Expected\n\t%v\nto be nil", e.actual))
}
}
func (e *Expectation) NotToBeNil() {
if e.actual == nil {
e.fail(fmt.Sprintf("Expected\n\t%v\nnot to be nil", e.actual))
}
}
func (e *Expectation) ToBeTrue() {
switch a := e.actual.(type) {
case bool:
if !a {
e.fail(fmt.Sprintf("Expected\n\t%v\nto be true", e.actual))
}
default:
e.fail(fmt.Sprintf("Cannot check if non-bool value\n\t%v\nis truthy", a))
}
}
func (e *Expectation) ToBeFalse() {
switch a := e.actual.(type) {
case bool:
if a {
e.fail(fmt.Sprintf("Expected\n\t%v\nto be false", e.actual))
}
default:
e.fail(fmt.Sprintf("Cannot check if non-bool value\n\t%v\nis truthy", a))
}
}
func (e *Expectation) NotToPanic() {
switch a := e.actual.(type) {
case func():
func() {
defer func() {
if recovered := recover(); recovered != nil {
e.fail(fmt.Sprintf("Expected panic\n\t%v\nto have not been raised", recovered))
}
}()
a()
}()
default:
e.fail(fmt.Sprintf("Cannot check if non-func value\n\t%v\nis truthy", a))
}
}
func (e *Expectation) ToSucceed() {
switch actual := e.actual.(type) {
case error:
if actual != nil {
e.fail(fmt.Sprintf("Expected error\n\t%v\nto have succeeded", actual))
}
default:
e.fail(fmt.Sprintf("Cannot check if non-error value\n\t%v\nsucceeded", actual))
}
}
func (e *Expectation) ToMatchError(expected interface{}) {
e.verifyExpectedNotNil(expected)
actual, ok := e.actual.(error)
if !ok {
e.fail(fmt.Sprintf("Cannot check if non-error value\n\t%v\nmatches error", e.actual))
}
switch expected := expected.(type) {
case error:
if !reflect.DeepEqual(actual, expected) {
e.fail(fmt.Sprintf("Expected\n\t%v\nto match error\n\t%v", actual, expected))
}
case string:
if actual.Error() != expected {
e.fail(fmt.Sprintf("Expected\n\t%v\nto match error\n\t%v", actual, expected))
}
default:
e.fail(fmt.Sprintf("Cannot match\n\t%v\nagainst non-error\n\t%v", actual, expected))
}
}
func (e *Expectation) ToContain(expected interface{}) {
actualValue := reflect.ValueOf(e.actual)
actualKind := actualValue.Kind()
switch actualKind {
case reflect.Array, reflect.Slice:
default:
e.fail(fmt.Sprintf("Expected\n\t%v\nto be an array", e.actual))
return
}
expectedValue := reflect.ValueOf(expected)
expectedKind := expectedValue.Kind()
switch expectedKind {
case reflect.Array, reflect.Slice:
default:
expectedValue = reflect.ValueOf([]interface{}{expected})
}
for i := 0; i < expectedValue.Len(); i++ {
var contained bool
expectedElem := expectedValue.Index(i).Interface()
for j := 0; j < actualValue.Len(); j++ {
if reflect.DeepEqual(actualValue.Index(j).Interface(), expectedElem) {
contained = true
break
}
}
if !contained {
e.fail(fmt.Sprintf("Expected\n\t%v\nto contain\n\t%v", e.actual, expectedElem))
return
}
}
}
func (e *Expectation) NotToContain(expected interface{}) {
actualValue := reflect.ValueOf(e.actual)
actualKind := actualValue.Kind()
switch actualKind {
case reflect.Array, reflect.Slice:
default:
e.fail(fmt.Sprintf("Expected\n\t%v\nto be an array", e.actual))
return
}
expectedValue := reflect.ValueOf(expected)
expectedKind := expectedValue.Kind()
switch expectedKind {
case reflect.Array, reflect.Slice:
default:
expectedValue = reflect.ValueOf([]interface{}{expected})
}
for i := 0; i < expectedValue.Len(); i++ {
expectedElem := expectedValue.Index(i).Interface()
for j := 0; j < actualValue.Len(); j++ {
if reflect.DeepEqual(actualValue.Index(j).Interface(), expectedElem) {
e.fail(fmt.Sprintf("Expected\n\t%v\nnot to contain\n\t%v", e.actual, expectedElem))
return
}
}
}
}
func (e *Expectation) ToMatchInAnyOrder(expected interface{}) {
expectedValue := reflect.ValueOf(expected)
expectedKind := expectedValue.Kind()
switch expectedKind {
case reflect.Array, reflect.Slice:
default:
e.fail(fmt.Sprintf("Expected\n\t%v\nto be an array", expected))
return
}
actualValue := reflect.ValueOf(e.actual)
actualKind := actualValue.Kind()
if actualKind != expectedKind {
e.fail(fmt.Sprintf("Expected\n\t%v\nto be the same type as\n\t%v", e.actual, expected))
return
}
if actualValue.Len() != expectedValue.Len() {
e.fail(fmt.Sprintf("Expected\n\t%v\nto have the same length as\n\t%v", e.actual, expected))
return
}
var unmatched []interface{}
for i := 0; i < expectedValue.Len(); i++ {
unmatched = append(unmatched, expectedValue.Index(i).Interface())
}
for i := 0; i < actualValue.Len(); i++ {
var found bool
for j, elem := range unmatched {
if reflect.DeepEqual(actualValue.Index(i).Interface(), elem) {
found = true
unmatched = append(unmatched[:j], unmatched[j+1:]...)
break
}
}
if !found {
e.fail(fmt.Sprintf("Expected\n\t%v\nto contain the same elements as\n\t%v", e.actual, expected))
}
}
}
func (e *Expectation) ToBeTemporally(matcher TemporalMatcher, compareTo interface{}) {
if actual, ok := e.actual.(time.Time); ok {
ct, ok := compareTo.(time.Time)
if !ok {
e.fail(fmt.Sprintf("Cannot compare to non-temporal value\n\t%v", compareTo))
return
}
switch matcher {
case Before:
if !actual.Before(ct) {
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally before\n\t%v", e.actual, compareTo))
}
case BeforeOrSameTime:
if actual.After(ct) {
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally before or at the same time as\n\t%v", e.actual, compareTo))
}
case After:
if !actual.After(ct) {
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally after\n\t%v", e.actual, compareTo))
}
case AfterOrSameTime:
if actual.Before(ct) {
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally after or at the same time as\n\t%v", e.actual, compareTo))
}
default:
e.fail("Cannot compare times with unexpected temporal matcher")
}
return
}
e.fail(fmt.Sprintf("Cannot compare non-temporal value\n\t%v", e.actual))
}
func (e *Expectation) verifyExpectedNotNil(expected interface{}) {
if expected == nil {
e.fail("Refusing to compare with <nil>. Use `ToBeNil` or `NotToBeNil` instead.")
}
}
func (e *Expectation) fail(msg string) {
// Prune the stack trace so that it's easier to see relevant lines
stack := strings.Split(string(debug.Stack()), "\n")
var prunedStack []string
for _, line := range stack {
if !stackTracePruneRE.MatchString(line) {
prunedStack = append(prunedStack, line)
}
}
e.t.Fatalf("\n%s\n%s\n", strings.Join(prunedStack, "\n"), msg)
}
|