public void UserDefinedTypeDefaultNameHasConflict()
        {
            var expectedIdentifier = "TTestModule2";
            var inputCode          =
                $@"

Private Type TBar
    First As Long
    Second As String
End Type

Private Type TTestModule1
    Bar As Long
End Type

Public fo|o As Long
Public myBar As TBar
";

            var userInput = new UserInputDataObject()
                            .UserSelectsField("foo")
                            .UserSelectsField("myBar");

            userInput.EncapsulateUsingUDTField();

            var presenterAction = Support.SetParameters(userInput);
            var actualCode      = Support.RefactoredCode(inputCode.ToCodeString(), presenterAction);

            StringAssert.Contains($"Private Type {expectedIdentifier}", actualCode);
        }
Exemple #2
0
        public void EncapsulateArray(string visibility, string arrayDeclaration, string expectedArrayDeclaration)
        {
            string inputCode =
                $@"Option Explicit

{visibility} {arrayDeclaration}";

            var selection = new Selection(3, 8, 3, 11);

            string expectedCode =
                $@"Option Explicit

Private {expectedArrayDeclaration}

Public Property Get MyArray() As Variant
    MyArray = mArray
End Property
";
            var userInput = new UserInputDataObject()
                            .UserSelectsField("mArray", "MyArray");

            var presenterAction = Support.SetParameters(userInput);
            var actualCode      = RefactoredCode(inputCode, selection, presenterAction);
            var expectedLines   = expectedCode.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            Assert.AreEqual(expectedCode.Trim(), actualCode);
        }
        public void UserModificationIsExistingPropertyNameConflicts(string fizz_modifiedPropertyName, string bazz_modifiedPropertyName, bool fizz_expectedResult, bool bazz_expectedResult)
        {
            var inputCode =
                $@"Public fizz As Integer
Public bazz As Integer
Public buzz As Integer

Private mTest As Integer

Public Property Get Test() As Integer
    Test = mTest
End Property";

            var fieldUT   = "fizz";
            var userInput = new UserInputDataObject()
                            .UserSelectsField(fieldUT, fizz_modifiedPropertyName, true)
                            .UserSelectsField("bazz", bazz_modifiedPropertyName, true);

            var presenterAction = Support.SetParameters(userInput);

            var model = Support.RetrieveUserModifiedModelPriorToRefactoring(inputCode, fieldUT, DeclarationType.Variable, presenterAction);

            Assert.AreEqual(fizz_expectedResult, model["fizz"].TryValidateEncapsulationAttributes(out _), "fizz failed");
            Assert.AreEqual(bazz_expectedResult, model["bazz"].TryValidateEncapsulationAttributes(out _), "bazz failed");
        }
        public void UserDefinedType_MultipleFieldsWithUDT()
        {
            var inputCode =
                $@"

Private Type TBar
    First As Long
    Second As String
End Type

Public fo|o As Long
Public myBar As TBar
";

            var userInput = new UserInputDataObject()
                            .UserSelectsField("myBar");

            userInput.EncapsulateUsingUDTField();

            var presenterAction = Support.SetParameters(userInput);

            var actualCode = Support.RefactoredCode(inputCode.ToCodeString(), presenterAction);

            StringAssert.Contains($"this.MyBar.First = {Support.RHSIdentifier}", actualCode);
            StringAssert.Contains("First = this.MyBar.First", actualCode);
            StringAssert.Contains($"this.MyBar.Second = {Support.RHSIdentifier}", actualCode);
            StringAssert.Contains("Second = this.MyBar.Second", actualCode);
            var index     = actualCode.IndexOf("Get Second", StringComparison.InvariantCultureIgnoreCase);
            var indexLast = actualCode.LastIndexOf("Get Second", StringComparison.InvariantCultureIgnoreCase);

            Assert.AreEqual(index, indexLast);
        }
        public Func <EncapsulateFieldModel, EncapsulateFieldModel> SetParameters(UserInputDataObject userInput)
        {
            return(model =>
            {
                if (userInput.ConvertFieldsToUDTMembers)
                {
                    model.EncapsulateFieldStrategy = EncapsulateFieldStrategy.ConvertFieldsToUDTMembers;
                    var stateUDT = model.ObjectStateUDTCandidates.Where(os => os.IdentifierName == userInput.ObjectStateUDTTargetID)
                                   .Select(sfc => sfc).SingleOrDefault();

                    if (stateUDT != null)
                    {
                        model.ObjectStateUDTField = stateUDT;
                    }
                }
                else
                {
                    model.EncapsulateFieldStrategy = EncapsulateFieldStrategy.UseBackingFields;
                }

                foreach (var testModifiedAttribute in userInput.EncapsulateFieldAttributes)
                {
                    var attrsInitializedByTheRefactoring = model[testModifiedAttribute.TargetFieldName];

                    attrsInitializedByTheRefactoring.EncapsulateFlag = testModifiedAttribute.EncapsulateFlag;
                    attrsInitializedByTheRefactoring.PropertyIdentifier = testModifiedAttribute.PropertyName;
                    attrsInitializedByTheRefactoring.IsReadOnly = testModifiedAttribute.IsReadOnly;
                }
                return model;
            });
        }
        public void UserDefinedTypeMember_ContainsVariant(string accessibility)
        {
            string inputCode =
                $@"
Private Type TBar
    First As String
    Second As Variant
End Type

{accessibility} th|is As TBar";

            var userInput = new UserInputDataObject()
                            .UserSelectsField("this", "MyType");

            var presenterAction = Support.SetParameters(userInput);

            var actualCode = Support.RefactoredCode(inputCode.ToCodeString(), presenterAction);

            StringAssert.Contains("Private this As TBar", actualCode);
            StringAssert.DoesNotContain($"this = {Support.RHSIdentifier}", actualCode);
            StringAssert.DoesNotContain("MyType = this", actualCode);
            StringAssert.Contains($"this.First = {Support.RHSIdentifier}", actualCode);
            StringAssert.Contains($"First = this.First", actualCode);
            StringAssert.Contains($"IsObject", actualCode);
            StringAssert.Contains($"this.Second = {Support.RHSIdentifier}", actualCode);
            StringAssert.Contains($"Second = this.Second", actualCode);
            StringAssert.DoesNotContain($"Second = Second", actualCode);
        }
        public void UserDefinedType_PrivateEnumField()
        {
            var inputCode =
                @"
Private Enum NumberTypes 
    Whole = -1 
    Integral = 0 
    Rational = 1 
End Enum

Public numberT|ype As NumberTypes
";

            var userInput = new UserInputDataObject()
                            .UserSelectsField("numberType");

            userInput.EncapsulateUsingUDTField();

            var presenterAction = Support.SetParameters(userInput);
            var actualCode      = Support.RefactoredCode(inputCode.ToCodeString(), presenterAction);

            StringAssert.Contains("Property Get NumberType() As Long", actualCode);
            StringAssert.Contains("NumberType = this.NumberType", actualCode);
            StringAssert.Contains(" NumberType As NumberTypes", actualCode);
        }
        public void DoesNotChangeExistingUDTMembers()
        {
            var inputCode =
                $@"
Private Type T{MockVbeBuilder.TestModuleName}
    Name As String
End Type

Private this As T{MockVbeBuilder.TestModuleName}

Public foo As Long
Public bar As String
Public foo|bar As Byte

Public Property Let Name(value As String)
    this.Name = value
End Property

Public Property Get Name() As String
    Name = this.Name
End Property
";

            var userInput = new UserInputDataObject()
                            .UserSelectsField("foobar");

            userInput.EncapsulateUsingUDTField($"T{MockVbeBuilder.TestModuleName}");

            var presenterAction = Support.SetParameters(userInput);
            var actualCode      = Support.RefactoredCode(inputCode.ToCodeString(), presenterAction);

            StringAssert.DoesNotContain($"Name_1 As String", actualCode);
            StringAssert.DoesNotContain($"ThisName As String", actualCode);
        }
        public void MultipleFields()
        {
            var inputCode =
                $@"
Public fo|o As Long
Public bar As String
Public foobar As Byte
";

            var userInput = new UserInputDataObject()
                            .UserSelectsField("foo")
                            .UserSelectsField("bar")
                            .UserSelectsField("foobar");

            userInput.EncapsulateUsingUDTField();

            var presenterAction = Support.SetParameters(userInput);
            var actualCode      = Support.RefactoredCode(inputCode.ToCodeString(), presenterAction);

            StringAssert.Contains($"Private this As {Support.StateUDTDefaultTypeName}", actualCode);
            StringAssert.Contains($"Private Type {Support.StateUDTDefaultTypeName}", actualCode);
            StringAssert.Contains("Foo As Long", actualCode);
            StringAssert.Contains("Bar As String", actualCode);
            StringAssert.Contains("Foobar As Byte", actualCode);
        }
        public void AddedUDTMemberConflictsWithExistingName()
        {
            var fieldUT   = "mFirstValue";
            var inputCode =
                $@"

Private Type MyType
    FirstValue As Integer
    SecondValue As Integer
End Type

Private {fieldUT} As Double

Private myType As MyType
";
            var userInput = new UserInputDataObject()
                            .UserSelectsField(fieldUT);

            userInput.EncapsulateUsingUDTField("myType");

            var presenterAction = Support.SetParameters(userInput);

            var model = Support.RetrieveUserModifiedModelPriorToRefactoring(inputCode, fieldUT, DeclarationType.Variable, presenterAction);

            Assert.AreEqual(false, model[fieldUT].TryValidateEncapsulationAttributes(out var errorMessage), errorMessage);
        }
        public void UserClearsConflictingNameByEncapsulatingConflictingVariable()
        {
            var inputCode =
                @"

Private Type TVehicle
    Wheels As Integer
    MPG As Double
End Type

Private mVehicle As TVehicle

Private seats As Integer

Private foo As String
";
            //By encapsulating variable mVehicle, the variable disappears and
            //is converted to UDTMember name "Vehicle" and "Vehicle" properties
            // - thus removing the conflict created by the user editing the "seats" property
            var userInput = new UserInputDataObject()
                            .UserSelectsField("seats", "MVehicle")
                            .UserSelectsField("mVehicle");

            userInput.ConvertFieldsToUDTMembers = true;

            var presenterAction = Support.SetParameters(userInput);

            var model = Support.RetrieveUserModifiedModelPriorToRefactoring(inputCode, "seats", DeclarationType.Variable, presenterAction);

            Assert.AreEqual(true, model["seats"].TryValidateEncapsulationAttributes(out var errorMessage), errorMessage);
        }
        public void UserEntersUDTMemberPropertyNameInConflictWithExistingField()
        {
            var inputCode =
                @"

Private Type TVehicle
    Wheels As Integer
    MPG As Double
End Type

Private vehicle As TVehicle

Private seats As Integer

Private foo As String
";
            var userInput = new UserInputDataObject()
                            .UserSelectsField("seats", "Foo");

            userInput.ConvertFieldsToUDTMembers = true;
            userInput.ObjectStateUDTTargetID    = "TVehicle";

            var presenterAction = Support.SetParameters(userInput);

            var model = Support.RetrieveUserModifiedModelPriorToRefactoring(inputCode, "seats", DeclarationType.Variable, presenterAction);

            Assert.AreEqual(false, model["seats"].TryValidateEncapsulationAttributes(out var errorMessage), errorMessage);
        }
        public void ExistingUDTConflictWithDefaultObjectStateType(string accessibility)
        {
            var moduleOneName = "ModuleOne";
            var inputCode     =
                $@"
Option Explicit

{accessibility} Type TModuleOne
    FirstVal As Long
    SecondVal As String
End Type

Public mF|oo As Long
";

            var fieldUT   = "mFoo";
            var userInput = new UserInputDataObject()
                            .UserSelectsField(fieldUT);

            userInput.EncapsulateUsingUDTField();

            var presenterAction = Support.SetParameters(userInput);

            var actualModuleCode = Support.RefactoredCode(presenterAction,
                                                          (moduleOneName, inputCode.ToCodeString(), ComponentType.StandardModule));

            StringAssert.Contains($"Private Type TModuleOne1", actualModuleCode[moduleOneName]);
        }
        public void UserDefinedType_LocallyReferencedArray(string arrayIdentifier, string dimensions)
        {
            var selectedInput = arrayIdentifier.Replace("n", "n|");
            var inputCode     =
                $@"
Public {selectedInput}({dimensions}) As String

Public Property Get AnArrayTest() As Variant
    AnArrayTest = anArray
End Property

";

            var userInput = new UserInputDataObject()
                            .UserSelectsField(arrayIdentifier);

            userInput.EncapsulateUsingUDTField();

            var presenterAction = Support.SetParameters(userInput);
            var actualCode      = Support.RefactoredCode(inputCode.ToCodeString(), presenterAction);

            StringAssert.Contains("Property Get AnArray() As Variant", actualCode);
            StringAssert.Contains("AnArray = this.AnArray", actualCode);
            StringAssert.Contains("AnArrayTest = this.AnArray", actualCode);
            StringAssert.Contains($" AnArray({dimensions}) As String", actualCode);
        }
        public void ObjectStateUDTs(string udtFieldAccessibility, int expectedCount)
        {
            var inputCode =
                $@"
Private Type TBar
    First As String
    Second As Long
End Type

Public mFoo As String
Public mBar As Long
Private mFizz

{udtFieldAccessibility} myBar As TBar";

            var userInput = new UserInputDataObject()
                            .UserSelectsField("mFizz");

            userInput.EncapsulateUsingUDTField();

            var presenterAction = Support.SetParameters(userInput);

            var model = Support.RetrieveUserModifiedModelPriorToRefactoring(inputCode, "mFizz", DeclarationType.Variable, presenterAction);
            var test  = model.ObjectStateUDTCandidates;

            Assert.AreEqual(expectedCount, model.ObjectStateUDTCandidates.Count());
        }
        public void UserDefinedTypeMembers_OnlyEncapsulateUDTMembers()
        {
            var inputCode =
                $@"
Private Type TBar
    First As String
    Second As Long
End Type

Private my|Bar As TBar";

            var userInput = new UserInputDataObject()
                            .UserSelectsField("myBar");

            userInput.EncapsulateUsingUDTField();

            var presenterAction = Support.SetParameters(userInput);

            var actualCode = Support.RefactoredCode(inputCode.ToCodeString(), presenterAction);

            StringAssert.Contains($"this.MyBar.First = {Support.RHSIdentifier}", actualCode);
            StringAssert.Contains($"First = this.MyBar.First", actualCode);
            StringAssert.Contains($"this.MyBar.Second = {Support.RHSIdentifier}", actualCode);
            StringAssert.Contains($"Second = this.MyBar.Second", actualCode);
            StringAssert.Contains($"MyBar As TBar", actualCode);
            StringAssert.Contains($"MyBar As TBar", actualCode);
        }
        public void UserDefinedTypeMembers_UDTFieldReferences()
        {
            var inputCode =
                $@"
Private Type TBar
    First As String
    Second As Long
End Type

Private my|Bar As TBar

Public Sub Foo(newValue As String)
    myBar.First = newValue
End Sub";

            var userInput = new UserInputDataObject()
                            .UserSelectsField("myBar");

            userInput.EncapsulateUsingUDTField();

            var presenterAction = Support.SetParameters(userInput);

            var actualCode = Support.RefactoredCode(inputCode.ToCodeString(), presenterAction);

            StringAssert.Contains("  First = newValue", actualCode);
        }
        public void UserDefinedTypeMember_ContainsArrays(string accessibility)
        {
            string inputCode =
                $@"
Private Type TBar
    First(5) As String
    Second(1 to 100) As Long
    Third() As Double
End Type

{accessibility} t|his As TBar";

            var userInput = new UserInputDataObject()
                            .UserSelectsField("this", "MyType");

            var presenterAction = Support.SetParameters(userInput);

            var actualCode = Support.RefactoredCode(inputCode.ToCodeString(), presenterAction);

            StringAssert.Contains($"Private this As TBar", actualCode);
            StringAssert.DoesNotContain($"this = {Support.RHSIdentifier}", actualCode);
            StringAssert.DoesNotContain($"this.First = {Support.RHSIdentifier}", actualCode);
            StringAssert.Contains($"Property Get First() As Variant", actualCode);
            StringAssert.Contains($"First = this.First", actualCode);
            StringAssert.DoesNotContain($"this.Second = {Support.RHSIdentifier}", actualCode);
            StringAssert.Contains($"Second = this.Second", actualCode);
            StringAssert.Contains($"Property Get Second() As Variant", actualCode);
            StringAssert.Contains($"Third = this.Third", actualCode);
            StringAssert.Contains($"Property Get Third() As Variant", actualCode);
        }
        public void GivenReferencedPublicField_UpdatesReferenceToNewProperty()
        {
            var codeClass1 =
                @"|Public fizz As Integer

Sub Foo()
    fizz = 1
End Sub";
            var codeClass2 =
                @"Sub Foo()
    Dim theClass As Class1
    theClass.fizz = 0
    Bar theClass.fizz
End Sub

Sub Bar(ByVal v As Integer)
End Sub";

            var userInput = new UserInputDataObject()
                            .AddUserInputSet("fizz", "Name");

            userInput.EncapsulateUsingUDTField();

            var presenterAction = Support.SetParameters(userInput);

            var refactoredCode = Support.RefactoredCode(presenterAction,
                                                        ("Class1", codeClass1.ToCodeString(), ComponentType.ClassModule),
                                                        ("Class2", codeClass2, ComponentType.ClassModule));

            StringAssert.Contains("Name = 1", refactoredCode["Class1"]);
            StringAssert.Contains("theClass.Name = 0", refactoredCode["Class2"]);
            StringAssert.Contains("Bar theClass.Name", refactoredCode["Class2"]);
            StringAssert.DoesNotContain("fizz", refactoredCode["Class2"]);
        }
        public void UDTMemberIsPublicUDT()
        {
            var inputCode =
                $@"

Public Type TFoo
    Foo As Integer
    Bar As Byte
End Type

Private Type TBar
    FooBar As TFoo
End Type

Private my|Bar As TBar
";

            var userInput = new UserInputDataObject();

            userInput.EncapsulateUsingUDTField();

            var presenterAction = Support.SetParameters(userInput);

            var actualCode = Support.RefactoredCode(inputCode.ToCodeString(), presenterAction);

            StringAssert.Contains("Public Property Let FooBar(", actualCode);
            StringAssert.Contains($"this.MyBar.FooBar = {Support.RHSIdentifier}", actualCode);
        }
        public void UserEntersConflictingName(string userModifiedPropertyName)
        {
            var inputCode =
                $@"Public fizz As String

            Private mName As String

            Public Property Get Name() As String
                Name = mName
            End Property

            Public Property Let Name(ByVal value As String)
                mName = value
            End Property
            ";

            var fieldUT   = "fizz";
            var userInput = new UserInputDataObject()
                            .UserSelectsField("fizz", userModifiedPropertyName);

            var presenterAction = Support.SetParameters(userInput);

            var model = Support.RetrieveUserModifiedModelPriorToRefactoring(inputCode, fieldUT, DeclarationType.Variable, presenterAction);

            Assert.IsFalse(model["fizz"].TryValidateEncapsulationAttributes(out _));
        }
        public void ClassModuleUDTFieldSelection_ExternalReferences_ClassModule()
        {
            var sourceModuleName = "SourceModule";
            var sourceClassName  = "theClass";
            var sourceModuleCode =
                $@"

Public th|is As TBar";


            string classModuleReferencingCode =
                $@"Option Explicit

Private {sourceClassName} As {sourceModuleName}
Private Const foo As String = ""Foo""
Private Const bar As Long = 7

Private Sub Class_Initialize()
    Set {sourceClassName} = New {sourceModuleName}
End Sub

Public Sub Foo()
    {sourceClassName}.this.First = foo
End Sub

Public Sub Bar()
    {sourceClassName}.this.Second = bar
End Sub

Public Sub FooBar()
    With {sourceClassName}
        .this.First = foo
        .this.Second = bar
    End With
End Sub
";

            var userInput = new UserInputDataObject()
                            .UserSelectsField("this", "MyType");

            var presenterAction = Support.SetParameters(userInput);

            var sourceCodeString = sourceModuleCode.ToCodeString();

            var actualModuleCode = RefactoredCode(
                sourceModuleName,
                sourceCodeString.CaretPosition.ToOneBased(),
                presenterAction,
                null,
                false,
                ("ClassModule", classModuleReferencingCode, ComponentType.ClassModule),
                (sourceModuleName, sourceCodeString.Code, ComponentType.ClassModule));

            var referencingClassCode = actualModuleCode["ClassModule"];

            StringAssert.Contains($"{sourceClassName}.MyType.First = ", referencingClassCode);
            StringAssert.Contains($"{sourceClassName}.MyType.Second = ", referencingClassCode);
            StringAssert.Contains($"  .MyType.Second = ", referencingClassCode);
        }
        public void StdModuleSource_PublicUDTField_PublicType_StdModuleReference(string sourceModuleName, string referenceQualifier)
        {
            var sourceModuleCode =
                $@"
Public Type TBar
    First As String
    Second As Long
End Type

Public th|is As TBar";

            var moduleReferencingCode =
                $@"Option Explicit

'StdModule referencing the UDT

Private Const fooConst As String = ""Foo""

Private Const barConst As Long = 7

Public Sub Foo()
    {referenceQualifier}.First = fooConst
End Sub

Public Sub Bar()
    {referenceQualifier}.Second = barConst
End Sub

Public Sub FooBar()
    With {sourceModuleName}
        .this.First = fooConst
        .this.Second = barConst
    End With
End Sub
";
            var userInput = new UserInputDataObject()
                            .UserSelectsField("this", "MyType");

            var presenterAction = Support.SetParameters(userInput);

            var sourceCodeString = sourceModuleCode.ToCodeString();

            var actualModuleCode = RefactoredCode(
                sourceModuleName,
                sourceCodeString.CaretPosition.ToOneBased(),
                presenterAction,
                null,
                false,
                ("StdModule", moduleReferencingCode, ComponentType.StandardModule),
                (sourceModuleName, sourceCodeString.Code, ComponentType.StandardModule));

            var referencingModuleCode = actualModuleCode["StdModule"];

            StringAssert.Contains($"{sourceModuleName}.MyType.First = ", referencingModuleCode);
            StringAssert.Contains($"{sourceModuleName}.MyType.Second = ", referencingModuleCode);
            StringAssert.Contains($"  .MyType.Second = ", referencingModuleCode);
        }
