...

Source file src/go.opentelemetry.io/otel/sdk/internal/matchers/expectation.go

Documentation: go.opentelemetry.io/otel/sdk/internal/matchers

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

View as plain text