How to use InOrder method of gomock Package

Best Mock code snippet using gomock.InOrder

snapshot_restore_test.go

Source:snapshot_restore_test.go Github

copy

Full Screen

...38			args: &types.CSISnapshotRestoreArgs{39				Cleanup: true,40			},41			prepare: func(f *fields) {42				gomock.InOrder(43					f.stepperOps.EXPECT().ValidateArgs(gomock.Any(), gomock.Any()).Return(nil),44					f.stepperOps.EXPECT().CreateApplication(gomock.Any(), gomock.Any(), gomock.Any()).Return(45						&v1.Pod{46							ObjectMeta: metav1.ObjectMeta{47								Name:      "pod1",48								Namespace: "ns",49							},50						},51						&v1.PersistentVolumeClaim{52							ObjectMeta: metav1.ObjectMeta{53								Name:      "pvc1",54								Namespace: "ns",55							},56						},57						nil,58					),59					f.stepperOps.EXPECT().ValidateData(gomock.Any(), &v1.Pod{60						ObjectMeta: metav1.ObjectMeta{61							Name:      "pod1",62							Namespace: "ns",63						},64					}, gomock.Any()).Return(nil),65					f.stepperOps.EXPECT().SnapshotApplication(gomock.Any(), gomock.Any(),66						&v1.PersistentVolumeClaim{67							ObjectMeta: metav1.ObjectMeta{68								Name:      "pvc1",69								Namespace: "ns",70							},71						}, gomock.Any(),72					).Return(73						&snapv1.VolumeSnapshot{74							ObjectMeta: metav1.ObjectMeta{75								Name: "snapshot",76							},77						},78						nil,79					),80					f.stepperOps.EXPECT().RestoreApplication(gomock.Any(), gomock.Any(),81						&snapv1.VolumeSnapshot{82							ObjectMeta: metav1.ObjectMeta{83								Name: "snapshot",84							},85						},86					).Return(87						&v1.Pod{88							ObjectMeta: metav1.ObjectMeta{89								Name:      "pod2",90								Namespace: "ns",91							},92						},93						&v1.PersistentVolumeClaim{94							ObjectMeta: metav1.ObjectMeta{95								Name:      "pvc2",96								Namespace: "ns",97							},98						},99						nil,100					),101					f.stepperOps.EXPECT().ValidateData(gomock.Any(), &v1.Pod{102						ObjectMeta: metav1.ObjectMeta{103							Name:      "pod2",104							Namespace: "ns",105						},106					}, gomock.Any()).Return(nil),107					f.stepperOps.EXPECT().Cleanup(gomock.Any(), gomock.Any()).Return(),108				)109			},110			result: &types.CSISnapshotRestoreResults{111				OriginalPVC: &v1.PersistentVolumeClaim{112					ObjectMeta: metav1.ObjectMeta{113						Name:      "pvc1",114						Namespace: "ns",115					},116				},117				OriginalPod: &v1.Pod{118					ObjectMeta: metav1.ObjectMeta{119						Name:      "pod1",120						Namespace: "ns",121					},122				},123				ClonedPVC: &v1.PersistentVolumeClaim{124					ObjectMeta: metav1.ObjectMeta{125						Name:      "pvc2",126						Namespace: "ns",127					},128				},129				ClonedPod: &v1.Pod{130					ObjectMeta: metav1.ObjectMeta{131						Name:      "pod2",132						Namespace: "ns",133					},134				},135				Snapshot: &snapv1.VolumeSnapshot{136					ObjectMeta: metav1.ObjectMeta{137						Name: "snapshot",138					},139				},140			},141			errChecker: IsNil,142		},143		{ // no cleanup144			kubeCli: fake.NewSimpleClientset(),145			dynCli:  fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),146			args: &types.CSISnapshotRestoreArgs{147				Cleanup: false,148			},149			prepare: func(f *fields) {150				gomock.InOrder(151					f.stepperOps.EXPECT().ValidateArgs(gomock.Any(), gomock.Any()).Return(nil),152					f.stepperOps.EXPECT().CreateApplication(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil, nil),153					f.stepperOps.EXPECT().ValidateData(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil),154					f.stepperOps.EXPECT().SnapshotApplication(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil),155					f.stepperOps.EXPECT().RestoreApplication(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil, nil),156					f.stepperOps.EXPECT().ValidateData(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil),157				)158			},159			result:     &types.CSISnapshotRestoreResults{},160			errChecker: IsNil,161		},162		{ // restored data validation fails163			kubeCli: fake.NewSimpleClientset(),164			dynCli:  fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),165			args: &types.CSISnapshotRestoreArgs{166				Cleanup: false,167			},168			prepare: func(f *fields) {169				gomock.InOrder(170					f.stepperOps.EXPECT().ValidateArgs(gomock.Any(), gomock.Any()).Return(nil),171					f.stepperOps.EXPECT().CreateApplication(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil, nil),172					f.stepperOps.EXPECT().ValidateData(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil),173					f.stepperOps.EXPECT().SnapshotApplication(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil),174					f.stepperOps.EXPECT().RestoreApplication(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil, nil),175					f.stepperOps.EXPECT().ValidateData(gomock.Any(), gomock.Any(), gomock.Any()).Return(fmt.Errorf("validation error")),176				)177			},178			result:     &types.CSISnapshotRestoreResults{},179			errChecker: NotNil,180		},181		{ // restore error, objects still returned182			kubeCli: fake.NewSimpleClientset(),183			dynCli:  fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),184			args: &types.CSISnapshotRestoreArgs{185				Cleanup: false,186			},187			prepare: func(f *fields) {188				gomock.InOrder(189					f.stepperOps.EXPECT().ValidateArgs(gomock.Any(), gomock.Any()).Return(nil),190					f.stepperOps.EXPECT().CreateApplication(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil, nil),191					f.stepperOps.EXPECT().ValidateData(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil),192					f.stepperOps.EXPECT().SnapshotApplication(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil),193					f.stepperOps.EXPECT().RestoreApplication(gomock.Any(), gomock.Any(), gomock.Any()).Return(194						&v1.Pod{195							ObjectMeta: metav1.ObjectMeta{196								Name:      "pod2",197								Namespace: "ns",198							},199						},200						&v1.PersistentVolumeClaim{201							ObjectMeta: metav1.ObjectMeta{202								Name:      "pvc2",203								Namespace: "ns",204							},205						},206						fmt.Errorf("restore error"),207					),208				)209			},210			result: &types.CSISnapshotRestoreResults{211				ClonedPVC: &v1.PersistentVolumeClaim{212					ObjectMeta: metav1.ObjectMeta{213						Name:      "pvc2",214						Namespace: "ns",215					},216				},217				ClonedPod: &v1.Pod{218					ObjectMeta: metav1.ObjectMeta{219						Name:      "pod2",220						Namespace: "ns",221					},222				},223			},224			errChecker: NotNil,225		},226		{ // restore error, no objects returned227			kubeCli: fake.NewSimpleClientset(),228			dynCli:  fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),229			args: &types.CSISnapshotRestoreArgs{230				Cleanup: false,231			},232			prepare: func(f *fields) {233				gomock.InOrder(234					f.stepperOps.EXPECT().ValidateArgs(gomock.Any(), gomock.Any()).Return(nil),235					f.stepperOps.EXPECT().CreateApplication(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil, nil),236					f.stepperOps.EXPECT().ValidateData(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil),237					f.stepperOps.EXPECT().SnapshotApplication(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil),238					f.stepperOps.EXPECT().RestoreApplication(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil, fmt.Errorf("restore error")),239				)240			},241			result:     &types.CSISnapshotRestoreResults{},242			errChecker: NotNil,243		},244		{ // snapshot error, object still returned245			kubeCli: fake.NewSimpleClientset(),246			dynCli:  fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),247			args: &types.CSISnapshotRestoreArgs{248				Cleanup: false,249			},250			prepare: func(f *fields) {251				gomock.InOrder(252					f.stepperOps.EXPECT().ValidateArgs(gomock.Any(), gomock.Any()).Return(nil),253					f.stepperOps.EXPECT().CreateApplication(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil, nil),254					f.stepperOps.EXPECT().ValidateData(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil),255					f.stepperOps.EXPECT().SnapshotApplication(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(256						&snapv1.VolumeSnapshot{257							ObjectMeta: metav1.ObjectMeta{258								Name: "snapshot",259							},260						},261						fmt.Errorf("snapshot error"),262					),263				)264			},265			result: &types.CSISnapshotRestoreResults{266				Snapshot: &snapv1.VolumeSnapshot{267					ObjectMeta: metav1.ObjectMeta{268						Name: "snapshot",269					},270				},271			},272			errChecker: NotNil,273		},274		{ // snapshot error, object not returned275			kubeCli: fake.NewSimpleClientset(),276			dynCli:  fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),277			args: &types.CSISnapshotRestoreArgs{278				Cleanup: false,279			},280			prepare: func(f *fields) {281				gomock.InOrder(282					f.stepperOps.EXPECT().ValidateArgs(gomock.Any(), gomock.Any()).Return(nil),283					f.stepperOps.EXPECT().CreateApplication(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil, nil),284					f.stepperOps.EXPECT().ValidateData(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil),285					f.stepperOps.EXPECT().SnapshotApplication(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("snapshot error")),286				)287			},288			result:     &types.CSISnapshotRestoreResults{},289			errChecker: NotNil,290		},291		{ // created data validation error292			kubeCli: fake.NewSimpleClientset(),293			dynCli:  fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),294			args: &types.CSISnapshotRestoreArgs{295				Cleanup: false,296			},297			prepare: func(f *fields) {298				gomock.InOrder(299					f.stepperOps.EXPECT().ValidateArgs(gomock.Any(), gomock.Any()).Return(nil),300					f.stepperOps.EXPECT().CreateApplication(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil, nil),301					f.stepperOps.EXPECT().ValidateData(gomock.Any(), gomock.Any(), gomock.Any()).Return(fmt.Errorf("validation error")),302				)303			},304			result:     &types.CSISnapshotRestoreResults{},305			errChecker: NotNil,306		},307		{ // create error, objects still returned308			kubeCli: fake.NewSimpleClientset(),309			dynCli:  fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),310			args: &types.CSISnapshotRestoreArgs{311				Cleanup: false,312			},313			prepare: func(f *fields) {314				gomock.InOrder(315					f.stepperOps.EXPECT().ValidateArgs(gomock.Any(), gomock.Any()).Return(nil),316					f.stepperOps.EXPECT().CreateApplication(gomock.Any(), gomock.Any(), gomock.Any()).Return(317						&v1.Pod{318							ObjectMeta: metav1.ObjectMeta{319								Name:      "pod1",320								Namespace: "ns",321							},322						},323						&v1.PersistentVolumeClaim{324							ObjectMeta: metav1.ObjectMeta{325								Name:      "pvc1",326								Namespace: "ns",327							},328						},329						fmt.Errorf("create error"),330					),331				)332			},333			result: &types.CSISnapshotRestoreResults{334				OriginalPVC: &v1.PersistentVolumeClaim{335					ObjectMeta: metav1.ObjectMeta{336						Name:      "pvc1",337						Namespace: "ns",338					},339				},340				OriginalPod: &v1.Pod{341					ObjectMeta: metav1.ObjectMeta{342						Name:      "pod1",343						Namespace: "ns",344					},345				},346			},347			errChecker: NotNil,348		},349		{ // create error, objects not returned350			kubeCli: fake.NewSimpleClientset(),351			dynCli:  fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),352			args: &types.CSISnapshotRestoreArgs{353				Cleanup: false,354			},355			prepare: func(f *fields) {356				gomock.InOrder(357					f.stepperOps.EXPECT().ValidateArgs(gomock.Any(), gomock.Any()).Return(nil),358					f.stepperOps.EXPECT().CreateApplication(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil, fmt.Errorf("create error")),359				)360			},361			result:     &types.CSISnapshotRestoreResults{},362			errChecker: NotNil,363		},364		{ // args validate error365			kubeCli: fake.NewSimpleClientset(),366			dynCli:  fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),367			args: &types.CSISnapshotRestoreArgs{368				Cleanup: false,369			},370			prepare: func(f *fields) {371				gomock.InOrder(372					f.stepperOps.EXPECT().ValidateArgs(gomock.Any(), gomock.Any()).Return(fmt.Errorf("create error")),373				)374			},375			result:     &types.CSISnapshotRestoreResults{},376			errChecker: NotNil,377		},378		{ // empty cli379			kubeCli:    nil,380			dynCli:     fakedynamic.NewSimpleDynamicClient(runtime.NewScheme()),381			result:     &types.CSISnapshotRestoreResults{},382			errChecker: NotNil,383		},384		{ // empty dyncli385			kubeCli:    fake.NewSimpleClientset(),...

Full Screen

Full Screen

database_test.go

Source:database_test.go Github

copy

Full Screen

...25	mockCtl := gomock.NewController(t)26	defer mockCtl.Finish()27	mockDB := mockdbaccessor.NewMockDbAccessor(mockCtl)28	SetDataBase(mockDB)29	gomock.InOrder(30		mockDB.EXPECT().ReadLeaf(dbaccessor.GetKeyOfPaasUUID()).Return(wantUUID, nil),31	)32	convey.Convey("TestCase1GetPaasUUID\n", t, func() {33		id := GetPaasUUID()34		convey.So(id, convey.ShouldEqual, wantUUID)35	})36}37func TestCase2GetPaasUUID(t *testing.T) {38	mockCtl := gomock.NewController(t)39	defer mockCtl.Finish()40	mockDB := mockdbaccessor.NewMockDbAccessor(mockCtl)41	SetDataBase(mockDB)42	gomock.InOrder(43		mockDB.EXPECT().ReadLeaf(dbaccessor.GetKeyOfPaasUUID()).Return("",44			errors.New("unknow errors")),45	)46	convey.Convey("TestCase2GetPaasUUID\n", t, func() {47		id := GetPaasUUID()48		convey.So(id, convey.ShouldEqual, uuid.NIL.String())49	})50}51func TestCase3GetPaasUUID(t *testing.T) {52	mockCtl := gomock.NewController(t)53	defer mockCtl.Finish()54	mockDB := mockdbaccessor.NewMockDbAccessor(mockCtl)55	SetDataBase(mockDB)56	gomock.InOrder(57		mockDB.EXPECT().ReadLeaf(dbaccessor.GetKeyOfPaasUUID()).Return("",58			errors.New(ErrorKeyNotFound)),59		mockDB.EXPECT().SaveLeaf(dbaccessor.GetKeyOfPaasUUID(),60			gomock.Any()).Return(nil),61	)62	convey.Convey("TestCase3GetPaasUUID\n", t, func() {63		id := GetPaasUUID()64		convey.So(id, convey.ShouldNotEqual, uuid.NIL.String())65	})66}67func TestCase4GetPaasUUID(t *testing.T) {68	mockCtl := gomock.NewController(t)69	defer mockCtl.Finish()70	mockDB := mockdbaccessor.NewMockDbAccessor(mockCtl)71	SetDataBase(mockDB)72	gomock.InOrder(73		mockDB.EXPECT().ReadLeaf(dbaccessor.GetKeyOfPaasUUID()).Return("",74			errors.New(ErrorKeyNotFound)),75		mockDB.EXPECT().SaveLeaf(dbaccessor.GetKeyOfPaasUUID(),76			gomock.Any()).Return(errors.New("unknow error happen")),77	)78	convey.Convey("TestCase4GetPaasUUID\n", t, func() {79		id := GetPaasUUID()80		convey.So(id, convey.ShouldEqual, uuid.NIL.String())81	})82}83func TestCase1GetOpenstackCfg(t *testing.T) {84	mockCtl := gomock.NewController(t)85	defer mockCtl.Finish()86	mockDB := mockdbaccessor.NewMockDbAccessor(mockCtl)87	SetDataBase(mockDB)88	gomock.InOrder(89		mockDB.EXPECT().ReadLeaf(dbaccessor.GetKeyOfOpenstack()).Return("",90			errors.New(ErrorKeyNotFound)),91	)92	convey.Convey("TestCase1GetOpenstackCfg\n", t, func() {93		cfg := GetOpenstackCfg()94		convey.So(cfg, convey.ShouldEqual, "")95	})96}97func TestCase2GetOpenstackCfg(t *testing.T) {98	const OpenstackConfig string = "right openstack configuration"99	mockCtl := gomock.NewController(t)100	defer mockCtl.Finish()101	mockDB := mockdbaccessor.NewMockDbAccessor(mockCtl)102	SetDataBase(mockDB)103	gomock.InOrder(104		mockDB.EXPECT().ReadLeaf(dbaccessor.GetKeyOfOpenstack()).Return(105			OpenstackConfig, nil),106	)107	convey.Convey("TestCase2GetOpenstackCfg\n", t, func() {108		cfg := GetOpenstackCfg()109		convey.So(cfg, convey.ShouldEqual, OpenstackConfig)110	})111}112func TestCase1GetVnfmCfg(t *testing.T) {113	mockCtl := gomock.NewController(t)114	defer mockCtl.Finish()115	mockDB := mockdbaccessor.NewMockDbAccessor(mockCtl)116	SetDataBase(mockDB)117	gomock.InOrder(118		mockDB.EXPECT().ReadLeaf(dbaccessor.GetKeyOfVnfm()).Return(119			"", errors.New(ErrorKeyNotFound)),120	)121	convey.Convey("TestCase1GetVnfmCfg\n", t, func() {122		cfg := GetVnfmCfg()123		convey.So(cfg, convey.ShouldEqual, "")124	})125}126func TestCase2GetVnfmCfg(t *testing.T) {127	const OpenstackConfig string = "right openstack configuration"128	mockCtl := gomock.NewController(t)129	defer mockCtl.Finish()130	mockDB := mockdbaccessor.NewMockDbAccessor(mockCtl)131	SetDataBase(mockDB)132	gomock.InOrder(133		mockDB.EXPECT().ReadLeaf(dbaccessor.GetKeyOfVnfm()).Return(134			OpenstackConfig, nil),135	)136	convey.Convey("TestCase2GetVnfmCfg\n", t, func() {137		cfg := GetVnfmCfg()138		convey.So(cfg, convey.ShouldEqual, OpenstackConfig)139	})140}141func TestCase1RegisterSelfToDb(t *testing.T) {142	const ManagerURL string = "https://paas.zte.com.cn:9527"143	mockCtl := gomock.NewController(t)144	defer mockCtl.Finish()145	mockDB := mockdbaccessor.NewMockDbAccessor(mockCtl)146	SetDataBase(mockDB)147	gomock.InOrder(148		mockDB.EXPECT().SaveLeaf(dbaccessor.GetKeyOfKnitterManagerUrl(),149			ManagerURL).Return(nil),150	)151	convey.Convey("TestCase1RegisterSelfToDb\n", t, func() {152		e := RegisterSelfToDb(ManagerURL)153		convey.So(e, convey.ShouldEqual, nil)154	})155}156func TestCase2RegisterSelfToDb(t *testing.T) {157	const ManagerURL string = "https://paas.zte.com.cn:9527"158	mockCtl := gomock.NewController(t)159	defer mockCtl.Finish()160	mockDB := mockdbaccessor.NewMockDbAccessor(mockCtl)161	SetDataBase(mockDB)162	gomock.InOrder(163		mockDB.EXPECT().SaveLeaf(dbaccessor.GetKeyOfKnitterManagerUrl(),164			ManagerURL).Return(errors.New(ErrorKeyNotFound)),165	)166	convey.Convey("TestCase1RegisterSelfToDb\n", t, func() {167		e := RegisterSelfToDb(ManagerURL)168		convey.So(e, convey.ShouldNotEqual, nil)169	})170}171func TestCase1SetDataBase(t *testing.T) {172	convey.Convey("TestCase1SetDataBase\n", t, func() {173		e := SetDataBase(nil)174		convey.So(e, convey.ShouldEqual, nil)175	})176}177func TestCase1GetDataBase(t *testing.T) {178	convey.Convey("TestCase1GetDataBase\n", t, func() {179		e := SetDataBase(nil)180		convey.So(e, convey.ShouldEqual, nil)181		p := GetDataBase()182		convey.So(p, convey.ShouldEqual, nil)183	})184}185//todo fix ut error186/*func TestCase1CheckDB(t *testing.T) {187	mockCtl := gomock.NewController(t)188	defer mockCtl.Finish()189	mockDB := mockdbaccessor.NewMockDbAccessor(mockCtl)190	SetDataBase(mockDB)191	gomock.InOrder(192		//case 1193		mockDB.EXPECT().SaveLeaf(gomock.Any(),194			gomock.Any()).Return(errors.New(ErrorKeyNotFound)),195		//case 2196		mockDB.EXPECT().SaveLeaf(gomock.Any(),197			gomock.Any()).Return(nil),198		mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(199			"", errors.New(ErrorKeyNotFound)),200		//case 3201		mockDB.EXPECT().SaveLeaf(gomock.Any(),202			gomock.Any()).Return(nil),203		mockDB.EXPECT().ReadLeaf(gomock.Any()).Return(204			"Error-DATA", nil),205		//case 4...

