public void LExpressionIsProjectAndUnrestrictedNameIsProject() { var enclosingModuleCode = string.Format("Public WithEvents anything As {0}.{0}", BindingTargetName); var builder = new MockVbeBuilder(); var enclosingProject = builder .ProjectBuilder(BindingTargetName, ProjectProtection.Unprotected) .AddComponent(TestClassName, ComponentType.ClassModule, enclosingModuleCode) .Build(); builder.AddProject(enclosingProject); var vbe = builder.Build(); using (var state = Parse(vbe)) { var declaration = state.AllUserDeclarations.Single(d => d.DeclarationType == DeclarationType.Project && d.ProjectName == BindingTargetName); // lExpression adds one reference, the MemberAcecssExpression adds another one. Assert.AreEqual(2, declaration.References.Count()); } }
public void LExpressionIsProjectAndUnrestrictedNameIsProceduralModule() { const string projectName = "AnyName"; var enclosingModuleCode = $"Public WithEvents anything As {projectName}.{BindingTargetName}"; var builder = new MockVbeBuilder(); var enclosingProject = builder .ProjectBuilder(projectName, ProjectProtection.Unprotected) .AddComponent(BindingTargetName, ComponentType.StandardModule, enclosingModuleCode) .Build(); builder.AddProject(enclosingProject); var vbe = builder.Build(); using (var state = Parse(vbe)) { var declaration = state.AllUserDeclarations.Single(d => d.DeclarationType == DeclarationType.ProceduralModule && d.IdentifierName == BindingTargetName); Assert.AreEqual(1, declaration.References.Count()); } }
public void NonReturningFunction_NoResult_GivenClassMemberCall() { const string code = @" Public Function Foo() As Boolean With New Class1 .ByRefAssign Foo End With End Function "; const string classCode = @" Public Sub ByRefAssign(ByRef b As Boolean) End Sub "; var builder = new MockVbeBuilder(); builder.ProjectBuilder("TestProject", ProjectProtection.Unprotected) .AddComponent("TestModule1", ComponentType.StandardModule, code) .AddComponent("Class1", ComponentType.ClassModule, classCode); var vbe = builder.Build(); Assert.AreEqual(0, InspectionResults(vbe.Object).Count()); }
public void UIRemovesRemovedTestMethods() { var testMethods = @"'@TestMethod Public Sub TestMethod1() End Sub"; var builder = new MockVbeBuilder(); var project = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected) .AddComponent("TestModule1", ComponentType.StandardModule, GetTestModuleInput + testMethods) .AddComponent("TestModule2", ComponentType.StandardModule, GetTestModuleInput + testMethods); builder.AddProject(project.Build()); var vbe = builder.Build().Object; var parser = MockParser.Create(vbe); using (var state = parser.State) { var model = new TestExplorerModel(vbe, state); parser.Parse(new CancellationTokenSource()); if (state.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } Assert.AreEqual(2, model.Tests.Count); project.RemoveComponent(project.MockComponents[1]); parser.Parse(new CancellationTokenSource()); if (state.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } Assert.AreEqual(1, model.Tests.Count); } }
public MockedTestEngine(IReadOnlyList <string> moduleNames, IReadOnlyList <int> methodCounts) : this() { if (moduleNames.Count != methodCounts.Count) { Assert.Inconclusive("Test setup error."); } var builder = new MockVbeBuilder(); var project = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected); for (var index = 0; index < moduleNames.Count; index++) { var testModuleCode = string.Join(Environment.NewLine, Enumerable.Range(1, methodCounts[index]).Select(num => GetTestMethod(num))); project.AddComponent(moduleNames[index], ComponentType.StandardModule, TestModuleHeader + testModuleCode); } project.AddProjectToVbeBuilder(); Vbe = builder.Build(); ParserState = MockParser.Create(Vbe.Object).State; TestEngine = new TestEngine(ParserState, _fakesFactory.Object, VbeInteraction.Object, WrapperProvider.Object, Dispatcher.Object, Vbe.Object); }
public void RecursiveDefaultMember() { string callerModule = @" Public Property Get Ok() As Klasse1 End Property Public Sub Test() Call Ok(1) End Sub "; string middleman = @" Public Property Get Test1() As Klasse2 End Property "; string defaultMemberClass = @" Public Property Get Test2(a As Integer) Test2 = 2 End Property "; var builder = new MockVbeBuilder(); var enclosingProjectBuilder = builder.ProjectBuilder("Any Project", ProjectProtection.Unprotected); enclosingProjectBuilder.AddComponent("AnyModule1", ComponentType.StandardModule, callerModule); enclosingProjectBuilder.AddComponent("AnyClass", ComponentType.ClassModule, middleman); enclosingProjectBuilder.AddComponent("AnyClass2", ComponentType.ClassModule, defaultMemberClass); var enclosingProject = enclosingProjectBuilder.Build(); builder.AddProject(enclosingProject); var vbe = builder.Build(); var state = Parse(vbe); var declaration = state.AllUserDeclarations.Single(d => d.DeclarationType == DeclarationType.PropertyGet && d.IdentifierName == "Test2"); Assert.AreEqual(1, declaration.References.Count()); }
public void IndexedDefaultMember() { string callerModule = @" Public Property Get Ok() As Klasse2 End Property Public Sub Test() Call Ok(1) End Sub "; string defaultMemberClass = @" Public Property Get Test2(a As Integer) Attribute Test2.VB_UserMemId = 0 Test2 = 2 End Property "; var builder = new MockVbeBuilder(); var enclosingProjectBuilder = builder.ProjectBuilder("Any Project", ProjectProtection.Unprotected); enclosingProjectBuilder.AddComponent("AnyModule1", ComponentType.StandardModule, callerModule); enclosingProjectBuilder.AddComponent("Klasse2", ComponentType.ClassModule, defaultMemberClass); var enclosingProject = enclosingProjectBuilder.Build(); builder.AddProject(enclosingProject); var vbe = builder.Build(); using (var state = Parse(vbe)) { var declaration = state.AllUserDeclarations.Single(d => d.DeclarationType == DeclarationType.PropertyGet && d.IdentifierName == "Test2"); //One for the assignment in the property itself and one for the default member access. Assert.AreEqual(2, declaration.References.Count()); } }
public void EnclosingProcedureComesBeforeEnclosingModule() { string testCode = string.Format(@" Public Sub Test() Dim {0} As Long Dim a As String * {0} End Sub", BindingTargetName); var builder = new MockVbeBuilder(); var enclosingProjectBuilder = builder.ProjectBuilder(BindingTargetName, ProjectProtection.Unprotected); enclosingProjectBuilder.AddComponent(TestClassName, ComponentType.ClassModule, testCode); var enclosingProject = enclosingProjectBuilder.Build(); builder.AddProject(enclosingProject); var vbe = builder.Build(); using (var state = Parse(vbe)) { var declaration = state.AllUserDeclarations.Single(d => d.DeclarationType == DeclarationType.Variable && d.IdentifierName == BindingTargetName); Assert.AreEqual(1, declaration.References.Count()); } }
public void LExpressionIsModuleAndUnrestrictedNameIsType() { var builder = new MockVbeBuilder(); const string className = "AnyName"; var enclosingProject = builder .ProjectBuilder("AnyProjectName", ProjectProtection.Unprotected) .AddComponent(TestClassName, ComponentType.ClassModule, $"Public WithEvents anything As {className}.{BindingTargetName}") .AddComponent(className, ComponentType.ClassModule, CreateUdt(BindingTargetName)) .Build(); builder.AddProject(enclosingProject); var vbe = builder.Build(); using (var state = Parse(vbe)) { var declaration = state.AllUserDeclarations.Single(d => d.DeclarationType == DeclarationType.UserDefinedType && d.IdentifierName == BindingTargetName); Assert.AreEqual(1, declaration.References.Count()); } }
public void RegistrationOfRubberduckIoCContainerWithoutSC_NoException() { var vbeBuilder = new MockVbeBuilder(); var ideMock = vbeBuilder.Build(); var sourceFileHandler = new Mock <ITempSourceFileHandler>().Object; ideMock.Setup(m => m.TempSourceFileHandler).Returns(() => sourceFileHandler); var ide = ideMock.Object; var addInBuilder = new MockAddInBuilder(); var addin = addInBuilder.Build().Object; var vbeNativeApi = new Mock <IVbeNativeApi>(); var initialSettings = new GeneralSettings { EnableExperimentalFeatures = new List <ExperimentalFeatures>() }; using (var container = new WindsorContainer().Install(new RubberduckIoCInstaller(ide, addin, initialSettings, vbeNativeApi.Object))) { } //This test does not need an assert because it tests that no exception has been thrown. }
public void TestEngine_ExposesTestMethod_AndRaisesRefresh() { var testMethods = @"'@TestMethod Public Sub TestMethod1() End Sub"; var builder = new MockVbeBuilder() .ProjectBuilder("TestProject1", ProjectProtection.Unprotected) .AddComponent("TestModule1", ComponentType.StandardModule, TestModuleHeader + testMethods) .AddProjectToVbeBuilder(); var vbe = builder.Build().Object; var parser = MockParser.Create(vbe); var interaction = new Mock <IVBEInteraction>(); var wrapperProvider = new Mock <ITypeLibWrapperProvider>(); var fakesFactory = new Mock <IFakesFactory>(); var dispatcher = new Mock <IUiDispatcher>(); dispatcher.Setup(d => d.InvokeAsync(It.IsAny <Action>())) .Callback((Action action) => action.Invoke()) .Verifiable(); using (var state = parser.State) { var engine = new TestEngine(state, fakesFactory.Object, interaction.Object, wrapperProvider.Object, dispatcher.Object, vbe); int refreshes = 0; engine.TestsRefreshed += (sender, args) => refreshes++; parser.Parse(new CancellationTokenSource()); if (!engine.CanRun) { Assert.Inconclusive("Parser Error"); } Assert.AreEqual(1, engine.Tests.Count()); Assert.AreEqual(1, refreshes); } }
public void UIDiscoversAnnotatedTestMethods() { var testMethods = @"'@TestMethod Public Sub TestMethod1() End Sub"; var builder = new MockVbeBuilder() .ProjectBuilder("TestProject1", ProjectProtection.Unprotected) .AddComponent("TestModule1", ComponentType.StandardModule, GetTestModuleInput + testMethods) .MockVbeBuilder(); var vbe = builder.Build().Object; var parser = MockParser.Create(vbe, new RubberduckParserState(vbe)); var model = new TestExplorerModel(vbe, parser.State); parser.Parse(new CancellationTokenSource()); if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } Assert.AreEqual(1, model.Tests.Count); }
public void MemberNotOnInterface_DoesNotReturnResult_ControlObject() { const string inputCode = @"Sub Foo(bar as MSForms.TextBox) Debug.Print bar.Left End Sub"; var vbeBuilder = new MockVbeBuilder(); var projectBuilder = vbeBuilder.ProjectBuilder("testproject", ProjectProtection.Unprotected); projectBuilder.MockUserFormBuilder("UserForm1", inputCode).AddFormToProjectBuilder() .AddReference("MSForms", MockVbeBuilder.LibraryPathMsForms, 2, 0, true); vbeBuilder.AddProject(projectBuilder.Build()); var vbe = vbeBuilder.Build(); using (var state = MockParser.CreateAndParse(vbe.Object)) { var inspection = new MemberNotOnInterfaceInspection(state); var inspectionResults = inspection.GetInspectionResults(CancellationToken.None); Assert.IsTrue(!inspectionResults.Any()); } }
public void FindAllReferences_FormNoResults_DisplaysMessageBox() { var code = @" Public Sub DoSomething() End Sub "; var builder = new MockVbeBuilder(); var project = builder.ProjectBuilder("TestProject1", ProjectProtection.Unprotected); var form = project.MockUserFormBuilder("Form1", code).Build(); project.AddComponent(form.Component, form.CodeModule); builder.AddProject(project.Build()); var vbe = builder.Build(); vbe.SetupGet(v => v.SelectedVBComponent).Returns(form.Component.Object); using (var state = MockParser.CreateAndParse(vbe.Object)) { AssertParserReady(state); var messageBox = new Mock <IMessageBox>(); messageBox.Setup(m => m.Show(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <MessageBoxButtons>(), It.IsAny <MessageBoxIcon>())).Returns(DialogResult.OK); var vm = new SearchResultsWindowViewModel(); var uiDispatcher = new Mock <IUiDispatcher>(); var command = new FindAllReferencesCommand(null, messageBox.Object, state, vbe.Object, vm, null, uiDispatcher.Object); var target = state.AllUserDeclarations.Single(s => s.IdentifierName == "Form1"); command.Execute(target); messageBox.Verify(m => m.Show(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <MessageBoxButtons>(), It.IsAny <MessageBoxIcon>()), Times.Once); } }
public void MoveCloser_RespectsObjectProperties_InUsages() { string inputClassCode = @" Option Explicit Private _name As Long Private _myOtherProperty As Long Public Property Set Name(name As String) _name = name End Property Public Property Get Name() As String Name = _name End Property Public Property Set OtherProperty(val As Long) _myOtherProperty = val End Property Public Property Get OtherProperty() As Long OtherProperty = _myOtherProperty End Property "; string inputCode = @"Private foo As Class1 Public Sub Test() Debug.Print ""Some statements between"" Debug.Print ""Declaration and first usage!"" Set foo = new Class1 foo.Name = ""FooName"" foo.OtherProperty = 1626 End Sub"; var selection = new Selection(1, 1); const string expectedCode = @" Public Sub Test() Debug.Print ""Some statements between"" Debug.Print ""Declaration and first usage!"" Dim foo As Class1 Set foo = new Class1 foo.Name = ""FooName"" foo.OtherProperty = 1626 End Sub"; var builder = new MockVbeBuilder(); var project = builder.ProjectBuilder("VBAProject", ProjectProtection.Unprotected); project.AddComponent("Class1", ComponentType.ClassModule, inputClassCode); project.AddComponent("Module1", ComponentType.StandardModule, inputCode); builder = builder.AddProject(project.Build()); var vbe = builder.Build(); var component = project.MockComponents.Find(mc => mc.Object.Name.Equals("Module1")).Object; var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(component), selection); var(state, rewritingManager) = MockParser.CreateAndParseWithRewritingManager(vbe.Object); using (state) { var messageBox = new Mock <IMessageBox>(); var refactoring = new MoveCloserToUsageRefactoring(vbe.Object, state, messageBox.Object, rewritingManager); refactoring.Refactor(qualifiedSelection); var actualCode = component.CodeModule.Content(); Assert.AreEqual(expectedCode, actualCode); } }
public void TestEngine_Run_AndAssertFailed_RaisesCompletionEvent_Failed() { var testMethods = @"'@TestMethod Public Sub TestMethod1() End Sub"; var builder = new MockVbeBuilder() .ProjectBuilder("TestProject1", ProjectProtection.Unprotected) .AddComponent("TestModule1", ComponentType.StandardModule, TestModuleHeader + testMethods) .AddProjectToVbeBuilder(); var vbe = builder.Build().Object; var parser = MockParser.Create(vbe); var interaction = new Mock <IVBEInteraction>(); var wrapperProvider = new Mock <ITypeLibWrapperProvider>(); var typeLibMock = new Mock <ITypeLibWrapper>(); wrapperProvider.Setup(p => p.TypeLibWrapperFromProject(It.IsAny <string>())) .Returns(typeLibMock.Object) .Verifiable(); long durationStub; interaction.Setup(ia => ia.RunTestMethod(typeLibMock.Object, It.IsAny <TestMethod>(), It.IsAny <EventHandler <AssertCompletedEventArgs> >(), out durationStub)) .Callback(new RunTestMethodCallback((ITypeLibWrapper _, TestMethod method, EventHandler <AssertCompletedEventArgs> assertHandler, out long duration) => { duration = 0; AssertHandler.OnAssertCompleted += assertHandler; AssertHandler.OnAssertFailed("Test Message", "TestMethod1"); AssertHandler.OnAssertCompleted -= assertHandler; })) .Verifiable(); typeLibMock.Setup(tlm => tlm.Dispose()).Verifiable(); var fakesFactory = new Mock <IFakesFactory>(); var createdFakes = new Mock <IFakes>(); fakesFactory.Setup(factory => factory.Create()) .Returns(createdFakes.Object); var dispatcher = new Mock <IUiDispatcher>(); dispatcher.Setup(d => d.InvokeAsync(It.IsAny <Action>())) .Callback((Action action) => action.Invoke()) .Verifiable(); var completionEvents = new List <TestCompletedEventArgs>(); using (var state = parser.State) { var engine = new TestEngine(state, fakesFactory.Object, interaction.Object, wrapperProvider.Object, dispatcher.Object, vbe); engine.TestCompleted += (source, args) => completionEvents.Add(args); parser.Parse(new CancellationTokenSource()); if (!engine.CanRun) { Assert.Inconclusive("Parser Error"); } engine.Run(engine.Tests); } Mock.Verify(dispatcher, interaction, wrapperProvider, typeLibMock); Assert.AreEqual(1, completionEvents.Count); Assert.AreEqual(new TestResult(TestOutcome.Failed, string.Format(AssertMessages.Assert_FailedMessageFormat, "TestMethod1", "Test Message")), completionEvents.First().Result); }