How to use EXPECT method of extra_import Package

Best Mock code snippet using extra_import.EXPECT

cmd_test.go

Source:cmd_test.go Github

copy

Full Screen

1// Copyright (c) 2018 Uber Technologies, Inc.2//3// Permission is hereby granted, free of charge, to any person obtaining a copy4// of this software and associated documentation files (the "Software"), to deal5// in the Software without restriction, including without limitation the rights6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell7// copies of the Software, and to permit persons to whom the Software is8// furnished to do so, subject to the following conditions:9//10// The above copyright notice and this permission notice shall be included in11// all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN19// THE SOFTWARE.20package cmd21import (22	"bytes"23	"context"24	"io"25	"io/ioutil"26	"net"27	"os"28	"strings"29	"sync"30	"testing"31	"github.com/stretchr/testify/assert"32	"github.com/stretchr/testify/require"33	"github.com/tgrpc/prototool/internal/x/cmd/testdata/grpc/gen/grpcpb"34	"google.golang.org/grpc"35)36const cleanEnvKey = "PROTOTOOL_TEST_CLEAN_CACHE"37var (38	testCleaned bool39	testLock    sync.Mutex40)41func TestCompile(t *testing.T) {42	t.Parallel()43	assertDoCompileFiles(44		t,45		false,46		`testdata/compile/dep_errors.proto:6:1:Expected ";".`,47		"testdata/compile/dep_errors.proto",48	)49	assertDoCompileFiles(50		t,51		false,52		`dep_errors.proto:6:1:Expected ";".53		testdata/compile/errors_on_import.proto:10:3:"foo.DepError" is not defined.`,54		"testdata/compile/errors_on_import.proto",55	)56	assertDoCompileFiles(57		t,58		false,59		`testdata/compile/extra_import.proto:1:1:Import "dep.proto" was not used.`,60		"testdata/compile/extra_import.proto",61	)62	assertDoCompileFiles(63		t,64		false,65		`testdata/compile/json_camel_case_conflict.proto:1:1:The JSON camel-case name of field "helloworld" conflicts with field "helloWorld". This is not allowed in proto3.`,66		"testdata/compile/json_camel_case_conflict.proto",67	)68	assertDoCompileFiles(69		t,70		false,71		`testdata/compile/missing_package_semicolon.proto:5:1:Expected ";".`,72		"testdata/compile/missing_package_semicolon.proto",73	)74	assertDoCompileFiles(75		t,76		false,77		`testdata/compile/missing_syntax.proto:1:1:No syntax specified. Please use 'syntax = "proto2";' or 'syntax = "proto3";' to specify a syntax version.78		testdata/compile/missing_syntax.proto:4:3:Expected "required", "optional", or "repeated".`,79		"testdata/compile/missing_syntax.proto",80	)81	assertDoCompileFiles(82		t,83		true,84		``,85		"testdata/compile/syntax_proto2.proto",86	)87	assertDoCompileFiles(88		t,89		false,90		`testdata/compile/not_imported.proto:11:3:"foo.Dep" seems to be defined in "dep.proto", which is not imported by "not_imported.proto".  To use it here, please add the necessary import.`,91		"testdata/compile/dep.proto",92		"testdata/compile/not_imported.proto",93	)94}95func TestLint(t *testing.T) {96	t.Parallel()97	assertDoLintFile(98		t,99		true,100		"",101		"testdata/foo/success.proto",102	)103	assertDoLintFile(104		t,105		false,106		"1:1:SYNTAX_PROTO3",107		"testdata/lint/syntax_proto2.proto",108	)109	assertDoLintFile(110		t,111		false,112		"11:1:MESSAGE_NAMES_CAPITALIZED",113		"testdata/lint/message_name_not_capitalized.proto",114	)115	assertDoLintFile(116		t,117		false,118		`1:1:FILE_OPTIONS_REQUIRE_GO_PACKAGE119		1:1:FILE_OPTIONS_REQUIRE_JAVA_PACKAGE`,120		"testdata/lint/file_options_required.proto",121	)122	assertDoLintFile(123		t,124		false,125		`5:1:FILE_OPTIONS_EQUAL_GO_PACKAGE_PB_SUFFIX126		8:1:FILE_OPTIONS_EQUAL_JAVA_PACKAGE_COM_PB`,127		"testdata/lint/file_options_incorrect.proto",128	)129	assertDoLintFiles(130		t,131		false,132		`testdata/lint/samedir/bar1.proto:1:1:PACKAGES_SAME_IN_DIR133		testdata/lint/samedir/foo1.proto:1:1:PACKAGES_SAME_IN_DIR134		testdata/lint/samedir/foo2.proto:1:1:PACKAGES_SAME_IN_DIR`,135		"testdata/lint/samedir",136	)137	assertDoLintFiles(138		t,139		false,140		`testdata/lint/samedirgopkg/bar1.proto:1:1:FILE_OPTIONS_GO_PACKAGE_SAME_IN_DIR141		testdata/lint/samedirgopkg/foo1.proto:1:1:FILE_OPTIONS_GO_PACKAGE_SAME_IN_DIR142		testdata/lint/samedirgopkg/foo2.proto:1:1:FILE_OPTIONS_GO_PACKAGE_SAME_IN_DIR`,143		"testdata/lint/samedirgopkg",144	)145	assertDoLintFiles(146		t,147		false,148		`testdata/lint/samedirjavapkg/bar1.proto:1:1:FILE_OPTIONS_JAVA_PACKAGE_SAME_IN_DIR149		testdata/lint/samedirjavapkg/foo1.proto:1:1:FILE_OPTIONS_JAVA_PACKAGE_SAME_IN_DIR150		testdata/lint/samedirjavapkg/foo2.proto:1:1:FILE_OPTIONS_JAVA_PACKAGE_SAME_IN_DIR`,151		"testdata/lint/samedirjavapkg",152	)153	assertDoLintFile(154		t,155		false,156		`1:1:FILE_OPTIONS_REQUIRE_GO_PACKAGE157		1:1:FILE_OPTIONS_REQUIRE_JAVA_PACKAGE158		3:1:PACKAGE_LOWER_SNAKE_CASE159		7:1:MESSAGE_NAMES_CAPITALIZED160		9:1:MESSAGE_NAMES_CAMEL_CASE161		12:3:MESSAGE_FIELD_NAMES_LOWER_SNAKE_CASE162		13:3:MESSAGE_FIELD_NAMES_LOWER_SNAKE_CASE163		14:3:MESSAGE_FIELD_NAMES_LOWER_SNAKE_CASE164		15:3:MESSAGE_FIELD_NAMES_LOWER_SNAKE_CASE165		22:3:COMMENTS_NO_C_STYLE166		23:3:COMMENTS_NO_C_STYLE167		26:1:SERVICE_NAMES_CAPITALIZED168		28:1:SERVICE_NAMES_CAMEL_CASE169		46:3:REQUEST_RESPONSE_TYPES_UNIQUE170		46:3:REQUEST_RESPONSE_TYPES_UNIQUE171		47:3:REQUEST_RESPONSE_TYPES_UNIQUE172		47:3:REQUEST_RESPONSE_TYPES_UNIQUE173		48:3:RPC_NAMES_CAPITALIZED174		49:3:REQUEST_RESPONSE_TYPES_IN_SAME_FILE175		49:3:REQUEST_RESPONSE_TYPES_UNIQUE176		50:3:REQUEST_RESPONSE_TYPES_IN_SAME_FILE177		50:3:REQUEST_RESPONSE_TYPES_IN_SAME_FILE178		50:3:REQUEST_RESPONSE_TYPES_UNIQUE179		58:3:ENUM_FIELD_PREFIXES180		64:7:ENUM_FIELD_PREFIXES181		64:7:ENUM_ZERO_VALUES_INVALID182		67:7:ENUM_ZERO_VALUES_INVALID183		73:3:ENUM_ZERO_VALUES_INVALID`,184		"testdata/lint/lots.proto",185	)186}187func TestGoldenFormat(t *testing.T) {188	t.Parallel()189	assertGoldenFormat(t, false, "testdata/format/bar/bar.proto")190	assertGoldenFormat(t, false, "testdata/format/bar/bar_proto2.proto")191	assertGoldenFormat(t, false, "testdata/format/foo/foo.proto")192	assertGoldenFormat(t, false, "testdata/format/foo/foo_proto2.proto")193}194func TestJSONToBinaryToJSON(t *testing.T) {195	t.Parallel()196	assertJSONToBinaryToJSON(t, "testdata/foo/success.proto", "foo.Baz", `{"hello":100}`)197}198func TestGRPC(t *testing.T) {199	t.Parallel()200	assertGRPC(t,201		0,202		`203		{204			"value": "hello!"205		}206		`,207		"testdata/grpc/grpc.proto",208		"grpc.ExcitedService/Exclamation",209		`{"value":"hello"}`,210	)211	assertGRPC(t,212		0,213		`214		{215			"value": "hellosalutations!"216		}217		`,218		"testdata/grpc/grpc.proto",219		"grpc.ExcitedService/ExclamationClientStream",220		`{"value":"hello"}221		{"value":"salutations"}`,222	)223	assertGRPC(t,224		0,225		`226		{227			"value": "h"228		}229		{230			"value": "e"231		}232		{233			"value": "l"234		}235		{236			"value": "l"237		}238		{239			"value": "o"240		}241		{242			"value": "!"243		}244		`,245		"testdata/grpc/grpc.proto",246		"grpc.ExcitedService/ExclamationServerStream",247		`{"value":"hello"}`,248	)249	assertGRPC(t,250		0,251		`252		{253			"value": "hello!"254		}255		{256			"value": "salutations!"257		}258		`,259		"testdata/grpc/grpc.proto",260		"grpc.ExcitedService/ExclamationBidiStream",261		`{"value":"hello"}262		{"value":"salutations"}`,263	)264}265func assertDoCompileFiles(t *testing.T, expectSuccess bool, expectedLinePrefixes string, filePaths ...string) {266	lines := getCleanLines(expectedLinePrefixes)267	expectedExitCode := 0268	if !expectSuccess {269		expectedExitCode = 255270	}271	assertDo(t, expectedExitCode, strings.Join(lines, "\n"), append([]string{"compile"}, filePaths...)...)272}273func assertDoLintFile(t *testing.T, expectSuccess bool, expectedLinePrefixesWithoutFile string, filePath string) {274	lines := getCleanLines(expectedLinePrefixesWithoutFile)275	for i, line := range lines {276		lines[i] = filePath + ":" + line277	}278	expectedExitCode := 0279	if !expectSuccess {280		expectedExitCode = 255281	}282	assertDo(t, expectedExitCode, strings.Join(lines, "\n"), "lint", filePath)283}284func assertDoLintFiles(t *testing.T, expectSuccess bool, expectedLinePrefixes string, filePaths ...string) {285	lines := getCleanLines(expectedLinePrefixes)286	expectedExitCode := 0287	if !expectSuccess {288		expectedExitCode = 255289	}290	assertDo(t, expectedExitCode, strings.Join(lines, "\n"), append([]string{"lint"}, filePaths...)...)291}292func assertGoldenFormat(t *testing.T, expectSuccess bool, filePath string) {293	output, exitCode := testDo(t, "format", filePath)294	expectedExitCode := 0295	if !expectSuccess {296		expectedExitCode = 255297	}298	assert.Equal(t, expectedExitCode, exitCode)299	golden, err := ioutil.ReadFile(filePath + ".golden")300	assert.NoError(t, err)301	assert.Equal(t, strings.TrimSpace(string(golden)), output)302}303func assertJSONToBinaryToJSON(t *testing.T, filePath string, messagePath string, jsonData string) {304	stdout, exitCode := testDo(t, "json-to-binary", filePath, messagePath, jsonData)305	assert.Equal(t, 0, exitCode)306	stdout, exitCode = testDo(t, "binary-to-json", filePath, messagePath, stdout)307	assert.Equal(t, 0, exitCode)308	assert.Equal(t, jsonData, stdout)309}310func assertGRPC(t *testing.T, expectedExitCode int, expectedLinePrefixes string, filePath string, method string, jsonData string) {311	excitedTestCase := startExcitedTestCase(t)312	defer excitedTestCase.Close()313	assertDoStdin(t, strings.NewReader(jsonData), expectedExitCode, expectedLinePrefixes, "grpc", filePath, excitedTestCase.Address(), method, "-")314}315func assertDoStdin(t *testing.T, stdin io.Reader, expectedExitCode int, expectedLinePrefixes string, args ...string) {316	assertDoInternal(t, stdin, expectedExitCode, expectedLinePrefixes, args...)317}318func assertDo(t *testing.T, expectedExitCode int, expectedLinePrefixes string, args ...string) {319	assertDoInternal(t, nil, expectedExitCode, expectedLinePrefixes, args...)320}321func testDoStdin(t *testing.T, stdin io.Reader, args ...string) (string, int) {322	testDownload(t)323	return testDoInternal(stdin, args...)324}325func testDo(t *testing.T, args ...string) (string, int) {326	testDownload(t)327	return testDoInternal(nil, args...)328}329func getCleanLines(output string) []string {330	var lines []string331	for _, line := range strings.Split(strings.TrimSpace(output), "\n") {332		line = strings.TrimSpace(line)333		if line == "" {334			continue335		}336		lines = append(lines, line)337	}338	return lines339}340type excitedTestCase struct {341	listener      net.Listener342	grpcServer    *grpc.Server343	excitedServer *excitedServer344}345func startExcitedTestCase(t *testing.T) *excitedTestCase {346	listener, err := getFreeListener()347	require.NoError(t, err)348	grpcServer := grpc.NewServer()349	excitedServer := newExcitedServer()350	grpcpb.RegisterExcitedServiceServer(grpcServer, excitedServer)351	go func() { _ = grpcServer.Serve(listener) }()352	return &excitedTestCase{353		listener:      listener,354		grpcServer:    grpcServer,355		excitedServer: excitedServer,356	}357}358func (c *excitedTestCase) Address() string {359	if c.listener == nil {360		return ""361	}362	return c.listener.Addr().String()363}364func (c *excitedTestCase) Close() {365	if c.grpcServer != nil {366		c.grpcServer.Stop()367	}368}369type excitedServer struct{}370func newExcitedServer() *excitedServer {371	return &excitedServer{}372}373func (s *excitedServer) Exclamation(ctx context.Context, request *grpcpb.ExclamationRequest) (*grpcpb.ExclamationResponse, error) {374	return &grpcpb.ExclamationResponse{375		Value: request.Value + "!",376	}, nil377}378func (s *excitedServer) ExclamationClientStream(streamServer grpcpb.ExcitedService_ExclamationClientStreamServer) error {379	value := ""380	for request, err := streamServer.Recv(); err != io.EOF; request, err = streamServer.Recv() {381		if err != nil {382			return err383		}384		value += request.Value385	}386	return streamServer.SendAndClose(&grpcpb.ExclamationResponse{387		Value: value + "!",388	})389}390func (s *excitedServer) ExclamationServerStream(request *grpcpb.ExclamationRequest, streamServer grpcpb.ExcitedService_ExclamationServerStreamServer) error {391	for _, c := range request.Value {392		if err := streamServer.Send(&grpcpb.ExclamationResponse{393			Value: string(c),394		}); err != nil {395			return err396		}397	}398	return streamServer.Send(&grpcpb.ExclamationResponse{399		Value: "!",400	})401}402func (s *excitedServer) ExclamationBidiStream(streamServer grpcpb.ExcitedService_ExclamationBidiStreamServer) error {403	for request, err := streamServer.Recv(); err != io.EOF; request, err = streamServer.Recv() {404		if err != nil {405			return err406		}407		if err := streamServer.Send(&grpcpb.ExclamationResponse{408			Value: request.Value + "!",409		}); err != nil {410			return err411		}412	}413	return nil414}415// do not use these in tests416func assertDoInternal(t *testing.T, stdin io.Reader, expectedExitCode int, expectedLinePrefixes string, args ...string) {417	stdout, exitCode := testDoStdin(t, stdin, args...)418	assert.Equal(t, expectedExitCode, exitCode)419	expectedLinePrefixesSplit := getCleanLines(expectedLinePrefixes)420	outputSplit := getCleanLines(stdout)421	require.Equal(t, len(expectedLinePrefixesSplit), len(outputSplit), strings.Join(outputSplit, "\n"))422	for i, expectedLinePrefix := range expectedLinePrefixesSplit {423		assert.True(t, strings.HasPrefix(outputSplit[i], expectedLinePrefix), "%s %d %s", expectedLinePrefix, i, strings.Join(outputSplit, "\n"))424	}425}426func testDownload(t *testing.T) {427	testLock.Lock()428	defer testLock.Unlock()429	if os.Getenv(cleanEnvKey) != "" {430		if !testCleaned {431			testCleaned = true432			stdout, exitCode := testDoInternal(nil, "clean")433			require.Equal(t, 0, exitCode, "had non-zero exit code when cleaning")434			require.Equal(t, "", stdout, "had output when cleaning")435		}436	}437	stdout, exitCode := testDoInternal(nil, "download")438	require.Equal(t, 0, exitCode, "had non-zero exit code when downloading: %s", stdout)439}440func testDoInternal(stdin io.Reader, args ...string) (string, int) {441	args = append(args,442		//"--debug",443		"--print-fields", "filename:line:column:id:message",444	)445	if stdin == nil {446		stdin = os.Stdin447	}448	buffer := bytes.NewBuffer(nil)449	exitCode := Do(args, stdin, buffer, os.Stderr)450	return strings.TrimSpace(buffer.String()), exitCode451}452func getFreeListener() (net.Listener, error) {453	address, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")454	if err != nil {455		return nil, err456	}457	return net.ListenTCP("tcp", address)458}...

