public void AssignsInteger() { const string inputcode = @"Sub Foo() Dim d If True Then d = 0 Else d = 1 EndIf End Sub"; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputcode, out _); using (var state = MockParser.CreateAndParse(vbe.Object)) { var inspection = new BooleanAssignedInIfElseInspection(state); var inspector = InspectionsHelper.GetInspector(inspection); var results = inspector.FindIssuesAsync(state, CancellationToken.None).Result; Assert.IsFalse(results.Any()); } }
private static string Parse(string code, string filename) { var builder = new MockVbeBuilder(); VBComponent component; var vbe = builder.BuildFromSingleStandardModule(code, out component); var mockHost = new Mock <IHostApplication>(); mockHost.SetupAllProperties(); var state = new RubberduckParserState(new Mock <ISinks>().Object); var parser = MockParser.Create(vbe.Object, state); parser.Parse(new CancellationTokenSource()); if (parser.State.Status == ParserState.Error) { Assert.Inconclusive("Parser Error: " + filename); } var tree = state.GetParseTree(component); var parsed = tree.GetText(); var withoutEOF = parsed.Substring(0, parsed.Length - 5); return(withoutEOF); }
public void ProcedureNotUsed_IgnoreQuickFixWorks() { const string inputCode = @"Private Sub Foo(ByVal arg1 as Integer) End Sub"; const string expectedCode = @"'@Ignore ProcedureNotUsed Private Sub Foo(ByVal arg1 as Integer) End Sub"; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component); using (var state = MockParser.CreateAndParse(vbe.Object)) { var inspection = new ProcedureNotUsedInspection(state); var inspectionResults = inspection.GetInspectionResults(CancellationToken.None); new IgnoreOnceQuickFix(state, new[] { inspection }).Fix(inspectionResults.First()); Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText()); } }
public void ImplicitVariantReturnType_DoesNotReturnResult_LibraryFunction() { const string inputCode = @"Declare PtrSafe Function CreateProcess Lib ""kernel32"" _ Alias ""CreateProcessA""(ByVal lpApplicationName As String, _ ByVal lpCommandLine As String, _ lpProcessAttributes As SECURITY_ATTRIBUTES, _ lpThreadAttributes As SECURITY_ATTRIBUTES, _ ByVal bInheritHandles As Long, _ ByVal dwCreationFlags As Long, _ lpEnvironment As Any, _ ByVal lpCurrentDirectory As String, _ lpStartupInfo As STARTUPINFO, _ lpProcessInformation As PROCESS_INFORMATION) As Long"; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out _); var state = MockParser.CreateAndParse(vbe.Object); var inspection = new ImplicitVariantReturnTypeInspection(state); var inspectionResults = inspection.GetInspectionResults(); Assert.AreEqual(0, inspectionResults.Count()); }
public void AddsTestModule() { var vbe = MockVbeBuilder.BuildFromSingleStandardModule(string.Empty, out var component); using (var state = MockParser.CreateAndParse(vbe.Object)) { var messageBox = new Mock <IMessageBox>(); var interaction = new Mock <IVBEInteraction>(); var settings = new Mock <ConfigurationLoader>(null, null, null, null, null, null, null, null); var config = GetUnitTestConfig(); settings.Setup(x => x.LoadConfiguration()).Returns(config); var addTestModuleCommand = new AddTestModuleCommand(vbe.Object, state, settings.Object, messageBox.Object, interaction.Object); addTestModuleCommand.Execute(null); // mock suite auto-assigns "TestModule1" to the first component when we create the mock var project = state.DeclarationFinder.FindProject("TestProject1"); var module = state.DeclarationFinder.FindStdModule("TestModule2", project); Assert.IsTrue(module.Annotations.Any(a => a.AnnotationType == AnnotationType.TestModule)); } }
public void ObsoleteCallStatement_ReturnsResults_SomeObsoleteCallStatements() { const string inputCode = @"Sub Foo() Call Goo(1, ""test"") End Sub Sub Goo(arg1 As Integer, arg1 As String) Foo End Sub"; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out _); using (var state = MockParser.CreateAndParse(vbe.Object)) { var inspection = new ObsoleteCallStatementInspection(state); var inspector = InspectionsHelper.GetInspector(inspection); var inspectionResults = inspector.FindIssuesAsync(state, CancellationToken.None).Result; Assert.AreEqual(1, inspectionResults.Count()); } }
public void ImplicitVariantReturnType_QuickFixWorks_PropertyGet() { const string inputCode = @"Property Get Foo() End Property"; const string expectedCode = @"Property Get Foo() As Variant End Property"; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component); using (var state = MockParser.CreateAndParse(vbe.Object)) { var inspection = new ImplicitVariantReturnTypeInspection(state); var inspectionResults = inspection.GetInspectionResults(CancellationToken.None); new SetExplicitVariantReturnTypeQuickFix(state).Fix(inspectionResults.First()); Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText()); } }
public void AddNoIndentAnnotation_CanExecute_NullActiveCodePane() { var builder = new MockVbeBuilder(); VBComponent component; var vbe = builder.BuildFromSingleStandardModule("", out component); vbe.Setup(v => v.ActiveCodePane).Returns((CodePane)null); var mockHost = new Mock <IHostApplication>(); mockHost.SetupAllProperties(); var parser = MockParser.Create(vbe.Object, new RubberduckParserState(new Mock <ISinks>().Object)); parser.Parse(new CancellationTokenSource()); if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } var noIndentAnnotationCommand = new NoIndentAnnotationCommand(vbe.Object, parser.State); Assert.IsFalse(noIndentAnnotationCommand.CanExecute(null)); }
public void OptionBaseZeroStatement_Ignored_DoesNotReturnResult() { string inputCode = @"'@Ignore OptionBaseZero Option Base 0"; var settings = new Mock <IGeneralConfigService>(); var config = GetTestConfig(); settings.Setup(x => x.LoadConfiguration()).Returns(config); IVBComponent component; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out component); var state = MockParser.CreateAndParse(vbe.Object); var inspection = new ObsoleteCallStatementInspection(state); var inspector = new Inspector(settings.Object, new IInspection[] { inspection }); var inspectionResults = inspector.FindIssuesAsync(state, CancellationToken.None).Result; Assert.IsFalse(inspectionResults.Any()); }
public void AnnotationDuplicatedTwice_ReturnsSingleResult() { const string inputCode = @" Public Sub Bar() End Sub '@Obsolete '@Obsolete '@Obsolete Public Sub Foo() End Sub"; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out _); using (var state = MockParser.CreateAndParse(vbe.Object)) { var inspection = new DuplicatedAnnotationInspection(state); var inspectionResults = inspection.GetInspectionResults(CancellationToken.None); Assert.AreEqual(1, inspectionResults.Count()); } }
public void AddNoIndentAnnotation() { var builder = new MockVbeBuilder(); VBComponent component; var vbe = builder.BuildFromSingleStandardModule("", out component); var mockHost = new Mock <IHostApplication>(); mockHost.SetupAllProperties(); var parser = MockParser.Create(vbe.Object, new RubberduckParserState(new Mock <ISinks>().Object)); parser.Parse(new CancellationTokenSource()); if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } var noIndentAnnotationCommand = new NoIndentAnnotationCommand(vbe.Object, parser.State); noIndentAnnotationCommand.Execute(null); Assert.AreEqual("'@NoIndent\r\n", component.CodeModule.Lines()); }
public void ModuleScopeDimKeyword_QuickFixWorks_MultipleDeclarations() { const string inputCode = @"Dim foo As String, _ bar As Integer"; const string expectedCode = @"Private foo As String, _ bar As Integer"; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component); using (var state = MockParser.CreateAndParse(vbe.Object)) { var inspection = new ModuleScopeDimKeywordInspection(state); var inspector = InspectionsHelper.GetInspector(inspection); var inspectionResults = inspector.FindIssuesAsync(state, CancellationToken.None).Result; new ChangeDimToPrivateQuickFix(state).Fix(inspectionResults.First()); Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText()); } }
public void ObsoleteCallStatement_QuickFixWorks_Variable_StringTypeHint() { const string inputCode = @"Public Sub Foo() Dim buzz$ End Sub"; const string expectedCode = @"Public Sub Foo() Dim buzz As String End Sub"; //Arrange var builder = new MockVbeBuilder(); VBComponent component; var vbe = builder.BuildFromSingleStandardModule(inputCode, out component); var project = vbe.Object.VBProjects.Item(0); var module = project.VBComponents.Item(0).CodeModule; var mockHost = new Mock <IHostApplication>(); mockHost.SetupAllProperties(); var parser = MockParser.Create(vbe.Object, new RubberduckParserState()); parser.Parse(); if (parser.State.Status == ParserState.Error) { Assert.Inconclusive("Parser Error"); } var inspection = new ObsoleteTypeHintInspection(parser.State); var inspectionResults = inspection.GetInspectionResults(); foreach (var inspectionResult in inspectionResults) { inspectionResult.QuickFixes.First().Fix(); } Assert.AreEqual(expectedCode, module.Lines()); }
public void GivenPrivateSub_DefaultQuickFixRemovesParameter() { const string inputCode = @" Private Sub Foo(ByVal arg1 as Integer) End Sub"; const string expectedCode = @" Private Sub Foo() End Sub"; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component); using (var state = MockParser.CreateAndParse(vbe.Object)) { var inspection = new ParameterNotUsedInspection(state); var inspectionResults = inspection.GetInspectionResults(); new RemoveUnusedParameterQuickFix(vbe.Object, state, new Mock <IMessageBox>().Object).Fix( inspectionResults.First()); Assert.AreEqual(expectedCode, component.CodeModule.Content()); } }
public void ParameterCanBeByVal_QuickFixWithOptionalByRefWorks() { const string inputCode = @"Sub Test(Optional ByRef foo As String = ""bar"") Debug.Print foo End Sub"; const string expectedCode = @"Sub Test(Optional ByVal foo As String = ""bar"") Debug.Print foo End Sub"; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component); using (var state = MockParser.CreateAndParse(vbe.Object)) { var inspection = new ParameterCanBeByValInspection(state); new PassParameterByValueQuickFix(state).Fix(inspection.GetInspectionResults().First()); Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText()); } }
public void MultilineParameter_QuickFixWorks() { const string inputCode = @"Public Sub Foo( _ ByVal _ Var1 _ As _ Integer) End Sub"; const string expectedCode = @"Public Sub Foo( _ ByVal Var1 As Integer) End Sub"; //Arrange var builder = new MockVbeBuilder(); VBComponent component; var vbe = builder.BuildFromSingleStandardModule(inputCode, out component); var project = vbe.Object.VBProjects.Item(0); var module = project.VBComponents.Item(0).CodeModule; var mockHost = new Mock <IHostApplication>(); mockHost.SetupAllProperties(); var parser = MockParser.Create(vbe.Object, new RubberduckParserState()); parser.Parse(); if (parser.State.Status == ParserState.Error) { Assert.Inconclusive("Parser Error"); } var inspection = new MultilineParameterInspection(parser.State); var inspectionResults = inspection.GetInspectionResults(); inspectionResults.First().QuickFixes.First().Fix(); Assert.AreEqual(expectedCode, module.Lines()); }
public void ParameterCanBeByVal_QuickFixWorks_PassedByUnspecified_MultilineParameter() { const string inputCode = @"Sub Foo( _ arg1 As String) End Sub"; const string expectedCode = @"Sub Foo( _ ByVal arg1 As String) End Sub"; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component); using (var state = MockParser.CreateAndParse(vbe.Object)) { var inspection = new ParameterCanBeByValInspection(state); new PassParameterByValueQuickFix(state).Fix(inspection.GetInspectionResults().First()); Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText()); } }
public void NonMemberAnnotationsAboveMemberDoNotGetScopedToMember() { const string inputCode = @" '@TestModule Public Sub Foo() End Sub Public Function Bar() As Variant End Function"; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out _); using (var state = MockParser.CreateAndParse(vbe.Object)) { var fooDeclaration = state.DeclarationFinder.UserDeclarations(DeclarationType.Procedure).Single(); var expectedAnnotationCount = 0; var actualAnnotationCount = fooDeclaration.Annotations.Count(); Assert.AreEqual(expectedAnnotationCount, actualAnnotationCount); } }
public void ObjectVariableNotSet_ForFunctionAssignment_ReturnsResult() { var expectedResultCount = 2; var input = @" Private Function CombineRanges(ByVal source As Range, ByVal toCombine As Range) As Range If source Is Nothing Then CombineRanges = toCombine 'no inspection result (but there should be one!) Else CombineRanges = Union(source, toCombine) 'no inspection result (but there should be one!) End If End Function"; var expectedCode = @" Private Function CombineRanges(ByVal source As Range, ByVal toCombine As Range) As Range If source Is Nothing Then Set CombineRanges = toCombine 'no inspection result (but there should be one!) Else Set CombineRanges = Union(source, toCombine) 'no inspection result (but there should be one!) End If End Function"; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(input, out var component); using (var state = MockParser.CreateAndParse(vbe.Object)) { var inspection = new ObjectVariableNotSetInspection(state); var inspectionResults = inspection.GetInspectionResults(CancellationToken.None).ToList(); Assert.AreEqual(expectedResultCount, inspectionResults.Count); var fix = new UseSetKeywordForObjectAssignmentQuickFix(state); foreach (var result in inspectionResults) { fix.Fix(result); } Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText()); } }
public void VariableAnnotationsAboveFirstNonAnnotationLineAboveVariableStillGetScopedToVariable() { const string inputCode = @" '@Obsolete 'SomeComment Public foo As Long Public Function Bar() As Variant End Function"; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out _); using (var state = MockParser.CreateAndParse(vbe.Object)) { var barDeclaration = state.DeclarationFinder.UserDeclarations(DeclarationType.Variable).Single(); var expectedAnnotationCount = 1; var actualAnnotationCount = barDeclaration.Annotations.Count(); Assert.AreEqual(expectedAnnotationCount, actualAnnotationCount); } }
public void VariableTypeNotDeclared_QuickFixWorks_Variable() { const string inputCode = @"Sub Foo() Dim var1 End Sub"; const string expectedCode = @"Sub Foo() Dim var1 As Variant End Sub"; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component); using (var state = MockParser.CreateAndParse(vbe.Object)) { var inspection = new VariableTypeNotDeclaredInspection(state); new DeclareAsExplicitVariantQuickFix(state).Fix(inspection.GetInspectionResults(CancellationToken.None).First()); Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText()); } }
public void IdentifierAnnotationsOnPreviousNonWhiteSpaceDoNotGetScopedToIdentifier() { const string inputCode = @" Public foo As Long Public Function Bar() As Variant '@Ignore MissingAttribute foo = 42 End Function"; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out _); using (var state = MockParser.CreateAndParse(vbe.Object)) { var fooDeclaration = state.DeclarationFinder.UserDeclarations(DeclarationType.Variable).Single(); var fooReference = fooDeclaration.References.Single(); var expectedAnnotationCount = 0; var actualAnnotationCount = fooReference.Annotations.Count(); Assert.AreEqual(expectedAnnotationCount, actualAnnotationCount); } }
public void AnnotationsAboveMemberGetScopedToMember_NotFirstMember() { const string inputCode = @" Public Sub Foo() End Sub '@TestMethod '@Enumerator 17, 12 @DefaultMember Public Function Bar() As Variant End Function"; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out _); using (var state = MockParser.CreateAndParse(vbe.Object)) { var barDeclaration = state.DeclarationFinder.UserDeclarations(DeclarationType.Function).Single(); var expectedAnnotationCount = 3; var actualAnnotationCount = barDeclaration.Annotations.Count(); Assert.AreEqual(expectedAnnotationCount, actualAnnotationCount); } }
public void UnassignedVariable_QuickFixWorks() { const string inputCode = @"Sub Foo() Dim var1 As String End Sub"; const string expectedCode = @"Sub Foo() End Sub"; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component); using (var state = MockParser.CreateAndParse(vbe.Object)) { var inspection = new VariableNotUsedInspection(state); new RemoveUnusedDeclarationQuickFix(state).Fix(inspection.GetInspectionResults().First()); var rewriter = state.GetRewriter(component); Assert.AreEqual(expectedCode, rewriter.GetText()); } }
public void MoveFieldCloserToUsage_QuickFixWorks() { const string inputCode = @"Private bar As String Public Sub Foo() bar = ""test"" End Sub"; const string expectedCode = @"Public Sub Foo() Dim bar As String bar = ""test"" End Sub"; //Arrange var builder = new MockVbeBuilder(); VBComponent component; var vbe = builder.BuildFromSingleStandardModule(inputCode, out component); var project = vbe.Object.VBProjects.Item(0); var module = project.VBComponents.Item(0).CodeModule; var mockHost = new Mock <IHostApplication>(); mockHost.SetupAllProperties(); var parser = MockParser.Create(vbe.Object, new RubberduckParserState(new Mock <ISinks>().Object)); parser.Parse(new CancellationTokenSource()); if (parser.State.Status >= ParserState.Error) { Assert.Inconclusive("Parser Error"); } var inspection = new MoveFieldCloserToUsageInspection(parser.State); var inspectionResults = inspection.GetInspectionResults(); inspectionResults.First().QuickFixes.First().Fix(); Assert.AreEqual(expectedCode, module.Lines()); }
public void ProviderKnowsAboutInspection() { const string inputCode = @"Public Sub Foo() Dim str As String str = """" End Sub"; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out _); var(state, rewritingManager) = MockParser.CreateAndParseWithRewritingManager(vbe.Object); using (state) { var inspection = new EmptyStringLiteralInspection(state); var inspector = InspectionsHelper.GetInspector(inspection); var inspectionResults = inspector.FindIssuesAsync(state, CancellationToken.None).Result; var failureNotifier = new Mock <IQuickFixFailureNotifier>().Object; var quickFixProvider = new QuickFixProvider(rewritingManager, failureNotifier, new IQuickFix[] { new ReplaceEmptyStringLiteralStatementQuickFix() }); Assert.AreEqual(1, quickFixProvider.QuickFixes(inspectionResults.First()).Count()); } }
public void ObsoleteCallStatement_IgnoreQuickFixWorks() { const string inputCode = @"Sub Foo() Call Goo(1, ""test"") End Sub Sub Goo(arg1 As Integer, arg1 As String) Call Foo End Sub"; const string expectedCode = @"Sub Foo() '@Ignore ObsoleteCallStatement Call Goo(1, ""test"") End Sub Sub Goo(arg1 As Integer, arg1 As String) '@Ignore ObsoleteCallStatement Call Foo End Sub"; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component); using (var state = MockParser.CreateAndParse(vbe.Object)) { var inspection = new ObsoleteCallStatementInspection(state); var inspector = InspectionsHelper.GetInspector(inspection); var inspectionResults = inspector.FindIssuesAsync(state, CancellationToken.None).Result; var fix = new IgnoreOnceQuickFix(state, new[] { inspection }); foreach (var result in inspectionResults) { fix.Fix(result); } Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText()); } }
public void GivenOnlyBlankLines_EndLine_Works() { const string inputCode = @" "; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component); var pane = component.CodeModule.CodePane; using (var state = MockParser.CreateAndParse(vbe.Object)) { var tree = (Antlr4.Runtime.ParserRuleContext)state.GetParseTree(new QualifiedModuleName(component)); var startToken = tree.Start; var endToken = tree.Stop; // Reminder: token columns are zero-based but lines are one-based Assert.IsTrue(startToken.EndLine() == 1); Assert.IsTrue(endToken.EndLine() == 4); } }
public void ImplicitVariantReturnType_IgnoreQuickFixWorks() { const string inputCode = @"Function Foo() End Function"; const string expectedCode = @"'@Ignore ImplicitVariantReturnType Function Foo() End Function"; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component); using (var state = MockParser.CreateAndParse(vbe.Object)) { var inspection = new ImplicitVariantReturnTypeInspection(state); var inspectionResults = inspection.GetInspectionResults(CancellationToken.None); new IgnoreOnceQuickFix(state, new[] { inspection }).Fix(inspectionResults.First()); Assert.AreEqual(expectedCode, state.GetRewriter(component).GetText()); } }
public void BlockContainsEOLComment() { const string inputcode = @"Sub Foo() Dim d If True Then d = True ' test Else d = False EndIf End Sub"; var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputcode, out _); using (var state = MockParser.CreateAndParse(vbe.Object)) { var inspection = new BooleanAssignedInIfElseInspection(state); var inspector = InspectionsHelper.GetInspector(inspection); var results = inspector.FindIssuesAsync(state, CancellationToken.None).Result; Assert.AreEqual(1, results.Count()); } }