public void TestCreateMatchMethodsForClosedGenericFSharpDiscriminatedUnion() { //Arrange var methodsClassCode = @"[CreateMatchMethods(typeof(DataObjectHelper.Tests.FSharpProject.Module.GenericFSharpDiscriminatedUnion<System.String>))] public static class Methods { }"; var expectedMethodsClassCodeAfterRefactoring = @"[CreateMatchMethods(typeof (DataObjectHelper.Tests.FSharpProject.Module.GenericFSharpDiscriminatedUnion<System.String>))] public static class Methods { public static TResult Match<TResult>( this DataObjectHelper.Tests.FSharpProject.Module.GenericFSharpDiscriminatedUnion<System.String> instance, System.Func<DataObjectHelper.Tests.FSharpProject.Module.GenericFSharpDiscriminatedUnion<System.String>.Option1, TResult> option1Case, System.Func<TResult> option2Case) { if (instance is DataObjectHelper.Tests.FSharpProject.Module.GenericFSharpDiscriminatedUnion<System.String>.Option1 option1) return option1Case(option1); if (instance.IsOption2) return option2Case(); throw new System.Exception(""Invalid GenericFSharpDiscriminatedUnion type""); } public static void Match( this DataObjectHelper.Tests.FSharpProject.Module.GenericFSharpDiscriminatedUnion<System.String> instance, System.Action<DataObjectHelper.Tests.FSharpProject.Module.GenericFSharpDiscriminatedUnion<System.String>.Option1> option1Case, System.Action option2Case) { if (instance is DataObjectHelper.Tests.FSharpProject.Module.GenericFSharpDiscriminatedUnion<System.String>.Option1 option1) { option1Case(option1); return; } if (instance.IsOption2) { option2Case(); return; } throw new System.Exception(""Invalid GenericFSharpDiscriminatedUnion type""); } }"; var content = Utilities.MergeParts(createMatchMethodsAttributeCode, methodsClassCode); var expectedContentAfterRefactoring = Utilities.NormalizeCode( Utilities.MergeParts(createMatchMethodsAttributeCode, expectedMethodsClassCodeAfterRefactoring)); //Act var actualContentAfterRefactoring = Utilities.NormalizeCode(Utilities.ApplyRefactoring( content, SelectSpanWhereCreateMatchMethodsAttributeIsApplied, Utilities.GetFsharpTestProjectReference())); //Assert Assert.AreEqual(expectedContentAfterRefactoring, actualContentAfterRefactoring); }
public void TestCreateMatchMethodsWhereClassIsInsideANamespace() { //Arrange var methodsClassCode = @"[CreateMatchMethods(typeof(SumType))] public static class Methods { }"; var expectedMethodsClassCodeAfterRefactoring = @"[CreateMatchMethods(typeof(SumType))] public static class Methods { public static TResult Match<TResult>(this Namespace1.SumType instance, System.Func<Namespace1.SumType.Option1, TResult> option1Case, System.Func<Namespace1.SumType.Option2, TResult> option2Case) { if (instance is Namespace1.SumType.Option1 option1) return option1Case(option1); if (instance is Namespace1.SumType.Option2 option2) return option2Case(option2); throw new System.Exception(""Invalid SumType type""); } public static void Match(this Namespace1.SumType instance, System.Action<Namespace1.SumType.Option1> option1Case, System.Action<Namespace1.SumType.Option2> option2Case) { if (instance is Namespace1.SumType.Option1 option1) { option1Case(option1); return; } if (instance is Namespace1.SumType.Option2 option2) { option2Case(option2); return; } throw new System.Exception(""Invalid SumType type""); } }"; var content = Utilities.InNamespace( Utilities.MergeParts(createMatchMethodsAttributeCode, sumTypeClassCode, methodsClassCode), "Namespace1"); var expectedContentAfterRefactoring = Utilities.NormalizeCode(Utilities.InNamespace( Utilities.MergeParts(createMatchMethodsAttributeCode, sumTypeClassCode, expectedMethodsClassCodeAfterRefactoring), "Namespace1")); //Act var actualContentAfterRefactoring = Utilities.NormalizeCode(Utilities.ApplyRefactoring(content, SelectSpanWhereCreateMatchMethodsAttributeIsApplied)); //Assert Assert.AreEqual(expectedContentAfterRefactoring, actualContentAfterRefactoring); }
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); }
public void TestCreateMatchMethodsForClosedGenericClassWhoseSubclassesAreInOuterScope() { //Arrange var methodsClassCode = @"[CreateMatchMethods(typeof(GenericSumType<System.String>))] public static class Methods { }"; var expectedMethodsClassCodeAfterRefactoring = @"[CreateMatchMethods(typeof(GenericSumType<System.String>))] public static class Methods { public static TResult Match<TResult>(this GenericSumType<System.String> instance, System.Func<Option1<System.String>, TResult> option1Case, System.Func<Option2<System.String>, TResult> option2Case) { if (instance is Option1<System.String> option1) return option1Case(option1); if (instance is Option2<System.String> option2) return option2Case(option2); throw new System.Exception(""Invalid GenericSumType type""); } public static void Match(this GenericSumType<System.String> instance, System.Action<Option1<System.String>> option1Case, System.Action<Option2<System.String>> option2Case) { if (instance is Option1<System.String> option1) { option1Case(option1); return; } if (instance is Option2<System.String> option2) { option2Case(option2); return; } throw new System.Exception(""Invalid GenericSumType type""); } }"; var content = Utilities.MergeParts(createMatchMethodsAttributeCode, genericSumTypeClassWithSubClassesInOuterScopeCode, methodsClassCode); var expectedContentAfterRefactoring = Utilities.NormalizeCode(Utilities.MergeParts( createMatchMethodsAttributeCode, genericSumTypeClassWithSubClassesInOuterScopeCode, expectedMethodsClassCodeAfterRefactoring)); //Act var actualContentAfterRefactoring = Utilities.NormalizeCode(Utilities.ApplyRefactoring(content, SelectSpanWhereCreateMatchMethodsAttributeIsApplied)); //Assert Assert.AreEqual(expectedContentAfterRefactoring, actualContentAfterRefactoring); }
public void TestCreateWithMethodsWhereClassIsInsideANamespace() { //Arrange var methodsClassCode = @"[CreateWithMethods(typeof(ProductType))] public static class Methods { }"; var expectedMethodsClassCodeAfterRefactoring = @"[CreateWithMethods(typeof(ProductType))] public static class Methods { public static Namespace1.ProductType WithAge(this Namespace1.ProductType instance, System.Int32 newValue) { return new Namespace1.ProductType(age: newValue, name: instance.Name); } public static Namespace1.ProductType WithName(this Namespace1.ProductType instance, System.String newValue) { return new Namespace1.ProductType(age: instance.Age, name: newValue); } }"; var content = Utilities.InNamespace( Utilities.MergeParts( createWithMethodsAttributeCode, productTypeClassCode, methodsClassCode), "Namespace1"); var expectedContentAfterRefactoring = Utilities.NormalizeCode( Utilities.InNamespace( Utilities.MergeParts( createWithMethodsAttributeCode, productTypeClassCode, expectedMethodsClassCodeAfterRefactoring), "Namespace1")); //Act var actualContentAfterRefactoring = Utilities.NormalizeCode( Utilities.ApplyRefactoring( content, SelectSpanWhereCreateWithMethodsAttributeIsApplied)); //Assert Assert.AreEqual(expectedContentAfterRefactoring, actualContentAfterRefactoring); }
public void TestCreateWithMethodsForFSharpDiscriminatedUnionGeneratesMethodsForCases() { //Arrange var methodsClassCode = @"[CreateWithMethods(typeof(DataObjectHelper.Tests.FSharpProject.Module.FSharpDiscriminatedUnion))] public static class Methods { }"; var expectedMethodsClassCodeAfterRefactoring = @"[CreateWithMethods(typeof(DataObjectHelper.Tests.FSharpProject.Module.FSharpDiscriminatedUnion))] public static class Methods { public static DataObjectHelper.Tests.FSharpProject.Module.FSharpDiscriminatedUnion.Option1 WithAge( this DataObjectHelper.Tests.FSharpProject.Module.FSharpDiscriminatedUnion.Option1 instance, System.Int32 newValue) { return (DataObjectHelper.Tests.FSharpProject.Module.FSharpDiscriminatedUnion.Option1)DataObjectHelper.Tests.FSharpProject.Module.FSharpDiscriminatedUnion.NewOption1(age: newValue, name: instance.Name); } public static DataObjectHelper.Tests.FSharpProject.Module.FSharpDiscriminatedUnion.Option1 WithName( this DataObjectHelper.Tests.FSharpProject.Module.FSharpDiscriminatedUnion.Option1 instance, System.String newValue) { return (DataObjectHelper.Tests.FSharpProject.Module.FSharpDiscriminatedUnion.Option1)DataObjectHelper.Tests.FSharpProject.Module.FSharpDiscriminatedUnion.NewOption1(age: instance.Age, name: newValue); } }"; var content = Utilities.MergeParts( createWithMethodsAttributeCode, methodsClassCode); var expectedContentAfterRefactoring = Utilities.NormalizeCode( Utilities.MergeParts( createWithMethodsAttributeCode, expectedMethodsClassCodeAfterRefactoring)); //Act var actualContentAfterRefactoring = Utilities.NormalizeCode( Utilities.ApplyRefactoring( content, SelectSpanWhereCreateWithMethodsAttributeIsApplied, Utilities.GetFsharpTestProjectReference())); //Assert Assert.AreEqual(expectedContentAfterRefactoring, actualContentAfterRefactoring); }
public void TestCreateWithMethodsForOpenGenericFSharpRecords() { //Arrange var methodsClassCode = @"[CreateWithMethods(typeof(DataObjectHelper.Tests.FSharpProject.Module.GenericFSharpRecord<>))] public static class Methods { }"; var expectedMethodsClassCodeAfterRefactoring = @"[CreateWithMethods(typeof(DataObjectHelper.Tests.FSharpProject.Module.GenericFSharpRecord<>))] public static class Methods { public static DataObjectHelper.Tests.FSharpProject.Module.GenericFSharpRecord<a> WithAge<a>( this DataObjectHelper.Tests.FSharpProject.Module.GenericFSharpRecord<a> instance, System.Int32 newValue) { return new DataObjectHelper.Tests.FSharpProject.Module.GenericFSharpRecord<a>(age: newValue, name: instance.Name); } public static DataObjectHelper.Tests.FSharpProject.Module.GenericFSharpRecord<a> WithName<a>( this DataObjectHelper.Tests.FSharpProject.Module.GenericFSharpRecord<a> instance, a newValue) { return new DataObjectHelper.Tests.FSharpProject.Module.GenericFSharpRecord<a>(age: instance.Age, name: newValue); } }"; var content = Utilities.MergeParts( createWithMethodsAttributeCode, methodsClassCode); var expectedContentAfterRefactoring = Utilities.NormalizeCode( Utilities.MergeParts( createWithMethodsAttributeCode, expectedMethodsClassCodeAfterRefactoring)); //Act var actualContentAfterRefactoring = Utilities.NormalizeCode( Utilities.ApplyRefactoring( content, SelectSpanWhereCreateWithMethodsAttributeIsApplied, Utilities.GetFsharpTestProjectReference())); //Assert Assert.AreEqual(expectedContentAfterRefactoring, actualContentAfterRefactoring); }
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); }
public void TestCreateWithMethodsForClosedGenericDataObjectThatHasAPropertyOfGenericType() { //Arrange var methodsClassCode = @"[CreateWithMethods(typeof(GenericProductTypeWithGenericPropertyType<System.String>))] public static class Methods { }"; var expectedMethodsClassCodeAfterRefactoring = @"[CreateWithMethods(typeof(GenericProductTypeWithGenericPropertyType<System.String>))] public static class Methods { public static GenericProductTypeWithGenericPropertyType<System.String> WithAge(this GenericProductTypeWithGenericPropertyType<System.String> instance, System.Int32 newValue) { return new GenericProductTypeWithGenericPropertyType<System.String>(age: newValue, name: instance.Name); } public static GenericProductTypeWithGenericPropertyType<System.String> WithName(this GenericProductTypeWithGenericPropertyType<System.String> instance, Maybe<System.String> newValue) { return new GenericProductTypeWithGenericPropertyType<System.String>(age: instance.Age, name: newValue); } }"; var content = Utilities.MergeParts( createWithMethodsAttributeCode, genericProductTypeWithPropertyOfGenericTypeCode, methodsClassCode); var expectedContentAfterRefactoring = Utilities.NormalizeCode( Utilities.MergeParts( createWithMethodsAttributeCode, genericProductTypeWithPropertyOfGenericTypeCode, expectedMethodsClassCodeAfterRefactoring)); //Act var actualContentAfterRefactoring = Utilities.NormalizeCode( Utilities.ApplyRefactoring( content, SelectSpanWhereCreateWithMethodsAttributeIsApplied)); //Assert Assert.AreEqual(expectedContentAfterRefactoring, actualContentAfterRefactoring); }