Esempio n. 1
0
        public void TestCreateMatchMethodsByApplyingRefactoringOnTheSumTypeClassItselfAndExtensionMethodsClassExistsAndIsDecoratedWithAttribute()
        {
            //Arrange
            var methodsClassCode =
                @"[CreateMatchMethods(typeof(SumType))]
public static class SumTypeExtensionMethods
{

}";

            var expectedMethodsClassCodeAfterRefactoring =
                @"[CreateMatchMethods(typeof(SumType))]
public static class SumTypeExtensionMethods
{
    public static TResult Match<TResult>(this SumType instance, System.Func<SumType.Option1, TResult> option1Case, System.Func<SumType.Option2, TResult> option2Case)
    {
        if (instance is SumType.Option1 option1)
            return option1Case(option1);
        if (instance is SumType.Option2 option2)
            return option2Case(option2);
        throw new System.Exception(""Invalid SumType type"");
    }

    public static void Match(this SumType instance, System.Action<SumType.Option1> option1Case, System.Action<SumType.Option2> option2Case)
    {
        if (instance is SumType.Option1 option1)
        {
            option1Case(option1);
            return;
        }

        if (instance is SumType.Option2 option2)
        {
            option2Case(option2);
            return;
        }

        throw new System.Exception(""Invalid SumType type"");
    }
}";
            var content = Utilities.MergeParts(createMatchMethodsAttributeCode, sumTypeClassCode, methodsClassCode);

            var expectedContentAfterRefactoring = Utilities.NormalizeCode(
                Utilities.MergeParts(createMatchMethodsAttributeCode, sumTypeClassCode,
                    expectedMethodsClassCodeAfterRefactoring));

            //Act
            var actualContentAfterRefactoring =
                Utilities.NormalizeCode(
                    Utilities.ApplyRefactoring(
                        content,
                        x => Utilities.SelectSpanWhereClassIsDeclared(x, "SumType"),
                        "Create Match methods"));

            //Assert
            Assert.AreEqual(expectedContentAfterRefactoring, actualContentAfterRefactoring);
        }
Esempio n. 2
0
        public void TestCreateWithMethodsByApplyingRefactoringOnTheProductTypeClassItselfAndExtensionMethodsClassExistsButAttributeIsAppliedOnStaticClass()
        {
            //Arrange

            var methodsClassCode =
                @"[CreateWithMethods(typeof(ProductType))]
public static class ProductTypeExtensionMethods
{

}";

            var expectedMethodsClassCodeAfterRefactoring =
                @"[CreateWithMethods(typeof(ProductType))]
public static class ProductTypeExtensionMethods
{
    public static ProductType WithAge(this ProductType instance, System.Int32 newValue)
    {
        return new ProductType(age: newValue, name: instance.Name);
    }

    public static ProductType WithName(this ProductType instance, System.String newValue)
    {
        return new ProductType(age: instance.Age, name: newValue);
    }
}";
            var content =
                Utilities.MergeParts(
                    createWithMethodsAttributeCode, productTypeClassCode, methodsClassCode);

            var expectedContentAfterRefactoring =
                Utilities.NormalizeCode(
                    Utilities.MergeParts(
                        createWithMethodsAttributeCode,
                        productTypeClassCode,
                        expectedMethodsClassCodeAfterRefactoring));

            //Act
            var actualContentAfterRefactoring =
                Utilities.NormalizeCode(
                    Utilities.ApplyRefactoring(
                        content,
                        x => Utilities.SelectSpanWhereClassIsDeclared(x, "ProductType"),
                        "Create With methods"));

            //Assert
            Assert.AreEqual(expectedContentAfterRefactoring, actualContentAfterRefactoring);
        }