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 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 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 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 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 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 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);
        }
        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 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 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_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 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 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 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);
        }
Ejemplo n.º 15
0
        public void MultipleUserDefinedTypesOfSameNameOtherModule()
        {
            var    moduleOneName = "ModuleOne";
            string inputCode     =
                $@"
Option Explicit

Public mF|oo As Long
";

            string module2Content =
                $@"
Public Type TModuleOne
    FirstVal As Long
    SecondVal As String
End Type
";

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

            userInput.EncapsulateUsingUDTField();

            var presenterAction = Support.SetParameters(userInput);

            var codeString       = inputCode.ToCodeString();
            var actualModuleCode = RefactoredCode(
                moduleOneName,
                codeString.CaretPosition.ToOneBased(),
                presenterAction,
                null,
                false,
                ("Module2", module2Content, ComponentType.StandardModule),
                (moduleOneName, codeString.Code, ComponentType.StandardModule));

            var actualCode = actualModuleCode[moduleOneName];

            StringAssert.Contains($"Private Type TModuleOne", actualCode);
        }
        public void LoadsExistingUDT()
        {
            var inputCode =
                $@"
Private Type TBar
    First As String
    Second As Long
End Type

Private my|Bar As TBar

Public foo As Long
Public bar As String
Public foobar As Byte
";

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

            userInput.EncapsulateUsingUDTField("myBar");

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

            StringAssert.DoesNotContain($"Private this As {Support.StateUDTDefaultTypeName}", actualCode);
            StringAssert.Contains("Foo As Long", actualCode);
            StringAssert.DoesNotContain("Public foo As Long", actualCode);
            StringAssert.Contains("Bar As String", actualCode);
            StringAssert.DoesNotContain("Public bar As Long", actualCode);
            StringAssert.Contains("Foobar As Byte", actualCode);
            StringAssert.DoesNotContain("Public foobar As Long", actualCode);
            StringAssert.DoesNotContain("MyBar As TBar", actualCode);
            StringAssert.DoesNotContain("Private this As TBar", actualCode);
            StringAssert.Contains("First As String", actualCode);
            StringAssert.Contains("Second As Long", actualCode);
        }
        public void UserDefinedType_MultipleFieldsOfSameUDT()
        {
            var inputCode =
                $@"

Private Type TBar
    First As Long
    Second As String
End Type

Public fooBar As TBar
Public myBar As TBar
";

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

            userInput.EncapsulateUsingUDTField();

            var presenterAction = Support.SetParameters(userInput);
            var model           = Support.RetrieveUserModifiedModelPriorToRefactoring(inputCode, "myBar", DeclarationType.Variable, presenterAction);

            Assert.AreEqual(1, model.ObjectStateUDTCandidates.Count());
        }
        public void StandardModuleSource_ExternalReferences(bool moduleResolve)
        {
            var sourceModuleName    = "SourceModule";
            var referenceExpression = moduleResolve ? $"{sourceModuleName}." : string.Empty;
            var sourceModuleCode    =
                $@"

Public th|is As Long";

            var procedureModuleReferencingCode =
                $@"Option Explicit

Private Const bar As Long = 7

Public Sub Bar()
    {referenceExpression}this = bar
End Sub

Public Sub Foo()
    With {sourceModuleName}
        .this = bar
    End With
End Sub
";

            var classModuleReferencingCode =
                $@"Option Explicit

Private Const bar As Long = 7

Public Sub Bar()
    {referenceExpression}this = bar
End Sub

Public Sub Foo()
    With {sourceModuleName}
        .this = bar
    End With
End Sub
";

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

            userInput.EncapsulateUsingUDTField();

            var presenterAction = Support.SetParameters(userInput);

            var refactoredCode = Support.RefactoredCode(presenterAction,
                                                        (sourceModuleName, sourceModuleCode.ToCodeString(), ComponentType.StandardModule),
                                                        ("StdModule", procedureModuleReferencingCode, ComponentType.StandardModule),
                                                        ("ClassModule", classModuleReferencingCode, ComponentType.ClassModule));

            var referencingModuleCode = refactoredCode["StdModule"];

            StringAssert.Contains($"{sourceModuleName}.MyProperty = ", referencingModuleCode);
            StringAssert.DoesNotContain($"{sourceModuleName}.{sourceModuleName}.MyProperty = ", referencingModuleCode);
            StringAssert.Contains($"  .MyProperty = bar", referencingModuleCode);

            var referencingClassCode = refactoredCode["ClassModule"];

            StringAssert.Contains($"{sourceModuleName}.MyProperty = ", referencingClassCode);
            StringAssert.DoesNotContain($"{sourceModuleName}.{sourceModuleName}.MyProperty = ", referencingClassCode);
            StringAssert.Contains($"  .MyProperty = bar", referencingClassCode);
        }