Full Screen

Full Screen

install_test.go

Source:install_test.go Github

copy

Full Screen

...31		newLine := "complete -C /home xxx"32		file := NewMockFile(ctrl)33		fs := NewMockFileSystem(ctrl)34		// FileContains35		gomock.InOrder(36			fs.EXPECT().Open(".profile").Return(file, nil),37			file.EXPECT().Read(gomock.Any()).Return(len(existingLine), io.EOF).Do(func(b []byte) {38				for i := 0; i < len(existingLine); i++ {39					b[i] = existingLine[i]40				}41			}),42			file.EXPECT().Close(),43		)44		// AppendToFile45		gomock.InOrder(46			fs.EXPECT().OpenFile(".profile", gomock.Any(), gomock.Any()).Return(file, nil),47			file.EXPECT().Write([]byte(fmt.Sprintf("\n%s\n", newLine))).Return(0, nil),48			file.EXPECT().Close(),49		)50		in, err := New(OptionExecutable(exec), OptionShell(StubShell(fs)))51		if err != nil {52			t.Fail()53		}54		err = in.Install("xxx")55		if expected, actual := true, err == nil; expected != actual {56			t.Errorf("expected: %v, actual: %v, err: %v", expected, actual, err)57		}58	})59	t.Run("install fails already exists", func(t *testing.T) {60		ctrl := gomock.NewController(t)61		defer ctrl.Finish()62		exec := NewMockExecutable(ctrl)63		exec.EXPECT().BinaryPath().Return("/home", nil)64		existingLine := "complete -C /home xxx"65		file := NewMockFile(ctrl)66		fs := NewMockFileSystem(ctrl)67		// FileContains68		gomock.InOrder(69			fs.EXPECT().Open(".profile").Return(file, nil),70			file.EXPECT().Read(gomock.Any()).Return(len(existingLine), io.EOF).Do(func(b []byte) {71				for i := 0; i < len(existingLine); i++ {72					b[i] = existingLine[i]73				}74			}),75			file.EXPECT().Close(),76		)77		in, err := New(OptionExecutable(exec), OptionShell(StubShell(fs)))78		if err != nil {79			t.Fail()80		}81		err = in.Install("xxx")82		if expected, actual := `file already contains line: "complete -C /home xxx"`, err.Error(); expected != actual {83			t.Errorf("expected: %v, actual: %v, err: %v", expected, actual, err)84		}85	})86	t.Run("install fails appending file", func(t *testing.T) {87		ctrl := gomock.NewController(t)88		defer ctrl.Finish()89		exec := NewMockExecutable(ctrl)90		exec.EXPECT().BinaryPath().Return("/home", nil)91		existingLine := "complete -C /home yyy"92		file := NewMockFile(ctrl)93		fs := NewMockFileSystem(ctrl)94		// FileContains95		gomock.InOrder(96			fs.EXPECT().Open(".profile").Return(file, nil),97			file.EXPECT().Read(gomock.Any()).Return(len(existingLine), io.EOF).Do(func(b []byte) {98				for i := 0; i < len(existingLine); i++ {99					b[i] = existingLine[i]100				}101			}),102			file.EXPECT().Close(),103		)104		// AppendToFile105		gomock.InOrder(106			fs.EXPECT().OpenFile(".profile", gomock.Any(), gomock.Any()).Return(file, errors.New("fail")),107		)108		in, err := New(OptionExecutable(exec), OptionShell(StubShell(fs)))109		if err != nil {110			t.Fail()111		}112		err = in.Install("xxx")113		if expected, actual := "fail", err.Error(); expected != actual {114			t.Errorf("expected: %v, actual: %v, err: %v", expected, actual, err)115		}116	})117}118func TestUninstall(t *testing.T) {119	t.Parallel()120	t.Run("uninstall", func(t *testing.T) {121		ctrl := gomock.NewController(t)122		defer ctrl.Finish()123		exec := NewMockExecutable(ctrl)124		exec.EXPECT().BinaryPath().Return("/home", nil)125		existingLine := "complete -C /home xxx"126		file := NewMockFile(ctrl)127		backupFile := NewMockFile(ctrl)128		tmpFile := NewMockFile(ctrl)129		fs := NewMockFileSystem(ctrl)130		// FileContains131		gomock.InOrder(132			fs.EXPECT().Open(".profile").Return(file, nil),133			file.EXPECT().Read(gomock.Any()).Return(len(existingLine), io.EOF).Do(func(b []byte) {134				for i := 0; i < len(existingLine); i++ {135					b[i] = existingLine[i]136				}137			}),138			file.EXPECT().Close(),139		)140		// CopyFile141		gomock.InOrder(142			fs.EXPECT().Open(".profile").Return(file, nil),143			fs.EXPECT().Create(".profile.bck").Return(backupFile, nil),144			file.EXPECT().Read(gomock.Any()).Return(len(existingLine), io.EOF).Do(func(b []byte) {145				for i := 0; i < len(existingLine); i++ {146					b[i] = existingLine[i]147				}148			}),149			backupFile.EXPECT().Write([]byte(fmt.Sprintf("%s", existingLine))).Return(len(existingLine), nil),150			backupFile.EXPECT().Close(),151			file.EXPECT().Close(),152		)153		// RemoveContentFromTmpFile154		gomock.InOrder(155			fs.EXPECT().Open(".profile").Return(file, nil),156			file.EXPECT().Read(gomock.Any()).Return(len(existingLine), io.EOF).Do(func(b []byte) {157				for i := 0; i < len(existingLine); i++ {158					b[i] = existingLine[i]159				}160			}),161			file.EXPECT().Close().Return(nil),162		)163		// CopyFile164		gomock.InOrder(165			fs.EXPECT().Open(gomock.Any()).Return(tmpFile, nil),166			fs.EXPECT().Create(".profile").Return(file, nil),167			tmpFile.EXPECT().Read(gomock.Any()).Return(len(existingLine), io.EOF).Do(func(b []byte) {168				for i := 0; i < len(existingLine); i++ {169					b[i] = existingLine[i]170				}171			}),172			file.EXPECT().Write([]byte(fmt.Sprintf("%s", existingLine))).Return(len(existingLine), nil),173			file.EXPECT().Close(),174			tmpFile.EXPECT().Close(),175		)176		// Remove177		gomock.InOrder(178			fs.EXPECT().Remove(".profile.bck").Return(nil),179		)180		in, err := New(OptionExecutable(exec), OptionShell(StubShell(fs)))181		if err != nil {182			t.Fail()183		}184		err = in.Uninstall("xxx")185		if expected, actual := true, err == nil; expected != actual {186			t.Errorf("expected: %v, actual: %v, err: %v", expected, actual, err)187		}188	})189}190func StubShell(fsys fsys.FileSystem) *Shell {191	return &Shell{...

