...

Source file src/go.mongodb.org/mongo-driver/internal/assert/assertion_format.go

Documentation: go.mongodb.org/mongo-driver/internal/assert

     1  // Copied from https://github.com/stretchr/testify/blob/1333b5d3bda8cf5aedcf3e1aaa95cac28aaab892/assert/assertion_format.go
     2  
     3  // Copyright 2020 Mat Ryer, Tyler Bunnell and all contributors. All rights reserved.
     4  // Use of this source code is governed by an MIT-style license that can be found in
     5  // the THIRD-PARTY-NOTICES file.
     6  
     7  package assert
     8  
     9  import (
    10  	time "time"
    11  )
    12  
    13  // Containsf asserts that the specified string, list(array, slice...) or map contains the
    14  // specified substring or element.
    15  //
    16  //	assert.Containsf(t, "Hello World", "World", "error message %s", "formatted")
    17  //	assert.Containsf(t, ["Hello", "World"], "World", "error message %s", "formatted")
    18  //	assert.Containsf(t, {"Hello": "World"}, "Hello", "error message %s", "formatted")
    19  func Containsf(t TestingT, s interface{}, contains interface{}, msg string, args ...interface{}) bool {
    20  	if h, ok := t.(tHelper); ok {
    21  		h.Helper()
    22  	}
    23  	return Contains(t, s, contains, append([]interface{}{msg}, args...)...)
    24  }
    25  
    26  // ElementsMatchf asserts that the specified listA(array, slice...) is equal to specified
    27  // listB(array, slice...) ignoring the order of the elements. If there are duplicate elements,
    28  // the number of appearances of each of them in both lists should match.
    29  //
    30  // assert.ElementsMatchf(t, [1, 3, 2, 3], [1, 3, 3, 2], "error message %s", "formatted")
    31  func ElementsMatchf(t TestingT, listA interface{}, listB interface{}, msg string, args ...interface{}) bool {
    32  	if h, ok := t.(tHelper); ok {
    33  		h.Helper()
    34  	}
    35  	return ElementsMatch(t, listA, listB, append([]interface{}{msg}, args...)...)
    36  }
    37  
    38  // Equalf asserts that two objects are equal.
    39  //
    40  //	assert.Equalf(t, 123, 123, "error message %s", "formatted")
    41  //
    42  // Pointer variable equality is determined based on the equality of the
    43  // referenced values (as opposed to the memory addresses). Function equality
    44  // cannot be determined and will always fail.
    45  func Equalf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool {
    46  	if h, ok := t.(tHelper); ok {
    47  		h.Helper()
    48  	}
    49  	return Equal(t, expected, actual, append([]interface{}{msg}, args...)...)
    50  }
    51  
    52  // EqualErrorf asserts that a function returned an error (i.e. not `nil`)
    53  // and that it is equal to the provided error.
    54  //
    55  //	actualObj, err := SomeFunction()
    56  //	assert.EqualErrorf(t, err,  expectedErrorString, "error message %s", "formatted")
    57  func EqualErrorf(t TestingT, theError error, errString string, msg string, args ...interface{}) bool {
    58  	if h, ok := t.(tHelper); ok {
    59  		h.Helper()
    60  	}
    61  	return EqualError(t, theError, errString, append([]interface{}{msg}, args...)...)
    62  }
    63  
    64  // EqualValuesf asserts that two objects are equal or convertible to the same types
    65  // and equal.
    66  //
    67  //	assert.EqualValuesf(t, uint32(123), int32(123), "error message %s", "formatted")
    68  func EqualValuesf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool {
    69  	if h, ok := t.(tHelper); ok {
    70  		h.Helper()
    71  	}
    72  	return EqualValues(t, expected, actual, append([]interface{}{msg}, args...)...)
    73  }
    74  
    75  // Errorf asserts that a function returned an error (i.e. not `nil`).
    76  //
    77  //	  actualObj, err := SomeFunction()
    78  //	  if assert.Errorf(t, err, "error message %s", "formatted") {
    79  //		   assert.Equal(t, expectedErrorf, err)
    80  //	  }
    81  func Errorf(t TestingT, err error, msg string, args ...interface{}) bool {
    82  	if h, ok := t.(tHelper); ok {
    83  		h.Helper()
    84  	}
    85  	return Error(t, err, append([]interface{}{msg}, args...)...)
    86  }
    87  
    88  // ErrorContainsf asserts that a function returned an error (i.e. not `nil`)
    89  // and that the error contains the specified substring.
    90  //
    91  //	actualObj, err := SomeFunction()
    92  //	assert.ErrorContainsf(t, err,  expectedErrorSubString, "error message %s", "formatted")
    93  func ErrorContainsf(t TestingT, theError error, contains string, msg string, args ...interface{}) bool {
    94  	if h, ok := t.(tHelper); ok {
    95  		h.Helper()
    96  	}
    97  	return ErrorContains(t, theError, contains, append([]interface{}{msg}, args...)...)
    98  }
    99  
   100  // Eventuallyf asserts that given condition will be met in waitFor time,
   101  // periodically checking target function each tick.
   102  //
   103  //	assert.Eventuallyf(t, func() bool { return true; }, time.Second, 10*time.Millisecond, "error message %s", "formatted")
   104  func Eventuallyf(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) bool {
   105  	if h, ok := t.(tHelper); ok {
   106  		h.Helper()
   107  	}
   108  	return Eventually(t, condition, waitFor, tick, append([]interface{}{msg}, args...)...)
   109  }
   110  
   111  // Failf reports a failure through
   112  func Failf(t TestingT, failureMessage string, msg string, args ...interface{}) bool {
   113  	if h, ok := t.(tHelper); ok {
   114  		h.Helper()
   115  	}
   116  	return Fail(t, failureMessage, append([]interface{}{msg}, args...)...)
   117  }
   118  
   119  // FailNowf fails test
   120  func FailNowf(t TestingT, failureMessage string, msg string, args ...interface{}) bool {
   121  	if h, ok := t.(tHelper); ok {
   122  		h.Helper()
   123  	}
   124  	return FailNow(t, failureMessage, append([]interface{}{msg}, args...)...)
   125  }
   126  
   127  // Falsef asserts that the specified value is false.
   128  //
   129  //	assert.Falsef(t, myBool, "error message %s", "formatted")
   130  func Falsef(t TestingT, value bool, msg string, args ...interface{}) bool {
   131  	if h, ok := t.(tHelper); ok {
   132  		h.Helper()
   133  	}
   134  	return False(t, value, append([]interface{}{msg}, args...)...)
   135  }
   136  
   137  // Greaterf asserts that the first element is greater than the second
   138  //
   139  //	assert.Greaterf(t, 2, 1, "error message %s", "formatted")
   140  //	assert.Greaterf(t, float64(2), float64(1), "error message %s", "formatted")
   141  //	assert.Greaterf(t, "b", "a", "error message %s", "formatted")
   142  func Greaterf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool {
   143  	if h, ok := t.(tHelper); ok {
   144  		h.Helper()
   145  	}
   146  	return Greater(t, e1, e2, append([]interface{}{msg}, args...)...)
   147  }
   148  
   149  // GreaterOrEqualf asserts that the first element is greater than or equal to the second
   150  //
   151  //	assert.GreaterOrEqualf(t, 2, 1, "error message %s", "formatted")
   152  //	assert.GreaterOrEqualf(t, 2, 2, "error message %s", "formatted")
   153  //	assert.GreaterOrEqualf(t, "b", "a", "error message %s", "formatted")
   154  //	assert.GreaterOrEqualf(t, "b", "b", "error message %s", "formatted")
   155  func GreaterOrEqualf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool {
   156  	if h, ok := t.(tHelper); ok {
   157  		h.Helper()
   158  	}
   159  	return GreaterOrEqual(t, e1, e2, append([]interface{}{msg}, args...)...)
   160  }
   161  
   162  // InDeltaf asserts that the two numerals are within delta of each other.
   163  //
   164  //	assert.InDeltaf(t, math.Pi, 22/7.0, 0.01, "error message %s", "formatted")
   165  func InDeltaf(t TestingT, expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool {
   166  	if h, ok := t.(tHelper); ok {
   167  		h.Helper()
   168  	}
   169  	return InDelta(t, expected, actual, delta, append([]interface{}{msg}, args...)...)
   170  }
   171  
   172  // IsTypef asserts that the specified objects are of the same type.
   173  func IsTypef(t TestingT, expectedType interface{}, object interface{}, msg string, args ...interface{}) bool {
   174  	if h, ok := t.(tHelper); ok {
   175  		h.Helper()
   176  	}
   177  	return IsType(t, expectedType, object, append([]interface{}{msg}, args...)...)
   178  }
   179  
   180  // Lenf asserts that the specified object has specific length.
   181  // Lenf also fails if the object has a type that len() not accept.
   182  //
   183  //	assert.Lenf(t, mySlice, 3, "error message %s", "formatted")
   184  func Lenf(t TestingT, object interface{}, length int, msg string, args ...interface{}) bool {
   185  	if h, ok := t.(tHelper); ok {
   186  		h.Helper()
   187  	}
   188  	return Len(t, object, length, append([]interface{}{msg}, args...)...)
   189  }
   190  
   191  // Lessf asserts that the first element is less than the second
   192  //
   193  //	assert.Lessf(t, 1, 2, "error message %s", "formatted")
   194  //	assert.Lessf(t, float64(1), float64(2), "error message %s", "formatted")
   195  //	assert.Lessf(t, "a", "b", "error message %s", "formatted")
   196  func Lessf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool {
   197  	if h, ok := t.(tHelper); ok {
   198  		h.Helper()
   199  	}
   200  	return Less(t, e1, e2, append([]interface{}{msg}, args...)...)
   201  }
   202  
   203  // LessOrEqualf asserts that the first element is less than or equal to the second
   204  //
   205  //	assert.LessOrEqualf(t, 1, 2, "error message %s", "formatted")
   206  //	assert.LessOrEqualf(t, 2, 2, "error message %s", "formatted")
   207  //	assert.LessOrEqualf(t, "a", "b", "error message %s", "formatted")
   208  //	assert.LessOrEqualf(t, "b", "b", "error message %s", "formatted")
   209  func LessOrEqualf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool {
   210  	if h, ok := t.(tHelper); ok {
   211  		h.Helper()
   212  	}
   213  	return LessOrEqual(t, e1, e2, append([]interface{}{msg}, args...)...)
   214  }
   215  
   216  // Negativef asserts that the specified element is negative
   217  //
   218  //	assert.Negativef(t, -1, "error message %s", "formatted")
   219  //	assert.Negativef(t, -1.23, "error message %s", "formatted")
   220  func Negativef(t TestingT, e interface{}, msg string, args ...interface{}) bool {
   221  	if h, ok := t.(tHelper); ok {
   222  		h.Helper()
   223  	}
   224  	return Negative(t, e, append([]interface{}{msg}, args...)...)
   225  }
   226  
   227  // Nilf asserts that the specified object is nil.
   228  //
   229  //	assert.Nilf(t, err, "error message %s", "formatted")
   230  func Nilf(t TestingT, object interface{}, msg string, args ...interface{}) bool {
   231  	if h, ok := t.(tHelper); ok {
   232  		h.Helper()
   233  	}
   234  	return Nil(t, object, append([]interface{}{msg}, args...)...)
   235  }
   236  
   237  // NoErrorf asserts that a function returned no error (i.e. `nil`).
   238  //
   239  //	  actualObj, err := SomeFunction()
   240  //	  if assert.NoErrorf(t, err, "error message %s", "formatted") {
   241  //		   assert.Equal(t, expectedObj, actualObj)
   242  //	  }
   243  func NoErrorf(t TestingT, err error, msg string, args ...interface{}) bool {
   244  	if h, ok := t.(tHelper); ok {
   245  		h.Helper()
   246  	}
   247  	return NoError(t, err, append([]interface{}{msg}, args...)...)
   248  }
   249  
   250  // NotContainsf asserts that the specified string, list(array, slice...) or map does NOT contain the
   251  // specified substring or element.
   252  //
   253  //	assert.NotContainsf(t, "Hello World", "Earth", "error message %s", "formatted")
   254  //	assert.NotContainsf(t, ["Hello", "World"], "Earth", "error message %s", "formatted")
   255  //	assert.NotContainsf(t, {"Hello": "World"}, "Earth", "error message %s", "formatted")
   256  func NotContainsf(t TestingT, s interface{}, contains interface{}, msg string, args ...interface{}) bool {
   257  	if h, ok := t.(tHelper); ok {
   258  		h.Helper()
   259  	}
   260  	return NotContains(t, s, contains, append([]interface{}{msg}, args...)...)
   261  }
   262  
   263  // NotEqualf asserts that the specified values are NOT equal.
   264  //
   265  //	assert.NotEqualf(t, obj1, obj2, "error message %s", "formatted")
   266  //
   267  // Pointer variable equality is determined based on the equality of the
   268  // referenced values (as opposed to the memory addresses).
   269  func NotEqualf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool {
   270  	if h, ok := t.(tHelper); ok {
   271  		h.Helper()
   272  	}
   273  	return NotEqual(t, expected, actual, append([]interface{}{msg}, args...)...)
   274  }
   275  
   276  // NotEqualValuesf asserts that two objects are not equal even when converted to the same type
   277  //
   278  //	assert.NotEqualValuesf(t, obj1, obj2, "error message %s", "formatted")
   279  func NotEqualValuesf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool {
   280  	if h, ok := t.(tHelper); ok {
   281  		h.Helper()
   282  	}
   283  	return NotEqualValues(t, expected, actual, append([]interface{}{msg}, args...)...)
   284  }
   285  
   286  // NotNilf asserts that the specified object is not nil.
   287  //
   288  //	assert.NotNilf(t, err, "error message %s", "formatted")
   289  func NotNilf(t TestingT, object interface{}, msg string, args ...interface{}) bool {
   290  	if h, ok := t.(tHelper); ok {
   291  		h.Helper()
   292  	}
   293  	return NotNil(t, object, append([]interface{}{msg}, args...)...)
   294  }
   295  
   296  // Positivef asserts that the specified element is positive
   297  //
   298  //	assert.Positivef(t, 1, "error message %s", "formatted")
   299  //	assert.Positivef(t, 1.23, "error message %s", "formatted")
   300  func Positivef(t TestingT, e interface{}, msg string, args ...interface{}) bool {
   301  	if h, ok := t.(tHelper); ok {
   302  		h.Helper()
   303  	}
   304  	return Positive(t, e, append([]interface{}{msg}, args...)...)
   305  }
   306  
   307  // Truef asserts that the specified value is true.
   308  //
   309  //	assert.Truef(t, myBool, "error message %s", "formatted")
   310  func Truef(t TestingT, value bool, msg string, args ...interface{}) bool {
   311  	if h, ok := t.(tHelper); ok {
   312  		h.Helper()
   313  	}
   314  	return True(t, value, append([]interface{}{msg}, args...)...)
   315  }
   316  
   317  // WithinDurationf asserts that the two times are within duration delta of each other.
   318  //
   319  //	assert.WithinDurationf(t, time.Now(), time.Now(), 10*time.Second, "error message %s", "formatted")
   320  func WithinDurationf(t TestingT, expected time.Time, actual time.Time, delta time.Duration, msg string, args ...interface{}) bool {
   321  	if h, ok := t.(tHelper); ok {
   322  		h.Helper()
   323  	}
   324  	return WithinDuration(t, expected, actual, delta, append([]interface{}{msg}, args...)...)
   325  }
   326  

View as plain text