1// Copyright 2020 Google LLC 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// https://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package mock 16 17import ( 18 "context" 19 "testing" 20 21 "github.com/googleapis/gax-go/v2" 22 translatepb "google.golang.org/genproto/googleapis/cloud/translate/v3" 23) 24 25// mockClient fullfills the TranslationClient interface and is used as a mock 26// standin for a `translate.Client` that is only used to TranslateText. 27type mockClient struct{} 28 29func (*mockClient) TranslateText(_ context.Context, req *translatepb.TranslateTextRequest, opts ...gax.CallOption) (*translatepb.TranslateTextResponse, error) { 30 resp := &translatepb.TranslateTextResponse{ 31 Translations: []*translatepb.Translation{ 32 {TranslatedText: "Hello World"}, 33 }, 34 } 35 return resp, nil 36} 37 38func TestTranslateTextWithInterfaceClient(t *testing.T) { 39 client := &mockClient{} 40 text, err := TranslateTextWithInterfaceClient(client, "Hola Mundo", "en-US") 41 if err != nil { 42 t.Fatal(err) 43 } 44 if text != "Hello World" { 45 t.Fatalf("got %q, want Hello World", text) 46 } 47} 48