public void AddAnnotationAddsIdentifierReferenceAnnotationsRightAboveTheReference()
        {
            const string inputCode =
                @"'@PredeclaredId
Option Explicit
'@Folder ""folder""

Private Sub FooBar()
    Dim bar As Variant
    bar = Foo(""x"")
End Sub


'@Obsolete
    Public Sub Foo(bar As String)
        bar = vbNullString
    End Sub
";

            const string expectedCode =
                @"'@PredeclaredId
Option Explicit
'@Folder ""folder""

Private Sub FooBar()
    Dim bar As Variant
    '@Ignore ObsoleteMemberUsage
    bar = Foo(""x"")
End Sub


'@Obsolete
    Public Sub Foo(bar As String)
        bar = vbNullString
    End Sub
";
            var annotationToAdd  = new IgnoreAnnotation();
            var annotationValues = new List <string> {
                "ObsoleteMemberUsage"
            };

            string actualCode;

            var(component, rewriteSession, state) = TestSetup(inputCode);
            using (state)
            {
                var fooReference = state.DeclarationFinder
                                   .UserDeclarations(DeclarationType.Procedure)
                                   .First(decl => decl.IdentifierName == "Foo")
                                   .References.First();
                var annotationUpdater = new AnnotationUpdater();

                annotationUpdater.AddAnnotation(rewriteSession, fooReference, annotationToAdd, annotationValues);
                rewriteSession.TryRewrite();

                actualCode = component.CodeModule.Content();
            }
            Assert.AreEqual(expectedCode, actualCode);
        }
        public void AddAnnotationAddsModuleAnnotationAboveTheFirstLine()
        {
            const string inputCode =
                @"'@PredeclaredId
Option Explicit
'@Folder ""folder""

Private Sub FooBar() 
End Sub


'@Obsolete
    Public Sub Foo(bar As String)
        bar = vbNullString
    End Sub
";

            const string expectedCode =
                @"'@ModuleAttribute VB_Ext_Key, ""Key"", ""Value""
'@PredeclaredId
Option Explicit
'@Folder ""folder""

Private Sub FooBar() 
End Sub


'@Obsolete
    Public Sub Foo(bar As String)
        bar = vbNullString
    End Sub
";
            var annotationToAdd  = new ModuleAttributeAnnotation();
            var annotationValues = new List <string> {
                "VB_Ext_Key", "\"Key\"", "\"Value\""
            };

            string actualCode;

            var(component, rewriteSession, state) = TestSetup(inputCode);
            using (state)
            {
                var moduleDeclaration = state.DeclarationFinder
                                        .UserDeclarations(DeclarationType.ProceduralModule)
                                        .First();
                var annotationUpdater = new AnnotationUpdater();

                annotationUpdater.AddAnnotation(rewriteSession, moduleDeclaration, annotationToAdd, annotationValues);
                rewriteSession.TryRewrite();

                actualCode = component.CodeModule.Content();
            }
            Assert.AreEqual(expectedCode, actualCode);
        }
        public void AddAnnotationAddsMemberAnnotationRightAboveMember()
        {
            const string inputCode =
                @"
Private Sub FooBar() 
End Sub


'@Obsolete
    Public Sub Foo(bar As String)
        bar = vbNullString
    End Sub
";

            const string expectedCode =
                @"
Private Sub FooBar() 
End Sub


'@Obsolete
    '@MemberAttribute VB_Ext_Key, ""Key"", ""Value""
    Public Sub Foo(bar As String)
        bar = vbNullString
    End Sub
";
            var annotationToAdd  = new MemberAttributeAnnotation();
            var annotationValues = new List <string> {
                "VB_Ext_Key", "\"Key\"", "\"Value\""
            };

            string actualCode;

            var(component, rewriteSession, state) = TestSetup(inputCode);
            using (state)
            {
                var fooDeclaration = state.DeclarationFinder
                                     .UserDeclarations(DeclarationType.Procedure)
                                     .First(decl => decl.IdentifierName == "Foo");
                var annotationUpdater = new AnnotationUpdater();

                annotationUpdater.AddAnnotation(rewriteSession, fooDeclaration, annotationToAdd, annotationValues);
                rewriteSession.TryRewrite();

                actualCode = component.CodeModule.Content();
            }
            Assert.AreEqual(expectedCode, actualCode);
        }
        public void AddAnnotationNotOnFirstPhysicalLineOfALogicalLineDoesNothing()
        {
            const string inputCode =
                @"
Private fooBar As Long, _
baz As Variant


'@Obsolete
    Public Sub Foo(bar As String)
        bar = vbNullString
    End Sub
";

            const string expectedCode =
                @"
Private fooBar As Long, _
baz As Variant


'@Obsolete
    Public Sub Foo(bar As String)
        bar = vbNullString
    End Sub
";
            var annotationToAdd = new ObsoleteAnnotation();

            string actualCode;

            var(component, rewriteSession, state) = TestSetup(inputCode);
            using (state)
            {
                var bazDeclaration = state.DeclarationFinder
                                     .UserDeclarations(DeclarationType.Variable)
                                     .First(decl => decl.IdentifierName == "baz");
                var annotationUpdater = new AnnotationUpdater();

                annotationUpdater.AddAnnotation(rewriteSession, bazDeclaration, annotationToAdd);
                rewriteSession.TryRewrite();

                actualCode = component.CodeModule.Content();
            }
            Assert.AreEqual(expectedCode, actualCode);
        }
        public void AddAnnotationOnTheFirstLineIgnoresIndentation()
        {
            const string inputCode =
                @"  Private Sub FooBar() 
  End Sub


'@Obsolete
    Public Sub Foo(bar As String)
        bar = vbNullString
    End Sub
";

            const string expectedCode =
                @"'@Obsolete
  Private Sub FooBar() 
  End Sub


'@Obsolete
    Public Sub Foo(bar As String)
        bar = vbNullString
    End Sub
";
            var annotationToAdd = new ObsoleteAnnotation();

            string actualCode;

            var(component, rewriteSession, state) = TestSetup(inputCode);
            using (state)
            {
                var fooBarDeclaration = state.DeclarationFinder
                                        .UserDeclarations(DeclarationType.Procedure)
                                        .First(decl => decl.IdentifierName == "FooBar");
                var annotationUpdater = new AnnotationUpdater();

                annotationUpdater.AddAnnotation(rewriteSession, fooBarDeclaration, annotationToAdd);
                rewriteSession.TryRewrite();

                actualCode = component.CodeModule.Content();
            }
            Assert.AreEqual(expectedCode, actualCode);
        }