Full Screen

Full Screen

InOrder

Using AI Code Generation

copy

Full Screen

1type GoMock interface {2    InOrder()3    PreOrder()4    PostOrder()5}6type GoMock interface {7    InOrder()8    PreOrder()9    PostOrder()10}11func UseGoMock(gm GoMock) {12    gm.InOrder()13    gm.PreOrder()14    gm.PostOrder()15}16func UseGoMock(gm GoMock) {17    gm.InOrder()18    gm.PreOrder()19    gm.PostOrder()20}21func UseGoMock(gm GoMock) {22    gm.InOrder()

Full Screen

Full Screen

InOrder

Using AI Code Generation

copy

Full Screen

1func (m *MockTree) InOrder() []int {2	ret := m.ctrl.Call(m, "InOrder")3	ret0, _ := ret[0].([]int)4}5func (m *MockTree) PostOrder() []int {6	ret := m.ctrl.Call(m, "PostOrder")7	ret0, _ := ret[0].([]int)8}9func (m *MockTree) PreOrder() []int {10	ret := m.ctrl.Call(m, "PreOrder")11	ret0, _ := ret[0].([]int)12}13func (m *MockTree) Search(arg0 int) bool {14	ret := m.ctrl.Call(m, "Search", arg0)15	ret0, _ := ret[0].(bool)16}17func (m *MockTree) Size() int {18	ret := m.ctrl.Call(m, "Size")19	ret0, _ := ret[0].(int)20}21func (m *MockTree) String() string {22	ret := m.ctrl.Call(m, "String")23	ret0, _ := ret[0].(string)24}25func (m *MockTree) Sum() int {26	ret := m.ctrl.Call(m, "Sum")27	ret0, _ := ret[0].(int)28}29func (m *MockTree) Value() int {30	ret := m.ctrl.Call(m, "Value")31	ret0, _ := ret[0].(int)32}33func (m *MockTree) Values() []int {34	ret := m.ctrl.Call(m, "Values")

Full Screen

Full Screen

InOrder

Using AI Code Generation

copy

Full Screen

1func TestInOrder(t *testing.T) {2    ctrl := gomock.NewController(t)3    defer ctrl.Finish()4    mock := NewMockInterface(ctrl)5    gomock.InOrder(6        mock.EXPECT().Method1().Return(1),7        mock.EXPECT().Method2().Return(2),8}9func TestInOrder(t *testing.T) {10    ctrl := gomock.NewController(t)11    defer ctrl.Finish()12    mock := NewMockInterface(ctrl)13    gomock.InOrder(14        mock.EXPECT().Method1().Return(1),15        mock.EXPECT().Method2().Return(2),16}17func TestInOrder(t *testing.T) {18    ctrl := gomock.NewController(t)19    defer ctrl.Finish()20    mock := NewMockInterface(ctrl)21    gomock.InOrder(22        mock.EXPECT().Method1().Return(1),23        mock.EXPECT().Method2().Return(2),24}25func TestInOrder(t *testing.T) {26    ctrl := gomock.NewController(t)27    defer ctrl.Finish()28    mock := NewMockInterface(ctrl)29    gomock.InOrder(30        mock.EXPECT().Method1().Return(1),31        mock.EXPECT().Method2().Return(2),32}33func TestInOrder(t *testing.T) {34    ctrl := gomock.NewController(t)35    defer ctrl.Finish()36    mock := NewMockInterface(ctrl)37    gomock.InOrder(38        mock.EXPECT().Method1().Return(1),39        mock.EXPECT().Method2().Return(2),40}41func TestInOrder(t *testing.T) {42    ctrl := gomock.NewController(t)43    defer ctrl.Finish()44    mock := NewMockInterface(ctrl)45    gomock.InOrder(46        mock.EXPECT().Method1().Return(1),

Full Screen

Full Screen

InOrder

Using AI Code Generation

copy

Full Screen

1func InOrder(call ...*gomock.Call) {2    for i := 0; i < len(call)-1; i++ {3        call[i].After(call[i+1])4    }5}6func InOrder(call ...*gomock.Call) {7    for i := 0; i < len(call)-1; i++ {8        call[i].After(call[i+1])9    }10}11func InOrder(call ...*gomock.Call) {12    for i := 0; i < len(call)-1; i++ {13        call[i].After(call[i+1])14    }15}16func InOrder(call ...*gomock.Call) {17    for i := 0; i < len(call)-1; i++ {18        call[i].After(call[i+1])19    }20}21func InOrder(call ...*gomock.Call) {22    for i := 0; i < len(call)-1; i++ {23        call[i].After(call[i+1])24    }25}26func InOrder(call ...*gomock.Call) {27    for i := 0; i < len(call)-1; i++ {28        call[i].After(call[i+1])29    }30}31func InOrder(call ...*gomock.Call) {32    for i := 0; i < len(call)-1; i++ {33        call[i].After(call[i+1])34    }35}36func InOrder(call ...*gomock.Call) {37    for i := 0; i < len(call)-1; i++ {38        call[i].After(call[i+1])39    }40}41func InOrder(call ...*gomock.Call)

Full Screen

Full Screen

InOrder

Using AI Code Generation

copy

Full Screen

1func (t *Tree) InOrder() []int {2  t.root.InOrder(&res)3}4func (t *Tree) InOrder() []int {5  t.root.InOrder(&res)6}7func (t *Tree) InOrder() []int {8  t.root.InOrder(&res)9}10func (t *Tree) InOrder() []int {11  t.root.InOrder(&res)12}13func (t *Tree) InOrder() []int {14  t.root.InOrder(&res)15}16func (t *Tree) InOrder() []int {17  t.root.InOrder(&res)18}19func (t *Tree) InOrder() []int {20  t.root.InOrder(&res)21}22func (t *Tree) InOrder() []int {23  t.root.InOrder(&res)24}25func (t *Tree) InOrder() []int {26  t.root.InOrder(&res)27}28func (t *Tree) InOrder() []int {29  t.root.InOrder(&res)30}

Full Screen

Full Screen

InOrder

Using AI Code Generation

copy

Full Screen

1type gomock struct {2    InOrder func()3    PostOrder func()4    PreOrder func()5    LevelOrder func()6}7func (g *gomock) InOrder() {8    fmt.Println("InOrder")9}10func (g *gomock) PostOrder() {11    fmt.Println("PostOrder")12}13func (g *gomock) PreOrder() {14    fmt.Println("PreOrder")15}16func (g *gomock) LevelOrder() {17    fmt.Println("LevelOrder")18}19func main() {20    g := new(gomock)21    g.InOrder()22    g.PostOrder()23    g.PreOrder()24    g.LevelOrder()25}

Full Screen

Full Screen

InOrder

Using AI Code Generation

copy

Full Screen

1import (2type tree struct {3}4func main() {5	mockCtrl := gomock.NewController()6	defer mockCtrl.Finish()7	mockTree := gomock.NewMockTree(mockCtrl)8	myTree := &tree{Value: 5}9	mockTree.EXPECT().InOrder().Return(myTree)10	result := mockTree.InOrder()11	fmt.Println(result.Value)12}

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful