diff --git a/.github/workflows/update-registry.yaml b/.github/workflows/update-registry.yaml new file mode 100644 index 0000000..6e3df70 --- /dev/null +++ b/.github/workflows/update-registry.yaml @@ -0,0 +1,27 @@ +--- +name: 'update-registry' + +on: + push: + tags: + - v* + workflow_dispatch: + +env: + MODULE_NAME: grpcmock + +jobs: + notify: + runs-on: ubuntu-latest + strategy: + matrix: + registry: [ go.nhat.io, go-staging.nhat.io ] + steps: + - name: notify ${{ matrix.registry }} + uses: benc-uk/workflow-dispatch@v1 + with: + workflow: build + repo: nhatthm/${{ matrix.registry }} + token: ${{ secrets.REGISTRY_TOKEN }} + inputs: '{"modules": "${{ env.MODULE_NAME }}"}' + ref: 'master' diff --git a/README.md b/README.md index 7cc72ca..e3516d8 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ -> ⚠️ From `v0.20.0`, the project will be rebranded to `go.nhat.io/grpcmock`. `v.19.x` is the last version with `github.com/nhatthm/grpcmock`. +> ⚠️ From `v0.20.0`, the project will be rebranded to `go.nhat.io/grpcmock`. `v.19.0` is the last version with `github.com/nhatthm/grpcmock`. # gRPC Test Utilities for Golang [![GitHub Releases](https://img.shields.io/github/v/release/nhatthm/grpcmock)](https://github.com/nhatthm/grpcmock/releases/latest) [![Build Status](https://github.com/nhatthm/grpcmock/actions/workflows/test.yaml/badge.svg)](https://github.com/nhatthm/grpcmock/actions/workflows/test.yaml) [![codecov](https://codecov.io/gh/nhatthm/grpcmock/branch/master/graph/badge.svg?token=eTdAgDE2vR)](https://codecov.io/gh/nhatthm/grpcmock) -[![Go Report Card](https://goreportcard.com/badge/github.com/nhatthm/grpcmock)](https://goreportcard.com/report/github.com/nhatthm/grpcmock) -[![GoDevDoc](https://img.shields.io/badge/dev-doc-00ADD8?logo=go)](https://pkg.go.dev/github.com/nhatthm/grpcmock) +[![Go Report Card](https://goreportcard.com/badge/go.nhat.io/grpcmock)](https://goreportcard.com/report/go.nhat.io/grpcmock) +[![GoDevDoc](https://img.shields.io/badge/dev-doc-00ADD8?logo=go)](https://pkg.go.dev/go.nhat.io/grpcmock) [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/donate/?hosted_button_id=PJZSGJN57TDJY) Test gRPC service and client like a pro. @@ -36,7 +36,7 @@ Test gRPC service and client like a pro. ## Install ```bash -go get github.com/nhatthm/grpcmock +go get go.nhat.io/grpcmock ``` [[table of contents]](#table-of-contents) @@ -61,9 +61,9 @@ import ( "testing" "time" - "github.com/nhatthm/grpcmock" - grpcAssert "github.com/nhatthm/grpcmock/assert" "github.com/stretchr/testify/assert" + "go.nhat.io/grpcmock" + xassert "go.nhat.io/grpcmock/assert" ) func TestGetItems(t *testing.T) { @@ -94,7 +94,7 @@ func TestGetItems(t *testing.T) { grpcmock.WithInsecure(), ) - grpcAssert.EqualMessage(t, expected, out) + xassert.EqualMessage(t, expected, out) assert.NoError(t, err) } ``` @@ -113,9 +113,9 @@ import ( "testing" "time" - "github.com/nhatthm/grpcmock" - grpcAssert "github.com/nhatthm/grpcmock/assert" "github.com/stretchr/testify/assert" + "go.nhat.io/grpcmock" + xassert "go.nhat.io/grpcmock/assert" ) func TestCreateItems(t *testing.T) { @@ -142,7 +142,7 @@ func TestCreateItems(t *testing.T) { grpcmock.WithInsecure(), ) - grpcAssert.EqualMessage(t, expected, out) + xassert.EqualMessage(t, expected, out) assert.NoError(t, err) } ``` @@ -161,9 +161,9 @@ import ( "testing" "time" - "github.com/nhatthm/grpcmock" - grpcAssert "github.com/nhatthm/grpcmock/assert" "github.com/stretchr/testify/assert" + "go.nhat.io/grpcmock" + xassert "go.nhat.io/grpcmock/assert" ) func TestListItems(t *testing.T) { @@ -200,7 +200,7 @@ func TestListItems(t *testing.T) { assert.Len(t, actual, len(expected)) for i := 0; i < len(expected); i++ { - grpcAssert.EqualMessage(t, expected[i], actual[i]) + xassert.EqualMessage(t, expected[i], actual[i]) } } ``` @@ -222,9 +222,9 @@ import ( "testing" "time" - "github.com/nhatthm/grpcmock" - grpcAssert "github.com/nhatthm/grpcmock/assert" "github.com/stretchr/testify/assert" + "go.nhat.io/grpcmock" + xassert "go.nhat.io/grpcmock/assert" "google.golang.org/grpc" ) @@ -284,7 +284,7 @@ func TestTransformItems(t *testing.T) { assert.Len(t, actual, len(expected)) for i := 0; i < len(expected); i++ { - grpcAssert.EqualMessage(t, expected[i], actual[i]) + xassert.EqualMessage(t, expected[i], actual[i]) } } ``` @@ -302,7 +302,7 @@ import ( "context" "time" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" "google.golang.org/grpc/test/bufconn" ) @@ -333,7 +333,7 @@ import ( "context" "time" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" "google.golang.org/grpc/test/bufconn" ) @@ -361,7 +361,7 @@ import ( "context" "time" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" "google.golang.org/grpc" "google.golang.org/grpc/test/bufconn" ) @@ -396,7 +396,7 @@ import ( "context" "time" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" "google.golang.org/grpc/test/bufconn" ) @@ -425,7 +425,7 @@ import ( "context" "time" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" "google.golang.org/grpc" "google.golang.org/grpc/test/bufconn" ) @@ -460,7 +460,7 @@ import ( "context" "time" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" "google.golang.org/grpc/test/bufconn" ) @@ -488,7 +488,7 @@ import ( "context" "time" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" "google.golang.org/grpc" "google.golang.org/grpc/test/bufconn" ) diff --git a/client.go b/client.go index 7a560c8..1182530 100644 --- a/client.go +++ b/client.go @@ -12,8 +12,8 @@ import ( "google.golang.org/grpc/metadata" "google.golang.org/grpc/test/bufconn" - "github.com/nhatthm/grpcmock/errors" - "github.com/nhatthm/grpcmock/stream" + "go.nhat.io/grpcmock/errors" + "go.nhat.io/grpcmock/stream" ) var methodRegex = regexp.MustCompile(`/?[^/]+/[^/]+$`) diff --git a/client_test.go b/client_test.go index 758ad43..ead39ec 100644 --- a/client_test.go +++ b/client_test.go @@ -17,11 +17,11 @@ import ( "google.golang.org/grpc/test/bufconn" "google.golang.org/protobuf/proto" - "github.com/nhatthm/grpcmock" - grpcAssert "github.com/nhatthm/grpcmock/assert" - grpcMock "github.com/nhatthm/grpcmock/mock/grpc" - "github.com/nhatthm/grpcmock/test" - "github.com/nhatthm/grpcmock/test/grpctest" + "go.nhat.io/grpcmock" + xassert "go.nhat.io/grpcmock/assert" + xmock "go.nhat.io/grpcmock/mock/grpc" + "go.nhat.io/grpcmock/test" + "go.nhat.io/grpcmock/test/grpctest" ) func TestInvokeUnary_MethodError(t *testing.T) { @@ -119,8 +119,8 @@ func TestInvokeUnary_Success(t *testing.T) { Name: "Foobar", } - grpcAssert.EqualMessage(t, expectedRequest, actualRequest) - grpcAssert.EqualMessage(t, expectedResponse, actualResponse) + xassert.EqualMessage(t, expectedRequest, actualRequest) + xassert.EqualMessage(t, expectedResponse, actualResponse) assert.NoError(t, err) } @@ -178,7 +178,7 @@ func TestInvokeServerStream_UnaryMethod(t *testing.T) { msg := &grpctest.Item{} err := s.RecvMsg(msg) - grpcAssert.EqualMessage(t, item, msg) + xassert.EqualMessage(t, item, msg) assert.NoError(t, err) // Stream is closed. @@ -246,7 +246,7 @@ func TestInvokeServerStream_Success(t *testing.T) { assert.Equal(t, len(expected), len(result)) for i := 0; i < len(expected); i++ { - grpcAssert.EqualMessage(t, expected[i], result[i]) + xassert.EqualMessage(t, expected[i], result[i]) } } @@ -349,12 +349,12 @@ func TestInvokeClientStream_Success(t *testing.T) { expectedResult := &grpctest.CreateItemsResponse{NumItems: int64(len(items))} - grpcAssert.EqualMessage(t, expectedResult, result) + xassert.EqualMessage(t, expectedResult, result) assert.NoError(t, err) assert.Equal(t, len(received), len(items)) for i := 0; i < len(received); i++ { - grpcAssert.EqualMessage(t, received[i], items[i]) + xassert.EqualMessage(t, received[i], items[i]) } } @@ -470,7 +470,7 @@ func TestInvokeBidirectionalStream_Success(t *testing.T) { assert.Equal(t, len(expected), len(result)) for i := 0; i < len(expected); i++ { - grpcAssert.EqualMessage(t, expected[i], result[i]) + xassert.EqualMessage(t, expected[i], result[i]) } } @@ -479,24 +479,24 @@ func TestSendAll(t *testing.T) { testCases := []struct { scenario string - mockStream grpcMock.ClientStreamMocker + mockStream xmock.ClientStreamMocker input interface{} expectedError string }{ { scenario: "input is nil", - mockStream: grpcMock.NoMockClientStream, + mockStream: xmock.NoMockClientStream, expectedError: `not a slice: `, }, { scenario: "input is not a slice", - mockStream: grpcMock.NoMockClientStream, + mockStream: xmock.NoMockClientStream, input: &grpctest.Item{}, expectedError: `not a slice: *grpctest.Item`, }, { scenario: "send error", - mockStream: grpcMock.MockClientStream(func(s *grpcMock.ClientStream) { + mockStream: xmock.MockClientStream(func(s *xmock.ClientStream) { s.On("SendMsg", mock.Anything). Return(errors.New("send error")) }), @@ -505,7 +505,7 @@ func TestSendAll(t *testing.T) { }, { scenario: "success with a slice of struct", - mockStream: grpcMock.MockClientStream(func(s *grpcMock.ClientStream) { + mockStream: xmock.MockClientStream(func(s *xmock.ClientStream) { for _, i := range test.DefaultItems() { s.On("SendMsg", i).Once(). Return(nil) @@ -534,7 +534,7 @@ func TestSendAll(t *testing.T) { func TestRecvAll(t *testing.T) { t.Parallel() - sendItems := func(s *grpcMock.ClientStream) { + sendItems := func(s *xmock.ClientStream) { for _, i := range test.DefaultItems() { i := i @@ -553,33 +553,33 @@ func TestRecvAll(t *testing.T) { testCases := []struct { scenario string - mockStream grpcMock.ClientStreamMocker + mockStream xmock.ClientStreamMocker output interface{} expectedOutput interface{} expectedError string }{ { scenario: "output is nil", - mockStream: grpcMock.NoMockClientStream, + mockStream: xmock.NoMockClientStream, expectedError: `not a pointer: `, }, { scenario: "output is not a pointer", - mockStream: grpcMock.NoMockClientStream, + mockStream: xmock.NoMockClientStream, output: grpctest.Item{}, expectedError: `not a pointer: grpctest.Item`, expectedOutput: grpctest.Item{}, }, { scenario: "output is not a slice", - mockStream: grpcMock.NoMockClientStream, + mockStream: xmock.NoMockClientStream, output: &grpctest.Item{}, expectedError: `not a slice: *grpctest.Item`, expectedOutput: &grpctest.Item{}, }, { scenario: "recv error", - mockStream: grpcMock.MockClientStream(func(s *grpcMock.ClientStream) { + mockStream: xmock.MockClientStream(func(s *xmock.ClientStream) { s.On("RecvMsg", mock.Anything). Return(errors.New("recv error")) }), @@ -589,7 +589,7 @@ func TestRecvAll(t *testing.T) { }, { scenario: "success with a slice of struct", - mockStream: grpcMock.MockClientStream(sendItems), + mockStream: xmock.MockClientStream(sendItems), output: &[]grpctest.Item{}, expectedOutput: &[]grpctest.Item{ { @@ -606,7 +606,7 @@ func TestRecvAll(t *testing.T) { }, { scenario: "success with a slice of pointer", - mockStream: grpcMock.MockClientStream(sendItems), + mockStream: xmock.MockClientStream(sendItems), output: &[]*grpctest.Item{}, expectedOutput: &[]*grpctest.Item{ { @@ -631,7 +631,7 @@ func TestRecvAll(t *testing.T) { result := tc.output err := grpcmock.RecvAll(result)(tc.mockStream(t)) - grpcAssert.JSONEq(t, tc.expectedOutput, result) + xassert.JSONEq(t, tc.expectedOutput, result) if tc.expectedError == "" { assert.NoError(t, err) @@ -645,7 +645,7 @@ func TestRecvAll(t *testing.T) { func TestSendAndRecvAll_SendError(t *testing.T) { t.Parallel() - stream := grpcMock.MockClientStream(func(s *grpcMock.ClientStream) { + stream := xmock.MockClientStream(func(s *xmock.ClientStream) { s.On("RecvMsg", mock.Anything).Maybe(). Return(io.EOF) @@ -664,7 +664,7 @@ func TestSendAndRecvAll_SendError(t *testing.T) { func TestSendAndRecvAll_RecvError(t *testing.T) { t.Parallel() - stream := grpcMock.MockClientStream(func(s *grpcMock.ClientStream) { + stream := xmock.MockClientStream(func(s *xmock.ClientStream) { s.On("RecvMsg", mock.Anything). Return(errors.New("recv error")) @@ -683,7 +683,7 @@ func TestSendAndRecvAll_RecvError(t *testing.T) { func TestSendAndRecvAll_CloseSendError(t *testing.T) { t.Parallel() - stream := grpcMock.MockClientStream(func(s *grpcMock.ClientStream) { + stream := xmock.MockClientStream(func(s *xmock.ClientStream) { s.On("RecvMsg", mock.Anything).Maybe(). Return(io.EOF) @@ -704,13 +704,13 @@ func TestSendAndRecvAll_Success(t *testing.T) { testCases := []struct { scenario string - mockStream grpcMock.ClientStreamMocker + mockStream xmock.ClientStreamMocker input []*grpctest.Item expectedResult []*grpctest.Item }{ { scenario: "send zero and receive zero", - mockStream: grpcMock.MockClientStream(func(s *grpcMock.ClientStream) { + mockStream: xmock.MockClientStream(func(s *xmock.ClientStream) { s.On("RecvMsg", mock.Anything). Return(io.EOF) @@ -721,7 +721,7 @@ func TestSendAndRecvAll_Success(t *testing.T) { }, { scenario: "send one and receive zero", - mockStream: grpcMock.MockClientStream(func(s *grpcMock.ClientStream) { + mockStream: xmock.MockClientStream(func(s *xmock.ClientStream) { s.On("RecvMsg", mock.Anything). Return(io.EOF) @@ -736,7 +736,7 @@ func TestSendAndRecvAll_Success(t *testing.T) { }, { scenario: "send zero and receive one", - mockStream: grpcMock.MockClientStream(func(s *grpcMock.ClientStream) { + mockStream: xmock.MockClientStream(func(s *xmock.ClientStream) { s.On("RecvMsg", mock.Anything).Once(). Run(func(args mock.Arguments) { out := args.Get(0).(*grpctest.Item) // nolint: errcheck @@ -767,7 +767,7 @@ func TestSendAndRecvAll_Success(t *testing.T) { assert.Equal(t, len(tc.expectedResult), len(result)) for i := 0; i < len(tc.expectedResult); i++ { - grpcAssert.EqualMessage(t, tc.expectedResult[i], result[i]) + xassert.EqualMessage(t, tc.expectedResult[i], result[i]) } }) } diff --git a/format/format.go b/format/format.go index d3e7f19..ff76482 100644 --- a/format/format.go +++ b/format/format.go @@ -7,22 +7,22 @@ import ( "go.nhat.io/matcher/v2" - grpcMatcher "github.com/nhatthm/grpcmock/matcher" - "github.com/nhatthm/grpcmock/must" - grpcReflect "github.com/nhatthm/grpcmock/reflect" - "github.com/nhatthm/grpcmock/service" - "github.com/nhatthm/grpcmock/value" + xmatcher "go.nhat.io/grpcmock/matcher" + "go.nhat.io/grpcmock/must" + xreflect "go.nhat.io/grpcmock/reflect" + "go.nhat.io/grpcmock/service" + "go.nhat.io/grpcmock/value" ) const indent = " " // ExpectedRequest formats an expected request. -func ExpectedRequest(w io.Writer, svc service.Method, header grpcMatcher.HeaderMatcher, payload matcher.Matcher) { +func ExpectedRequest(w io.Writer, svc service.Method, header xmatcher.HeaderMatcher, payload matcher.Matcher) { ExpectedRequestTimes(w, svc, header, payload, 0, 0) } // ExpectedRequestTimes formats an expected request with total and remaining calls. -func ExpectedRequestTimes(w io.Writer, svc service.Method, header grpcMatcher.HeaderMatcher, payload matcher.Matcher, totalCalls, remainingCalls int) { +func ExpectedRequestTimes(w io.Writer, svc service.Method, header xmatcher.HeaderMatcher, payload matcher.Matcher, totalCalls, remainingCalls int) { expectedHeader := map[string]interface{}(nil) if header != nil { @@ -77,7 +77,7 @@ func formatRequestTimes(w io.Writer, svc service.Method, header map[string]inter } } - if !grpcReflect.IsNil(payload) { + if !xreflect.IsNil(payload) { bodyStr := formatValue(payload) if bodyStr != "" { @@ -94,7 +94,7 @@ func formatValueInline(v interface{}) string { switch m := v.(type) { case matcher.ExactMatcher, - grpcMatcher.FnMatcher, + xmatcher.FnMatcher, []byte, string: return formatValue(v) @@ -102,7 +102,7 @@ func formatValueInline(v interface{}) string { case matcher.Callback: return formatValueInline(m.Matcher()) - case *grpcMatcher.PayloadMatcher: + case *xmatcher.PayloadMatcher: if m == nil { return "" } @@ -113,7 +113,7 @@ func formatValueInline(v interface{}) string { return fmt.Sprintf("%T(%q)", v, m.Expected()) default: - if grpcReflect.IsNil(v) { + if xreflect.IsNil(v) { return "" } @@ -122,13 +122,13 @@ func formatValueInline(v interface{}) string { } func formatType(v interface{}) string { - if grpcReflect.IsNil(v) { + if xreflect.IsNil(v) { return "" } switch m := v.(type) { case matcher.ExactMatcher, - grpcMatcher.FnMatcher, + xmatcher.FnMatcher, []byte, string: return "" @@ -136,7 +136,7 @@ func formatType(v interface{}) string { case matcher.Callback: return formatType(m.Matcher()) - case *grpcMatcher.PayloadMatcher: + case *xmatcher.PayloadMatcher: return formatType(m.Matcher()) default: @@ -160,14 +160,14 @@ func formatValue(v interface{}) string { case matcher.Callback: return formatValue(m.Matcher()) - case grpcMatcher.FnMatcher: + case xmatcher.FnMatcher: if e := m.Expected(); e != "" { return e } return "matches custom expectation" - case *grpcMatcher.PayloadMatcher: + case *xmatcher.PayloadMatcher: if m == nil { return "" } @@ -178,7 +178,7 @@ func formatValue(v interface{}) string { return m.Expected() default: - if grpcReflect.IsNil(v) { + if xreflect.IsNil(v) { return "" } diff --git a/format/format_internal_test.go b/format/format_internal_test.go index 075685a..033d1c6 100644 --- a/format/format_internal_test.go +++ b/format/format_internal_test.go @@ -6,8 +6,8 @@ import ( "github.com/stretchr/testify/assert" "go.nhat.io/matcher/v2" - grpcMatcher "github.com/nhatthm/grpcmock/matcher" - "github.com/nhatthm/grpcmock/test/grpctest" + xmatcher "go.nhat.io/grpcmock/matcher" + "go.nhat.io/grpcmock/test/grpctest" ) func TestFormatValueInline(t *testing.T) { @@ -52,18 +52,18 @@ func TestFormatValueInline(t *testing.T) { { scenario: "Random Matcher", input: matcher.Match(func() matcher.Matcher { - return grpcMatcher.Fn("en-US", nil) + return xmatcher.Fn("en-US", nil) }), expected: `en-US`, }, { scenario: "Payload Matcher is nil", - input: (*grpcMatcher.PayloadMatcher)(nil), + input: (*xmatcher.PayloadMatcher)(nil), expected: "", }, { scenario: "Payload Matcher is not nil", - input: grpcMatcher.Payload(matcher.Exact(`expected`), nil), + input: xmatcher.Payload(matcher.Exact(`expected`), nil), expected: "expected", }, } @@ -124,13 +124,13 @@ func TestFormatType(t *testing.T) { }, { scenario: "PayloadMatcher", - input: grpcMatcher.Payload(matcher.JSON(`{"id": 42}`), nil), + input: xmatcher.Payload(matcher.JSON(`{"id": 42}`), nil), expected: ` using matcher.JSONMatcher`, }, { scenario: "Random Matcher", input: matcher.Match(func() matcher.Matcher { - return grpcMatcher.Fn("en-US", nil) + return xmatcher.Fn("en-US", nil) }), }, } @@ -194,25 +194,25 @@ func TestFormatValue(t *testing.T) { { scenario: "Random Matcher", input: matcher.Match(func() matcher.Matcher { - return grpcMatcher.Fn("en-US", nil) + return xmatcher.Fn("en-US", nil) }), expected: `en-US`, }, { scenario: "Random Matcher without expectation", input: matcher.Match(func() matcher.Matcher { - return grpcMatcher.Fn("", nil) + return xmatcher.Fn("", nil) }), expected: `matches custom expectation`, }, { scenario: "Payload Matcher is nil", - input: (*grpcMatcher.PayloadMatcher)(nil), + input: (*xmatcher.PayloadMatcher)(nil), expected: "", }, { scenario: "Payload Matcher is not nil", - input: grpcMatcher.Payload(matcher.Exact(`expected`), nil), + input: xmatcher.Payload(matcher.Exact(`expected`), nil), expected: "expected", }, } diff --git a/format/format_test.go b/format/format_test.go index 2a27bc8..db2bc28 100644 --- a/format/format_test.go +++ b/format/format_test.go @@ -8,9 +8,9 @@ import ( "github.com/stretchr/testify/assert" "go.nhat.io/matcher/v2" - "github.com/nhatthm/grpcmock/format" - grpcMatcher "github.com/nhatthm/grpcmock/matcher" - "github.com/nhatthm/grpcmock/test" + "go.nhat.io/grpcmock/format" + xmatcher "go.nhat.io/grpcmock/matcher" + "go.nhat.io/grpcmock/test" ) func TestExpectedRequest(t *testing.T) { @@ -18,11 +18,11 @@ func TestExpectedRequest(t *testing.T) { buf := new(bytes.Buffer) - header := grpcMatcher.HeaderMatcher{ + header := xmatcher.HeaderMatcher{ "Authorization": matcher.Match(`Bearer token`), } - payload := grpcMatcher.Payload(matcher.JSON(`{"id": 42}`), nil) + payload := xmatcher.Payload(matcher.JSON(`{"id": 42}`), nil) format.ExpectedRequest(buf, test.GetItemsSvc(), header, payload) @@ -101,11 +101,11 @@ func TestExpectedRequestTimes(t *testing.T) { buf := new(bytes.Buffer) - header := grpcMatcher.HeaderMatcher{ + header := xmatcher.HeaderMatcher{ "Authorization": matcher.Match(`Bearer token`), } - payload := grpcMatcher.Payload(matcher.JSON(`{"id": 42}`), nil) + payload := xmatcher.Payload(matcher.JSON(`{"id": 42}`), nil) format.ExpectedRequestTimes(buf, test.GetItemsSvc(), header, payload, tc.totalCalls, tc.remainingCalls) diff --git a/go.mod b/go.mod index 58c3129..de8f53b 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/nhatthm/grpcmock +module go.nhat.io/grpcmock go 1.17 diff --git a/invoker/invoker.go b/invoker/invoker.go index ee86037..f9848b1 100644 --- a/invoker/invoker.go +++ b/invoker/invoker.go @@ -6,8 +6,8 @@ import ( "strings" "time" - "github.com/nhatthm/grpcmock" - "github.com/nhatthm/grpcmock/service" + "go.nhat.io/grpcmock" + "go.nhat.io/grpcmock/service" ) const noTimeout time.Duration = 0 diff --git a/invoker/invoker_test.go b/invoker/invoker_test.go index 9a19725..edd2ed7 100644 --- a/invoker/invoker_test.go +++ b/invoker/invoker_test.go @@ -15,12 +15,12 @@ import ( "google.golang.org/grpc/status" "google.golang.org/grpc/test/bufconn" - "github.com/nhatthm/grpcmock" - grpcAssert "github.com/nhatthm/grpcmock/assert" - "github.com/nhatthm/grpcmock/invoker" - "github.com/nhatthm/grpcmock/service" - "github.com/nhatthm/grpcmock/test" - "github.com/nhatthm/grpcmock/test/grpctest" + "go.nhat.io/grpcmock" + xassert "go.nhat.io/grpcmock/assert" + "go.nhat.io/grpcmock/invoker" + "go.nhat.io/grpcmock/service" + "go.nhat.io/grpcmock/test" + "go.nhat.io/grpcmock/test/grpctest" ) func TestInvoker_Invoke_Unary_Unimplemented(t *testing.T) { @@ -87,8 +87,8 @@ func TestInvoker_Invoke_Unary_Success(t *testing.T) { Name: "Foobar", } - grpcAssert.EqualMessage(t, expectedRequest, actualRequest) - grpcAssert.EqualMessage(t, expectedResponse, actualResponse) + xassert.EqualMessage(t, expectedRequest, actualRequest) + xassert.EqualMessage(t, expectedResponse, actualResponse) assert.NoError(t, err) } @@ -151,7 +151,7 @@ func TestInvoker_Invoke_ServerStream_Success(t *testing.T) { assert.Equal(t, len(expected), len(result)) for i := 0; i < len(expected); i++ { - grpcAssert.EqualMessage(t, expected[i], result[i]) + xassert.EqualMessage(t, expected[i], result[i]) } } @@ -201,12 +201,12 @@ func TestInvoker_Invoke_ClientStream_Success(t *testing.T) { expectedResult := &grpctest.CreateItemsResponse{NumItems: int64(len(items))} - grpcAssert.EqualMessage(t, expectedResult, result) + xassert.EqualMessage(t, expectedResult, result) assert.NoError(t, err) assert.Equal(t, len(received), len(items)) for i := 0; i < len(received); i++ { - grpcAssert.EqualMessage(t, received[i], items[i]) + xassert.EqualMessage(t, received[i], items[i]) } } @@ -270,7 +270,7 @@ func TestInvoker_Invoke_BidirectionalStream_Success(t *testing.T) { assert.Equal(t, len(expected), len(result)) for i := 0; i < len(expected); i++ { - grpcAssert.EqualMessage(t, expected[i], result[i]) + xassert.EqualMessage(t, expected[i], result[i]) } } diff --git a/invoker/options.go b/invoker/options.go index d1d2762..88996fc 100644 --- a/invoker/options.go +++ b/invoker/options.go @@ -6,7 +6,7 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/test/bufconn" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" ) // WithAddress sets server address. diff --git a/listerner.go b/listerner.go index 2a08766..8d7b89f 100644 --- a/listerner.go +++ b/listerner.go @@ -4,7 +4,7 @@ import ( "net" "sync" - "github.com/nhatthm/grpcmock/must" + "go.nhat.io/grpcmock/must" ) var _ net.Listener = (*listener)(nil) diff --git a/matcher/fn_test.go b/matcher/fn_test.go index 6c50ea8..edffbb9 100644 --- a/matcher/fn_test.go +++ b/matcher/fn_test.go @@ -6,7 +6,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/nhatthm/grpcmock/matcher" + "go.nhat.io/grpcmock/matcher" ) func TestFn(t *testing.T) { diff --git a/matcher/header_test.go b/matcher/header_test.go index 550d42a..e2b924f 100644 --- a/matcher/header_test.go +++ b/matcher/header_test.go @@ -10,7 +10,7 @@ import ( "go.nhat.io/matcher/v2" "google.golang.org/grpc/metadata" - grpcMatcher "github.com/nhatthm/grpcmock/matcher" + xmatcher "go.nhat.io/grpcmock/matcher" ) func TestHeaderMatcher_Match(t *testing.T) { @@ -18,7 +18,7 @@ func TestHeaderMatcher_Match(t *testing.T) { testCases := []struct { scenario string - matcher grpcMatcher.HeaderMatcher + matcher xmatcher.HeaderMatcher header map[string]string expectedError string }{ @@ -27,12 +27,12 @@ func TestHeaderMatcher_Match(t *testing.T) { }, { scenario: "empty", - matcher: grpcMatcher.HeaderMatcher{}, + matcher: xmatcher.HeaderMatcher{}, }, { scenario: "match error", - matcher: grpcMatcher.HeaderMatcher{ - "Authorization": grpcMatcher.Fn("", func(interface{}) (bool, error) { + matcher: xmatcher.HeaderMatcher{ + "Authorization": xmatcher.Fn("", func(interface{}) (bool, error) { return false, errors.New("match error") }), }, @@ -40,7 +40,7 @@ func TestHeaderMatcher_Match(t *testing.T) { }, { scenario: "mismatched", - matcher: grpcMatcher.HeaderMatcher{ + matcher: xmatcher.HeaderMatcher{ "Authorization": matcher.Match("Bearer token"), }, header: map[string]string{ @@ -50,7 +50,7 @@ func TestHeaderMatcher_Match(t *testing.T) { }, { scenario: "matched", - matcher: grpcMatcher.HeaderMatcher{ + matcher: xmatcher.HeaderMatcher{ "Authorization": matcher.Match(regexp.MustCompile(`Bearer .*`)), }, header: map[string]string{ diff --git a/matcher/payload.go b/matcher/payload.go index 4b954e6..e4af80b 100644 --- a/matcher/payload.go +++ b/matcher/payload.go @@ -3,7 +3,7 @@ package matcher import ( "go.nhat.io/matcher/v2" - "github.com/nhatthm/grpcmock/value" + "go.nhat.io/grpcmock/value" ) const initActual = "" diff --git a/matcher/payload_test.go b/matcher/payload_test.go index 8a3b0c8..9698885 100644 --- a/matcher/payload_test.go +++ b/matcher/payload_test.go @@ -9,7 +9,7 @@ import ( "github.com/stretchr/testify/assert" "go.nhat.io/matcher/v2" - srvMatcher "github.com/nhatthm/grpcmock/matcher" + xmatcher "go.nhat.io/grpcmock/matcher" ) func TestPayloadMatcher_Match(t *testing.T) { @@ -77,7 +77,7 @@ func TestPayloadMatcher_Match(t *testing.T) { t.Run(tc.scenario, func(t *testing.T) { t.Parallel() - m := srvMatcher.Payload(tc.matcher, tc.decoder) + m := xmatcher.Payload(tc.matcher, tc.decoder) matched, err := m.Match(tc.payload) assert.Equal(t, tc.expectedResult, matched) @@ -91,7 +91,7 @@ func TestBodyMatcher_Matcher(t *testing.T) { t.Parallel() expected := `payload` - m := srvMatcher.Payload(matcher.Match(expected), nil) + m := xmatcher.Payload(matcher.Match(expected), nil) assert.Equal(t, matcher.Match(expected), m.Matcher()) assert.Equal(t, expected, m.Expected()) diff --git a/matcher_stream.go b/matcher_stream.go index 6f367d6..95ebadc 100644 --- a/matcher_stream.go +++ b/matcher_stream.go @@ -4,7 +4,7 @@ import ( "fmt" "reflect" - "github.com/nhatthm/grpcmock/matcher" + "go.nhat.io/grpcmock/matcher" ) // MatchClientStreamMsgCount matches a number of messages. diff --git a/matcher_stream_internal_test.go b/matcher_stream_internal_test.go index c713103..eb8a94c 100644 --- a/matcher_stream_internal_test.go +++ b/matcher_stream_internal_test.go @@ -5,7 +5,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/nhatthm/grpcmock/test/grpctest" + "go.nhat.io/grpcmock/test/grpctest" ) func TestMatchClientStreamMsgCount(t *testing.T) { diff --git a/mock/planner/planner.go b/mock/planner/planner.go index 3f19ff9..95155ec 100644 --- a/mock/planner/planner.go +++ b/mock/planner/planner.go @@ -7,9 +7,9 @@ import ( "github.com/stretchr/testify/assert" tMock "github.com/stretchr/testify/mock" - "github.com/nhatthm/grpcmock/planner" - "github.com/nhatthm/grpcmock/request" - "github.com/nhatthm/grpcmock/service" + "go.nhat.io/grpcmock/planner" + "go.nhat.io/grpcmock/request" + "go.nhat.io/grpcmock/service" ) // Mocker is Planner mocker. diff --git a/mock/planner/planner_test.go b/mock/planner/planner_test.go index fa99fc6..e54ffe9 100644 --- a/mock/planner/planner_test.go +++ b/mock/planner/planner_test.go @@ -8,10 +8,10 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" - "github.com/nhatthm/grpcmock/mock/planner" - "github.com/nhatthm/grpcmock/request" - "github.com/nhatthm/grpcmock/service" - "github.com/nhatthm/grpcmock/test" + "go.nhat.io/grpcmock/mock/planner" + "go.nhat.io/grpcmock/request" + "go.nhat.io/grpcmock/service" + "go.nhat.io/grpcmock/test" ) func TestPlanner_IsEmpty(t *testing.T) { diff --git a/mock_example_test.go b/mock_example_test.go index 42078a0..71042fe 100644 --- a/mock_example_test.go +++ b/mock_example_test.go @@ -7,9 +7,9 @@ import ( "github.com/stretchr/testify/require" - "github.com/nhatthm/grpcmock" - grpcAssert "github.com/nhatthm/grpcmock/assert" - "github.com/nhatthm/grpcmock/test/grpctest" + "go.nhat.io/grpcmock" + xassert "go.nhat.io/grpcmock/assert" + "go.nhat.io/grpcmock/test/grpctest" ) func ExampleMockServer() { @@ -51,7 +51,7 @@ func ExampleMockServer() { } require.NoError(t, err) - grpcAssert.EqualMessage(t, expected, out) + xassert.EqualMessage(t, expected, out) output, err := json.MarshalIndent(out, "", " ") require.NoError(t, err) @@ -109,7 +109,7 @@ func ExampleMockServerWithBufConn() { } require.NoError(t, err) - grpcAssert.EqualMessage(t, expected, out) + xassert.EqualMessage(t, expected, out) output, err := json.MarshalIndent(out, "", " ") require.NoError(t, err) diff --git a/must/must_test.go b/must/must_test.go index b8bc3f5..0ab95e4 100644 --- a/must/must_test.go +++ b/must/must_test.go @@ -6,7 +6,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/nhatthm/grpcmock/must" + "go.nhat.io/grpcmock/must" ) func TestNotFail(t *testing.T) { diff --git a/planner/error.go b/planner/error.go index a3e26c5..1ab029d 100644 --- a/planner/error.go +++ b/planner/error.go @@ -10,10 +10,10 @@ import ( "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" - "github.com/nhatthm/grpcmock/format" - "github.com/nhatthm/grpcmock/request" - "github.com/nhatthm/grpcmock/service" - "github.com/nhatthm/grpcmock/value" + "go.nhat.io/grpcmock/format" + "go.nhat.io/grpcmock/request" + "go.nhat.io/grpcmock/service" + "go.nhat.io/grpcmock/value" ) // Error represents an error that occurs while matching a request. diff --git a/planner/error_test.go b/planner/error_test.go index bf55129..6cda039 100644 --- a/planner/error_test.go +++ b/planner/error_test.go @@ -5,9 +5,9 @@ import ( "github.com/stretchr/testify/assert" - "github.com/nhatthm/grpcmock/planner" - "github.com/nhatthm/grpcmock/test" - "github.com/nhatthm/grpcmock/test/grpctest" + "go.nhat.io/grpcmock/planner" + "go.nhat.io/grpcmock/test" + "go.nhat.io/grpcmock/test/grpctest" ) func TestUnexpectedRequestError_Unary(t *testing.T) { diff --git a/planner/first_match.go b/planner/first_match.go index 4157c30..8c4d7be 100644 --- a/planner/first_match.go +++ b/planner/first_match.go @@ -4,8 +4,8 @@ import ( "context" "sync" - "github.com/nhatthm/grpcmock/request" - "github.com/nhatthm/grpcmock/service" + "go.nhat.io/grpcmock/request" + "go.nhat.io/grpcmock/service" ) var _ Planner = (*firstMatch)(nil) diff --git a/planner/first_match_test.go b/planner/first_match_test.go index ab4451d..7c01cd4 100644 --- a/planner/first_match_test.go +++ b/planner/first_match_test.go @@ -6,10 +6,10 @@ import ( "github.com/stretchr/testify/assert" - "github.com/nhatthm/grpcmock/planner" - "github.com/nhatthm/grpcmock/request" - "github.com/nhatthm/grpcmock/test" - "github.com/nhatthm/grpcmock/test/grpctest" + "go.nhat.io/grpcmock/planner" + "go.nhat.io/grpcmock/request" + "go.nhat.io/grpcmock/test" + "go.nhat.io/grpcmock/test/grpctest" ) func TestFirstMatch_Plan_Unary_Error(t *testing.T) { diff --git a/planner/helper.go b/planner/helper.go index 41ca4ad..5667f10 100644 --- a/planner/helper.go +++ b/planner/helper.go @@ -3,7 +3,7 @@ package planner import ( "fmt" - "github.com/nhatthm/grpcmock/request" + "go.nhat.io/grpcmock/request" ) func recovered(v interface{}) string { diff --git a/planner/helper_internal_test.go b/planner/helper_internal_test.go index 3a7d59f..73cadb7 100644 --- a/planner/helper_internal_test.go +++ b/planner/helper_internal_test.go @@ -6,7 +6,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/nhatthm/grpcmock/request" + "go.nhat.io/grpcmock/request" ) func TestRecovered(t *testing.T) { diff --git a/planner/matcher.go b/planner/matcher.go index 64e7422..925173d 100644 --- a/planner/matcher.go +++ b/planner/matcher.go @@ -3,8 +3,8 @@ package planner import ( "context" - "github.com/nhatthm/grpcmock/request" - "github.com/nhatthm/grpcmock/service" + "go.nhat.io/grpcmock/request" + "go.nhat.io/grpcmock/service" ) // MatchRequest checks whether a request is matched. diff --git a/planner/matcher_test.go b/planner/matcher_test.go index d51d313..ae2b7a8 100644 --- a/planner/matcher_test.go +++ b/planner/matcher_test.go @@ -11,14 +11,14 @@ import ( "google.golang.org/grpc/metadata" "google.golang.org/protobuf/proto" - "github.com/nhatthm/grpcmock/matcher" - grpcMock "github.com/nhatthm/grpcmock/mock/grpc" - "github.com/nhatthm/grpcmock/planner" - "github.com/nhatthm/grpcmock/request" - "github.com/nhatthm/grpcmock/service" - "github.com/nhatthm/grpcmock/streamer" - "github.com/nhatthm/grpcmock/test" - "github.com/nhatthm/grpcmock/test/grpctest" + "go.nhat.io/grpcmock/matcher" + xmock "go.nhat.io/grpcmock/mock/grpc" + "go.nhat.io/grpcmock/planner" + "go.nhat.io/grpcmock/request" + "go.nhat.io/grpcmock/service" + "go.nhat.io/grpcmock/streamer" + "go.nhat.io/grpcmock/test" + "go.nhat.io/grpcmock/test/grpctest" ) func TestMatchService(t *testing.T) { @@ -241,7 +241,7 @@ Error: header "locale" with value "en-US" expected, "en-CA" received mockRequest: func(r *request.ClientStreamRequest) { r.WithHeader("locale", "en-US") }, - mockStreamer: test.MockCreateItemsStreamer(func(s *grpcMock.ServerStream) { + mockStreamer: test.MockCreateItemsStreamer(func(s *xmock.ServerStream) { s.On("RecvMsg", mock.Anything). Return(errors.New("read error")) }), @@ -588,7 +588,7 @@ func TestMatchPayload_ClientStream(t *testing.T) { mockRequest: func(r *request.ClientStreamRequest) { r.WithPayload(`[{"id":42}]`) }, - mockStreamer: test.MockCreateItemsStreamer(func(s *grpcMock.ServerStream) { + mockStreamer: test.MockCreateItemsStreamer(func(s *xmock.ServerStream) { s.On("RecvMsg", mock.Anything). Return(errors.New("recv error")) }), @@ -751,7 +751,7 @@ Error: payload does not match expectation, received: {} func TestMatchPayload_Panic(t *testing.T) { t.Parallel() - s := test.MockCreateItemsStreamer(func(s *grpcMock.ServerStream) { + s := test.MockCreateItemsStreamer(func(s *xmock.ServerStream) { s.On("RecvMsg", mock.Anything). Panic("recv panic") })(t) @@ -774,7 +774,7 @@ Error: could not match payload: recv panic var noMockCreateItemsStream = test.MockCreateItemsStreamer() func mockCreateItemsStreamer() func(t *testing.T) *streamer.ClientStreamer { - return test.MockCreateItemsStreamer(func(s *grpcMock.ServerStream) { + return test.MockCreateItemsStreamer(func(s *xmock.ServerStream) { s.On("RecvMsg", &grpctest.Item{}).Once(). Run(func(args mock.Arguments) { item := args.Get(0).(*grpctest.Item) // nolint: errcheck diff --git a/planner/planner.go b/planner/planner.go index 2c2408e..c6ad0a8 100644 --- a/planner/planner.go +++ b/planner/planner.go @@ -3,8 +3,8 @@ package planner import ( "context" - "github.com/nhatthm/grpcmock/request" - "github.com/nhatthm/grpcmock/service" + "go.nhat.io/grpcmock/request" + "go.nhat.io/grpcmock/service" ) // Planner or Request Execution Planner is in charge of selecting the right expectation for a given request. diff --git a/planner/planner_test.go b/planner/planner_test.go index dd27ead..8c3521e 100644 --- a/planner/planner_test.go +++ b/planner/planner_test.go @@ -6,8 +6,8 @@ import ( "google.golang.org/grpc/metadata" - "github.com/nhatthm/grpcmock/request" - "github.com/nhatthm/grpcmock/test" + "go.nhat.io/grpcmock/request" + "go.nhat.io/grpcmock/test" ) func expectGetItems() *request.UnaryRequest { diff --git a/planner/request_test.go b/planner/request_test.go index 899b568..e500231 100644 --- a/planner/request_test.go +++ b/planner/request_test.go @@ -1,7 +1,7 @@ package planner import ( - "github.com/nhatthm/grpcmock/request" + "go.nhat.io/grpcmock/request" ) func newUnaryRequestWithTimes(i request.RepeatedTime) *request.UnaryRequest { diff --git a/planner/sequence.go b/planner/sequence.go index 95b28df..53c6891 100644 --- a/planner/sequence.go +++ b/planner/sequence.go @@ -4,8 +4,8 @@ import ( "context" "sync" - "github.com/nhatthm/grpcmock/request" - "github.com/nhatthm/grpcmock/service" + "go.nhat.io/grpcmock/request" + "go.nhat.io/grpcmock/service" ) var _ Planner = (*sequence)(nil) diff --git a/planner/sequence_internal_test.go b/planner/sequence_internal_test.go index 7269b81..656015a 100644 --- a/planner/sequence_internal_test.go +++ b/planner/sequence_internal_test.go @@ -5,7 +5,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/nhatthm/grpcmock/request" + "go.nhat.io/grpcmock/request" ) func TestNextExpectations(t *testing.T) { diff --git a/planner/sequence_test.go b/planner/sequence_test.go index 100f8f6..6c2fd14 100644 --- a/planner/sequence_test.go +++ b/planner/sequence_test.go @@ -7,10 +7,10 @@ import ( "github.com/stretchr/testify/assert" "google.golang.org/grpc/metadata" - "github.com/nhatthm/grpcmock/planner" - "github.com/nhatthm/grpcmock/service" - "github.com/nhatthm/grpcmock/test" - "github.com/nhatthm/grpcmock/test/grpctest" + "go.nhat.io/grpcmock/planner" + "go.nhat.io/grpcmock/service" + "go.nhat.io/grpcmock/test" + "go.nhat.io/grpcmock/test/grpctest" ) func TestSequence(t *testing.T) { diff --git a/reflect/reflect_test.go b/reflect/reflect_test.go index 7a5ecd0..a51875b 100644 --- a/reflect/reflect_test.go +++ b/reflect/reflect_test.go @@ -8,8 +8,8 @@ import ( "github.com/stretchr/testify/assert" "google.golang.org/grpc" - grpcReflect "github.com/nhatthm/grpcmock/reflect" - "github.com/nhatthm/grpcmock/test/grpctest" + xreflect "go.nhat.io/grpcmock/reflect" + "go.nhat.io/grpcmock/test/grpctest" ) type testServer interface { @@ -40,17 +40,17 @@ func TestBuildServiceMethods(t *testing.T) { testCases := []struct { scenario string service interface{} - expected []grpcReflect.ServiceMethod + expected []xreflect.ServiceMethod }{ { scenario: "no method", service: struct{}{}, - expected: []grpcReflect.ServiceMethod{}, + expected: []xreflect.ServiceMethod{}, }, { scenario: "interface with multiple cases", service: (*testServer)(nil), - expected: []grpcReflect.ServiceMethod{ + expected: []xreflect.ServiceMethod{ { Name: "GetItem", Input: &getItemRequest{}, @@ -61,7 +61,7 @@ func TestBuildServiceMethods(t *testing.T) { { scenario: "generated server", service: (*grpctest.ItemServiceServer)(nil), - expected: []grpcReflect.ServiceMethod{ + expected: []xreflect.ServiceMethod{ { Name: "CreateItems", Input: &grpctest.Item{}, @@ -95,7 +95,7 @@ func TestBuildServiceMethods(t *testing.T) { t.Run(tc.scenario, func(t *testing.T) { t.Parallel() - actual := grpcReflect.FindServiceMethods(tc.service) + actual := xreflect.FindServiceMethods(tc.service) assert.Equal(t, tc.expected, actual) }) @@ -140,7 +140,7 @@ func TestIsNil(t *testing.T) { t.Run(tc.scenario, func(t *testing.T) { t.Parallel() - assert.Equal(t, tc.expected, grpcReflect.IsNil(tc.input)) + assert.Equal(t, tc.expected, xreflect.IsNil(tc.input)) }) } } @@ -185,7 +185,7 @@ func TestIsPtr(t *testing.T) { t.Run(tc.scenario, func(t *testing.T) { t.Parallel() - assert.Equal(t, tc.expected, grpcReflect.IsPtr(tc.input)) + assert.Equal(t, tc.expected, xreflect.IsPtr(tc.input)) }) } } @@ -224,7 +224,7 @@ func TestUnwrapType(t *testing.T) { t.Run(tc.scenario, func(t *testing.T) { t.Parallel() - assert.Equal(t, expected, grpcReflect.UnwrapType(tc.input)) + assert.Equal(t, expected, xreflect.UnwrapType(tc.input)) }) } } @@ -263,7 +263,7 @@ func TestUnwrapValue(t *testing.T) { t.Run(tc.scenario, func(t *testing.T) { t.Parallel() - assert.Equal(t, expected.Interface(), grpcReflect.UnwrapValue(tc.input).Interface()) + assert.Equal(t, expected.Interface(), xreflect.UnwrapValue(tc.input).Interface()) }) } } @@ -271,7 +271,7 @@ func TestUnwrapValue(t *testing.T) { func TestNew(t *testing.T) { t.Parallel() - actual := grpcReflect.New(&grpctest.Item{}) + actual := xreflect.New(&grpctest.Item{}) expected := &grpctest.Item{} assert.Equal(t, expected, actual) @@ -280,7 +280,7 @@ func TestNew(t *testing.T) { func TestNewZero(t *testing.T) { t.Parallel() - actual := grpcReflect.NewZero(&grpctest.Item{}) + actual := xreflect.NewZero(&grpctest.Item{}) expected := (*grpctest.Item)(nil) assert.Equal(t, expected, actual) @@ -291,7 +291,7 @@ func TestNewValue(t *testing.T) { item := &grpctest.Item{Id: 42} - actual := grpcReflect.NewValue(item) + actual := xreflect.NewValue(item) expected := &grpctest.Item{Id: 42} assert.Equal(t, expected, actual) @@ -327,7 +327,7 @@ func TestNewSlicePre(t *testing.T) { t.Run(tc.scenario, func(t *testing.T) { t.Parallel() - actual := grpcReflect.NewSlicePtr(tc.v) + actual := xreflect.NewSlicePtr(tc.v) expected := &[]*grpctest.Item{} assert.Equal(t, expected, actual) @@ -385,7 +385,7 @@ func TestSetPtrValue(t *testing.T) { run := func() { result := tc.dst - grpcReflect.SetPtrValue(result, tc.value) + xreflect.SetPtrValue(result, tc.value) assert.Equal(t, tc.expectedResult, result) } @@ -426,7 +426,7 @@ func TestPtrValue(t *testing.T) { t.Run(tc.scenario, func(t *testing.T) { t.Parallel() - actual := grpcReflect.PtrValue(tc.value) + actual := xreflect.PtrValue(tc.value) assert.Equal(t, tc.expected, actual) }) @@ -437,7 +437,7 @@ func TestPtrValue_Panic(t *testing.T) { t.Parallel() assert.Panics(t, func() { - grpcReflect.PtrValue(nil) + xreflect.PtrValue(nil) }) } @@ -500,10 +500,10 @@ func TestParseRegisterFunc(t *testing.T) { if tc.expectedError != "" { assert.PanicsWithError(t, tc.expectedError, func() { - grpcReflect.ParseRegisterFunc(tc.input) + xreflect.ParseRegisterFunc(tc.input) }) } else { - serviceDesc, instance := grpcReflect.ParseRegisterFunc(tc.input) + serviceDesc, instance := xreflect.ParseRegisterFunc(tc.input) assert.Equal(t, tc.expectedServiceDesc, serviceDesc) assert.Equal(t, tc.expectedInstance, instance) @@ -549,7 +549,7 @@ func TestUnwrapPtrSliceType(t *testing.T) { t.Run(tc.scenario, func(t *testing.T) { t.Parallel() - result, err := grpcReflect.UnwrapPtrSliceType(tc.input) + result, err := xreflect.UnwrapPtrSliceType(tc.input) assert.Equal(t, tc.expectedResult, result) diff --git a/request/base.go b/request/base.go index a0a56a8..3183b19 100644 --- a/request/base.go +++ b/request/base.go @@ -5,7 +5,7 @@ import ( "github.com/spf13/afero" - "github.com/nhatthm/grpcmock/service" + "go.nhat.io/grpcmock/service" ) type baseRequest struct { diff --git a/request/bidirectional_stream.go b/request/bidirectional_stream.go index 4c07692..ce823d2 100644 --- a/request/bidirectional_stream.go +++ b/request/bidirectional_stream.go @@ -12,10 +12,10 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - grpcErrors "github.com/nhatthm/grpcmock/errors" - grpcMatcher "github.com/nhatthm/grpcmock/matcher" - "github.com/nhatthm/grpcmock/service" - "github.com/nhatthm/grpcmock/streamer" + xerrors "go.nhat.io/grpcmock/errors" + xmatcher "go.nhat.io/grpcmock/matcher" + "go.nhat.io/grpcmock/service" + "go.nhat.io/grpcmock/streamer" ) // BidirectionalStreamRequest represents the expectation for a client-stream request. @@ -32,7 +32,7 @@ type BidirectionalStreamRequest struct { run func(ctx context.Context, s grpc.ServerStream) error // requestHeader is a list of expected headers of the given request. - requestHeader grpcMatcher.HeaderMatcher + requestHeader xmatcher.HeaderMatcher // statusCode is the response code when the request is handled. statusCode codes.Code @@ -64,7 +64,7 @@ func (r *BidirectionalStreamRequest) WithHeader(header string, value interface{} defer r.unlock() if r.requestHeader == nil { - r.requestHeader = grpcMatcher.HeaderMatcher{} + r.requestHeader = xmatcher.HeaderMatcher{} } r.requestHeader[header] = matcher.Match(value) @@ -166,7 +166,7 @@ func (r *BidirectionalStreamRequest) handle(ctx context.Context, in interface{}, return status.Error(r.statusCode, r.statusMessage) } - return grpcErrors.StatusError(r.run(ctx, in.(*streamer.BidirectionalStreamer))) + return xerrors.StatusError(r.run(ctx, in.(*streamer.BidirectionalStreamer))) } // Once indicates that the mock should only return the value once. @@ -260,10 +260,10 @@ func (r *BidirectionalStreamRequest) After(d time.Duration) *BidirectionalStream return r } -func (r *BidirectionalStreamRequest) headerMatcher() grpcMatcher.HeaderMatcher { +func (r *BidirectionalStreamRequest) headerMatcher() xmatcher.HeaderMatcher { return r.requestHeader } -func (r *BidirectionalStreamRequest) payloadMatcher() *grpcMatcher.PayloadMatcher { +func (r *BidirectionalStreamRequest) payloadMatcher() *xmatcher.PayloadMatcher { return nil } diff --git a/request/bidirectional_stream_internal_test.go b/request/bidirectional_stream_internal_test.go index 2d20850..2c9b370 100644 --- a/request/bidirectional_stream_internal_test.go +++ b/request/bidirectional_stream_internal_test.go @@ -15,10 +15,10 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - grpcMatcher "github.com/nhatthm/grpcmock/matcher" - "github.com/nhatthm/grpcmock/streamer" - "github.com/nhatthm/grpcmock/test" - "github.com/nhatthm/grpcmock/test/grpctest" + xmatcher "go.nhat.io/grpcmock/matcher" + "go.nhat.io/grpcmock/streamer" + "go.nhat.io/grpcmock/test" + "go.nhat.io/grpcmock/test/grpctest" ) func TestBidirectionalStreamRequest_WithHeader(t *testing.T) { @@ -27,11 +27,11 @@ func TestBidirectionalStreamRequest_WithHeader(t *testing.T) { r := newTransformItemsRequest() r.WithHeader("foo", "bar") - assert.Equal(t, grpcMatcher.HeaderMatcher{"foo": matcher.Exact("bar")}, r.requestHeader) + assert.Equal(t, xmatcher.HeaderMatcher{"foo": matcher.Exact("bar")}, r.requestHeader) r.WithHeader("john", "doe") - assert.Equal(t, grpcMatcher.HeaderMatcher{"foo": matcher.Exact("bar"), "john": matcher.Exact("doe")}, r.requestHeader) + assert.Equal(t, xmatcher.HeaderMatcher{"foo": matcher.Exact("bar"), "john": matcher.Exact("doe")}, r.requestHeader) } func TestBidirectionalStreamRequest_WithHeaders(t *testing.T) { @@ -40,11 +40,11 @@ func TestBidirectionalStreamRequest_WithHeaders(t *testing.T) { r := newTransformItemsRequest() r.WithHeaders(map[string]interface{}{"foo": "bar"}) - assert.Equal(t, grpcMatcher.HeaderMatcher{"foo": matcher.Exact("bar")}, r.requestHeader) + assert.Equal(t, xmatcher.HeaderMatcher{"foo": matcher.Exact("bar")}, r.requestHeader) r.WithHeader("john", "doe") - assert.Equal(t, grpcMatcher.HeaderMatcher{"foo": matcher.Exact("bar"), "john": matcher.Exact("doe")}, r.requestHeader) + assert.Equal(t, xmatcher.HeaderMatcher{"foo": matcher.Exact("bar"), "john": matcher.Exact("doe")}, r.requestHeader) } func TestBidirectionalStreamRequest_ReturnCode(t *testing.T) { @@ -401,7 +401,7 @@ func TestBidirectionalStreamRequest_HeaderMatcher(t *testing.T) { r := newTransformItemsRequest().WithHeader("locale", "en-US") actual := HeaderMatcher(r) - expected := grpcMatcher.HeaderMatcher{"locale": matcher.Match("en-US")} + expected := xmatcher.HeaderMatcher{"locale": matcher.Match("en-US")} assert.Equal(t, expected, actual) } diff --git a/request/client_stream.go b/request/client_stream.go index 90d0162..805089f 100644 --- a/request/client_stream.go +++ b/request/client_stream.go @@ -14,13 +14,13 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - grpcErrors "github.com/nhatthm/grpcmock/errors" - grpcMatcher "github.com/nhatthm/grpcmock/matcher" - "github.com/nhatthm/grpcmock/must" - "github.com/nhatthm/grpcmock/reflect" - "github.com/nhatthm/grpcmock/service" - "github.com/nhatthm/grpcmock/streamer" - "github.com/nhatthm/grpcmock/value" + xerrors "go.nhat.io/grpcmock/errors" + xmatcher "go.nhat.io/grpcmock/matcher" + "go.nhat.io/grpcmock/must" + "go.nhat.io/grpcmock/reflect" + "go.nhat.io/grpcmock/service" + "go.nhat.io/grpcmock/streamer" + "go.nhat.io/grpcmock/value" ) // ClientStreamRequest represents the expectation for a client-stream request. @@ -37,9 +37,9 @@ type ClientStreamRequest struct { run func(ctx context.Context, s grpc.ServerStream) (interface{}, error) // requestHeader is a list of expected headers of the given request. - requestHeader grpcMatcher.HeaderMatcher + requestHeader xmatcher.HeaderMatcher // requestPayload is the expected parameters of the given request. - requestPayload *grpcMatcher.PayloadMatcher + requestPayload *xmatcher.PayloadMatcher // statusCode is the response code when the request is handled. statusCode codes.Code @@ -71,7 +71,7 @@ func (r *ClientStreamRequest) WithHeader(header string, value interface{}) *Clie defer r.unlock() if r.requestHeader == nil { - r.requestHeader = grpcMatcher.HeaderMatcher{} + r.requestHeader = xmatcher.HeaderMatcher{} } r.requestHeader[header] = matcher.Match(value) @@ -257,13 +257,13 @@ func (r *ClientStreamRequest) handle(ctx context.Context, in interface{}, out in resp, err := r.run(ctx, stream) if err != nil { - return grpcErrors.StatusError(err) + return xerrors.StatusError(err) } if reflect.UnwrapType(out) == reflect.UnwrapType(resp) { reflect.SetPtrValue(out, resp) - return grpcErrors.StatusError(stream.SendMsg(out)) + return xerrors.StatusError(stream.SendMsg(out)) } switch resp := resp.(type) { @@ -272,7 +272,7 @@ func (r *ClientStreamRequest) handle(ctx context.Context, in interface{}, out in return status.Error(codes.Internal, err.Error()) } - return grpcErrors.StatusError(stream.SendMsg(out)) + return xerrors.StatusError(stream.SendMsg(out)) } return status.Errorf(codes.Internal, "invalid response type, got %T, want %T", resp, out) @@ -357,10 +357,10 @@ func (r *ClientStreamRequest) After(d time.Duration) *ClientStreamRequest { return r } -func (r *ClientStreamRequest) headerMatcher() grpcMatcher.HeaderMatcher { +func (r *ClientStreamRequest) headerMatcher() xmatcher.HeaderMatcher { return r.requestHeader } -func (r *ClientStreamRequest) payloadMatcher() *grpcMatcher.PayloadMatcher { +func (r *ClientStreamRequest) payloadMatcher() *xmatcher.PayloadMatcher { return r.requestPayload } diff --git a/request/client_stream_internal_test.go b/request/client_stream_internal_test.go index f2fa277..00768a3 100644 --- a/request/client_stream_internal_test.go +++ b/request/client_stream_internal_test.go @@ -14,13 +14,13 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - grpcAssert "github.com/nhatthm/grpcmock/assert" - grpcMatcher "github.com/nhatthm/grpcmock/matcher" - grpcMock "github.com/nhatthm/grpcmock/mock/grpc" - "github.com/nhatthm/grpcmock/stream" - "github.com/nhatthm/grpcmock/streamer" - "github.com/nhatthm/grpcmock/test" - "github.com/nhatthm/grpcmock/test/grpctest" + xassert "go.nhat.io/grpcmock/assert" + xmatcher "go.nhat.io/grpcmock/matcher" + xmock "go.nhat.io/grpcmock/mock/grpc" + "go.nhat.io/grpcmock/stream" + "go.nhat.io/grpcmock/streamer" + "go.nhat.io/grpcmock/test" + "go.nhat.io/grpcmock/test/grpctest" ) func TestClientStreamRequest_WithHeader(t *testing.T) { @@ -29,11 +29,11 @@ func TestClientStreamRequest_WithHeader(t *testing.T) { r := newCreateItemsRequest() r.WithHeader("foo", "bar") - assert.Equal(t, grpcMatcher.HeaderMatcher{"foo": matcher.Exact("bar")}, r.requestHeader) + assert.Equal(t, xmatcher.HeaderMatcher{"foo": matcher.Exact("bar")}, r.requestHeader) r.WithHeader("john", "doe") - assert.Equal(t, grpcMatcher.HeaderMatcher{"foo": matcher.Exact("bar"), "john": matcher.Exact("doe")}, r.requestHeader) + assert.Equal(t, xmatcher.HeaderMatcher{"foo": matcher.Exact("bar"), "john": matcher.Exact("doe")}, r.requestHeader) } func TestClientStreamRequest_WithHeaders(t *testing.T) { @@ -42,11 +42,11 @@ func TestClientStreamRequest_WithHeaders(t *testing.T) { r := newCreateItemsRequest() r.WithHeaders(map[string]interface{}{"foo": "bar"}) - assert.Equal(t, grpcMatcher.HeaderMatcher{"foo": matcher.Exact("bar")}, r.requestHeader) + assert.Equal(t, xmatcher.HeaderMatcher{"foo": matcher.Exact("bar")}, r.requestHeader) r.WithHeader("john", "doe") - assert.Equal(t, grpcMatcher.HeaderMatcher{"foo": matcher.Exact("bar"), "john": matcher.Exact("doe")}, r.requestHeader) + assert.Equal(t, xmatcher.HeaderMatcher{"foo": matcher.Exact("bar"), "john": matcher.Exact("doe")}, r.requestHeader) } func TestClientStreamRequest_WithPayload(t *testing.T) { @@ -293,7 +293,7 @@ func TestClientStreamRequest_WithPayload_CustomMatcher_Matched(t *testing.T) { }, { scenario: "with expectation", - matcher: func() (string, grpcMatcher.MatchFn) { + matcher: func() (string, xmatcher.MatchFn) { return "2 messages", func(actual interface{}) (bool, error) { return expectStreamMsgsCount(actual, 2) } @@ -335,7 +335,7 @@ func TestClientStreamRequest_WithPayload_CustomMatcher_Mismatched(t *testing.T) }, { scenario: "with expectation", - matcher: func() (string, grpcMatcher.MatchFn) { + matcher: func() (string, xmatcher.MatchFn) { return "always fail", func(interface{}) (bool, error) { return false, nil } @@ -380,7 +380,7 @@ func TestClientStreamRequest_WithPayload_CustomMatcher_MatchError(t *testing.T) }, { scenario: "with expectation", - matcher: func() (string, grpcMatcher.MatchFn) { + matcher: func() (string, xmatcher.MatchFn) { return "always fail", func(interface{}) (bool, error) { return false, errors.New("match error") } @@ -413,7 +413,7 @@ func TestClientStreamRequest_WithPayload_CustomMatcher_MatchError(t *testing.T) func TestClientStreamRequest_WithPayload_CustomMatcher_RecvError(t *testing.T) { t.Parallel() - in := test.MockCreateItemsStreamer(func(s *grpcMock.ServerStream) { + in := test.MockCreateItemsStreamer(func(s *xmock.ServerStream) { s.On("RecvMsg", &grpctest.Item{}). Return(errors.New("recv error")) })(t) @@ -433,7 +433,7 @@ func TestClientStreamRequest_WithPayload_CustomMatcher_RecvError(t *testing.T) { func TestClientStreamRequest_WithPayload_Match_CouldNotRecvMsg(t *testing.T) { t.Parallel() - in := test.MockCreateItemsStreamer(func(s *grpcMock.ServerStream) { + in := test.MockCreateItemsStreamer(func(s *xmock.ServerStream) { s.On("RecvMsg", &grpctest.Item{}). Return(errors.New("recv error")) })(t) @@ -656,7 +656,7 @@ func TestClientStreamRequest_Return(t *testing.T) { }, { scenario: "json string", - mockStreamer: test.MockCreateItemsStreamer(func(s *grpcMock.ServerStream) { + mockStreamer: test.MockCreateItemsStreamer(func(s *xmock.ServerStream) { s.On("SendMsg", expected).Return(nil) }), output: payload, @@ -664,7 +664,7 @@ func TestClientStreamRequest_Return(t *testing.T) { }, { scenario: "json []byte", - mockStreamer: test.MockCreateItemsStreamer(func(s *grpcMock.ServerStream) { + mockStreamer: test.MockCreateItemsStreamer(func(s *xmock.ServerStream) { s.On("SendMsg", expected).Return(nil) }), output: []byte(payload), @@ -672,7 +672,7 @@ func TestClientStreamRequest_Return(t *testing.T) { }, { scenario: "same type and a pointer", - mockStreamer: test.MockCreateItemsStreamer(func(s *grpcMock.ServerStream) { + mockStreamer: test.MockCreateItemsStreamer(func(s *xmock.ServerStream) { s.On("SendMsg", expected).Return(nil) }), output: &grpctest.CreateItemsResponse{NumItems: 1}, @@ -724,7 +724,7 @@ func TestClientStreamRequest_ReturnUnimplemented(t *testing.T) { func TestClientStreamRequest_Returnf(t *testing.T) { t.Parallel() - in := test.MockCreateItemsStreamer(func(s *grpcMock.ServerStream) { + in := test.MockCreateItemsStreamer(func(s *xmock.ServerStream) { s.On("SendMsg", &grpctest.CreateItemsResponse{NumItems: 1}). Return(nil) })(t) @@ -739,7 +739,7 @@ func TestClientStreamRequest_Returnf(t *testing.T) { expected := &grpctest.CreateItemsResponse{NumItems: 1} assert.NoError(t, err) - grpcAssert.EqualMessage(t, expected, out) + xassert.EqualMessage(t, expected, out) } func TestClientStreamRequest_ReturnJSON(t *testing.T) { @@ -759,7 +759,7 @@ func TestClientStreamRequest_ReturnJSON(t *testing.T) { expected := &grpctest.CreateItemsResponse{NumItems: 1} assert.NoError(t, err) - grpcAssert.EqualMessage(t, expected, out) + xassert.EqualMessage(t, expected, out) } func TestClientStreamRequest_ReturnJSON_Error(t *testing.T) { @@ -791,7 +791,7 @@ func TestClientStreamRequest_ReturnFile_Success(t *testing.T) { expected := &grpctest.CreateItemsResponse{NumItems: 1} assert.NoError(t, err) - grpcAssert.EqualMessage(t, expected, out) + xassert.EqualMessage(t, expected, out) } func TestClientStreamRequest_ReturnFile_NotFound(t *testing.T) { @@ -841,7 +841,7 @@ func TestClientStreamRequest_Run(t *testing.T) { expected := &grpctest.CreateItemsResponse{NumItems: 2} - grpcAssert.EqualMessage(t, expected, out) + xassert.EqualMessage(t, expected, out) assert.NoError(t, err) } @@ -934,7 +934,7 @@ func TestClientStreamRequest_HeaderMatcher(t *testing.T) { r := newCreateItemsRequest().WithHeader("locale", "en-US") actual := HeaderMatcher(r) - expected := grpcMatcher.HeaderMatcher{"locale": matcher.Match("en-US")} + expected := xmatcher.HeaderMatcher{"locale": matcher.Match("en-US")} assert.Equal(t, expected, actual) } diff --git a/request/matcher.go b/request/matcher.go index 6a430b0..6b66ad6 100644 --- a/request/matcher.go +++ b/request/matcher.go @@ -6,55 +6,55 @@ import ( "go.nhat.io/matcher/v2" - grpcMatcher "github.com/nhatthm/grpcmock/matcher" - "github.com/nhatthm/grpcmock/must" - "github.com/nhatthm/grpcmock/streamer" - "github.com/nhatthm/grpcmock/value" + xmatcher "go.nhat.io/grpcmock/matcher" + "go.nhat.io/grpcmock/must" + "go.nhat.io/grpcmock/streamer" + "go.nhat.io/grpcmock/value" ) -func matchUnaryPayload(in interface{}) *grpcMatcher.PayloadMatcher { +func matchUnaryPayload(in interface{}) *xmatcher.PayloadMatcher { switch v := in.(type) { case []byte, string: - return grpcMatcher.Payload(matcher.JSON(value.String(in)), decodeUnaryPayload) + return xmatcher.Payload(matcher.JSON(value.String(in)), decodeUnaryPayload) case matcher.Matcher, func() matcher.Matcher, *regexp.Regexp: - return grpcMatcher.Payload(matcher.Match(v), decodeUnaryPayload) + return xmatcher.Payload(matcher.Match(v), decodeUnaryPayload) - case grpcMatcher.MatchFn: - return grpcMatcher.Payload(grpcMatcher.Fn("", v), nil) + case xmatcher.MatchFn: + return xmatcher.Payload(xmatcher.Fn("", v), nil) } - return grpcMatcher.Payload(matcher.JSON(in), decodeUnaryPayload) + return xmatcher.Payload(matcher.JSON(in), decodeUnaryPayload) } -func matchServerStreamPayload(in interface{}) *grpcMatcher.PayloadMatcher { +func matchServerStreamPayload(in interface{}) *xmatcher.PayloadMatcher { return matchUnaryPayload(in) } -func matchClientStreamPayload(in interface{}) *grpcMatcher.PayloadMatcher { +func matchClientStreamPayload(in interface{}) *xmatcher.PayloadMatcher { switch v := in.(type) { case []byte, string: - return grpcMatcher.Payload(matcher.JSON(value.String(v)), decodeClientStreamPayload) + return xmatcher.Payload(matcher.JSON(value.String(v)), decodeClientStreamPayload) case matcher.Matcher, func() matcher.Matcher, *regexp.Regexp: - return grpcMatcher.Payload(matcher.Match(v), decodeClientStreamPayload) + return xmatcher.Payload(matcher.Match(v), decodeClientStreamPayload) - case grpcMatcher.MatchFn: + case xmatcher.MatchFn: return matchClientStreamPayloadWithCustomMatcher("", v) - case func() (string, grpcMatcher.MatchFn): + case func() (string, xmatcher.MatchFn): return matchClientStreamPayloadWithCustomMatcher(v()) } - return grpcMatcher.Payload(matcher.JSON(in), decodeClientStreamPayload) + return xmatcher.Payload(matcher.JSON(in), decodeClientStreamPayload) } -func matchClientStreamPayloadWithCustomMatcher(expected string, match grpcMatcher.MatchFn) *grpcMatcher.PayloadMatcher { - return grpcMatcher.Payload(grpcMatcher.Fn(expected, func(actual interface{}) (bool, error) { +func matchClientStreamPayloadWithCustomMatcher(expected string, match xmatcher.MatchFn) *xmatcher.PayloadMatcher { + return xmatcher.Payload(xmatcher.Fn(expected, func(actual interface{}) (bool, error) { in, err := streamer.ClientStreamerPayload(actual.(*streamer.ClientStreamer)) // This should never happen because the PayloadMatcher will read the stream first. // If there is an error while reading the stream, it is caught inside the PayloadMatcher. diff --git a/request/request.go b/request/request.go index 8640516..c7a59da 100644 --- a/request/request.go +++ b/request/request.go @@ -3,8 +3,8 @@ package request import ( "context" - "github.com/nhatthm/grpcmock/matcher" - "github.com/nhatthm/grpcmock/service" + "go.nhat.io/grpcmock/matcher" + "go.nhat.io/grpcmock/service" ) // UnlimitedTimes indicates that a request could be repeated without limits. diff --git a/request/server_stream.go b/request/server_stream.go index 59ec7ff..f5d12fb 100644 --- a/request/server_stream.go +++ b/request/server_stream.go @@ -14,11 +14,11 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - grpcErrors "github.com/nhatthm/grpcmock/errors" - grpcMatcher "github.com/nhatthm/grpcmock/matcher" - "github.com/nhatthm/grpcmock/must" - "github.com/nhatthm/grpcmock/service" - "github.com/nhatthm/grpcmock/streamer" + xerrors "go.nhat.io/grpcmock/errors" + xmatcher "go.nhat.io/grpcmock/matcher" + "go.nhat.io/grpcmock/must" + "go.nhat.io/grpcmock/service" + "go.nhat.io/grpcmock/streamer" ) // ServerStreamRequest represents the expectation for a server-stream request. @@ -35,9 +35,9 @@ type ServerStreamRequest struct { run func(ctx context.Context, in interface{}, s grpc.ServerStream) error // requestHeader is a list of expected headers of the given request. - requestHeader grpcMatcher.HeaderMatcher + requestHeader xmatcher.HeaderMatcher // requestPayload is the expected parameters of the given request. - requestPayload *grpcMatcher.PayloadMatcher + requestPayload *xmatcher.PayloadMatcher // statusCode is the response code when the request is handled. statusCode codes.Code @@ -71,7 +71,7 @@ func (r *ServerStreamRequest) WithHeader(header string, value interface{}) *Serv defer r.unlock() if r.requestHeader == nil { - r.requestHeader = grpcMatcher.HeaderMatcher{} + r.requestHeader = xmatcher.HeaderMatcher{} } r.requestHeader[header] = matcher.Match(value) @@ -294,7 +294,7 @@ func (r *ServerStreamRequest) handle(ctx context.Context, in interface{}, out in return status.Error(r.statusCode, r.statusMessage) } - return grpcErrors.StatusError(r.run(ctx, in, out.(*streamer.ServerStreamer))) + return xerrors.StatusError(r.run(ctx, in, out.(*streamer.ServerStreamer))) } // Once indicates that the mock should only return the value once. @@ -376,10 +376,10 @@ func (r *ServerStreamRequest) After(d time.Duration) *ServerStreamRequest { return r } -func (r *ServerStreamRequest) headerMatcher() grpcMatcher.HeaderMatcher { +func (r *ServerStreamRequest) headerMatcher() xmatcher.HeaderMatcher { return r.requestHeader } -func (r *ServerStreamRequest) payloadMatcher() *grpcMatcher.PayloadMatcher { +func (r *ServerStreamRequest) payloadMatcher() *xmatcher.PayloadMatcher { return r.requestPayload } diff --git a/request/server_stream_internal_test.go b/request/server_stream_internal_test.go index e2bd67e..79b91f3 100644 --- a/request/server_stream_internal_test.go +++ b/request/server_stream_internal_test.go @@ -16,11 +16,11 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - srvMatcher "github.com/nhatthm/grpcmock/matcher" - "github.com/nhatthm/grpcmock/service" - "github.com/nhatthm/grpcmock/streamer" - "github.com/nhatthm/grpcmock/test" - "github.com/nhatthm/grpcmock/test/grpctest" + xmatcher "go.nhat.io/grpcmock/matcher" + "go.nhat.io/grpcmock/service" + "go.nhat.io/grpcmock/streamer" + "go.nhat.io/grpcmock/test" + "go.nhat.io/grpcmock/test/grpctest" ) func TestServerStreamRequest_WithHeader(t *testing.T) { @@ -29,11 +29,11 @@ func TestServerStreamRequest_WithHeader(t *testing.T) { r := newListItemsRequest() r.WithHeader("foo", "bar") - assert.Equal(t, srvMatcher.HeaderMatcher{"foo": matcher.Exact("bar")}, r.requestHeader) + assert.Equal(t, xmatcher.HeaderMatcher{"foo": matcher.Exact("bar")}, r.requestHeader) r.WithHeader("john", "doe") - assert.Equal(t, srvMatcher.HeaderMatcher{"foo": matcher.Exact("bar"), "john": matcher.Exact("doe")}, r.requestHeader) + assert.Equal(t, xmatcher.HeaderMatcher{"foo": matcher.Exact("bar"), "john": matcher.Exact("doe")}, r.requestHeader) } func TestServerStreamRequest_WithHeaders(t *testing.T) { @@ -42,11 +42,11 @@ func TestServerStreamRequest_WithHeaders(t *testing.T) { r := newListItemsRequest() r.WithHeaders(map[string]interface{}{"foo": "bar"}) - assert.Equal(t, srvMatcher.HeaderMatcher{"foo": matcher.Exact("bar")}, r.requestHeader) + assert.Equal(t, xmatcher.HeaderMatcher{"foo": matcher.Exact("bar")}, r.requestHeader) r.WithHeader("john", "doe") - assert.Equal(t, srvMatcher.HeaderMatcher{"foo": matcher.Exact("bar"), "john": matcher.Exact("doe")}, r.requestHeader) + assert.Equal(t, xmatcher.HeaderMatcher{"foo": matcher.Exact("bar"), "john": matcher.Exact("doe")}, r.requestHeader) } func TestServerStreamRequest_WithPayloadPanic(t *testing.T) { @@ -826,7 +826,7 @@ func TestServerStreamRequest_HeaderMatcher(t *testing.T) { r.WithHeader("locale", "en-US") actual := HeaderMatcher(r) - expected := srvMatcher.HeaderMatcher{"locale": matcher.Match("en-US")} + expected := xmatcher.HeaderMatcher{"locale": matcher.Match("en-US")} assert.Equal(t, expected, actual) } diff --git a/request/stream_handler_server.go b/request/stream_handler_server.go index a5b9401..3c89503 100644 --- a/request/stream_handler_server.go +++ b/request/stream_handler_server.go @@ -7,7 +7,7 @@ import ( "google.golang.org/grpc" - "github.com/nhatthm/grpcmock/streamer" + "go.nhat.io/grpcmock/streamer" ) type serverStreamHandler struct { diff --git a/request/stream_handler_server_internal_test.go b/request/stream_handler_server_internal_test.go index ca2586d..25bef79 100644 --- a/request/stream_handler_server_internal_test.go +++ b/request/stream_handler_server_internal_test.go @@ -11,10 +11,10 @@ import ( "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" - grpcMock "github.com/nhatthm/grpcmock/mock/grpc" - "github.com/nhatthm/grpcmock/streamer" - "github.com/nhatthm/grpcmock/test" - "github.com/nhatthm/grpcmock/test/grpctest" + xmock "go.nhat.io/grpcmock/mock/grpc" + "go.nhat.io/grpcmock/streamer" + "go.nhat.io/grpcmock/test" + "go.nhat.io/grpcmock/test/grpctest" ) func TestServerStreamHandler_AddHeader_SendHeader(t *testing.T) { @@ -27,7 +27,7 @@ func TestServerStreamHandler_AddHeader_SendHeader(t *testing.T) { }{ { scenario: "error", - mockStreamer: test.MockListItemsStreamer(func(s *grpcMock.ServerStream) { + mockStreamer: test.MockListItemsStreamer(func(s *xmock.ServerStream) { s.On("SendHeader", mock.Anything). Return(status.Error(codes.Internal, "send error")) }), @@ -35,7 +35,7 @@ func TestServerStreamHandler_AddHeader_SendHeader(t *testing.T) { }, { scenario: "success", - mockStreamer: test.MockListItemsStreamer(func(s *grpcMock.ServerStream) { + mockStreamer: test.MockListItemsStreamer(func(s *xmock.ServerStream) { s.On("SendHeader", metadata.New(map[string]string{ "user": "foobar", "email": "test@example.com", @@ -73,7 +73,7 @@ func TestServerStreamHandler_SetHeader_SendHeader(t *testing.T) { }{ { scenario: "error", - mockStreamer: test.MockListItemsStreamer(func(s *grpcMock.ServerStream) { + mockStreamer: test.MockListItemsStreamer(func(s *xmock.ServerStream) { s.On("SendHeader", mock.Anything). Return(status.Error(codes.Internal, "send error")) }), @@ -81,7 +81,7 @@ func TestServerStreamHandler_SetHeader_SendHeader(t *testing.T) { }, { scenario: "success", - mockStreamer: test.MockListItemsStreamer(func(s *grpcMock.ServerStream) { + mockStreamer: test.MockListItemsStreamer(func(s *xmock.ServerStream) { s.On("SendHeader", metadata.New(map[string]string{ "user": "foobar", "email": "test@example.com", @@ -121,7 +121,7 @@ func TestServerStreamHandler_Send(t *testing.T) { }{ { scenario: "error", - mockStreamer: test.MockListItemsStreamer(func(s *grpcMock.ServerStream) { + mockStreamer: test.MockListItemsStreamer(func(s *xmock.ServerStream) { s.On("SendMsg", mock.Anything). Return(status.Error(codes.Internal, "send error")) }), @@ -129,7 +129,7 @@ func TestServerStreamHandler_Send(t *testing.T) { }, { scenario: "success", - mockStreamer: test.MockListItemsStreamer(func(s *grpcMock.ServerStream) { + mockStreamer: test.MockListItemsStreamer(func(s *xmock.ServerStream) { s.On("SendMsg", &grpctest.Item{Id: 42}). Return(nil) }), @@ -162,7 +162,7 @@ func TestServerStreamHandler_SendMany(t *testing.T) { }{ { scenario: "error", - mockStreamer: test.MockListItemsStreamer(func(s *grpcMock.ServerStream) { + mockStreamer: test.MockListItemsStreamer(func(s *xmock.ServerStream) { s.On("SendMsg", mock.Anything). Return(status.Error(codes.Internal, "send error")) }), @@ -170,7 +170,7 @@ func TestServerStreamHandler_SendMany(t *testing.T) { }, { scenario: "success", - mockStreamer: test.MockListItemsStreamer(func(s *grpcMock.ServerStream) { + mockStreamer: test.MockListItemsStreamer(func(s *xmock.ServerStream) { s.On("SendMsg", &grpctest.Item{Id: 41}). Return(nil) @@ -201,7 +201,7 @@ func TestServerStreamHandler_WaitFor(t *testing.T) { duration := 50 * time.Millisecond - s := test.MockListItemsStreamer(func(s *grpcMock.ServerStream) { + s := test.MockListItemsStreamer(func(s *xmock.ServerStream) { s.On("SendMsg", &grpctest.Item{Id: 42}). Return(nil) })(t) diff --git a/request/stream_step.go b/request/stream_step.go index 870369c..f3111f9 100644 --- a/request/stream_step.go +++ b/request/stream_step.go @@ -11,9 +11,9 @@ import ( "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" - "github.com/nhatthm/grpcmock/errors" - grpcReflect "github.com/nhatthm/grpcmock/reflect" - "github.com/nhatthm/grpcmock/value" + "go.nhat.io/grpcmock/errors" + xreflect "go.nhat.io/grpcmock/reflect" + "go.nhat.io/grpcmock/value" ) type streamStep interface { @@ -46,13 +46,13 @@ func stepSend(expectedType reflect.Type, msg interface{}) streamStepFunc { return nil } - if grpcReflect.UnwrapType(msg) == grpcReflect.UnwrapType(expectedType) { + if xreflect.UnwrapType(msg) == xreflect.UnwrapType(expectedType) { return send(msg) } switch resp := msg.(type) { case []byte, string: - out := grpcReflect.New(expectedType) + out := xreflect.New(expectedType) if err := json.Unmarshal([]byte(value.String(resp)), out); err != nil { return status.Error(codes.Internal, err.Error()) @@ -77,7 +77,7 @@ func stepSendMany(msgType reflect.Type, msg interface{}) streamStepFunc { } for i := 0; i < valueOf.Len(); i++ { - if err := s.SendMsg(grpcReflect.PtrValue(valueOf.Index(i).Interface())); err != nil { + if err := s.SendMsg(xreflect.PtrValue(valueOf.Index(i).Interface())); err != nil { return err } } @@ -87,21 +87,21 @@ func stepSendMany(msgType reflect.Type, msg interface{}) streamStepFunc { // item -> []*item. // *item -> []*item. - expectedPtrType := reflect.SliceOf(reflect.New(grpcReflect.UnwrapType(msgType)).Type()) + expectedPtrType := reflect.SliceOf(reflect.New(xreflect.UnwrapType(msgType)).Type()) - if grpcReflect.UnwrapType(msg) == expectedPtrType { + if xreflect.UnwrapType(msg) == expectedPtrType { return sendMany(msg) } // item -> []item. // *item -> []*item. - if grpcReflect.UnwrapType(msg) == expectedType { + if xreflect.UnwrapType(msg) == expectedType { return sendMany(msg) } switch resp := msg.(type) { case []byte, string: - out := grpcReflect.New(expectedPtrType) + out := xreflect.New(expectedPtrType) if err := json.Unmarshal([]byte(value.String(resp)), out); err != nil { return status.Error(codes.Internal, err.Error()) diff --git a/request/stream_step_internal_test.go b/request/stream_step_internal_test.go index 4958932..e1cc197 100644 --- a/request/stream_step_internal_test.go +++ b/request/stream_step_internal_test.go @@ -11,9 +11,9 @@ import ( "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" - grpcMock "github.com/nhatthm/grpcmock/mock/grpc" - "github.com/nhatthm/grpcmock/reflect" - "github.com/nhatthm/grpcmock/test/grpctest" + xmock "go.nhat.io/grpcmock/mock/grpc" + "go.nhat.io/grpcmock/reflect" + "go.nhat.io/grpcmock/test/grpctest" ) func TestStepSendHeader(t *testing.T) { @@ -21,12 +21,12 @@ func TestStepSendHeader(t *testing.T) { testCases := []struct { scenario string - mockStream grpcMock.ServerStreamMocker + mockStream xmock.ServerStreamMocker error error }{ { scenario: "error", - mockStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("SendHeader", mock.Anything). Return(status.Error(codes.Internal, "send error")) }), @@ -34,7 +34,7 @@ func TestStepSendHeader(t *testing.T) { }, { scenario: "no error", - mockStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("SendHeader", metadata.New(map[string]string{"locale": "en-us"})). Return(nil) }), @@ -63,19 +63,19 @@ func TestStepSend(t *testing.T) { testCases := []struct { scenario string - mockServerStream grpcMock.ServerStreamMocker + mockServerStream xmock.ServerStreamMocker msg interface{} expectedError string }{ { scenario: "wrong type", - mockServerStream: grpcMock.NoMockServerStream, + mockServerStream: xmock.NoMockServerStream, msg: 42, expectedError: `rpc error: code = Internal desc = unsupported data type: got int, want grpctest.Item`, }, { scenario: "exact type error", - mockServerStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockServerStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("SendMsg", mock.Anything). Return(status.Error(codes.Internal, "send error")) }), @@ -84,7 +84,7 @@ func TestStepSend(t *testing.T) { }, { scenario: "exact type success", - mockServerStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockServerStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("SendMsg", &grpctest.Item{Id: 42}). Return(nil) }), @@ -92,13 +92,13 @@ func TestStepSend(t *testing.T) { }, { scenario: "byte error", - mockServerStream: grpcMock.NoMockServerStream, + mockServerStream: xmock.NoMockServerStream, msg: []byte(`{`), expectedError: `rpc error: code = Internal desc = unexpected end of JSON input`, }, { scenario: "byte", - mockServerStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockServerStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("SendMsg", &grpctest.Item{Id: 42}). Return(nil) }), @@ -106,7 +106,7 @@ func TestStepSend(t *testing.T) { }, { scenario: "string", - mockServerStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockServerStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("SendMsg", &grpctest.Item{Id: 42}). Return(nil) }), @@ -140,25 +140,25 @@ func TestStepSendMany(t *testing.T) { testCases := []struct { scenario string - mockServerStream grpcMock.ServerStreamMocker + mockServerStream xmock.ServerStreamMocker msg interface{} expectedError string }{ { scenario: "wrong type", - mockServerStream: grpcMock.NoMockServerStream, + mockServerStream: xmock.NoMockServerStream, msg: 42, expectedError: `rpc error: code = Internal desc = unsupported data type: got int, want []grpctest.Item`, }, { scenario: "wrong type - not a slice", - mockServerStream: grpcMock.NoMockServerStream, + mockServerStream: xmock.NoMockServerStream, msg: grpctest.Item{}, expectedError: `rpc error: code = Internal desc = unsupported data type: got grpctest.Item, want []grpctest.Item`, }, { scenario: "exact type error", - mockServerStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockServerStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("SendMsg", mock.Anything). Return(status.Error(codes.Internal, "send error")) }), @@ -167,7 +167,7 @@ func TestStepSendMany(t *testing.T) { }, { scenario: "exact type success", - mockServerStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockServerStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("SendMsg", &grpctest.Item{Id: 42}). Return(nil) }), @@ -175,7 +175,7 @@ func TestStepSendMany(t *testing.T) { }, { scenario: "exact type ptr success", - mockServerStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockServerStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("SendMsg", &grpctest.Item{Id: 42}). Return(nil) }), @@ -183,7 +183,7 @@ func TestStepSendMany(t *testing.T) { }, { scenario: "exact type many success", - mockServerStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockServerStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("SendMsg", &grpctest.Item{Id: 42}). Return(nil) @@ -197,13 +197,13 @@ func TestStepSendMany(t *testing.T) { }, { scenario: "byte error", - mockServerStream: grpcMock.NoMockServerStream, + mockServerStream: xmock.NoMockServerStream, msg: []byte(`[`), expectedError: `rpc error: code = Internal desc = unexpected end of JSON input`, }, { scenario: "byte", - mockServerStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockServerStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("SendMsg", &grpctest.Item{Id: 42}). Return(nil) }), @@ -211,7 +211,7 @@ func TestStepSendMany(t *testing.T) { }, { scenario: "string", - mockServerStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockServerStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("SendMsg", &grpctest.Item{Id: 42}). Return(nil) }), diff --git a/request/unary.go b/request/unary.go index 5ad313a..0da8d01 100644 --- a/request/unary.go +++ b/request/unary.go @@ -13,12 +13,12 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - grpcErrors "github.com/nhatthm/grpcmock/errors" - grpcMatcher "github.com/nhatthm/grpcmock/matcher" - "github.com/nhatthm/grpcmock/must" - "github.com/nhatthm/grpcmock/reflect" - "github.com/nhatthm/grpcmock/service" - "github.com/nhatthm/grpcmock/value" + xerrors "go.nhat.io/grpcmock/errors" + xmatcher "go.nhat.io/grpcmock/matcher" + "go.nhat.io/grpcmock/must" + "go.nhat.io/grpcmock/reflect" + "go.nhat.io/grpcmock/service" + "go.nhat.io/grpcmock/value" ) // UnaryRequest represents the expectation for a unary request. @@ -35,9 +35,9 @@ type UnaryRequest struct { run func(ctx context.Context, in interface{}) (interface{}, error) // requestHeader is a list of expected headers of the given request. - requestHeader grpcMatcher.HeaderMatcher + requestHeader xmatcher.HeaderMatcher // requestPayload is the expected parameters of the given request. - requestPayload *grpcMatcher.PayloadMatcher + requestPayload *xmatcher.PayloadMatcher // statusCode is the response code when the request is handled. statusCode codes.Code @@ -71,7 +71,7 @@ func (r *UnaryRequest) WithHeader(header string, value interface{}) *UnaryReques defer r.unlock() if r.requestHeader == nil { - r.requestHeader = grpcMatcher.HeaderMatcher{} + r.requestHeader = xmatcher.HeaderMatcher{} } r.requestHeader[header] = matcher.Match(value) @@ -250,7 +250,7 @@ func (r *UnaryRequest) handle(ctx context.Context, in interface{}, out interface resp, err := r.run(ctx, in) if err != nil { - return grpcErrors.StatusError(err) + return xerrors.StatusError(err) } if reflect.UnwrapType(out) == reflect.UnwrapType(resp) { @@ -342,10 +342,10 @@ func (r *UnaryRequest) After(d time.Duration) *UnaryRequest { return r } -func (r *UnaryRequest) headerMatcher() grpcMatcher.HeaderMatcher { +func (r *UnaryRequest) headerMatcher() xmatcher.HeaderMatcher { return r.requestHeader } -func (r *UnaryRequest) payloadMatcher() *grpcMatcher.PayloadMatcher { +func (r *UnaryRequest) payloadMatcher() *xmatcher.PayloadMatcher { return r.requestPayload } diff --git a/request/unary_internal_test.go b/request/unary_internal_test.go index f10bb79..b7ac762 100644 --- a/request/unary_internal_test.go +++ b/request/unary_internal_test.go @@ -13,9 +13,9 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - srvMatcher "github.com/nhatthm/grpcmock/matcher" - "github.com/nhatthm/grpcmock/test" - "github.com/nhatthm/grpcmock/test/grpctest" + xmatcher "go.nhat.io/grpcmock/matcher" + "go.nhat.io/grpcmock/test" + "go.nhat.io/grpcmock/test/grpctest" ) func TestUnaryRequest_WithHeader(t *testing.T) { @@ -24,11 +24,11 @@ func TestUnaryRequest_WithHeader(t *testing.T) { r := newGetItemRequest() r.WithHeader("foo", "bar") - assert.Equal(t, srvMatcher.HeaderMatcher{"foo": matcher.Exact("bar")}, r.requestHeader) + assert.Equal(t, xmatcher.HeaderMatcher{"foo": matcher.Exact("bar")}, r.requestHeader) r.WithHeader("john", "doe") - assert.Equal(t, srvMatcher.HeaderMatcher{"foo": matcher.Exact("bar"), "john": matcher.Exact("doe")}, r.requestHeader) + assert.Equal(t, xmatcher.HeaderMatcher{"foo": matcher.Exact("bar"), "john": matcher.Exact("doe")}, r.requestHeader) } func TestUnaryRequest_WithHeaders(t *testing.T) { @@ -37,11 +37,11 @@ func TestUnaryRequest_WithHeaders(t *testing.T) { r := newGetItemRequest() r.WithHeaders(map[string]interface{}{"foo": "bar"}) - assert.Equal(t, srvMatcher.HeaderMatcher{"foo": matcher.Exact("bar")}, r.requestHeader) + assert.Equal(t, xmatcher.HeaderMatcher{"foo": matcher.Exact("bar")}, r.requestHeader) r.WithHeader("john", "doe") - assert.Equal(t, srvMatcher.HeaderMatcher{"foo": matcher.Exact("bar"), "john": matcher.Exact("doe")}, r.requestHeader) + assert.Equal(t, xmatcher.HeaderMatcher{"foo": matcher.Exact("bar"), "john": matcher.Exact("doe")}, r.requestHeader) } func TestUnaryRequest_WithPayload_Panic(t *testing.T) { @@ -694,7 +694,7 @@ func TestUnaryRequest_HeaderMatcher(t *testing.T) { r.WithHeader("locale", "en-US") actual := HeaderMatcher(r) - expected := srvMatcher.HeaderMatcher{"locale": matcher.Match("en-US")} + expected := xmatcher.HeaderMatcher{"locale": matcher.Match("en-US")} assert.Equal(t, expected, actual) } diff --git a/resources/docs/SERVER.md b/resources/docs/SERVER.md index 5953634..e81961b 100644 --- a/resources/docs/SERVER.md +++ b/resources/docs/SERVER.md @@ -60,7 +60,7 @@ import ( "context" "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" ) func TestServer(t *testing.T) { @@ -114,9 +114,9 @@ import ( "context" "testing" - "github.com/nhatthm/grpcmock" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.nhat.io/grpcmock" ) func mockItemServiceServer(m ...grpcmock.ServerOption) grpcmock.ServerMockerWithContextDialer { @@ -216,7 +216,7 @@ package main import ( "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" ) func TestServer(t *testing.T) { @@ -260,7 +260,7 @@ package main import ( "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" ) func TestServer(t *testing.T) { @@ -283,7 +283,7 @@ package main import ( "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" ) func TestServer(t *testing.T) { @@ -313,8 +313,8 @@ package main import ( "testing" - "github.com/nhatthm/grpcmock" - "github.com/nhatthm/grpcmock/service" + "go.nhat.io/grpcmock" + "go.nhat.io/grpcmock/service" ) func TestServer(t *testing.T) { @@ -423,7 +423,7 @@ import ( "regexp" "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" ) func TestServer(t *testing.T) { @@ -453,7 +453,7 @@ import ( "regexp" "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" ) func TestServer(t *testing.T) { @@ -499,7 +499,7 @@ package main import ( "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" ) func TestServer(t *testing.T) { @@ -559,7 +559,7 @@ package main import ( "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" "google.golang.org/grpc/codes" ) @@ -595,7 +595,7 @@ package main import ( "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" ) func TestServer(t *testing.T) { @@ -640,7 +640,7 @@ import ( "context" "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" "google.golang.org/grpc/metadata" ) @@ -695,7 +695,7 @@ import ( "regexp" "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" ) func TestServer(t *testing.T) { @@ -725,7 +725,7 @@ import ( "regexp" "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" ) func TestServer(t *testing.T) { @@ -773,7 +773,7 @@ package main import ( "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" ) func TestServer(t *testing.T) { @@ -833,7 +833,7 @@ package main import ( "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" "google.golang.org/grpc/codes" ) @@ -869,7 +869,7 @@ package main import ( "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" ) func TestServer(t *testing.T) { @@ -914,8 +914,8 @@ import ( "context" "testing" - "github.com/nhatthm/grpcmock" - "github.com/nhatthm/grpcmock/stream" + "go.nhat.io/grpcmock" + "go.nhat.io/grpcmock/stream" "google.golang.org/grpc" "google.golang.org/grpc/metadata" ) @@ -972,7 +972,7 @@ import ( "regexp" "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" ) func TestServer(t *testing.T) { @@ -1002,7 +1002,7 @@ import ( "regexp" "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" ) func TestServer(t *testing.T) { @@ -1048,8 +1048,8 @@ package main import ( "testing" + "go.nhat.io/grpcmock" "go.nhat.io/matcher/v2" - "github.com/nhatthm/grpcmock" ) func TestServer(t *testing.T) { @@ -1108,7 +1108,7 @@ package main import ( "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" "google.golang.org/grpc/codes" ) @@ -1144,7 +1144,7 @@ package main import ( "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" ) func TestServer(t *testing.T) { @@ -1200,7 +1200,7 @@ package main import ( "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" "google.golang.org/grpc/codes" ) @@ -1235,7 +1235,7 @@ import ( "context" "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" "google.golang.org/grpc" ) @@ -1279,7 +1279,7 @@ import ( "regexp" "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" ) func TestServer(t *testing.T) { @@ -1309,7 +1309,7 @@ import ( "regexp" "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" ) func TestServer(t *testing.T) { @@ -1356,7 +1356,7 @@ package main import ( "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" "google.golang.org/grpc/codes" ) @@ -1389,7 +1389,7 @@ import ( "io" "testing" - "github.com/nhatthm/grpcmock" + "go.nhat.io/grpcmock" "google.golang.org/grpc" "google.golang.org/grpc/metadata" ) @@ -1430,7 +1430,7 @@ func TestServer(t *testing.T) { ## Execution Plan -The mocked gRPC server is created with the `github.com/nhatthm/grpcmock/planner.Sequence()` by default, and it matches incoming requests sequentially. You can +The mocked gRPC server is created with the `go.nhat.io/grpcmock/planner.Sequence()` by default, and it matches incoming requests sequentially. You can easily change this behavior to match your application execution by implementing the `planner.Planner` interface. ```go @@ -1439,8 +1439,8 @@ package planner import ( "context" - "github.com/nhatthm/grpcmock/request" - "github.com/nhatthm/grpcmock/service" + "go.nhat.io/grpcmock/request" + "go.nhat.io/grpcmock/service" ) type Planner interface { diff --git a/server.go b/server.go index 6f3718d..e9d43ea 100644 --- a/server.go +++ b/server.go @@ -10,19 +10,19 @@ import ( "sync" "time" - grpcRecovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery" - grpcTags "github.com/grpc-ecosystem/go-grpc-middleware/tags" + recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery" + tags "github.com/grpc-ecosystem/go-grpc-middleware/tags" "github.com/stretchr/testify/assert" "google.golang.org/grpc" - grpcErrors "github.com/nhatthm/grpcmock/errors" - "github.com/nhatthm/grpcmock/format" - "github.com/nhatthm/grpcmock/must" - "github.com/nhatthm/grpcmock/planner" - grpcReflect "github.com/nhatthm/grpcmock/reflect" - "github.com/nhatthm/grpcmock/request" - "github.com/nhatthm/grpcmock/service" - "github.com/nhatthm/grpcmock/streamer" + xerrors "go.nhat.io/grpcmock/errors" + "go.nhat.io/grpcmock/format" + "go.nhat.io/grpcmock/must" + "go.nhat.io/grpcmock/planner" + xreflect "go.nhat.io/grpcmock/reflect" + "go.nhat.io/grpcmock/request" + "go.nhat.io/grpcmock/service" + "go.nhat.io/grpcmock/streamer" ) // Server wraps a grpc server and provides mocking functionalities. @@ -66,12 +66,12 @@ func NewUnstartedServer(opts ...ServerOption) *Server { services: map[string]*service.Method{}, serverOpts: []grpc.ServerOption{ grpc.ChainUnaryInterceptor( - grpcRecovery.UnaryServerInterceptor(), - grpcTags.UnaryServerInterceptor(), + recovery.UnaryServerInterceptor(), + tags.UnaryServerInterceptor(), ), grpc.ChainStreamInterceptor( - grpcRecovery.StreamServerInterceptor(), - grpcTags.StreamServerInterceptor(), + recovery.StreamServerInterceptor(), + tags.StreamServerInterceptor(), ), }, closeServer: closeNothing, @@ -124,7 +124,7 @@ func (s *Server) method(method string) *service.Method { s.mu.Unlock() if !ok { - panic(fmt.Errorf("%w: %s", grpcErrors.ErrMethodNotFound, method)) + panic(fmt.Errorf("%w: %s", xerrors.ErrMethodNotFound, method)) } return svc @@ -137,7 +137,7 @@ func (s *Server) ExpectUnary(method string) *request.UnaryRequest { svc := s.method(method) if !service.IsMethodUnary(svc.MethodType) { - panic(fmt.Errorf("%w: %s", grpcErrors.ErrMethodNotUnary, method)) + panic(fmt.Errorf("%w: %s", xerrors.ErrMethodNotUnary, method)) } r := request.NewUnaryRequest(&s.mu, svc).Once() @@ -154,7 +154,7 @@ func (s *Server) ExpectClientStream(method string) *request.ClientStreamRequest svc := s.method(method) if !service.IsMethodClientStream(svc.MethodType) { - panic(fmt.Errorf("%w: %s", grpcErrors.ErrMethodNotClientStream, method)) + panic(fmt.Errorf("%w: %s", xerrors.ErrMethodNotClientStream, method)) } r := request.NewClientStreamRequest(&s.mu, svc).Once() @@ -171,7 +171,7 @@ func (s *Server) ExpectServerStream(method string) *request.ServerStreamRequest svc := s.method(method) if !service.IsMethodServerStream(svc.MethodType) { - panic(fmt.Errorf("%w: %s", grpcErrors.ErrMethodNotServerStream, method)) + panic(fmt.Errorf("%w: %s", xerrors.ErrMethodNotServerStream, method)) } r := request.NewServerStreamRequest(&s.mu, svc).Once() @@ -188,7 +188,7 @@ func (s *Server) ExpectBidirectionalStream(method string) *request.Bidirectional svc := s.method(method) if !service.IsMethodBidirectionalStream(svc.MethodType) { - panic(fmt.Errorf("%w: %s", grpcErrors.ErrMethodNotBidirectionalStream, method)) + panic(fmt.Errorf("%w: %s", xerrors.ErrMethodNotBidirectionalStream, method)) } r := request.NewBidirectionalStreamRequest(&s.mu, svc).Once() @@ -309,7 +309,7 @@ func (s *Server) handleRequest(ctx context.Context, svc service.Method, in inter assert.NoError(s.test, err) if err != nil { - return grpcErrors.StatusError(err) + return xerrors.StatusError(err) } // Log the request. @@ -327,7 +327,7 @@ func (s *Server) registerServiceMethod(svc service.Method) { } func (s *Server) registerService(id string, svc interface{}) { - for _, method := range grpcReflect.FindServiceMethods(svc) { + for _, method := range xreflect.FindServiceMethods(svc) { s.registerServiceMethod(service.Method{ ServiceName: id, MethodName: method.Name, @@ -425,17 +425,17 @@ func newUnaryHandler( handle func(ctx context.Context, svc service.Method, in interface{}, out interface{}) error, ) func(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { return func(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := grpcReflect.New(svc.Input) + in := xreflect.New(svc.Input) if err := dec(in); err != nil { - return grpcReflect.NewZero(svc.Output), grpcErrors.StatusError(err) + return xreflect.NewZero(svc.Output), xerrors.StatusError(err) } intercept := func(ctx context.Context, in interface{}) (interface{}, error) { - out := grpcReflect.New(svc.Output) + out := xreflect.New(svc.Output) if err := handle(ctx, svc, in, out); err != nil { - return grpcReflect.NewZero(svc.Output), err + return xreflect.NewZero(svc.Output), err } return out, nil @@ -469,19 +469,19 @@ func newStreamHandler( // nolint: exhaustive switch svc.MethodType { case service.TypeServerStream: - in = grpcReflect.New(svc.Input) + in = xreflect.New(svc.Input) if err := s.RecvMsg(in); err != nil { - return grpcErrors.StatusError(err) + return xerrors.StatusError(err) } - out = streamer.NewServerStreamer(s, grpcReflect.UnwrapType(svc.Output)) + out = streamer.NewServerStreamer(s, xreflect.UnwrapType(svc.Output)) case service.TypeClientStream: - in = streamer.NewClientStreamer(s, grpcReflect.UnwrapType(svc.Input), grpcReflect.UnwrapType(svc.Output)) - out = grpcReflect.New(svc.Output) + in = streamer.NewClientStreamer(s, xreflect.UnwrapType(svc.Input), xreflect.UnwrapType(svc.Output)) + out = xreflect.New(svc.Output) default: - in = streamer.NewBidirectionalStreamer(s, grpcReflect.UnwrapType(svc.Input), grpcReflect.UnwrapType(svc.Output)) + in = streamer.NewBidirectionalStreamer(s, xreflect.UnwrapType(svc.Input), xreflect.UnwrapType(svc.Output)) out = in } @@ -518,7 +518,7 @@ func WithPlanner(p planner.Planner) ServerOption { // See: RegisterServiceFromInstance(), RegisterServiceFromMethods(). func RegisterService(registerFunc interface{}) ServerOption { return func(s *Server) { - serviceDesc, svc := grpcReflect.ParseRegisterFunc(registerFunc) + serviceDesc, svc := xreflect.ParseRegisterFunc(registerFunc) s.registerService(serviceDesc.ServiceName, svc) } @@ -605,7 +605,7 @@ func FindServerMethod(srv *Server, method string) *service.Method { ServiceName: svc.ServiceName, MethodName: svc.MethodName, MethodType: svc.MethodType, - Input: grpcReflect.New(svc.Input), - Output: grpcReflect.New(svc.Output), + Input: xreflect.New(svc.Input), + Output: xreflect.New(svc.Output), } } diff --git a/server_example_test.go b/server_example_test.go index 0198359..614b09e 100644 --- a/server_example_test.go +++ b/server_example_test.go @@ -15,13 +15,13 @@ import ( "google.golang.org/grpc/metadata" "google.golang.org/grpc/test/bufconn" - "github.com/nhatthm/grpcmock" - plannerMock "github.com/nhatthm/grpcmock/mock/planner" - "github.com/nhatthm/grpcmock/must" - "github.com/nhatthm/grpcmock/planner" - "github.com/nhatthm/grpcmock/service" - "github.com/nhatthm/grpcmock/stream" - "github.com/nhatthm/grpcmock/test/grpctest" + "go.nhat.io/grpcmock" + plannerMock "go.nhat.io/grpcmock/mock/planner" + "go.nhat.io/grpcmock/must" + "go.nhat.io/grpcmock/planner" + "go.nhat.io/grpcmock/service" + "go.nhat.io/grpcmock/stream" + "go.nhat.io/grpcmock/test/grpctest" ) func ExampleServer_WithPlanner() { diff --git a/server_internal_test.go b/server_internal_test.go index 5bb89ad..3a209b6 100644 --- a/server_internal_test.go +++ b/server_internal_test.go @@ -12,10 +12,10 @@ import ( "google.golang.org/grpc/status" "google.golang.org/grpc/test/bufconn" - grpcMock "github.com/nhatthm/grpcmock/mock/grpc" - "github.com/nhatthm/grpcmock/planner" - "github.com/nhatthm/grpcmock/service" - "github.com/nhatthm/grpcmock/test/grpctest" + xmock "go.nhat.io/grpcmock/mock/grpc" + "go.nhat.io/grpcmock/planner" + "go.nhat.io/grpcmock/service" + "go.nhat.io/grpcmock/test/grpctest" ) func TestServer_HandleRequest_Unexpected(t *testing.T) { @@ -187,13 +187,13 @@ func TestNewStreamHandler_ServerStream(t *testing.T) { testCases := []struct { scenario string - mockStream grpcMock.ServerStreamMocker + mockStream xmock.ServerStreamMocker handle func(ctx context.Context, svc service.Method, in interface{}, out interface{}) error expectedError error }{ { scenario: "could not receive message", - mockStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("RecvMsg", &grpctest.ListItemsRequest{}). Return(errors.New("recv error")) }), @@ -206,7 +206,7 @@ func TestNewStreamHandler_ServerStream(t *testing.T) { }, { scenario: "handle error", - mockStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("RecvMsg", &grpctest.ListItemsRequest{}). Return(nil) @@ -220,7 +220,7 @@ func TestNewStreamHandler_ServerStream(t *testing.T) { }, { scenario: "success", - mockStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("RecvMsg", &grpctest.ListItemsRequest{}). Return(nil) @@ -256,13 +256,13 @@ func TestNewStreamHandler_ClientStream(t *testing.T) { testCases := []struct { scenario string - mockStream grpcMock.ServerStreamMocker + mockStream xmock.ServerStreamMocker handle func(ctx context.Context, svc service.Method, in interface{}, out interface{}) error expectedError error }{ { scenario: "handle error", - mockStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("Context"). Return(context.Background()) }), @@ -273,7 +273,7 @@ func TestNewStreamHandler_ClientStream(t *testing.T) { }, { scenario: "success", - mockStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("Context"). Return(context.Background()) }), @@ -306,13 +306,13 @@ func TestNewStreamHandler_BidirectionalStream(t *testing.T) { testCases := []struct { scenario string - mockStream grpcMock.ServerStreamMocker + mockStream xmock.ServerStreamMocker handle func(ctx context.Context, svc service.Method, in interface{}, out interface{}) error expectedError error }{ { scenario: "handle error", - mockStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("Context"). Return(context.Background()) }), @@ -323,7 +323,7 @@ func TestNewStreamHandler_BidirectionalStream(t *testing.T) { }, { scenario: "success", - mockStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("Context"). Return(context.Background()) }), diff --git a/server_test.go b/server_test.go index 66fe505..97fe914 100644 --- a/server_test.go +++ b/server_test.go @@ -15,12 +15,12 @@ import ( "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" - "github.com/nhatthm/grpcmock" - grpcAssert "github.com/nhatthm/grpcmock/assert" - "github.com/nhatthm/grpcmock/mock/planner" - "github.com/nhatthm/grpcmock/service" - testSrv "github.com/nhatthm/grpcmock/test" - "github.com/nhatthm/grpcmock/test/grpctest" + "go.nhat.io/grpcmock" + xassert "go.nhat.io/grpcmock/assert" + "go.nhat.io/grpcmock/mock/planner" + "go.nhat.io/grpcmock/service" + testSrv "go.nhat.io/grpcmock/test" + "go.nhat.io/grpcmock/test/grpctest" ) const ( @@ -201,7 +201,7 @@ func TestServer_ExpectUnary_Success(t *testing.T) { Name: "Foobar", } - grpcAssert.EqualMessage(t, expected, actual) + xassert.EqualMessage(t, expected, actual) assert.NoError(t, err) } @@ -307,7 +307,7 @@ func TestServer_ExpectServerStream_Success(t *testing.T) { assert.Len(t, actual, len(expected)) for i := 0; i < len(expected); i++ { - grpcAssert.EqualMessage(t, expected[i], actual[i]) + xassert.EqualMessage(t, expected[i], actual[i]) } } @@ -400,7 +400,7 @@ func TestServer_ExpectClientStream_Success(t *testing.T) { expected := &grpctest.CreateItemsResponse{NumItems: 1} assert.NoError(t, err) - grpcAssert.EqualMessage(t, expected, actual) + xassert.EqualMessage(t, expected, actual) } func TestServer_ExpectClientStream_CustomMatcher_Success(t *testing.T) { @@ -435,7 +435,7 @@ func TestServer_ExpectClientStream_CustomMatcher_Success(t *testing.T) { expected := &grpctest.CreateItemsResponse{NumItems: 1} assert.NoError(t, err) - grpcAssert.EqualMessage(t, expected, actual) + xassert.EqualMessage(t, expected, actual) } func TestServer_ExpectClientStream_MatchMsgCount_Mismatched(t *testing.T) { @@ -584,7 +584,7 @@ func TestServer_ExpectBidirectionalStream_Success(t *testing.T) { assert.Len(t, actual, len(expected)) for i := 0; i < len(expected); i++ { - grpcAssert.EqualMessage(t, expected[i], actual[i]) + xassert.EqualMessage(t, expected[i], actual[i]) } } diff --git a/service/service_test.go b/service/service_test.go index 95496f8..533279f 100644 --- a/service/service_test.go +++ b/service/service_test.go @@ -5,7 +5,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/nhatthm/grpcmock/service" + "go.nhat.io/grpcmock/service" ) func TestServiceMethod_FullName(t *testing.T) { diff --git a/stream/bidirectional_test.go b/stream/bidirectional_test.go index 7c44e66..89caac7 100644 --- a/stream/bidirectional_test.go +++ b/stream/bidirectional_test.go @@ -8,17 +8,17 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" - grpcAssert "github.com/nhatthm/grpcmock/assert" - grpcMock "github.com/nhatthm/grpcmock/mock/grpc" - "github.com/nhatthm/grpcmock/stream" - "github.com/nhatthm/grpcmock/test" - "github.com/nhatthm/grpcmock/test/grpctest" + xassert "go.nhat.io/grpcmock/assert" + xmock "go.nhat.io/grpcmock/mock/grpc" + "go.nhat.io/grpcmock/stream" + "go.nhat.io/grpcmock/test" + "go.nhat.io/grpcmock/test/grpctest" ) func TestSendAndRecvAll_SendError(t *testing.T) { t.Parallel() - s := grpcMock.MockClientStream(func(s *grpcMock.ClientStream) { + s := xmock.MockClientStream(func(s *xmock.ClientStream) { s.On("RecvMsg", mock.Anything).Maybe(). Return(io.EOF) @@ -37,7 +37,7 @@ func TestSendAndRecvAll_SendError(t *testing.T) { func TestSendAndRecvAll_RecvError(t *testing.T) { t.Parallel() - s := grpcMock.MockClientStream(func(s *grpcMock.ClientStream) { + s := xmock.MockClientStream(func(s *xmock.ClientStream) { s.On("RecvMsg", mock.Anything). Return(errors.New("recv error")) @@ -56,7 +56,7 @@ func TestSendAndRecvAll_RecvError(t *testing.T) { func TestSendAndRecvAll_CloseSendError(t *testing.T) { t.Parallel() - s := grpcMock.MockClientStream(func(s *grpcMock.ClientStream) { + s := xmock.MockClientStream(func(s *xmock.ClientStream) { s.On("RecvMsg", mock.Anything).Maybe(). Return(io.EOF) @@ -77,13 +77,13 @@ func TestSendAndRecvAll_Success_ClientStream(t *testing.T) { testCases := []struct { scenario string - mockStream grpcMock.ClientStreamMocker + mockStream xmock.ClientStreamMocker input []*grpctest.Item expectedResult []*grpctest.Item }{ { scenario: "send zero and receive zero", - mockStream: grpcMock.MockClientStream(func(s *grpcMock.ClientStream) { + mockStream: xmock.MockClientStream(func(s *xmock.ClientStream) { s.On("RecvMsg", mock.Anything). Return(io.EOF) @@ -94,7 +94,7 @@ func TestSendAndRecvAll_Success_ClientStream(t *testing.T) { }, { scenario: "send one and receive zero", - mockStream: grpcMock.MockClientStream(func(s *grpcMock.ClientStream) { + mockStream: xmock.MockClientStream(func(s *xmock.ClientStream) { s.On("RecvMsg", mock.Anything). Return(io.EOF) @@ -109,7 +109,7 @@ func TestSendAndRecvAll_Success_ClientStream(t *testing.T) { }, { scenario: "send zero and receive one", - mockStream: grpcMock.MockClientStream(func(s *grpcMock.ClientStream) { + mockStream: xmock.MockClientStream(func(s *xmock.ClientStream) { s.On("RecvMsg", mock.Anything).Once(). Run(func(args mock.Arguments) { out := args.Get(0).(*grpctest.Item) // nolint: errcheck @@ -140,7 +140,7 @@ func TestSendAndRecvAll_Success_ClientStream(t *testing.T) { assert.Equal(t, len(tc.expectedResult), len(result)) for i := 0; i < len(tc.expectedResult); i++ { - grpcAssert.EqualMessage(t, tc.expectedResult[i], result[i]) + xassert.EqualMessage(t, tc.expectedResult[i], result[i]) } }) } @@ -151,13 +151,13 @@ func TestSendAndRecvAll_Success_ServerStream(t *testing.T) { testCases := []struct { scenario string - mockStream grpcMock.ServerStreamMocker + mockStream xmock.ServerStreamMocker input []*grpctest.Item expectedResult []*grpctest.Item }{ { scenario: "send zero and receive zero", - mockStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("RecvMsg", mock.Anything). Return(io.EOF) }), @@ -165,7 +165,7 @@ func TestSendAndRecvAll_Success_ServerStream(t *testing.T) { }, { scenario: "send one and receive zero", - mockStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("RecvMsg", mock.Anything). Return(io.EOF) @@ -177,7 +177,7 @@ func TestSendAndRecvAll_Success_ServerStream(t *testing.T) { }, { scenario: "send zero and receive one", - mockStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("RecvMsg", mock.Anything).Once(). Run(func(args mock.Arguments) { out := args.Get(0).(*grpctest.Item) // nolint: errcheck @@ -205,7 +205,7 @@ func TestSendAndRecvAll_Success_ServerStream(t *testing.T) { assert.Equal(t, len(tc.expectedResult), len(result)) for i := 0; i < len(tc.expectedResult); i++ { - grpcAssert.EqualMessage(t, tc.expectedResult[i], result[i]) + xassert.EqualMessage(t, tc.expectedResult[i], result[i]) } }) } diff --git a/stream/buffer.go b/stream/buffer.go index 04bd411..93be210 100644 --- a/stream/buffer.go +++ b/stream/buffer.go @@ -7,7 +7,7 @@ import ( "google.golang.org/protobuf/proto" - grpcReflect "github.com/nhatthm/grpcmock/reflect" + xreflect "go.nhat.io/grpcmock/reflect" ) var _ SendReceiver = (*Buffer)(nil) @@ -24,12 +24,12 @@ func (b *Buffer) Len() int { // SendMsg persists the message into the Buffer. func (b *Buffer) SendMsg(m interface{}) error { - if !grpcReflect.IsPtr(m) { - if grpcReflect.IsNil(m) { - return fmt.Errorf("send msg error: %w", grpcReflect.ErrPtrIsNil) + if !xreflect.IsPtr(m) { + if xreflect.IsNil(m) { + return fmt.Errorf("send msg error: %w", xreflect.ErrPtrIsNil) } - return fmt.Errorf("send msg error: %w: %T", grpcReflect.ErrIsNotPtr, m) + return fmt.Errorf("send msg error: %w: %T", xreflect.ErrIsNotPtr, m) } if m, ok := m.(proto.Message); ok { @@ -43,12 +43,12 @@ func (b *Buffer) SendMsg(m interface{}) error { // RecvMsg returns the messages in buffer. func (b *Buffer) RecvMsg(m interface{}) error { - if !grpcReflect.IsPtr(m) { - if grpcReflect.IsNil(m) { - return fmt.Errorf("recv msg error: %w", grpcReflect.ErrPtrIsNil) + if !xreflect.IsPtr(m) { + if xreflect.IsNil(m) { + return fmt.Errorf("recv msg error: %w", xreflect.ErrPtrIsNil) } - return fmt.Errorf("recv msg error: %w: %T", grpcReflect.ErrIsNotPtr, m) + return fmt.Errorf("recv msg error: %w: %T", xreflect.ErrIsNotPtr, m) } if _, ok := m.(proto.Message); !ok { @@ -59,8 +59,8 @@ func (b *Buffer) RecvMsg(m interface{}) error { return io.EOF } - if grpcReflect.UnwrapType(b.buf[0]) != grpcReflect.UnwrapType(m) { - return fmt.Errorf("recv msg error: %w: got %T and %T", grpcReflect.ErrIsNotSameType, b.buf[0], m) + if xreflect.UnwrapType(b.buf[0]) != xreflect.UnwrapType(m) { + return fmt.Errorf("recv msg error: %w: got %T and %T", xreflect.ErrIsNotSameType, b.buf[0], m) } reflect.ValueOf(m).Elem(). diff --git a/stream/buffer_test.go b/stream/buffer_test.go index 93f2d10..40808b8 100644 --- a/stream/buffer_test.go +++ b/stream/buffer_test.go @@ -6,9 +6,9 @@ import ( "github.com/stretchr/testify/assert" - grpcAssert "github.com/nhatthm/grpcmock/assert" - "github.com/nhatthm/grpcmock/stream" - "github.com/nhatthm/grpcmock/test/grpctest" + xassert "go.nhat.io/grpcmock/assert" + "go.nhat.io/grpcmock/stream" + "go.nhat.io/grpcmock/test/grpctest" ) func TestBuffer_SendMsg_Error(t *testing.T) { @@ -135,7 +135,7 @@ func TestBuffer(t *testing.T) { expected := &grpctest.Item{Id: 42} - grpcAssert.EqualMessage(t, expected, actual) + xassert.EqualMessage(t, expected, actual) assert.ErrorIs(t, buf.RecvMsg(actual), io.EOF) } diff --git a/stream/receiever_test.go b/stream/receiever_test.go index 15a9508..649d3c8 100644 --- a/stream/receiever_test.go +++ b/stream/receiever_test.go @@ -9,17 +9,17 @@ import ( "github.com/stretchr/testify/mock" "google.golang.org/protobuf/proto" - grpcAssert "github.com/nhatthm/grpcmock/assert" - grpcMock "github.com/nhatthm/grpcmock/mock/grpc" - "github.com/nhatthm/grpcmock/stream" - "github.com/nhatthm/grpcmock/test" - "github.com/nhatthm/grpcmock/test/grpctest" + xassert "go.nhat.io/grpcmock/assert" + xmock "go.nhat.io/grpcmock/mock/grpc" + "go.nhat.io/grpcmock/stream" + "go.nhat.io/grpcmock/test" + "go.nhat.io/grpcmock/test/grpctest" ) func TestRecvAll(t *testing.T) { t.Parallel() - sendItems := func(s *grpcMock.ClientStream) { + sendItems := func(s *xmock.ClientStream) { for _, i := range test.DefaultItems() { i := i @@ -38,33 +38,33 @@ func TestRecvAll(t *testing.T) { testCases := []struct { scenario string - mockStream grpcMock.ClientStreamMocker + mockStream xmock.ClientStreamMocker output interface{} expectedOutput interface{} expectedError string }{ { scenario: "output is nil", - mockStream: grpcMock.NoMockClientStream, + mockStream: xmock.NoMockClientStream, expectedError: `not a pointer: `, }, { scenario: "output is not a pointer", - mockStream: grpcMock.NoMockClientStream, + mockStream: xmock.NoMockClientStream, output: grpctest.Item{}, expectedError: `not a pointer: grpctest.Item`, expectedOutput: grpctest.Item{}, }, { scenario: "output is not a slice", - mockStream: grpcMock.NoMockClientStream, + mockStream: xmock.NoMockClientStream, output: &grpctest.Item{}, expectedError: `not a slice: *grpctest.Item`, expectedOutput: &grpctest.Item{}, }, { scenario: "recv error", - mockStream: grpcMock.MockClientStream(func(s *grpcMock.ClientStream) { + mockStream: xmock.MockClientStream(func(s *xmock.ClientStream) { s.On("RecvMsg", mock.Anything). Return(errors.New("recv error")) }), @@ -74,7 +74,7 @@ func TestRecvAll(t *testing.T) { }, { scenario: "success with a slice of struct", - mockStream: grpcMock.MockClientStream(sendItems), + mockStream: xmock.MockClientStream(sendItems), output: &[]grpctest.Item{}, expectedOutput: &[]grpctest.Item{ { @@ -91,7 +91,7 @@ func TestRecvAll(t *testing.T) { }, { scenario: "success with a slice of pointer", - mockStream: grpcMock.MockClientStream(sendItems), + mockStream: xmock.MockClientStream(sendItems), output: &[]*grpctest.Item{}, expectedOutput: &[]*grpctest.Item{ { @@ -116,7 +116,7 @@ func TestRecvAll(t *testing.T) { result := tc.output err := stream.RecvAll(tc.mockStream(t), result) - grpcAssert.JSONEq(t, tc.expectedOutput, result) + xassert.JSONEq(t, tc.expectedOutput, result) if tc.expectedError == "" { assert.NoError(t, err) diff --git a/stream/receiver.go b/stream/receiver.go index 076ae06..726938c 100644 --- a/stream/receiver.go +++ b/stream/receiver.go @@ -5,7 +5,7 @@ import ( "io" "reflect" - grpcReflect "github.com/nhatthm/grpcmock/reflect" + xreflect "go.nhat.io/grpcmock/reflect" ) // Receiver is an interface wrapper around grpc.ClientStream and grpc.ServerStream. @@ -15,7 +15,7 @@ type Receiver interface { // RecvAll reads all messages using a receiver until io.EOF. func RecvAll(r Receiver, out interface{}) error { - outType, err := grpcReflect.UnwrapPtrSliceType(out) + outType, err := xreflect.UnwrapPtrSliceType(out) if err != nil { return err } @@ -34,7 +34,7 @@ func RecvAll(r Receiver, out interface{}) error { func recvAllMessages(r Receiver, out reflect.Value, msgType reflect.Type) (reflect.Value, error) { for { - msg := grpcReflect.New(msgType) + msg := xreflect.New(msgType) err := r.RecvMsg(msg) if errors.Is(err, io.EOF) { @@ -64,5 +64,5 @@ func newSliceMessageValue(t reflect.Type, v reflect.Value) reflect.Value { } func appendMessage(s reflect.Value, v interface{}) reflect.Value { - return reflect.Append(s, newSliceMessageValue(s.Type().Elem(), grpcReflect.UnwrapValue(v))) + return reflect.Append(s, newSliceMessageValue(s.Type().Elem(), xreflect.UnwrapValue(v))) } diff --git a/stream/sender.go b/stream/sender.go index 48cd2f2..535263f 100644 --- a/stream/sender.go +++ b/stream/sender.go @@ -4,7 +4,7 @@ import ( "fmt" "reflect" - grpcReflect "github.com/nhatthm/grpcmock/reflect" + xreflect "go.nhat.io/grpcmock/reflect" ) // Sender is an interface wrapper around grpc.ClientStream and grpc.ServerStream. @@ -21,14 +21,14 @@ type SendCloser interface { // SendAll sends all the messages from a given input. func SendAll(s Sender, in interface{}) error { - if !grpcReflect.IsSlice(in) { - return fmt.Errorf("%w: %T", grpcReflect.ErrIsNotSlice, in) + if !xreflect.IsSlice(in) { + return fmt.Errorf("%w: %T", xreflect.ErrIsNotSlice, in) } valueOf := reflect.ValueOf(in) for i := 0; i < valueOf.Len(); i++ { - msg := grpcReflect.NewValue(valueOf.Index(i).Interface()) + msg := xreflect.NewValue(valueOf.Index(i).Interface()) if err := s.SendMsg(msg); err != nil { return err diff --git a/stream/sender_test.go b/stream/sender_test.go index 79fff22..011980c 100644 --- a/stream/sender_test.go +++ b/stream/sender_test.go @@ -7,10 +7,10 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" - grpcMock "github.com/nhatthm/grpcmock/mock/grpc" - "github.com/nhatthm/grpcmock/stream" - "github.com/nhatthm/grpcmock/test" - "github.com/nhatthm/grpcmock/test/grpctest" + xmock "go.nhat.io/grpcmock/mock/grpc" + "go.nhat.io/grpcmock/stream" + "go.nhat.io/grpcmock/test" + "go.nhat.io/grpcmock/test/grpctest" ) func TestSendAll(t *testing.T) { @@ -18,24 +18,24 @@ func TestSendAll(t *testing.T) { testCases := []struct { scenario string - mockStream grpcMock.ClientStreamMocker + mockStream xmock.ClientStreamMocker input interface{} expectedError string }{ { scenario: "input is nil", - mockStream: grpcMock.NoMockClientStream, + mockStream: xmock.NoMockClientStream, expectedError: `not a slice: `, }, { scenario: "input is not a slice", - mockStream: grpcMock.NoMockClientStream, + mockStream: xmock.NoMockClientStream, input: &grpctest.Item{}, expectedError: `not a slice: *grpctest.Item`, }, { scenario: "send error", - mockStream: grpcMock.MockClientStream(func(s *grpcMock.ClientStream) { + mockStream: xmock.MockClientStream(func(s *xmock.ClientStream) { s.On("SendMsg", mock.Anything). Return(errors.New("send error")) }), @@ -44,7 +44,7 @@ func TestSendAll(t *testing.T) { }, { scenario: "success with a slice of struct", - mockStream: grpcMock.MockClientStream(func(s *grpcMock.ClientStream) { + mockStream: xmock.MockClientStream(func(s *xmock.ClientStream) { for _, i := range test.DefaultItems() { s.On("SendMsg", i).Once(). Return(nil) diff --git a/stream/stream_test.go b/stream/stream_test.go index 7648c07..347d11e 100644 --- a/stream/stream_test.go +++ b/stream/stream_test.go @@ -6,9 +6,9 @@ import ( "github.com/stretchr/testify/assert" - grpcMock "github.com/nhatthm/grpcmock/mock/grpc" - "github.com/nhatthm/grpcmock/stream" - "github.com/nhatthm/grpcmock/test/grpctest" + xmock "go.nhat.io/grpcmock/mock/grpc" + "go.nhat.io/grpcmock/stream" + "go.nhat.io/grpcmock/test/grpctest" ) func TestWrappedStream_SendMsg_Upstream(t *testing.T) { @@ -18,13 +18,13 @@ func TestWrappedStream_SendMsg_Upstream(t *testing.T) { testCases := []struct { scenario string - mockUpstream grpcMock.ServerStreamMocker - mockSender grpcMock.ServerStreamMocker + mockUpstream xmock.ServerStreamMocker + mockSender xmock.ServerStreamMocker expectedError error }{ { scenario: "upstream error", - mockUpstream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockUpstream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("SendMsg", msg). Return(errors.New("upstream error")) }), @@ -32,15 +32,15 @@ func TestWrappedStream_SendMsg_Upstream(t *testing.T) { }, { scenario: "upstream no error", - mockUpstream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockUpstream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("SendMsg", msg). Return(nil) }), }, { scenario: "sender error", - mockUpstream: grpcMock.NoMockServerStream, - mockSender: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockUpstream: xmock.NoMockServerStream, + mockSender: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("SendMsg", msg). Return(errors.New("sender error")) }), @@ -48,8 +48,8 @@ func TestWrappedStream_SendMsg_Upstream(t *testing.T) { }, { scenario: "sender no error", - mockUpstream: grpcMock.NoMockServerStream, - mockSender: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockUpstream: xmock.NoMockServerStream, + mockSender: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("SendMsg", msg). Return(nil) }), @@ -81,13 +81,13 @@ func TestWrappedStream_RecvMsg_Upstream(t *testing.T) { testCases := []struct { scenario string - mockUpstream grpcMock.ServerStreamMocker - mockReceiver grpcMock.ServerStreamMocker + mockUpstream xmock.ServerStreamMocker + mockReceiver xmock.ServerStreamMocker expectedError error }{ { scenario: "upstream error", - mockUpstream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockUpstream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("RecvMsg", msg). Return(errors.New("upstream error")) }), @@ -95,15 +95,15 @@ func TestWrappedStream_RecvMsg_Upstream(t *testing.T) { }, { scenario: "upstream no error", - mockUpstream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockUpstream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("RecvMsg", msg). Return(nil) }), }, { scenario: "receiver error", - mockUpstream: grpcMock.NoMockServerStream, - mockReceiver: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockUpstream: xmock.NoMockServerStream, + mockReceiver: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("RecvMsg", msg). Return(errors.New("receiver error")) }), @@ -111,8 +111,8 @@ func TestWrappedStream_RecvMsg_Upstream(t *testing.T) { }, { scenario: "receiver no error", - mockUpstream: grpcMock.NoMockServerStream, - mockReceiver: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockUpstream: xmock.NoMockServerStream, + mockReceiver: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("RecvMsg", msg). Return(nil) }), diff --git a/stream/tee_test.go b/stream/tee_test.go index 3e6cddd..ca4d23c 100644 --- a/stream/tee_test.go +++ b/stream/tee_test.go @@ -7,9 +7,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" - grpcMock "github.com/nhatthm/grpcmock/mock/grpc" - "github.com/nhatthm/grpcmock/stream" - "github.com/nhatthm/grpcmock/test/grpctest" + xmock "go.nhat.io/grpcmock/mock/grpc" + "go.nhat.io/grpcmock/stream" + "go.nhat.io/grpcmock/test/grpctest" ) func TestTeeReceiver_RecvMsg(t *testing.T) { @@ -17,26 +17,26 @@ func TestTeeReceiver_RecvMsg(t *testing.T) { testCases := []struct { scenario string - mockReceiver grpcMock.ServerStreamMocker - mockSender grpcMock.ServerStreamMocker + mockReceiver xmock.ServerStreamMocker + mockSender xmock.ServerStreamMocker expectedError error }{ { scenario: "recv error", - mockReceiver: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockReceiver: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("RecvMsg", &grpctest.Item{}). Return(errors.New("recv error")) }), - mockSender: grpcMock.NoMockServerStream, + mockSender: xmock.NoMockServerStream, expectedError: errors.New("recv error"), }, { scenario: "send error", - mockReceiver: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockReceiver: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("RecvMsg", &grpctest.Item{}). Return(nil) }), - mockSender: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockSender: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("SendMsg", &grpctest.Item{}). Return(errors.New("send error")) }), @@ -44,7 +44,7 @@ func TestTeeReceiver_RecvMsg(t *testing.T) { }, { scenario: "no error", - mockReceiver: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockReceiver: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("RecvMsg", &grpctest.Item{}). Run(func(args mock.Arguments) { out := args.Get(0).(*grpctest.Item) // nolint: errcheck @@ -52,7 +52,7 @@ func TestTeeReceiver_RecvMsg(t *testing.T) { }). Return(nil) }), - mockSender: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockSender: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("SendMsg", &grpctest.Item{Id: 42}). Return(nil) }), diff --git a/streamer/bidirectional_test.go b/streamer/bidirectional_test.go index a3a6a4b..76c5e62 100644 --- a/streamer/bidirectional_test.go +++ b/streamer/bidirectional_test.go @@ -6,8 +6,8 @@ import ( "github.com/stretchr/testify/assert" - "github.com/nhatthm/grpcmock/streamer" - "github.com/nhatthm/grpcmock/test/grpctest" + "go.nhat.io/grpcmock/streamer" + "go.nhat.io/grpcmock/test/grpctest" ) func TestBidirectionalStreamer_Types(t *testing.T) { diff --git a/streamer/client.go b/streamer/client.go index 50c9d62..1bfbf4e 100644 --- a/streamer/client.go +++ b/streamer/client.go @@ -5,8 +5,8 @@ import ( "google.golang.org/grpc" - grpcReflect "github.com/nhatthm/grpcmock/reflect" - "github.com/nhatthm/grpcmock/stream" + xreflect "go.nhat.io/grpcmock/reflect" + "go.nhat.io/grpcmock/stream" ) // ClientStreamer is a client-stream RPC. @@ -53,7 +53,7 @@ func TeeClientStreamer(s *ClientStreamer) *ClientStreamer { // ClientStreamerPayload tees the stream till io.EOF and return the payload. func ClientStreamerPayload(s *ClientStreamer) (interface{}, error) { s = TeeClientStreamer(s) - out := grpcReflect.NewSlicePtr(s.InputType()) + out := xreflect.NewSlicePtr(s.InputType()) err := stream.RecvAll(s, out) result := reflect.ValueOf(out).Elem() diff --git a/streamer/client_test.go b/streamer/client_test.go index ec0db5a..7fdc6be 100644 --- a/streamer/client_test.go +++ b/streamer/client_test.go @@ -10,10 +10,10 @@ import ( "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" - grpcAssert "github.com/nhatthm/grpcmock/assert" - grpcMock "github.com/nhatthm/grpcmock/mock/grpc" - "github.com/nhatthm/grpcmock/streamer" - "github.com/nhatthm/grpcmock/test/grpctest" + xassert "go.nhat.io/grpcmock/assert" + xmock "go.nhat.io/grpcmock/mock/grpc" + "go.nhat.io/grpcmock/streamer" + "go.nhat.io/grpcmock/test/grpctest" ) func TestClientStreamer_Types(t *testing.T) { @@ -30,7 +30,7 @@ func TestClientStreamer_Types(t *testing.T) { func TestTeeClientStreamer(t *testing.T) { t.Parallel() - s := grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + s := xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("RecvMsg", &grpctest.Item{}). Once(). Run(func(args mock.Arguments) { @@ -54,14 +54,14 @@ func TestTeeClientStreamer(t *testing.T) { // Wrapped stream could get the item normally. err := wrappedClientStream.RecvMsg(item1) - grpcAssert.EqualMessage(t, expected, item1) + xassert.EqualMessage(t, expected, item1) assert.NoError(t, err) // The item is sent to the buffer as well. item2 := &grpctest.Item{} err = clientStream.RecvMsg(item2) - grpcAssert.EqualMessage(t, expected, item2) + xassert.EqualMessage(t, expected, item2) assert.NoError(t, err) // Changing the item from the wrapped stream does not affect the one from buffer. @@ -83,13 +83,13 @@ func TestClientStreamerPayload(t *testing.T) { testCases := []struct { scenario string - mockStream grpcMock.ServerStreamMocker + mockStream xmock.ServerStreamMocker expectedResult []*grpctest.Item expectedError error }{ { scenario: "read error", - mockStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("RecvMsg", &grpctest.Item{}). Return(errors.New("recv error")) }), @@ -97,7 +97,7 @@ func TestClientStreamerPayload(t *testing.T) { }, { scenario: "success", - mockStream: grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + mockStream: xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("RecvMsg", &grpctest.Item{}).Once(). Run(func(args mock.Arguments) { msg := args.Get(0).(*grpctest.Item) // nolint: errcheck @@ -127,7 +127,7 @@ func TestClientStreamerPayload(t *testing.T) { assert.Equal(t, tc.expectedError, err) for i, m := range result { - grpcAssert.EqualMessage(t, tc.expectedResult[i], m) + xassert.EqualMessage(t, tc.expectedResult[i], m) } }) } diff --git a/streamer/server_test.go b/streamer/server_test.go index 6df7a02..1ce8161 100644 --- a/streamer/server_test.go +++ b/streamer/server_test.go @@ -6,7 +6,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/nhatthm/grpcmock/streamer" + "go.nhat.io/grpcmock/streamer" ) func TestServerStreamer_OutputType(t *testing.T) { diff --git a/test/bidirectional_stream.go b/test/bidirectional_stream.go index e51073a..378cc0b 100644 --- a/test/bidirectional_stream.go +++ b/test/bidirectional_stream.go @@ -4,21 +4,21 @@ import ( "reflect" "testing" - grpcMock "github.com/nhatthm/grpcmock/mock/grpc" - "github.com/nhatthm/grpcmock/streamer" - "github.com/nhatthm/grpcmock/test/grpctest" + xmock "go.nhat.io/grpcmock/mock/grpc" + "go.nhat.io/grpcmock/streamer" + "go.nhat.io/grpcmock/test/grpctest" ) // NoMockBidirectionalStreamer creates an empty mocked stream. var NoMockBidirectionalStreamer = MockTransformItemsStreamer() // MockTransformItemsStreamer creates a mocked stream for creating items. -func MockTransformItemsStreamer(mocks ...func(s *grpcMock.ServerStream)) func(t *testing.T) *streamer.BidirectionalStreamer { +func MockTransformItemsStreamer(mocks ...func(s *xmock.ServerStream)) func(t *testing.T) *streamer.BidirectionalStreamer { return func(t *testing.T) *streamer.BidirectionalStreamer { t.Helper() return streamer.NewBidirectionalStreamer( - grpcMock.MockServerStream(mocks...)(t), + xmock.MockServerStream(mocks...)(t), reflect.TypeOf(&grpctest.Item{}), reflect.TypeOf(&grpctest.Item{}), ) diff --git a/test/client_stream.go b/test/client_stream.go index 4e6111d..979c034 100644 --- a/test/client_stream.go +++ b/test/client_stream.go @@ -8,21 +8,21 @@ import ( "github.com/stretchr/testify/mock" "google.golang.org/protobuf/proto" - grpcMock "github.com/nhatthm/grpcmock/mock/grpc" - "github.com/nhatthm/grpcmock/streamer" - "github.com/nhatthm/grpcmock/test/grpctest" + xmock "go.nhat.io/grpcmock/mock/grpc" + "go.nhat.io/grpcmock/streamer" + "go.nhat.io/grpcmock/test/grpctest" ) // NoMockClientStreamer creates an empty mocked stream. var NoMockClientStreamer = MockCreateItemsStreamer() // MockCreateItemsStreamer creates a mocked stream for creating items. -func MockCreateItemsStreamer(mocks ...func(s *grpcMock.ServerStream)) func(t *testing.T) *streamer.ClientStreamer { +func MockCreateItemsStreamer(mocks ...func(s *xmock.ServerStream)) func(t *testing.T) *streamer.ClientStreamer { return func(t *testing.T) *streamer.ClientStreamer { t.Helper() return streamer.NewClientStreamer( - grpcMock.MockServerStream(mocks...)(t), + xmock.MockServerStream(mocks...)(t), reflect.TypeOf(&grpctest.Item{}), reflect.TypeOf(&grpctest.CreateItemsResponse{}), ) @@ -30,8 +30,8 @@ func MockCreateItemsStreamer(mocks ...func(s *grpcMock.ServerStream)) func(t *te } // MockStreamRecvItemSuccess mocks the stream to receive the given item. -func MockStreamRecvItemSuccess(i *grpctest.Item) func(s *grpcMock.ServerStream) { - return func(s *grpcMock.ServerStream) { +func MockStreamRecvItemSuccess(i *grpctest.Item) func(s *xmock.ServerStream) { + return func(s *xmock.ServerStream) { s.On("RecvMsg", &grpctest.Item{}).Once(). Run(func(args mock.Arguments) { item := args.Get(0).(*grpctest.Item) // nolint: errcheck @@ -43,8 +43,8 @@ func MockStreamRecvItemSuccess(i *grpctest.Item) func(s *grpcMock.ServerStream) } // MockStreamRecvItemsSuccess mocks the stream to receive the given items. -func MockStreamRecvItemsSuccess(items ...*grpctest.Item) func(s *grpcMock.ServerStream) { - return func(s *grpcMock.ServerStream) { +func MockStreamRecvItemsSuccess(items ...*grpctest.Item) func(s *xmock.ServerStream) { + return func(s *xmock.ServerStream) { for _, i := range items { MockStreamRecvItemSuccess(i)(s) } @@ -54,16 +54,16 @@ func MockStreamRecvItemsSuccess(items ...*grpctest.Item) func(s *grpcMock.Server } // MockStreamSendCreateItemsResponseSuccess mocks the stream to send grpctest.CreateItemsResponse. -func MockStreamSendCreateItemsResponseSuccess(numItems int64) func(s *grpcMock.ServerStream) { - return func(s *grpcMock.ServerStream) { +func MockStreamSendCreateItemsResponseSuccess(numItems int64) func(s *xmock.ServerStream) { + return func(s *xmock.ServerStream) { s.On("SendMsg", &grpctest.CreateItemsResponse{NumItems: numItems}).Once(). Return(nil) } } // MockStreamRecvItemEOF mocks the stream to return io.EOF. -func MockStreamRecvItemEOF() func(s *grpcMock.ServerStream) { - return func(s *grpcMock.ServerStream) { +func MockStreamRecvItemEOF() func(s *xmock.ServerStream) { + return func(s *xmock.ServerStream) { s.On("RecvMsg", &grpctest.Item{}).Once(). Return(io.EOF) } diff --git a/test/model.go b/test/model.go index b0769e9..42a8059 100644 --- a/test/model.go +++ b/test/model.go @@ -3,7 +3,7 @@ package test import ( "google.golang.org/protobuf/proto" - "github.com/nhatthm/grpcmock/test/grpctest" + "go.nhat.io/grpcmock/test/grpctest" ) // ItemBuilder builds an item. diff --git a/test/server_stream.go b/test/server_stream.go index 480f422..54c5362 100644 --- a/test/server_stream.go +++ b/test/server_stream.go @@ -4,37 +4,37 @@ import ( "reflect" "testing" - grpcMock "github.com/nhatthm/grpcmock/mock/grpc" - "github.com/nhatthm/grpcmock/streamer" - "github.com/nhatthm/grpcmock/test/grpctest" + xmock "go.nhat.io/grpcmock/mock/grpc" + "go.nhat.io/grpcmock/streamer" + "go.nhat.io/grpcmock/test/grpctest" ) // NoMockServerStreamer creates an empty mocked stream. var NoMockServerStreamer = MockListItemsStreamer() // MockListItemsStreamer creates a mocked stream for creating items. -func MockListItemsStreamer(mocks ...func(s *grpcMock.ServerStream)) func(t *testing.T) *streamer.ServerStreamer { +func MockListItemsStreamer(mocks ...func(s *xmock.ServerStream)) func(t *testing.T) *streamer.ServerStreamer { return func(t *testing.T) *streamer.ServerStreamer { t.Helper() return streamer.NewServerStreamer( - grpcMock.MockServerStream(mocks...)(t), + xmock.MockServerStream(mocks...)(t), reflect.TypeOf(&grpctest.Item{}), ) } } // MockStreamSendItemSuccess mocks the stream to send the given item. -func MockStreamSendItemSuccess(i *grpctest.Item) func(s *grpcMock.ServerStream) { - return func(s *grpcMock.ServerStream) { +func MockStreamSendItemSuccess(i *grpctest.Item) func(s *xmock.ServerStream) { + return func(s *xmock.ServerStream) { s.On("SendMsg", i).Once(). Return(nil) } } // MockStreamSendItemsSuccess mocks the stream to send the given items. -func MockStreamSendItemsSuccess(items ...*grpctest.Item) func(s *grpcMock.ServerStream) { - return func(s *grpcMock.ServerStream) { +func MockStreamSendItemsSuccess(items ...*grpctest.Item) func(s *xmock.ServerStream) { + return func(s *xmock.ServerStream) { for _, i := range items { MockStreamSendItemSuccess(i)(s) } diff --git a/test/service.go b/test/service.go index 7361359..229cdbc 100644 --- a/test/service.go +++ b/test/service.go @@ -8,8 +8,8 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/test/bufconn" - "github.com/nhatthm/grpcmock/service" - "github.com/nhatthm/grpcmock/test/grpctest" + "go.nhat.io/grpcmock/service" + "go.nhat.io/grpcmock/test/grpctest" ) var _ grpctest.ItemServiceServer = (*Service)(nil) diff --git a/value/value.go b/value/value.go index f3de14e..b1ce5b9 100644 --- a/value/value.go +++ b/value/value.go @@ -4,9 +4,9 @@ import ( "encoding/json" "fmt" - "github.com/nhatthm/grpcmock/errors" - "github.com/nhatthm/grpcmock/must" - "github.com/nhatthm/grpcmock/streamer" + "go.nhat.io/grpcmock/errors" + "go.nhat.io/grpcmock/must" + "go.nhat.io/grpcmock/streamer" ) // String returns the string value of the given object. diff --git a/value/value_test.go b/value/value_test.go index 312baf0..9cb9ad5 100644 --- a/value/value_test.go +++ b/value/value_test.go @@ -9,10 +9,10 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" - grpcMock "github.com/nhatthm/grpcmock/mock/grpc" - "github.com/nhatthm/grpcmock/streamer" - "github.com/nhatthm/grpcmock/test/grpctest" - "github.com/nhatthm/grpcmock/value" + xmock "go.nhat.io/grpcmock/mock/grpc" + "go.nhat.io/grpcmock/streamer" + "go.nhat.io/grpcmock/test/grpctest" + "go.nhat.io/grpcmock/value" ) func TestString_Success(t *testing.T) { @@ -97,7 +97,7 @@ func TestMarshal(t *testing.T) { }, { scenario: "client stream error", - in: streamer.NewClientStreamer(grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + in: streamer.NewClientStreamer(xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("RecvMsg", &grpctest.Item{}). Return(errors.New("recv error")) })(t), reflect.TypeOf(&grpctest.Item{}), reflect.TypeOf(&grpctest.CreateItemsResponse{})), @@ -105,7 +105,7 @@ func TestMarshal(t *testing.T) { }, { scenario: "client stream success", - in: streamer.NewClientStreamer(grpcMock.MockServerStream(func(s *grpcMock.ServerStream) { + in: streamer.NewClientStreamer(xmock.MockServerStream(func(s *xmock.ServerStream) { s.On("RecvMsg", &grpctest.Item{}). Once(). Run(func(args mock.Arguments) {