Exemple #24
0
        public void ClassModuleUDTFieldSelection_ExternalReferences_StdModule()
        {
            var classModuleName   = "SourceModule";
            var classInstanceName = "theClass";
            var sourceModuleCode  =
                $@"

Public th|is As TBar";

            var procedureModuleReferencingCode =
                $@"Option Explicit

Public Type TBar
    First As String
    Second As Long
End Type

Private {classInstanceName} As {classModuleName}
Private Const foo As String = ""Foo""
Private Const bar As Long = 7

Public Sub Initialize()
    Set {classInstanceName} = New {classModuleName}
End Sub

Public Sub Foo()
    {classInstanceName}.this.First = foo
End Sub

Public Sub Bar()
    {classInstanceName}.this.Second = bar
End Sub

Public Sub FooBar()
    With {classInstanceName}
        .this.First = foo
        .this.Second = bar
    End With
End Sub
";

            var userInput = new UserInputDataObject()
                            .UserSelectsField("this", "MyType");

            var presenterAction = Support.SetParameters(userInput);

            var actualModuleCode = Support.RefactoredCode(presenterAction,
                                                          (classModuleName, sourceModuleCode.ToCodeString(), ComponentType.ClassModule),
                                                          ("StdModule", procedureModuleReferencingCode, ComponentType.StandardModule));

            var referencingModuleCode = actualModuleCode["StdModule"];

            StringAssert.Contains($"{classInstanceName}.MyType.First = ", referencingModuleCode);
            StringAssert.Contains($"{classInstanceName}.MyType.Second = ", referencingModuleCode);
            StringAssert.Contains($"  .MyType.Second = ", referencingModuleCode);
        }