Full Screen

Full Screen

EXPECT

Using AI Code Generation

copy

Full Screen

1import (2func main() {3    g := gomega.NewGomegaWithT(t)4    g.Expect(1).To(gomega.BeEquivalentTo(1))5    fmt.Println("Hello, playground")6}7import (8func main() {9    g := gomega.NewGomegaWithT(t)10    g.Expect(1).To(gomega.BeEquivalentTo(1))11    fmt.Println("Hello, playground")12}13import (14func main() {15    g := gomega.NewGomegaWithT(t)16    g.Expect(1).To(gomega.BeEquivalentTo(1))17    fmt.Println("Hello, playground")18}19import (20func main() {21    g := gomega.NewGomegaWithT(t)22    g.Expect(1).To(gomega.BeEquivalentTo(1))23    fmt.Println("Hello, playground")24}25import (26func main() {27    g := gomega.NewGomegaWithT(t)28    g.Expect(1).To(gomega.BeEquivalentTo(1))29    fmt.Println("Hello, playground")30}31import (32func main() {33    g := gomega.NewGomegaWithT(t)34    g.Expect(1).To(gomega.BeEquivalentTo(1))35    fmt.Println("Hello, playground")36}

Full Screen

Full Screen

EXPECT

Using AI Code Generation

copy

Full Screen

1import (2"extra_import"3func main() {4fmt.Println("Hello, World!")5fmt.Println(extra_import.EXPECT("Hello, World!"))6}7import (8"extra_import"9func main() {10fmt.Println("Hello, World!")11fmt.Println(extra_import.EXPECT("Hello, World!"))12}13import (14"extra_import"15func main() {16fmt.Println("Hello, World!")17fmt.Println(extra_import.EXPECT("Hello, World!"))18}19import (20"extra_import"21func main() {22fmt.Println("Hello, World!")23fmt.Println(extra_import.EXPECT("Hello, World!"))24}25import (26"extra_import"27func main() {28fmt.Println("Hello, World!")29fmt.Println(extra_import.EXPECT("Hello, World!"))30}31import (32"extra_import"33func main() {34fmt.Println("Hello, World!")35fmt.Println(extra_import.EXPECT("Hello, World!"))36}37import (38"extra_import"39func main() {40fmt.Println("Hello, World!")41fmt.Println(extra_import.EXPECT("Hello, World!"))42}43import (44"extra_import"45func main() {46fmt.Println("Hello, World!")47fmt.Println(extra_import.EXPECT("Hello, World!"))48}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Mock automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful