go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/testing/assertions/error_tests_test.go (about)

     1  // Copyright 2015 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package assertions
    16  
    17  import (
    18  	"errors"
    19  	"testing"
    20  
    21  	multierror "go.chromium.org/luci/common/errors"
    22  
    23  	. "github.com/smartystreets/goconvey/convey"
    24  )
    25  
    26  type customError struct{}
    27  
    28  func (customError) Error() string { return "customError noob" }
    29  
    30  func TestShouldErrLike(t *testing.T) {
    31  	t.Parallel()
    32  
    33  	ce := customError{}
    34  	e := errors.New("e is for error")
    35  	f := errors.New("f is not for error")
    36  	me := multierror.MultiError{
    37  		e,
    38  		nil,
    39  		ce,
    40  	}
    41  
    42  	Convey("Test ShouldContainErr", t, func() {
    43  		Convey("too many params", func() {
    44  			So(ShouldContainErr(nil, nil, nil), ShouldContainSubstring, "requires 0 or 1")
    45  		})
    46  		Convey("no expectation", func() {
    47  			So(ShouldContainErr(multierror.MultiError(nil)), ShouldContainSubstring, "Expected '<nil>' to NOT be nil")
    48  			So(ShouldContainErr(me), ShouldEqual, "")
    49  		})
    50  		Convey("nil expectation", func() {
    51  			So(ShouldContainErr(multierror.MultiError(nil), nil), ShouldContainSubstring, "expected MultiError to contain")
    52  			So(ShouldContainErr(me, nil), ShouldEqual, "")
    53  		})
    54  		Convey("nil actual", func() {
    55  			So(ShouldContainErr(nil, nil), ShouldContainSubstring, "Expected '<nil>' to NOT be nil")
    56  			So(ShouldContainErr(nil, "wut"), ShouldContainSubstring, "Expected '<nil>' to NOT be nil")
    57  		})
    58  		Convey("not a multierror", func() {
    59  			So(ShouldContainErr(100, "wut"), ShouldContainSubstring, "Expected '100' to be: 'errors.MultiError'")
    60  		})
    61  		Convey("string actual", func() {
    62  			So(ShouldContainErr(me, "is for error"), ShouldEqual, "")
    63  			So(ShouldContainErr(me, "customError"), ShouldEqual, "")
    64  			So(ShouldContainErr(me, "is not for error"), ShouldContainSubstring, "expected MultiError to contain")
    65  		})
    66  		Convey("error actual", func() {
    67  			So(ShouldContainErr(me, e), ShouldEqual, "")
    68  			So(ShouldContainErr(me, ce), ShouldEqual, "")
    69  			So(ShouldContainErr(me, f), ShouldContainSubstring, "expected MultiError to contain")
    70  		})
    71  		Convey("bad expected type", func() {
    72  			So(ShouldContainErr(me, 20), ShouldContainSubstring, "unexpected argument type int")
    73  		})
    74  	})
    75  
    76  	Convey("Test ShouldErrLike", t, func() {
    77  		Convey("too many params", func() {
    78  			So(ShouldErrLike(nil, nil, nil), ShouldContainSubstring, "only accepts `nil` on the right hand side")
    79  		})
    80  		Convey("no expectation", func() {
    81  			So(ShouldErrLike(nil), ShouldEqual, "ShouldErrLike requires 1 or more expected values, got 0")
    82  			So(ShouldErrLike(e), ShouldEqual, "ShouldErrLike requires 1 or more expected values, got 0")
    83  			So(ShouldErrLike(ce), ShouldEqual, "ShouldErrLike requires 1 or more expected values, got 0")
    84  		})
    85  		Convey("nil expectation", func() {
    86  			So(ShouldErrLike(nil, nil), ShouldEqual, "")
    87  			So(ShouldErrLike(e, nil), ShouldContainSubstring, "Expected: nil")
    88  			So(ShouldErrLike(ce, nil), ShouldContainSubstring, "Expected: nil")
    89  		})
    90  		Convey("nil actual", func() {
    91  			So(ShouldErrLike(nil, "wut"), ShouldContainSubstring, "Expected '<nil>' to NOT be nil")
    92  			So(ShouldErrLike(nil, ""), ShouldContainSubstring, "Expected '<nil>' to NOT be nil")
    93  		})
    94  		Convey("not an err", func() {
    95  			So(ShouldErrLike(100, "wut"), ShouldContainSubstring, "Expected: 'error interface support'")
    96  		})
    97  		Convey("string actual", func() {
    98  			So(ShouldErrLike(e, "is for error"), ShouldEqual, "")
    99  			So(ShouldErrLike(ce, "customError"), ShouldEqual, "")
   100  			So(ShouldErrLike(e, ""), ShouldEqual, "")
   101  			So(ShouldErrLike(ce, ""), ShouldEqual, "")
   102  		})
   103  		Convey("error actual", func() {
   104  			So(ShouldErrLike(e, e), ShouldEqual, "")
   105  			So(ShouldErrLike(ce, ce), ShouldEqual, "")
   106  		})
   107  		Convey("bad expected type", func() {
   108  			So(ShouldErrLike(e, 20), ShouldContainSubstring, "unexpected argument type int")
   109  		})
   110  	})
   111  }