Exemple #25
0
        public void EncapsulateMultipleFieldsInList(
            string var1, bool var1Flag,
            string var2, bool var2Flag,
            string var3, bool var3Flag,
            string var4, bool var4Flag)
        {
            string inputCode =
                $@"Public {var1} As Integer, {var2} As Integer, {var3} As Integer, {var4} As Integer";

            var selection = new Selection(1, 9);

            var userInput = new UserInputDataObject()
                            .AddUserInputSet(var1, $"{var1}Prop", var1Flag)
                            .AddUserInputSet(var2, $"{var2}Prop", var2Flag)
                            .AddUserInputSet(var3, $"{var3}Prop", var3Flag)
                            .AddUserInputSet(var4, $"{var4}Prop", var4Flag);

            var flags = new Dictionary <string, bool>()
            {
                [var1] = var1Flag,
                [var2] = var2Flag,
                [var3] = var3Flag,
                [var4] = var4Flag
            };

            var presenterAction = Support.SetParameters(userInput);

            var actualCode = RefactoredCode(inputCode, selection, presenterAction);

            var remainInList = flags.Keys.Where(k => !flags[k])
                               .Select(k => $"{k} As Integer");

            if (remainInList.Any())
            {
                var declarationList = $"Public {string.Join(", ", remainInList)}";
                StringAssert.Contains(declarationList, actualCode);
            }
            else
            {
                StringAssert.DoesNotContain($"Public {Environment.NewLine}", actualCode);
            }

            foreach (var key in flags.Keys)
            {
                if (flags[key])
                {
                    StringAssert.Contains($"Private {key} As", actualCode);
                    StringAssert.Contains($"{key}Prop = {key}", actualCode);
                    StringAssert.Contains($"{key} = {Support.RHSIdentifier}", actualCode);
                    StringAssert.Contains($"Let {key}Prop(ByVal {Support.RHSIdentifier} As", actualCode);
                    StringAssert.Contains($"Property Get {key}Prop()", actualCode);
                }
            }
        }
        public void UserDefinedTypeMembers_ExternallyDefinedType(string accessibility)
        {
            string inputCode =
                $@"
Option Explicit

{accessibility} th|is As TBar";

            string typeDefinition =
                $@"
Public Type TBar
    First As String
    Second As Long
End Type
";

            var userInput = new UserInputDataObject()
                            .UserSelectsField("this", "MyType");

            var presenterAction = Support.SetParameters(userInput);

            var codeString       = inputCode.ToCodeString();
            var actualModuleCode = RefactoredCode(
                "Class1",
                codeString.CaretPosition.ToOneBased(),
                presenterAction,
                null,
                false,
                ("Class1", codeString.Code, ComponentType.ClassModule),
                ("Module1", typeDefinition, ComponentType.StandardModule));

            Assert.AreEqual(typeDefinition, actualModuleCode["Module1"]);


            var actualCode = actualModuleCode["Class1"];

            StringAssert.Contains("Private this As TBar", actualCode);

            if (accessibility == "Public")
            {
                StringAssert.Contains($"this = {Support.RHSIdentifier}", actualCode);
                StringAssert.Contains("MyType = this", actualCode);
                StringAssert.Contains($"Public Property Get MyType", actualCode);
                Assert.AreEqual(actualCode.IndexOf("MyType = this"), actualCode.LastIndexOf("MyType = this"));
                StringAssert.DoesNotContain($"this.First = {Support.RHSIdentifier}", actualCode);
                StringAssert.DoesNotContain($"this.Second = {Support.RHSIdentifier}", actualCode);
                StringAssert.DoesNotContain($"Second = Second", actualCode);
                StringAssert.Contains($"Public Property Let MyType(ByRef {Support.RHSIdentifier} As TBar", actualCode);
            }
        }
