github.com/m3db/m3@v1.5.0/src/x/test/reporter.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  // Package test contains utility methods for testing.
    22  package test
    23  
    24  import (
    25  	"fmt"
    26  
    27  	"github.com/golang/mock/gomock"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  // Reporter wraps a *testing.T, and provides a more useful failure mode
    32  // when interacting with gomock.Controller.
    33  //
    34  // For example, consider:
    35  //   func TestMyThing(t *testing.T) {
    36  //     mockCtrl := gomock.NewController(t)
    37  //     defer mockCtrl.Finish()
    38  //     mockObj := something.NewMockMyInterface(mockCtrl)
    39  //     go func() {
    40  //       mockObj.SomeMethod(4, "blah")
    41  //     }
    42  //   }
    43  //
    44  // It hangs without any indication that it's missing an EXPECT() on `mockObj`.
    45  // Providing the Reporter to the gomock.Controller ctor avoids this, and terminates
    46  // with useful feedback. i.e.
    47  //   func TestMyThing(t *testing.T) {
    48  //     mockCtrl := gomock.NewController(test.Reporter{t})
    49  //     defer mockCtrl.Finish()
    50  //     mockObj := something.NewMockMyInterface(mockCtrl)
    51  //     go func() {
    52  //       mockObj.SomeMethod(4, "blah") // crashes the test now
    53  //     }
    54  //   }
    55  type Reporter struct {
    56  	T require.TestingT
    57  }
    58  
    59  // ensure Reporter implements gomock.TestReporter.
    60  var _ gomock.TestReporter = Reporter{}
    61  
    62  // Errorf is equivalent testing.T.Errorf.
    63  func (r Reporter) Errorf(format string, args ...interface{}) {
    64  	r.T.Errorf(format, args...)
    65  }
    66  
    67  // Fatalf crashes the program with a panic to allow users to diagnose
    68  // missing expects.
    69  func (r Reporter) Fatalf(format string, args ...interface{}) {
    70  	panic(fmt.Sprintf(format, args...))
    71  }
    72  
    73  // NewController provides a gomock.Controller wrapped with a xtest.Reporter,
    74  // which gives more useful error modes on unexpected mock calls.
    75  // See xtest.Reporter for more context.
    76  func NewController(t require.TestingT) *gomock.Controller {
    77  	return gomock.NewController(Reporter{T: t})
    78  }