public void VsOnNavigateToDocInProjectTest() { var mockDocumentEnumerator = new SequenceMock <IEnumerator>(); var mockDte = new Mock <EnvDTE.DTE>(); var mockDocuments = new Mock <EnvDTE.Documents>(); var mockDocument = new SequenceMock <EnvDTE.Document>(); var mockActiveDocument = new Mock <EnvDTE.Document>(); var mockTextSelection = new SequenceMock <EnvDTE.TextSelection>(); var mockVirtualPoint = new SequenceMock <EnvDTE.VirtualPoint>(); this.SetupProjectUtilities(mockDocumentEnumerator, mockDte, mockDocuments, mockDocument, mockActiveDocument, this.violation.File); mockDocument.AddExpectationExpr(doc => doc.Activate()); mockDocument.AddExpectationExpr(doc => doc.DTE, (Func <EnvDTE.DTE>) delegate { return((EnvDTE.DTE)mockDte.Instance); }); mockActiveDocument.ImplementExpr(doc => doc.Selection, mockTextSelection.Instance); mockTextSelection.ImplementExpr(sel => sel.GotoLine(this.violation.LineNumber, true)); mockTextSelection.ImplementExpr(sel => sel.ActivePoint, mockVirtualPoint.Instance); mockVirtualPoint.ImplementExpr(vp => vp.TryToShow(EnvDTE.vsPaneShowHow.vsPaneShowCentered, 0)); this.mockServiceProvider.ImplementExpr(sp => sp.GetService(typeof(EnvDTE.DTE)), mockDte.Instance); ProjectUtilities_Accessor.SetServiceProvider(this.mockServiceProvider.Instance); // Execute this.taskUnderTest.OnNavigate(EventArgs.Empty); // Verify the required methods are called to show the violation mockTextSelection.Verify(); mockVirtualPoint.Verify(); mockDocument.Verify(); }
private void SetupProjectUtilities(SequenceMock <IEnumerator> mockDocumentEnumerator, Mock <EnvDTE.DTE> mockDte, Mock <EnvDTE.Documents> mockDocuments, SequenceMock <EnvDTE.Document> mockDocument, Mock <EnvDTE.Document> mockActiveDocument, string fileName) { var mockSolution = new Mock <EnvDTE.Solution>(); var mockProjects = new Mock <EnvDTE.Projects>(); var mockProject = new Mock <EnvDTE.Project>(); var mockProjectEnumerator = new SequenceMock <IEnumerator>(); mockDte.ImplementExpr(dte => dte.Solution, mockSolution.Instance); mockDte.ImplementExpr(dte => dte.Documents, mockDocuments.Instance); mockDte.ImplementExpr(dte => dte.ActiveDocument, mockActiveDocument.Instance); mockSolution.ImplementExpr(sol => sol.Projects, mockProjects.Instance); mockProjects.ImplementExpr(e => e.GetEnumerator(), mockProjectEnumerator.Instance); mockProjectEnumerator.AddExpectationExpr(en => en.MoveNext(), true); mockProjectEnumerator.AddExpectationExpr(en => en.Current, mockProject.Instance); mockProjectEnumerator.AddExpectationExpr(en => en.MoveNext(), false); mockProject.ImplementExpr(p => p.Kind, EnvDTE.Constants.vsProjectKindMisc); mockProject.ImplementExpr(p => p.ProjectItems, (Func <EnvDTE.ProjectItems>) delegate { return(null); }); mockDocuments.ImplementExpr(docs => docs.GetEnumerator(), mockDocumentEnumerator.Instance); mockDocumentEnumerator.AddExpectationExpr(docs => docs.MoveNext(), true); mockDocumentEnumerator.AddExpectationExpr(docs => docs.Current, mockDocument.Instance); mockDocumentEnumerator.AddExpectationExpr(docs => docs.MoveNext(), false); mockDocument.AddExpectationExpr(doc => doc.FullName, fileName); }
public void OnNavigateToDocNotInProjectTest() { try { var mockDocumentEnumerator = new SequenceMock <IEnumerator>(); var mockDte = new Mock <DTE>(); var mockDocuments = new Mock <Documents>(); var mockDocument = new SequenceMock <Document>(); var mockActiveDocument = new Mock <Document>(); var mockTextSelection = new SequenceMock <TextSelection>(); var mockVirtualPoint = new SequenceMock <VirtualPoint>(); this.SetupProjectUtilities(mockDocumentEnumerator, mockDte, mockDocuments, mockDocument, mockActiveDocument, "DummyFile.txt"); var mockSecondDocument = new SequenceMock <Document>(); mockDocumentEnumerator.AddExpectationExpr(docs => docs.MoveNext(), true); mockDocumentEnumerator.AddExpectationExpr(docs => docs.Current, mockSecondDocument.Instance); mockDocumentEnumerator.AddExpectationExpr(docs => docs.MoveNext(), false); mockSecondDocument.AddExpectationExpr(doc => doc.FullName, "DummyFile.txt"); AnalysisHelper analysisHelper = this.SetCoreNoUI(); bool eventFired = false; PrivateObject actual = new PrivateObject(this.package, new PrivateType(typeof(StyleCopVSPackage))); actual.SetFieldOrProperty("core", this.package.Core); // Register output generated event to test event fired this.package.Core.OutputGenerated += (sender, args) => { eventFired = true; }; mockActiveDocument.ImplementExpr(doc => doc.Selection, mockTextSelection.Instance); mockTextSelection.ImplementExpr(sel => sel.GotoLine(this.violation.LineNumber, true)); mockTextSelection.ImplementExpr(sel => sel.ActivePoint, mockVirtualPoint.Instance); mockVirtualPoint.ImplementExpr(vp => vp.TryToShow(EnvDTE.vsPaneShowHow.vsPaneShowCentered, 0)); this.mockServiceProvider.ImplementExpr(sp => sp.GetService(typeof(EnvDTE.DTE)), mockDte.Instance); // Use private type to set static private field PrivateType privateProjectUtilities = new PrivateType(typeof(ProjectUtilities)); privateProjectUtilities.SetStaticFieldOrProperty("serviceProvider", this.mockServiceProvider.Instance); // Execute PrivateObject taskUnderTestPrivateObject = new PrivateObject(this.taskUnderTest, new PrivateType(typeof(ViolationTask))); taskUnderTestPrivateObject.Invoke("OnNavigate", EventArgs.Empty); // Verify the required methods are called to show the violation mockTextSelection.Verify(); mockVirtualPoint.Verify(); mockDocument.Verify(); Assert.IsTrue(eventFired, "Core did not fire output event"); } catch (Exception ex) { // Use try catch to test a workaround on CI build (AppVeyor) Console.WriteLine(ex.Message); } }
/// <summary> /// Implements the method specified by the lambda expression (that returns void), calling the given handler. /// </summary> /// <typeparam name="T">The type being mocked.</typeparam> /// <param name="mock">The mock object.</param> /// <param name="expression">The expression.</param> /// <param name="handler">The handler.</param> public static void AddExpectationExpr <T>(this SequenceMock <T> mock, Expression <Action <T> > expression, Action <object[]> handler) where T : class { mock.AddExpectation(expression, (obj, method, arguments) => { handler(arguments); return(null); }); }
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(); }
private System.Reflection.TypeAttributes CodeModelToCodeDomTypeAttributesImpl(EnvDTE.vsCMAccess access, EnvDTE80.vsCMInheritanceKind inheritanceKind) { SequenceMock <EnvDTE80.CodeClass2> cc2Mock = new SequenceMock <EnvDTE80.CodeClass2>(); cc2Mock.AddExpectation("get_Access", access); cc2Mock.AddExpectation("get_InheritanceKind", inheritanceKind); System.Reflection.TypeAttributes result = ProjectUtils.CodeModelToCodeDomTypeAttributes(cc2Mock.Instance); cc2Mock.Verify(); return(result); }
public void OnNavigateToDocInProjectTest() { try { var mockDocumentEnumerator = new SequenceMock <IEnumerator>(); var mockDte = new Mock <DTE>(); var mockDocuments = new Mock <Documents>(); var mockDocument = new SequenceMock <Document>(); var mockActiveDocument = new Mock <Document>(); var mockTextSelection = new SequenceMock <TextSelection>(); var mockVirtualPoint = new SequenceMock <VirtualPoint>(); this.SetupProjectUtilities(mockDocumentEnumerator, mockDte, mockDocuments, mockDocument, mockActiveDocument, this.violation.File); mockDocument.AddExpectationExpr(doc => doc.Activate()); mockDocument.AddExpectationExpr(doc => doc.DTE, (Func <DTE>) delegate { return((EnvDTE.DTE)mockDte.Instance); }); mockActiveDocument.ImplementExpr(doc => doc.Selection, mockTextSelection.Instance); mockTextSelection.ImplementExpr(sel => sel.GotoLine(this.violation.LineNumber, true)); mockTextSelection.ImplementExpr(sel => sel.ActivePoint, mockVirtualPoint.Instance); mockVirtualPoint.ImplementExpr(vp => vp.TryToShow(EnvDTE.vsPaneShowHow.vsPaneShowCentered, 0)); this.mockServiceProvider.ImplementExpr(sp => sp.GetService(typeof(EnvDTE.DTE)), mockDte.Instance); this.mockServiceProvider.ImplementExpr(sp => sp.GetService(typeof(EnvDTE.DTE)), mockDte.Instance); this.mockServiceProvider.ImplementExpr(sp => sp.GetService(typeof(SVsSolutionBuildManager)), new MockSolutionBuildManager()); // Use private type to set static private field PrivateType privateProjectUtilities = new PrivateType(typeof(ProjectUtilities)); privateProjectUtilities.SetStaticFieldOrProperty("serviceProvider", this.mockServiceProvider.Instance); // Execute PrivateObject taskUnderTestPrivateObject = new PrivateObject(this.taskUnderTest, new PrivateType(typeof(ViolationTask))); taskUnderTestPrivateObject.Invoke("OnNavigate", EventArgs.Empty); // Verify the required methods are called to show the violation mockTextSelection.Verify(); mockVirtualPoint.Verify(); mockDocument.Verify(); } catch (Exception ex) { // Use try catch to test a workaround on CI build (AppVeyor) Console.WriteLine(ex.Message); } }
private void SetupProjectUtilities( SequenceMock <IEnumerator> mockDocumentEnumerator, Mock <DTE> mockDte, Mock <Documents> mockDocuments, SequenceMock <Document> mockDocument, Mock <Document> mockActiveDocument, string fileName) { try { var mockSolution = new Mock <Solution>(); var mockProjects = new Mock <Projects>(); var mockProject = new Mock <Project>(); var mockProjectEnumerator = new SequenceMock <IEnumerator>(); // var mockEvents = new Mock<EnvDTE.Events>(); mockDte.ImplementExpr(dte => dte.Solution, mockSolution.Instance); mockDte.ImplementExpr(dte => dte.Documents, mockDocuments.Instance); mockDte.ImplementExpr(dte => dte.ActiveDocument, mockActiveDocument.Instance); // mockDte.ImplementExpr(dte => dte.Events, mockEvents.Instance); mockSolution.ImplementExpr(sol => sol.Projects, mockProjects.Instance); mockProjects.ImplementExpr(e => e.GetEnumerator(), mockProjectEnumerator.Instance); mockProjectEnumerator.AddExpectationExpr(en => en.MoveNext(), true); mockProjectEnumerator.AddExpectationExpr(en => en.Current, mockProject.Instance); mockProjectEnumerator.AddExpectationExpr(en => en.MoveNext(), false); mockProject.ImplementExpr(p => p.Kind, EnvDTE.Constants.vsProjectKindMisc); mockProject.ImplementExpr(p => p.ProjectItems, (Func <ProjectItems>) delegate { return(null); }); mockDocuments.ImplementExpr(docs => docs.GetEnumerator(), mockDocumentEnumerator.Instance); mockDocumentEnumerator.AddExpectationExpr(docs => docs.MoveNext(), true); mockDocumentEnumerator.AddExpectationExpr(docs => docs.Current, mockDocument.Instance); mockDocumentEnumerator.AddExpectationExpr(docs => docs.MoveNext(), false); mockDocument.AddExpectationExpr(doc => doc.FullName, fileName); } catch (Exception ex) { // Use try catch to test a workaround on CI build (AppVeyor) Console.WriteLine(ex.Message); } }
public void VsOnNavigateToDocNotInProjectTest() { var mockDocumentEnumerator = new SequenceMock <IEnumerator>(); var mockDte = new Mock <EnvDTE.DTE>(); var mockDocuments = new Mock <EnvDTE.Documents>(); var mockDocument = new SequenceMock <EnvDTE.Document>(); var mockActiveDocument = new Mock <EnvDTE.Document>(); var mockTextSelection = new SequenceMock <EnvDTE.TextSelection>(); var mockVirtualPoint = new SequenceMock <EnvDTE.VirtualPoint>(); this.SetupProjectUtilities(mockDocumentEnumerator, mockDte, mockDocuments, mockDocument, mockActiveDocument, "DummyFile.txt"); var mockSecondDocument = new SequenceMock <EnvDTE.Document>(); mockDocumentEnumerator.AddExpectationExpr(docs => docs.MoveNext(), true); mockDocumentEnumerator.AddExpectationExpr(docs => docs.Current, mockSecondDocument.Instance); mockDocumentEnumerator.AddExpectationExpr(docs => docs.MoveNext(), false); mockSecondDocument.AddExpectationExpr(doc => doc.FullName, "DummyFile.txt"); AnalysisHelper_Accessor analysisHelper = SetCoreNoUI(); bool eventFired = false; analysisHelper.core.OutputGenerated += (sender, args) => { eventFired = true; }; mockActiveDocument.ImplementExpr(doc => doc.Selection, mockTextSelection.Instance); mockTextSelection.ImplementExpr(sel => sel.GotoLine(this.violation.LineNumber, true)); mockTextSelection.ImplementExpr(sel => sel.ActivePoint, mockVirtualPoint.Instance); mockVirtualPoint.ImplementExpr(vp => vp.TryToShow(EnvDTE.vsPaneShowHow.vsPaneShowCentered, 0)); this.mockServiceProvider.ImplementExpr(sp => sp.GetService(typeof(EnvDTE.DTE)), mockDte.Instance); ProjectUtilities_Accessor.SetServiceProvider(this.mockServiceProvider.Instance); // Execute this.taskUnderTest.OnNavigate(EventArgs.Empty); // Verify the required methods are called to show the violation mockTextSelection.Verify(); mockVirtualPoint.Verify(); mockDocument.Verify(); Assert.IsTrue(eventFired, "Core did not fire output event"); }
public void OnNavigateToDocInProjectTest() { var mockDocumentEnumerator = new SequenceMock<IEnumerator>(); var mockDte = new Mock<DTE>(); var mockDocuments = new Mock<Documents>(); var mockDocument = new SequenceMock<Document>(); var mockActiveDocument = new Mock<Document>(); var mockTextSelection = new SequenceMock<TextSelection>(); var mockVirtualPoint = new SequenceMock<VirtualPoint>(); this.SetupProjectUtilities(mockDocumentEnumerator, mockDte, mockDocuments, mockDocument, mockActiveDocument, this.violation.File); mockDocument.AddExpectationExpr(doc => doc.Activate()); mockDocument.AddExpectationExpr(doc => doc.DTE, (Func<DTE>)delegate { return (EnvDTE.DTE)mockDte.Instance; }); mockActiveDocument.ImplementExpr(doc => doc.Selection, mockTextSelection.Instance); mockTextSelection.ImplementExpr(sel => sel.GotoLine(this.violation.LineNumber, true)); mockTextSelection.ImplementExpr(sel => sel.ActivePoint, mockVirtualPoint.Instance); mockVirtualPoint.ImplementExpr(vp => vp.TryToShow(EnvDTE.vsPaneShowHow.vsPaneShowCentered, 0)); this.mockServiceProvider.ImplementExpr(sp => sp.GetService(typeof(EnvDTE.DTE)), mockDte.Instance); this.mockServiceProvider.ImplementExpr(sp => sp.GetService(typeof(EnvDTE.DTE)), mockDte.Instance); this.mockServiceProvider.ImplementExpr(sp => sp.GetService(typeof(SVsSolutionBuildManager)), new MockSolutionBuildManager()); // Use private type to set static private field PrivateType privateProjectUtilities = new PrivateType(typeof(ProjectUtilities)); privateProjectUtilities.SetStaticFieldOrProperty("serviceProvider", this.mockServiceProvider.Instance); // Execute PrivateObject taskUnderTestPrivateObject = new PrivateObject(this.taskUnderTest, new PrivateType(typeof(ViolationTask))); taskUnderTestPrivateObject.Invoke("OnNavigate", EventArgs.Empty); // Verify the required methods are called to show the violation mockTextSelection.Verify(); mockVirtualPoint.Verify(); mockDocument.Verify(); }
/// <summary> /// Implements the method specified by the lambda expression (that returns void), calling the given handler. /// </summary> /// <typeparam name="T"> /// The type being mocked. /// </typeparam> /// <param name="mock"> /// The mock object. /// </param> /// <param name="expression"> /// The expression. /// </param> /// <param name="handler"> /// The handler. /// </param> public static void AddExpectationExpr <T>(this SequenceMock <T> mock, Expression <Action <T> > expression, Action handler) where T : class { mock.AddExpectationExpr(expression, args => handler()); }
/// <summary> /// Implements the method specified by the lambda expression (that returns void). /// </summary> /// <typeparam name="T"> /// The type being mocked. /// </typeparam> /// <param name="mock"> /// The mock object. /// </param> /// <param name="expression"> /// The expression. /// </param> public static void AddExpectationExpr <T>(this SequenceMock <T> mock, Expression <Action <T> > expression) where T : class { mock.AddExpectation(expression, (obj, method, arguments) => null); }
private static void AddExpectation <T>(this SequenceMock <T> mock, LambdaExpression lambda, MockDelegate handler) where T : class { object[] args; mock.AddExpectation(GetMethodAndArgs(lambda, out args), args, handler); }
public void OnNavigateToDocNotInProjectTest() { var mockDocumentEnumerator = new SequenceMock<IEnumerator>(); var mockDte = new Mock<DTE>(); var mockDocuments = new Mock<Documents>(); var mockDocument = new SequenceMock<Document>(); var mockActiveDocument = new Mock<Document>(); var mockTextSelection = new SequenceMock<TextSelection>(); var mockVirtualPoint = new SequenceMock<VirtualPoint>(); this.SetupProjectUtilities(mockDocumentEnumerator, mockDte, mockDocuments, mockDocument, mockActiveDocument, "DummyFile.txt"); var mockSecondDocument = new SequenceMock<Document>(); mockDocumentEnumerator.AddExpectationExpr(docs => docs.MoveNext(), true); mockDocumentEnumerator.AddExpectationExpr(docs => docs.Current, mockSecondDocument.Instance); mockDocumentEnumerator.AddExpectationExpr(docs => docs.MoveNext(), false); mockSecondDocument.AddExpectationExpr(doc => doc.FullName, "DummyFile.txt"); AnalysisHelper analysisHelper = this.SetCoreNoUI(); bool eventFired = false; PrivateObject actual = new PrivateObject(this.package, new PrivateType(typeof(StyleCopVSPackage))); actual.SetFieldOrProperty("core", this.package.Core); // Register output generated event to test event fired this.package.Core.OutputGenerated += (sender, args) => { eventFired = true; }; mockActiveDocument.ImplementExpr(doc => doc.Selection, mockTextSelection.Instance); mockTextSelection.ImplementExpr(sel => sel.GotoLine(this.violation.LineNumber, true)); mockTextSelection.ImplementExpr(sel => sel.ActivePoint, mockVirtualPoint.Instance); mockVirtualPoint.ImplementExpr(vp => vp.TryToShow(EnvDTE.vsPaneShowHow.vsPaneShowCentered, 0)); this.mockServiceProvider.ImplementExpr(sp => sp.GetService(typeof(EnvDTE.DTE)), mockDte.Instance); // Use private type to set static private field PrivateType privateProjectUtilities = new PrivateType(typeof(ProjectUtilities)); privateProjectUtilities.SetStaticFieldOrProperty("serviceProvider", this.mockServiceProvider.Instance); // Execute PrivateObject taskUnderTestPrivateObject = new PrivateObject(this.taskUnderTest, new PrivateType(typeof(ViolationTask))); taskUnderTestPrivateObject.Invoke("OnNavigate", EventArgs.Empty); // Verify the required methods are called to show the violation mockTextSelection.Verify(); mockVirtualPoint.Verify(); mockDocument.Verify(); Assert.IsTrue(eventFired, "Core did not fire output event"); }
public void OnNavigateToDocNotInProjectTest() { var mockDocumentEnumerator = new SequenceMock<IEnumerator>(); var mockDte = new Mock<DTE>(); var mockDocuments = new Mock<Documents>(); var mockDocument = new SequenceMock<Document>(); var mockActiveDocument = new Mock<Document>(); var mockTextSelection = new SequenceMock<TextSelection>(); var mockVirtualPoint = new SequenceMock<VirtualPoint>(); this.SetupProjectUtilities(mockDocumentEnumerator, mockDte, mockDocuments, mockDocument, mockActiveDocument, "DummyFile.txt"); var mockSecondDocument = new SequenceMock<Document>(); mockDocumentEnumerator.AddExpectationExpr(docs => docs.MoveNext(), true); mockDocumentEnumerator.AddExpectationExpr(docs => docs.Current, mockSecondDocument.Instance); mockDocumentEnumerator.AddExpectationExpr(docs => docs.MoveNext(), false); mockSecondDocument.AddExpectationExpr(doc => doc.FullName, "DummyFile.txt"); AnalysisHelper analysisHelper = this.SetCoreNoUI(); bool eventFired = false; var styleCopCore = (StyleCopCore)typeof(AnalysisHelper) .GetField("core", BindingFlags.Instance | BindingFlags.NonPublic) .GetValue(analysisHelper); styleCopCore.OutputGenerated += (sender, args) => { eventFired = true; }; mockActiveDocument.ImplementExpr(doc => doc.Selection, mockTextSelection.Instance); mockTextSelection.ImplementExpr(sel => sel.GotoLine(this.violation.LineNumber, true)); mockTextSelection.ImplementExpr(sel => sel.ActivePoint, mockVirtualPoint.Instance); mockVirtualPoint.ImplementExpr(vp => vp.TryToShow(EnvDTE.vsPaneShowHow.vsPaneShowCentered, 0)); this.mockServiceProvider.ImplementExpr(sp => sp.GetService(typeof(EnvDTE.DTE)), mockDte.Instance); typeof(ProjectUtilities).GetField("serviceProvider", BindingFlags.Static | BindingFlags.NonPublic) .SetValue(null, this.mockServiceProvider.Instance); // Execute typeof(ViolationTask).GetMethod("OnNavigate", BindingFlags.Instance | BindingFlags.NonPublic) .Invoke(this.taskUnderTest, new object[] { EventArgs.Empty }); // Verify the required methods are called to show the violation mockTextSelection.Verify(); mockVirtualPoint.Verify(); mockDocument.Verify(); Assert.IsTrue(eventFired, "Core did not fire output event"); }
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); }
public void OnNavigateToDocNotInProjectTest() { var mockDocumentEnumerator = new SequenceMock<IEnumerator>(); var mockDte = new Mock<DTE>(); var mockDocuments = new Mock<Documents>(); var mockDocument = new SequenceMock<Document>(); var mockActiveDocument = new Mock<Document>(); var mockTextSelection = new SequenceMock<TextSelection>(); var mockVirtualPoint = new SequenceMock<VirtualPoint>(); this.SetupProjectUtilities(mockDocumentEnumerator, mockDte, mockDocuments, mockDocument, mockActiveDocument, "DummyFile.txt"); var mockSecondDocument = new SequenceMock<Document>(); mockDocumentEnumerator.AddExpectationExpr(docs => docs.MoveNext(), true); mockDocumentEnumerator.AddExpectationExpr(docs => docs.Current, mockSecondDocument.Instance); mockDocumentEnumerator.AddExpectationExpr(docs => docs.MoveNext(), false); mockSecondDocument.AddExpectationExpr(doc => doc.FullName, "DummyFile.txt"); AnalysisHelper_Accessor analysisHelper = this.SetCoreNoUI(); bool eventFired = false; analysisHelper.core.OutputGenerated += (sender, args) => { eventFired = true; }; mockActiveDocument.ImplementExpr(doc => doc.Selection, mockTextSelection.Instance); mockTextSelection.ImplementExpr(sel => sel.GotoLine(this.violation.LineNumber, true)); mockTextSelection.ImplementExpr(sel => sel.ActivePoint, mockVirtualPoint.Instance); mockVirtualPoint.ImplementExpr(vp => vp.TryToShow(EnvDTE.vsPaneShowHow.vsPaneShowCentered, 0)); this.mockServiceProvider.ImplementExpr(sp => sp.GetService(typeof(EnvDTE.DTE)), mockDte.Instance); ProjectUtilities_Accessor.serviceProvider = this.mockServiceProvider.Instance; // Execute this.taskUnderTest.OnNavigate(EventArgs.Empty); // Verify the required methods are called to show the violation mockTextSelection.Verify(); mockVirtualPoint.Verify(); mockDocument.Verify(); Assert.IsTrue(eventFired, "Core did not fire output event"); }
public void OnNavigateToDocInProjectTest() { var mockDocumentEnumerator = new SequenceMock<IEnumerator>(); var mockDte = new Mock<DTE>(); var mockDocuments = new Mock<Documents>(); var mockDocument = new SequenceMock<Document>(); var mockActiveDocument = new Mock<Document>(); var mockTextSelection = new SequenceMock<TextSelection>(); var mockVirtualPoint = new SequenceMock<VirtualPoint>(); this.SetupProjectUtilities(mockDocumentEnumerator, mockDte, mockDocuments, mockDocument, mockActiveDocument, this.violation.File); mockDocument.AddExpectationExpr(doc => doc.Activate()); mockDocument.AddExpectationExpr(doc => doc.DTE, (Func<DTE>)delegate { return (EnvDTE.DTE)mockDte.Instance; }); mockActiveDocument.ImplementExpr(doc => doc.Selection, mockTextSelection.Instance); mockTextSelection.ImplementExpr(sel => sel.GotoLine(this.violation.LineNumber, true)); mockTextSelection.ImplementExpr(sel => sel.ActivePoint, mockVirtualPoint.Instance); mockVirtualPoint.ImplementExpr(vp => vp.TryToShow(EnvDTE.vsPaneShowHow.vsPaneShowCentered, 0)); this.mockServiceProvider.ImplementExpr(sp => sp.GetService(typeof(EnvDTE.DTE)), mockDte.Instance); ProjectUtilities_Accessor.serviceProvider = this.mockServiceProvider.Instance; // Execute this.taskUnderTest.OnNavigate(EventArgs.Empty); // Verify the required methods are called to show the violation mockTextSelection.Verify(); mockVirtualPoint.Verify(); mockDocument.Verify(); }
public void OnNavigateToDocInProjectTest() { var mockDocumentEnumerator = new SequenceMock<IEnumerator>(); var mockDte = new Mock<DTE>(); var mockDocuments = new Mock<Documents>(); var mockDocument = new SequenceMock<Document>(); var mockActiveDocument = new Mock<Document>(); var mockTextSelection = new SequenceMock<TextSelection>(); var mockVirtualPoint = new SequenceMock<VirtualPoint>(); this.SetupProjectUtilities(mockDocumentEnumerator, mockDte, mockDocuments, mockDocument, mockActiveDocument, this.violation.File); mockDocument.AddExpectationExpr(doc => doc.Activate()); mockDocument.AddExpectationExpr(doc => doc.DTE, (Func<DTE>)delegate { return (EnvDTE.DTE)mockDte.Instance; }); mockActiveDocument.ImplementExpr(doc => doc.Selection, mockTextSelection.Instance); mockTextSelection.ImplementExpr(sel => sel.GotoLine(this.violation.LineNumber, true)); mockTextSelection.ImplementExpr(sel => sel.ActivePoint, mockVirtualPoint.Instance); mockVirtualPoint.ImplementExpr(vp => vp.TryToShow(EnvDTE.vsPaneShowHow.vsPaneShowCentered, 0)); this.mockServiceProvider.ImplementExpr(sp => sp.GetService(typeof(EnvDTE.DTE)), mockDte.Instance); typeof(ProjectUtilities).GetField("serviceProvider", BindingFlags.Static | BindingFlags.NonPublic) .SetValue(null, this.mockServiceProvider.Instance); // Execute typeof(ViolationTask).GetMethod("OnNavigate", BindingFlags.Instance | BindingFlags.NonPublic) .Invoke(this.taskUnderTest, new object[] { EventArgs.Empty }); // Verify the required methods are called to show the violation mockTextSelection.Verify(); mockVirtualPoint.Verify(); mockDocument.Verify(); }
/// <summary> /// Implements the method or property getter specified by the lambda expression, returning default(TReturn) /// </summary> /// <typeparam name="T"> /// The type being mocked. /// </typeparam> /// <typeparam name="TReturn"> /// The return type of the method. /// </typeparam> /// <param name="mock"> /// The mock object. /// </param> /// <param name="expression"> /// The expression. /// </param> public static void AddExpectationExpr <T, TReturn>(this SequenceMock <T> mock, Expression <Func <T, TReturn> > expression) where T : class { mock.AddExpectation(expression, (obj, method, arguments) => default(TReturn)); }
private void SetupProjectUtilities( SequenceMock<IEnumerator> mockDocumentEnumerator, Mock<DTE> mockDte, Mock<Documents> mockDocuments, SequenceMock<Document> mockDocument, Mock<Document> mockActiveDocument, string fileName) { var mockSolution = new Mock<Solution>(); var mockProjects = new Mock<Projects>(); var mockProject = new Mock<Project>(); var mockProjectEnumerator = new SequenceMock<IEnumerator>(); // var mockEvents = new Mock<EnvDTE.Events>(); mockDte.ImplementExpr(dte => dte.Solution, mockSolution.Instance); mockDte.ImplementExpr(dte => dte.Documents, mockDocuments.Instance); mockDte.ImplementExpr(dte => dte.ActiveDocument, mockActiveDocument.Instance); // mockDte.ImplementExpr(dte => dte.Events, mockEvents.Instance); mockSolution.ImplementExpr(sol => sol.Projects, mockProjects.Instance); mockProjects.ImplementExpr(e => e.GetEnumerator(), mockProjectEnumerator.Instance); mockProjectEnumerator.AddExpectationExpr(en => en.MoveNext(), true); mockProjectEnumerator.AddExpectationExpr(en => en.Current, mockProject.Instance); mockProjectEnumerator.AddExpectationExpr(en => en.MoveNext(), false); mockProject.ImplementExpr(p => p.Kind, EnvDTE.Constants.vsProjectKindMisc); mockProject.ImplementExpr(p => p.ProjectItems, (Func<ProjectItems>)delegate { return null; }); mockDocuments.ImplementExpr(docs => docs.GetEnumerator(), mockDocumentEnumerator.Instance); mockDocumentEnumerator.AddExpectationExpr(docs => docs.MoveNext(), true); mockDocumentEnumerator.AddExpectationExpr(docs => docs.Current, mockDocument.Instance); mockDocumentEnumerator.AddExpectationExpr(docs => docs.MoveNext(), false); mockDocument.AddExpectationExpr(doc => doc.FullName, fileName); }
/// <summary> /// Implements the method or property getter specified by the lambda expression, returning the result of the /// given handler. /// </summary> /// <typeparam name="T"> /// The type being mocked. /// </typeparam> /// <typeparam name="TReturn"> /// The return type of the method. /// </typeparam> /// <param name="mock"> /// The mock object. /// </param> /// <param name="expression"> /// The expression. /// </param> /// <param name="handler"> /// The handler. /// </param> public static void AddExpectationExpr <T, TReturn>(this SequenceMock <T> mock, Expression <Func <T, TReturn> > expression, Func <object[], TReturn> handler) where T : class { mock.AddExpectation(expression, (obj, method, arguments) => handler(arguments)); }