Exemple #27
0
        public void EncapsulateMultipleFields(
            string var1, bool var1Flag,
            string var2, bool var2Flag,
            string var3, bool var3Flag)
        {
            string inputCode =
                $@"Public {var1} As Integer
Public {var2} As Integer
Public {var3} As Integer";

            var selection = new Selection(1, 1);

            var userInput = new UserInputDataObject()
                            .AddUserInputSet(var1, $"{var1}Prop", var1Flag)
                            .AddUserInputSet(var2, $"{var2}Prop", var2Flag)
                            .AddUserInputSet(var3, $"{var3}Prop", var3Flag);

            var flags = new Dictionary <string, bool>()
            {
                [var1] = var1Flag,
                [var2] = var2Flag,
                [var3] = var3Flag
            };

            var presenterAction = Support.SetParameters(userInput);

            var actualCode = RefactoredCode(inputCode, selection, presenterAction);

            var notEncapsulated = flags.Keys.Where(k => !flags[k])
                                  .Select(k => k);

            var encapsulated = flags.Keys.Where(k => flags[k])
                               .Select(k => k);

            foreach (var variable in notEncapsulated)
            {
                StringAssert.Contains($"Public {variable} As Integer", actualCode);
            }

            foreach (var variable in encapsulated)
            {
                StringAssert.Contains($"Private {variable} As", actualCode);
                StringAssert.Contains($"{variable}Prop = {variable}", actualCode);
                StringAssert.Contains($"{variable} = {Support.RHSIdentifier}", actualCode);
                StringAssert.Contains($"Let {variable}Prop(ByVal {Support.RHSIdentifier} As", actualCode);
                StringAssert.Contains($"Property Get {variable}Prop()", actualCode);
            }
        }
        public void UserDefinedTypeMember_ContainsObjects(string accessibility)
        {
            string inputCode =
                $@"
Private Type TBar
    First As Class1
    Second As Long
End Type

{accessibility} th|is As TBar";

            string class1Code =
                @"Option Explicit

Public Sub Foo()
End Sub
";

            var userInput = new UserInputDataObject()
                            .UserSelectsField("this", "MyType");

            var presenterAction = Support.SetParameters(userInput);

            var codeString       = inputCode.ToCodeString();
            var actualModuleCode = RefactoredCode(
                "Module1",
                codeString.CaretPosition.ToOneBased(),
                presenterAction,
                null,
                false,
                ("Class1", class1Code, ComponentType.ClassModule),
                ("Module1", codeString.Code, ComponentType.StandardModule));

            var actualCode = actualModuleCode["Module1"];

            StringAssert.Contains("Private this As TBar", actualCode);
            StringAssert.DoesNotContain($"this = {Support.RHSIdentifier}", actualCode);
            StringAssert.DoesNotContain("MyType = this", actualCode);
            StringAssert.Contains($"Property Set First(ByVal {Support.RHSIdentifier} As Class1)", actualCode);
            StringAssert.Contains("Property Get First() As Class1", actualCode);
            StringAssert.Contains($"Set this.First = {Support.RHSIdentifier}", actualCode);
            StringAssert.Contains($"Set First = this.First", actualCode);
            StringAssert.Contains($"this.Second = {Support.RHSIdentifier}", actualCode);
            StringAssert.Contains($"Second = this.Second", actualCode);
            StringAssert.DoesNotContain($"Second = Second", actualCode);
            Assert.AreEqual(actualCode.IndexOf($"this.First = {Support.RHSIdentifier}"), actualCode.LastIndexOf($"this.First = {Support.RHSIdentifier}"));
        }
        public void ModuleAndProjectNamesAreValid(string userEnteredName)
        {
            var fieldUT   = "foo";
            var userInput = new UserInputDataObject()
                            .UserSelectsField(fieldUT, userEnteredName, true);

            var presenterAction = Support.SetParameters(userInput);

            var vbe = MockVbeBuilder.BuildFromModules(
                (MockVbeBuilder.TestModuleName, "Private foo As String", ComponentType.StandardModule),
                ("TestModule", "Private foo1 As String", ComponentType.StandardModule),
                ("TestClass", "Private foo2 As String", ComponentType.ClassModule)).Object;

            var model = Support.RetrieveUserModifiedModelPriorToRefactoring(vbe, fieldUT, DeclarationType.Variable, presenterAction);

            Assert.AreEqual(true, model[fieldUT].TryValidateEncapsulationAttributes(out _));
        }
        public void PrivateUDT_SelectedOtherThanObjectStateUDT()
        {
            var inputCode =
                $@"

Private Type TTest
    TestValue As String
    TestNumber As Long
End Type

Private Type TTestModule1
    SomeValue As Long
End Type

Private mTest As TTest

Private this As TTestModule1

Private the|Target As Variant

Public Property Get SomeValue() As Long
    SomeValue = this.SomeValue
End Property

Public Property Let SomeValue(ByVal value As Long)
    this.SomeValue = value
End Property

Public Sub Foo(arg As Long)
    SomeValue = arg * 4
End Sub
";

            var userInput = new UserInputDataObject()
                            .UserSelectsField("theTarget");

            userInput.EncapsulateUsingUDTField("mTest");

            var presenterAction = Support.SetParameters(userInput);

            var actualCode = Support.RefactoredCode(inputCode.ToCodeString(), presenterAction);

            StringAssert.DoesNotContain("TheTarget = this.TheTarget", actualCode);
            StringAssert.Contains("TheTarget = mTest.TheTarget", actualCode);
            StringAssert.Contains("TheTarget As Variant", actualCode);
        }