예제 #1
0
        public void AddOrUpdateAttribute_UsualAttribute_AlreadyThere_UpdatesAttribute_Variable()
        {
            const string inputCode =
                @"VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = ""ClassKeys""
Attribute VB_GlobalNameSpace = False

Public MyVariable As Variant
Attribute MyVariable.VB_VarDescription = ""NotMyDesc""

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

Public MyVariable As Variant
Attribute MyVariable.VB_VarDescription = ""MyDesc""

Public Sub Foo(bar As String)
    bar = vbNullString
End Sub
";
            var attribute = "MyVariable.VB_VarDescription";
            var newValues = new List <string> {
                "\"MyDesc\""
            };

            string actualCode;

            var(component, rewriteSession, state) = TestSetup(inputCode);
            using (state)
            {
                var moduleDeclaration = state.DeclarationFinder
                                        .UserDeclarations(DeclarationType.Variable)
                                        .Single(declaration => declaration.IdentifierName.Equals("MyVariable"));
                var attributesUpdater = new AttributesUpdater(state);

                attributesUpdater.AddOrUpdateAttribute(rewriteSession, moduleDeclaration, attribute, newValues);
                rewriteSession.TryRewrite();

                actualCode = component.CodeModule.Content();
            }
            Assert.AreEqual(expectedCode, actualCode);
        }
예제 #2
0
        public void AddOrUpdateAttribute_ExtKey_KeyAlreadyThere_UpdatesAttribute()
        {
            const string inputCode =
                @"VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = ""ClassKeys""
Attribute VB_GlobalNameSpace = False
Attribute VB_Ext_Key = ""AnotherKey"", ""MyValue""
Attribute VB_Ext_Key = ""MyKey"", ""AnotherValue""
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
Attribute VB_Ext_Key = ""AnotherKey"", ""MyValue""
Attribute VB_Ext_Key = ""MyKey"", ""MyValue""
Public Sub Foo(bar As String)
    bar = vbNullString
End Sub
";
            var attribute = "VB_Ext_Key";
            var newValues = new List <string> {
                "\"MyKey\"", "\"MyValue\""
            };

            string actualCode;

            var(component, rewriteSession, state) = TestSetup(inputCode);
            using (state)
            {
                var moduleDeclaration = state.DeclarationFinder
                                        .UserDeclarations(DeclarationType.Module)
                                        .Single();
                var attributesUpdater = new AttributesUpdater(state);

                attributesUpdater.AddOrUpdateAttribute(rewriteSession, moduleDeclaration, attribute, newValues);
                rewriteSession.TryRewrite();

                actualCode = component.CodeModule.Content();
            }
            Assert.AreEqual(expectedCode, actualCode);
        }
예제 #3
0
        public void AddOrUpdateAttribute_UsualAttribute_AlreadyThere_UpdatesAttribute_Member()
        {
            const string inputCode =
                @"VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = ""ClassKeys""
Attribute VB_GlobalNameSpace = False
Public Sub Foo(bar As String)
Attribute Foo.VB_UserMemId = -4
    bar = vbNullString
End Sub
";

            const string expectedCode =
                @"VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = ""ClassKeys""
Attribute VB_GlobalNameSpace = False
Public Sub Foo(bar As String)
Attribute Foo.VB_UserMemId = 0
    bar = vbNullString
End Sub
";
            var attribute = "Foo.VB_UserMemId";
            var newValues = new List <string> {
                "0"
            };

            string actualCode;

            var(component, rewriteSession, state) = TestSetup(inputCode);
            using (state)
            {
                var moduleDeclaration = state.DeclarationFinder
                                        .UserDeclarations(DeclarationType.Procedure)
                                        .Single();
                var attributesUpdater = new AttributesUpdater(state);

                attributesUpdater.AddOrUpdateAttribute(rewriteSession, moduleDeclaration, attribute, newValues);
                rewriteSession.TryRewrite();

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