예제 #6
0
        public void AddAnnotationAddsModuleAnnotationBelowTheLastAttributeForAttributeRewriteSession()
        {
            const string inputCode =
                @"VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = ""ClassKeys""
Attribute VB_GlobalNameSpace = False
'@PredeclaredId
Option Explicit
'@Folder ""folder""

Private Sub FooBar() 
End Sub


'@Obsolete
    Public Sub Foo(bar As String)
        bar = vbNullString
    End Sub
";

            const string expectedCode =
                @"VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = ""ClassKeys""
Attribute VB_GlobalNameSpace = False
'@ModuleAttribute VB_Ext_Key, ""Key"", ""Value""
'@PredeclaredId
Option Explicit
'@Folder ""folder""

Private Sub FooBar() 
End Sub


'@Obsolete
    Public Sub Foo(bar As String)
        bar = vbNullString
    End Sub
";
            var annotationToAdd  = new ModuleAttributeAnnotation();
            var annotationValues = new List <string> {
                "VB_Ext_Key", "\"Key\"", "\"Value\""
            };

            string actualCode;

            var(component, rewriteSession, state) = TestSetup(inputCode, ComponentType.ClassModule, CodeKind.AttributesCode);
            using (state)
            {
                var moduleDeclaration = state.DeclarationFinder
                                        .UserDeclarations(DeclarationType.ClassModule)
                                        .First();
                var annotationUpdater = new AnnotationUpdater(state);

                annotationUpdater.AddAnnotation(rewriteSession, moduleDeclaration, annotationToAdd, annotationValues);
                rewriteSession.TryRewrite();

                actualCode = component.CodeModule.Content();
            }
            Assert.AreEqual(expectedCode, actualCode);
        }