public void VsWhidbey604087() { // Make sure that an exception during a flush doesn't crash // VS. //Prepare const string basePath = "c:\\temp\\Fake\\"; ServiceProviderMock spMock = new ServiceProviderMock(); spMock.Fake_AddUiServiceFake(); Mock <IComponentChangeService> componentChangeServiceMock = new Mock <IComponentChangeService>(); componentChangeServiceMock.Implement("add_ComponentChanged", new object[] { MockConstraint.IsAnything <ComponentChangedEventHandler>() }); componentChangeServiceMock.Implement("add_ComponentRename", new object[] { MockConstraint.IsAnything <ComponentRenameEventHandler>() }); componentChangeServiceMock.Implement("add_ComponentRemoved", new object[] { MockConstraint.IsAnything <ComponentEventHandler>() }); componentChangeServiceMock.Implement("add_ComponentAdded", new object[] { MockConstraint.IsAnything <ComponentEventHandler>() }); spMock.Fake_AddService(typeof(IComponentChangeService), componentChangeServiceMock.Instance); Mock <IVsHierarchy> hierarchyMock = new Mock <IVsHierarchy>(typeof(IVsProject)); hierarchyMock.Implement(new MethodId(typeof(IVsProject), "GetItemContext"), new object[] { (uint)VSITEMID.ROOT, MockConstraint.IsAnything <Microsoft.VisualStudio.OLE.Interop.IServiceProvider>() }, new object[] { (uint)0, null }, VSConstants.E_FAIL); spMock.Fake_AddService(typeof(IVsHierarchy), hierarchyMock.Instance); ResourceEditorRootComponent rootComponent = new ResourceEditorRootComponent(); Mock <ResourceFile> resourceFileMock = new Mock <ResourceFile>(); resourceFileMock.SetCreateArguments(new object[] { null, rootComponent, spMock.Instance, basePath }); SequenceMock <ResourceEditorDesignerLoader> designerLoaderMock = new SequenceMock <ResourceEditorDesignerLoader>(); ResourceEditorViewMock view = new ResourceEditorViewMock(spMock.Instance); view.Fake_designerLoader = designerLoaderMock.Instance; resourceFileMock.Implement("get_View", view); Dictionary <string, object> styles = new Dictionary <string, object>(); //Make The RunSingleFileGenerator call throw an exception, and make sure we don't // blow up because of it. designerLoaderMock.AddExpectation("RunSingleFileGenerator", new object[] { true }, new Exception("Whoops")); //Run test Microsoft_VisualStudio_Editors_ResourceEditor_ResourceFileAccessor accessor = new Microsoft_VisualStudio_Editors_ResourceEditor_ResourceFileAccessor(resourceFileMock.Instance); accessor.DelayFlushAndRunCustomToolImpl(); //Verify Assert.AreEqual("Whoops", view.FakeResult_DSMsgBoxWasCalledWithThisString); designerLoaderMock.Verify(); }
/// <summary> /// Constructor /// </summary> public ServiceProviderMock() { //Implementation of GetService Implement("GetService", new object[] { MockConstraint.IsAnything <Type>() }, delegate(object obj, MethodInfo method, object[] arguments) { Type serviceType = (Type)arguments[0]; if (m_services.ContainsKey(serviceType)) { return(m_services[serviceType]); } else { throw new ArgumentException("serviceType '" + serviceType.FullName + "' not implemented in Mocks.ServiceProviderMock()"); } } ); // Default services (can be replaced) Fake_AddService(typeof(IUIService), new UIServiceMock().Instance); //UNDONE: don't do this by default }
public void AddExpectationShowMessageBox(string message, MessageBoxButtons buttons, DialogResult returnValue) { //Implement("ShowMessageBox", // new object[] { // 0, // Guid.Empty, // MockConstraint.IsAnything<string>(), // pszText, // pszHelpLink, // 0, // msgbtn, // MockConstraint.IsAnything<OLEMSGDEFBUTTON>(), // MockConstraint.IsAnything<OLEMSGICON>(), // 0, // 0}, // new object[] { returnedResult }); //Implement("ShowMessageBox", // new object[] { // 0, // Guid.Empty, // MockConstraint.IsAnything<string>(), // MockConstraint.IsAnything<string>(), // MockConstraint.IsAnything<string>(), // 0, // msgbtn, // MockConstraint.IsAnything<OLEMSGDEFBUTTON>(), // MockConstraint.IsAnything<OLEMSGICON>(), // 0, // 0}, // new object[] { returnedResult }); Implement("ShowMessage", new object[] { message, MockConstraint.IsAnything <string>(), buttons }, returnValue); }
private void TestGenerate <G>(string language, out int hr, out IntPtr[] fileContents, out uint bytesOutput, out System.CodeDom.CodeCompileUnit compileUnit, out System.CodeDom.CodeTypeDeclaration typeDeclaration) where G : Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGeneratorBase, new() { SettingsSingleFileGeneratorSite site = new SettingsSingleFileGeneratorSite(language); string inputFile = site.DefaultSettingsFilePath; Mock <G> generatorMock = new Mock <G>(); System.CodeDom.CodeCompileUnit actualCompileUnit = null; System.CodeDom.CodeTypeDeclaration actualTypeDeclaration = null; generatorMock.Implement("GetProjectRootNamespace", "MyLittleNamespace"); generatorMock.Implement("AddRequiredReferences", new object[] { (Shell.Interop.IVsGeneratorProgress)null }); generatorMock.Implement("OnCompileUnitCreated", new object[] { MockConstraint.IsAnything <System.CodeDom.CodeCompileUnit>(), MockConstraint.IsAnything <System.CodeDom.CodeTypeDeclaration>() }, delegate(object obj, System.Reflection.MethodInfo method, object[] arguments) { actualCompileUnit = (System.CodeDom.CodeCompileUnit)arguments[0]; actualTypeDeclaration = (System.CodeDom.CodeTypeDeclaration)arguments[1]; return(null); }); IVsSingleFileGenerator generator = generatorMock.Instance; ((Microsoft.VisualStudio.OLE.Interop.IObjectWithSite)generator).SetSite(site); fileContents = new IntPtr[] { new IntPtr() }; bytesOutput = 0; hr = generator.Generate(inputFile, "", "", fileContents, out bytesOutput, null); compileUnit = actualCompileUnit; typeDeclaration = actualTypeDeclaration; }
public void ExpectMsgBox(Type exceptionType, string helpLink) { AddExpectation("DsMsgBox", new object[] { MockConstraint.IsInstanceOfType <Exception>(exceptionType), helpLink }); }
/// <summary> /// Call this method in a lamba expression to state that an argument to a method being mocked should not be null. /// </summary> /// <remarks> /// Note that ConvertibleConstraintWrapper is implicitly convertible to T, unless T is an interface. In that case, /// Convert() must be called on the return object. /// </remarks> /// <typeparam name="T"> /// The type of the return value /// </typeparam> public static ConvertibleConstraintWrapper <T> NotNull <T>() where T : class { return(new ConvertibleConstraintWrapper <T>(MockConstraint.IsNotNull <T>())); }
/// <summary> /// Call this method in a lamba expression to state that an argument to a method being mocked can be anything. /// </summary> /// <remarks> /// Note that ConvertibleConstraintWrapper is implicitly convertible to T, unless T is an interface. In that case, /// Convert() must be called on the return object. /// </remarks> /// <typeparam name="T"> /// The type of the return value /// </typeparam> public static ConvertibleConstraintWrapper <T> Any <T>() where T : class { return(new ConvertibleConstraintWrapper <T>(MockConstraint.IsAnything <T>())); }
public void CreateDesignerWhenPropertyPageInfoTryLoadPropertyPageFailsSoSiteIsNothing_DevDivBugs17865() { SequenceMock <IVsWindowFrame> outerWindowFrameMock = new SequenceMock <IVsWindowFrame>(typeof(IVsWindowFrame2)); SequenceMock <IVsWindowFrame> innerWindowFrameMock = new SequenceMock <IVsWindowFrame>(typeof(IVsWindowFrame2)); Control parentControl = new Control(); parentControl.Size = new Size(10, 10); ServiceProviderMock spMock = new ServiceProviderMock(); spMock.Fake_AddService(typeof(IVsWindowFrame), outerWindowFrameMock.Instance); UIShellService2FakeWithColors shellServiceFake = new UIShellService2FakeWithColors(); spMock.Fake_AddService(typeof(IVsUIShell), shellServiceFake); VsShellFake shellFake = new VsShellFake(); spMock.Fake_AddService(typeof(IVsShell), shellFake); UIServiceFake uiServiceFake = new UIServiceFake(); spMock.Fake_AddService(typeof(IUIService), uiServiceFake); SequenceMock <IVsUIShellOpenDocument> uiShellOpenDocumentMock = new SequenceMock <IVsUIShellOpenDocument>(); spMock.Fake_AddService(typeof(IVsUIShellOpenDocument), uiShellOpenDocumentMock.Instance); Mock <OLE.Interop.IServiceProvider> oleServiceProviderMock = new Mock <Microsoft.VisualStudio.OLE.Interop.IServiceProvider>(); spMock.Fake_AddService(typeof(OLE.Interop.IServiceProvider), oleServiceProviderMock.Instance); ApplicationDesignerView view = new ApplicationDesignerView(spMock.Instance); IVsHierarchy hierarchy = new Mock <IVsHierarchy>(typeof(IVsUIHierarchy)).Instance; Guid guid = new Guid(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11); Mock <PropertyPageInfo> infoMock = new Mock <PropertyPageInfo>(); infoMock.SetCreateArguments(view, guid, false); infoMock.Implement("TryLoadPropertyPage"); // TryLoadPropertyPage is a NOOP, so Site will remain NULL uint itemid = 123; ApplicationDesignerPanelFake panel = new ApplicationDesignerPanelFake(view, hierarchy, itemid, infoMock.Instance); parentControl.Controls.Add(panel); panel.MkDocument = "my moniker"; // Fail the IsDocumentInAProject call uiShellOpenDocumentMock.AddExpectation("IsDocumentInAProject", //string pszMkDocument, out IVsUIHierarchy ppUIH, out uint pitemid, out Microsoft.VisualStudio.OLE.Interop.IServiceProvider ppSP, out int pDocInProj) new object[] { MockConstraint.IsAnything <string>(), null, (uint)0, null, 0 }, new object[] { null, null, (uint)0, null, 0 }, VSConstants.S_OK); // OpenSpecificEditor call should succeed uiShellOpenDocumentMock.AddExpectation("OpenSpecificEditor", //int OpenSpecificEditor(uint grfOpenSpecific, string pszMkDocument, ref Guid rguidEditorType, string pszPhysicalView, ref Guid rguidLogicalView, string pszOwnerCaption, IVsUIHierarchy pHier, uint itemid, IntPtr punkDocDataExisting, Microsoft.VisualStudio.OLE.Interop.IServiceProvider pSPHierContext, out IVsWindowFrame ppWindowFrame); new object[] { MockConstraint.IsAnything <uint>(), //uint grfOpenSpecific MockConstraint.IsAnything <string>(), //string pszMkDocument MockConstraint.IsAnything <Guid>(), //ref Guid rguidEditorType MockConstraint.IsAnything <string>(), //string pszPhysicalView MockConstraint.IsAnything <Guid>(), //ref Guid rguidLogicalView MockConstraint.IsAnything <string>(), //string pszOwnerCaption MockConstraint.IsAnything <IVsUIHierarchy>(), //IVsUIHierarchy pHier MockConstraint.IsAnything <uint>(), //uint itemid MockConstraint.IsAnything <IntPtr>(), //IntPtr punkDocDataExisting MockConstraint.IsAnything <OLE.Interop.IServiceProvider>(), //OLE.Interop.IServiceProvider> pSPHierContext null //out IVsWindowFrame ppWindowFrame }, new object[] { (uint)0, //uint grfOpenSpecific null, //string pszMkDocument Guid.Empty, //ref Guid rguidEditorType null, //string pszPhysicalView Guid.Empty, //ref Guid rguidLogicalView null, //string pszOwnerCaption null, //IVsUIHierarchy pHier (uint)0, //uint itemid IntPtr.Zero, //IntPtr punkDocDataExisting null, //OLE.Interop.IServiceProvider> pSPHierContext innerWindowFrameMock.Instance //out IVsWindowFrame ppWindowFrame }, VSConstants.S_OK); innerWindowFrameMock.Implement("GetProperty", new object[] { (int)__VSFPROPID.VSFPROPID_Hierarchy, null }, new object[] { (int)0, hierarchy }, VSConstants.S_OK); innerWindowFrameMock.Implement("GetProperty", new object[] { (int)__VSFPROPID.VSFPROPID_ItemID, null }, new object[] { (int)0, 321 }, VSConstants.S_OK); innerWindowFrameMock.Implement("GetProperty", new object[] { (int)__VSFPROPID2.VSFPROPID_ParentHwnd, null }, new object[] { (int)0, (int)0 }, VSConstants.S_OK); innerWindowFrameMock.AddExpectation("SetProperty", new object[] { (int)__VSFPROPID2.VSFPROPID_ParentHwnd, MockConstraint.IsAnything <object>() }, VSConstants.S_OK); innerWindowFrameMock.AddExpectation("SetProperty", new object[] { (int)__VSFPROPID2.VSFPROPID_ParentFrame, MockConstraint.IsAnything <object>() }, VSConstants.S_OK); innerWindowFrameMock.Implement(new MethodId(typeof(IVsWindowFrame2), "Advise"), new object[] { MockConstraint.IsAnything <IVsWindowFrameNotify>(), MockConstraint.IsAnything <uint>() }, new object[] { null, (uint)432 }, VSConstants.S_OK); innerWindowFrameMock.Implement(new MethodId(typeof(IVsWindowFrame2), "Unadvise"), new object[] { (uint)432 }, VSConstants.S_OK); innerWindowFrameMock.AddExpectation("GetProperty", new object[] { (int)__VSFPROPID.VSFPROPID_DocData, null }, new object[] { (int)0, null }, VSConstants.S_OK); innerWindowFrameMock.AddExpectation("GetProperty", new object[] { (int)__VSFPROPID.VSFPROPID_DocCookie, null }, new object[] { (int)0, (uint)678 }, VSConstants.S_OK); innerWindowFrameMock.AddExpectation("GetProperty", new object[] { (int)__VSFPROPID.VSFPROPID_DocView, null }, new object[] { (int)0, null }, VSConstants.S_OK); innerWindowFrameMock.Implement("GetProperty", new object[] { (int)__VSFPROPID.VSFPROPID_EditorCaption, null }, new object[] { (int)0, null }, VSConstants.S_OK); panel.CreateDesigner(); //Verify uiShellOpenDocumentMock.Verify(); innerWindowFrameMock.Verify(); outerWindowFrameMock.Verify(); Assert.IsTrue(panel.Fake_wasShowWindowFrameCalled); }
/// <summary> /// Call this method in a lamba expression to state that an argument to a method must return true when applied to the /// given predicate. /// </summary> /// <typeparam name="T">The return type of the method.</typeparam> /// <param name="predicate">The predicate.</param> public static ConvertibleConstraintWrapper <T> Match <T>(Predicate <T> predicate) where T : class { return(new ConvertibleConstraintWrapper <T>(MockConstraint.IsMatch(predicate))); }
/// <summary> /// Call this method in a lamba expression to state that an argument to a method being mocked must match the given regular expression /// pattern. /// </summary> /// <param name="pattern">The pattern.</param> public static ConvertibleConstraintWrapper <string> Match(string pattern) { return(new ConvertibleConstraintWrapper <string>(MockConstraint.IsMatch(pattern))); }